Skip to content

Commit

Permalink
added authenticated account to RequestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Nov 22, 2016
1 parent d177db7 commit 8c352fe
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/main/java/org/restheart/Bootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
import java.nio.file.Paths;
import static io.undertow.Handlers.path;
import static org.fusesource.jansi.Ansi.ansi;
import org.restheart.handlers.ResponseSenderHandler;
import org.restheart.handlers.injectors.AccountInjectorHandler;

/**
*
Expand Down Expand Up @@ -138,8 +138,8 @@ public static void main(final String[] args) {
String instanceName = configuration == null
? "undefined"
: configuration.getInstanceName() == null
? "undefined"
: configuration.getInstanceName();
? "undefined"
: configuration.getInstanceName();

LOGGER.info("Starting "
+ ansi().fg(RED).bold().a("RESTHeart").reset().toString()
Expand Down Expand Up @@ -184,8 +184,8 @@ public static void main(final String[] args) {
String instanceName = configuration == null
? "undefined"
: configuration.getInstanceName() == null
? "undefined"
: configuration.getInstanceName();
? "undefined"
: configuration.getInstanceName();

LOGGER.info("Starting "
+ ansi().fg(RED).bold().a("RESTHeart").reset().toString()
Expand Down Expand Up @@ -337,8 +337,8 @@ private static void startServer(boolean fork) {
String instanceName = configuration == null
? "undefined"
: configuration.getInstanceName() == null
? "undefined"
: configuration.getInstanceName();
? "undefined"
: configuration.getInstanceName();

LOGGER.info("Starting "
+ ansi().fg(RED).bold().a("RESTHeart").reset().toString()
Expand Down Expand Up @@ -560,15 +560,15 @@ private static void startCoreSystem() {
if (configuration.isLocalCacheEnabled()) {
LOGGER.info("Local cache for db and collection properties enabled with TTL {} msecs",
configuration.getLocalCacheTtl() < 0 ? "∞"
: configuration.getLocalCacheTtl());
: configuration.getLocalCacheTtl());
} else {
LOGGER.info("Local cache for db and collection properties not enabled");
}

if (configuration.isSchemaCacheEnabled()) {
LOGGER.info("Local cache for schema stores enabled with TTL {} msecs",
configuration.getSchemaCacheTtl() < 0 ? "∞"
: configuration.getSchemaCacheTtl());
: configuration.getSchemaCacheTtl());
} else {
LOGGER.info("Local cache for schema stores not enabled");
}
Expand Down Expand Up @@ -675,10 +675,11 @@ private static void logErrorAndExit(String message, Throwable t, boolean silent,
*/
private static GracefulShutdownHandler getHandlersPipe(final IdentityManager identityManager, final AccessManager accessManager) {
PipedHttpHandler coreHandlerChain
= new DbPropsInjectorHandler(
new CollectionPropsInjectorHandler(
new RequestDispacherHandler()
));
= new AccountInjectorHandler(
new DbPropsInjectorHandler(
new CollectionPropsInjectorHandler(
new RequestDispacherHandler()
)));

PathHandler paths = path();

Expand Down Expand Up @@ -823,8 +824,8 @@ private static void pipeStaticResourcesHandlers(

Path _path = Paths.get(
locationFile.getParent()
.concat(File.separator)
.concat(path));
.concat(File.separator)
.concat(path));

// normalize addresses https://issues.jboss.org/browse/UNDERTOW-742
file = _path.normalize().toFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* RESTHeart - the Web API for MongoDB
* Copyright (C) SoftInstigate Srl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.restheart.handlers.injectors;

import io.undertow.server.HttpServerExchange;
import org.restheart.handlers.PipedHttpHandler;
import org.restheart.handlers.RequestContext;

/**
*
* injects the context authenticatedAccount
*
* @author Andrea Di Cesare {@literal <[email protected]>}
*/
public class AccountInjectorHandler extends PipedHttpHandler {
/**
* Creates a new instance of AccountInjectorHandler
*
* @param next
*/
public AccountInjectorHandler(PipedHttpHandler next) {
super(next);
}

/**
*
* @param exchange
* @param context
* @throws Exception
*/
@Override
public void handleRequest(
final HttpServerExchange exchange,
final RequestContext context)
throws Exception {
// inject authenticatedAccount
if (exchange.getSecurityContext() != null) {
context.setAuthenticatedAccount(exchange.getSecurityContext().getAuthenticatedAccount());
}

next(exchange, context);
}
}

0 comments on commit 8c352fe

Please sign in to comment.