Search in sources :

Example 51 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project camel by apache.

the class HttpProducerSessionTest method initServer.

@BeforeClass
public static void initServer() throws Exception {
    port = AvailablePortFinder.getNextAvailable(24000);
    localServer = new Server(new InetSocketAddress("127.0.0.1", port));
    ContextHandler contextHandler = new ContextHandler();
    contextHandler.setContextPath("/session");
    SessionHandler sessionHandler = new SessionHandler();
    sessionHandler.setHandler(new SessionReflectionHandler());
    contextHandler.setHandler(sessionHandler);
    localServer.setHandler(contextHandler);
    localServer.start();
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) SessionReflectionHandler(org.apache.camel.component.http.handler.SessionReflectionHandler) BeforeClass(org.junit.BeforeClass)

Example 52 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project drill by axbaretto.

the class WebServer method createSessionHandler.

/**
 * @return A {@link SessionHandler} which contains a {@link HashSessionManager}
 */
private SessionHandler createSessionHandler(final SecurityHandler securityHandler) {
    SessionManager sessionManager = new HashSessionManager();
    sessionManager.setMaxInactiveInterval(config.getInt(ExecConstants.HTTP_SESSION_MAX_IDLE_SECS));
    sessionManager.addEventListener(new HttpSessionListener() {

        @Override
        public void sessionCreated(HttpSessionEvent se) {
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            final HttpSession session = se.getSession();
            if (session == null) {
                return;
            }
            final Object authCreds = session.getAttribute(SessionAuthentication.__J_AUTHENTICATED);
            if (authCreds != null) {
                final SessionAuthentication sessionAuth = (SessionAuthentication) authCreds;
                securityHandler.logout(sessionAuth);
                session.removeAttribute(SessionAuthentication.__J_AUTHENTICATED);
            }
            // Clear all the resources allocated for this session
            @SuppressWarnings("resource") final WebSessionResources webSessionResources = (WebSessionResources) session.getAttribute(WebSessionResources.class.getSimpleName());
            if (webSessionResources != null) {
                webSessionResources.close();
                session.removeAttribute(WebSessionResources.class.getSimpleName());
            }
        }
    });
    return new SessionHandler(sessionManager);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpSessionListener(javax.servlet.http.HttpSessionListener) HashSessionManager(org.eclipse.jetty.server.session.HashSessionManager) HashSessionManager(org.eclipse.jetty.server.session.HashSessionManager) SessionManager(org.eclipse.jetty.server.SessionManager) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) SessionAuthentication(org.eclipse.jetty.security.authentication.SessionAuthentication)

Example 53 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project api-core by ca-cwds.

the class BaseApiApplication method run.

@Override
public final void run(final T configuration, final Environment environment) throws Exception {
    environment.servlets().setSessionHandler(new SessionHandler());
    registerExceptionMappers(environment);
    LOGGER.info("Application name: {}, Version: {}", configuration.getApplicationName(), configuration.getVersion());
    LOGGER.info("Configuring CORS: Cross-Origin Resource Sharing");
    configureCors(environment);
    LOGGER.info("Configuring ObjectMapper");
    ObjectMapperUtils.configureObjectMapper(environment.getObjectMapper());
    LOGGER.info("Configuring SWAGGER");
    configureSwagger(configuration, environment);
    LOGGER.info("Registering Filters");
    registerFilters(environment, guiceBundle);
    LOGGER.info("Registering SystemCodeCache");
    LOGGER.info("Setting up Guice injector");
    // Providing access to the guice injector from external classes such as custom validators
    InjectorHolder.INSTANCE.setInjector(injector);
    runInternal(configuration, environment);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler)

Example 54 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project ddf by codice.

the class HttpSessionFactory method getCachedSession.

/**
 * DDF-6587 - This check is made so that when simultaneous requests are received for the same
 * context, the second request will not attempt to create a new session. The first request will
 * create a new session object and the second one will look up the previously created session
 * object from the jetty session cache. This is necessary because at this point in the processing
 * chain, jetty was not able to associate a session for either of the simultaneous requests.
 * Therefore, we need to manually attach the session to the second request. Otherwise, jetty will
 * attempt to recreate the same session object and fail, causing the second request to fail.
 */
