Search in sources :

Example 1 with SslSessionConfig

use of io.undertow.server.session.SslSessionConfig in project undertow by undertow-io.

the class SSLSessionTestCase method testSslSession.

@Test
public void testSslSession() throws IOException {
    TestHttpClient client = new TestHttpClient();
    try {
        InMemorySessionManager sessionManager = new InMemorySessionManager("");
        final SslSessionConfig sessionConfig = new SslSessionConfig(sessionManager);
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(sessionManager, sessionConfig).setNext(new HttpHandler() {

            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                final SessionManager manager = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
                Session session = manager.getSession(exchange, sessionConfig);
                if (session == null) {
                    session = manager.createSession(exchange, sessionConfig);
                    session.setAttribute(COUNT, 0);
                }
                Integer count = (Integer) session.getAttribute(COUNT);
                exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
                session.setAttribute(COUNT, ++count);
            }
        });
        DefaultServer.startSSLServer();
        client.setSSLContext(DefaultServer.getClientSSLContext());
        DefaultServer.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        Header[] header = result.getHeaders(COUNT);
        Assert.assertEquals("0", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("1", header[0].getValue());
        get = new HttpGet(DefaultServer.getDefaultServerSSLAddress() + "/notamatchingpath");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        HttpClientUtils.readResponse(result);
        header = result.getHeaders(COUNT);
        Assert.assertEquals("2", header[0].getValue());
        Assert.assertEquals(0, client.getCookieStore().getCookies().size());
    } finally {
        DefaultServer.stopSSLServer();
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SslSessionConfig(io.undertow.server.session.SslSessionConfig) Header(org.apache.http.Header) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 2 with SslSessionConfig

use of io.undertow.server.session.SslSessionConfig in project undertow by undertow-io.

the class ServletContextImpl method initDone.

public void initDone() {
    initialized = true;
    Set<SessionTrackingMode> trackingMethods = sessionTrackingModes;
    SessionConfig sessionConfig = sessionCookieConfig;
    if (trackingMethods != null && !trackingMethods.isEmpty()) {
        if (sessionTrackingModes.contains(SessionTrackingMode.SSL)) {
            sessionConfig = new SslSessionConfig(deployment.getSessionManager());
        } else {
            if (sessionTrackingModes.contains(SessionTrackingMode.COOKIE) && sessionTrackingModes.contains(SessionTrackingMode.URL)) {
                sessionCookieConfig.setFallback(new PathParameterSessionConfig(sessionCookieConfig.getName().toLowerCase(Locale.ENGLISH)));
            } else if (sessionTrackingModes.contains(SessionTrackingMode.URL)) {
                sessionConfig = new PathParameterSessionConfig(sessionCookieConfig.getName().toLowerCase(Locale.ENGLISH));
            }
        }
    }
    SessionConfigWrapper wrapper = deploymentInfo.getSessionConfigWrapper();
    if (wrapper != null) {
        sessionConfig = wrapper.wrap(sessionConfig, deployment);
    }
    this.sessionConfig = new ServletContextSessionConfig(sessionConfig);
    this.onWritePossibleTask = deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, WriteListener>() {

        @Override
        public Void call(HttpServerExchange exchange, WriteListener context) throws Exception {
            context.onWritePossible();
            return null;
        }
    });
    this.runnableTask = new ThreadSetupHandler.Action<Void, Runnable>() {

        @Override
        public Void call(HttpServerExchange exchange, Runnable context) throws Exception {
            context.run();
            return null;
        }
    };
    this.onDataAvailableTask = deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, ReadListener>() {

        @Override
        public Void call(HttpServerExchange exchange, ReadListener context) throws Exception {
            context.onDataAvailable();
            return null;
        }
    });
    this.onAllDataReadTask = deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, ReadListener>() {

        @Override
        public Void call(HttpServerExchange exchange, ReadListener context) throws Exception {
            context.onAllDataRead();
            return null;
        }
    });
    this.invokeActionTask = deployment.createThreadSetupAction(new ThreadSetupHandler.Action<Void, ThreadSetupHandler.Action<Void, Object>>() {

        @Override
        public Void call(HttpServerExchange exchange, ThreadSetupHandler.Action<Void, Object> context) throws Exception {
            context.call(exchange, null);
            return null;
        }
    });
}
Also used : PrivilegedAction(java.security.PrivilegedAction) SessionTrackingMode(javax.servlet.SessionTrackingMode) PathParameterSessionConfig(io.undertow.server.session.PathParameterSessionConfig) SslSessionConfig(io.undertow.server.session.SslSessionConfig) PathParameterSessionConfig(io.undertow.server.session.PathParameterSessionConfig) SessionConfig(io.undertow.server.session.SessionConfig) ReadListener(javax.servlet.ReadListener) ServletException(javax.servlet.ServletException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) ThreadSetupHandler(io.undertow.servlet.api.ThreadSetupHandler) SslSessionConfig(io.undertow.server.session.SslSessionConfig) SessionConfigWrapper(io.undertow.servlet.api.SessionConfigWrapper) WriteListener(javax.servlet.WriteListener)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)2 SslSessionConfig (io.undertow.server.session.SslSessionConfig)2 IOException (java.io.IOException)2 HttpHandler (io.undertow.server.HttpHandler)1 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)1 PathParameterSessionConfig (io.undertow.server.session.PathParameterSessionConfig)1 Session (io.undertow.server.session.Session)1 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)1 SessionConfig (io.undertow.server.session.SessionConfig)1 SessionManager (io.undertow.server.session.SessionManager)1 SessionConfigWrapper (io.undertow.servlet.api.SessionConfigWrapper)1 ThreadSetupHandler (io.undertow.servlet.api.ThreadSetupHandler)1 TestHttpClient (io.undertow.testutils.TestHttpClient)1 HttpString (io.undertow.util.HttpString)1 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 PrivilegedAction (java.security.PrivilegedAction)1 ReadListener (javax.servlet.ReadListener)1 ServletException (javax.servlet.ServletException)1 SessionTrackingMode (javax.servlet.SessionTrackingMode)1