private HttpSession getCachedSession(HttpServletRequest httpRequest) {
    if (httpRequest instanceof Request && hasValidDependencies((Request) httpRequest)) {
        try {
            Request request = (Request) httpRequest;
            SessionHandler sessionHandler = request.getSessionHandler();
            SessionCache sessionCache = sessionHandler.getSessionCache();
            String sessionId = sessionHandler.getSessionIdManager().newSessionId(request, System.currentTimeMillis());
            Session cachedSession = sessionCache.get(sessionId);
            if (cachedSession != null && cachedSession instanceof HttpSession && cachedSession.isValid()) {
                HttpSession session = (HttpSession) cachedSession;
                request.enterSession(session);
                request.setSession(session);
                return session;
            }
        } catch (Exception e) {
            LOGGER.trace("Unable to get session from cache, letting a new one get created", e);
        }
    }
    return null;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpSession(javax.servlet.http.HttpSession) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionCache(org.eclipse.jetty.server.session.SessionCache) HttpSession(javax.servlet.http.HttpSession) Session(org.eclipse.jetty.server.session.Session)

Example 55 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project dropbox-sdk-java by dropbox.

the class Main method main.

// -------------------------------------------------------------------------------------------
// main()
// -------------------------------------------------------------------------------------------
// Parse command-line arguments and start server.
public static void main(String[] args) throws IOException {
    if (args.length != 3) {
        System.out.println("");
        System.out.println("Usage: COMMAND <http-listen-port> <app-info-file> <database-file>");
        System.out.println("");
        System.out.println(" <http-listen-port>: The port to run the HTTP server on.  For example,");
        System.out.println("    \"8080\").");
        System.out.println("");
        System.out.println(" <app-info-file>: A JSON file containing your Dropbox API app key, secret");
        System.out.println("    and access type.  For example, \"my-app.app\" with:");
        System.out.println("");
        System.out.println("    {");
        System.out.println("      \"key\": \"Your Dropbox app key...\",");
        System.out.println("      \"secret\": \"Your Dropbox app secret...\"");
        System.out.println("    }");
        System.out.println("");
        System.out.println(" <database-file>: Where you want this program to store its database.  For");
        System.out.println("    example, \"web-file-browser.db\".");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argPort = args[0];
    String argAppInfo = args[1];
    String argDatabase = args[2];
    // Figure out what port to listen on.
    int port;
    try {
        port = Integer.parseInt(argPort);
        if (port < 1 || port > 65535) {
            System.err.println("Expecting <http-listen-port> to be a number from 1 to 65535.  Got: " + port + ".");
            System.exit(1);
            return;
        }
    } catch (NumberFormatException ex) {
        System.err.println("Expecting <http-listen-port> to be a number from 1 to 65535.  Got: " + jq(argPort) + ".");
        System.exit(1);
        return;
    }
    // Read app info file (contains app key and app secret)
    DbxAppInfo dbxAppInfo;
    try {
        dbxAppInfo = DbxAppInfo.Reader.readFromFile(argAppInfo);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <app-info-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.println("Loaded app info from " + jq(argAppInfo));
    File dbFile = new File(argDatabase);
    // Run server
    try {
        Main main = new Main(new PrintWriter(System.out, true), dbxAppInfo, dbFile);
        Server server = new Server(port);
        SessionHandler sessionHandler = new SessionHandler();
        sessionHandler.setServer(server);
        sessionHandler.setHandler(main);
        server.setHandler(sessionHandler);
        server.start();
        System.out.println("Server running: http://localhost:" + port + "/");
        server.join();
    } catch (Exception ex) {
        System.err.println("Error running server: " + ex.getMessage());
        System.exit(1);
    }
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Server(org.eclipse.jetty.server.Server) DbxAppInfo(com.dropbox.core.DbxAppInfo) JsonReader(com.dropbox.core.json.JsonReader) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

SessionHandler (org.eclipse.jetty.server.session.SessionHandler)70 Server (org.eclipse.jetty.server.Server)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)17 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)13 IOException (java.io.IOException)10 Test (org.junit.Test)9 ServletException (javax.servlet.ServletException)8 HttpSession (javax.servlet.http.HttpSession)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 HashSessionManager (org.eclipse.jetty.server.session.HashSessionManager)6 File (java.io.File)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Handler (org.eclipse.jetty.server.Handler)5 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)5 ArrayList (java.util.ArrayList)4 SessionCookieConfig (javax.servlet.SessionCookieConfig)4 HttpSessionEvent (javax.servlet.http.HttpSessionEvent)4 HttpSessionListener (javax.servlet.http.HttpSessionListener)4 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)4