Search in sources :

Example 16 with Session

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

the class ServletFormAuthenticationMechanism method handleRedirectBack.

@Override
protected void handleRedirectBack(final HttpServerExchange exchange) {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    HttpServletResponse resp = (HttpServletResponse) servletRequestContext.getServletResponse();
    HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchange, false);
    if (httpSession != null) {
        Session session;
        if (System.getSecurityManager() == null) {
            session = httpSession.getSession();
        } else {
            session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
        }
        String path = (String) session.getAttribute(SESSION_KEY);
        if ((path == null || overrideInitial) && defaultPage != null) {
            path = defaultPage;
        }
        if (path != null) {
            try {
                resp.sendRedirect(path);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Session(io.undertow.server.session.Session)

Example 17 with Session

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

the class SessionRestoringHandler method stop.

public void stop() {
    ClassLoader old = getTccl();
    try {
        setTccl(servletContext.getClassLoader());
        this.started = false;
        final Map<String, SessionPersistenceManager.PersistentSession> objectData = new HashMap<>();
        for (String sessionId : sessionManager.getTransientSessions()) {
            Session session = sessionManager.getSession(sessionId);
            if (session != null) {
                final HttpSessionEvent event = new HttpSessionEvent(SecurityActions.forSession(session, servletContext, false));
                final Map<String, Object> sessionData = new HashMap<>();
                for (String attr : session.getAttributeNames()) {
                    final Object attribute = session.getAttribute(attr);
                    sessionData.put(attr, attribute);
                    if (attribute instanceof HttpSessionActivationListener) {
                        ((HttpSessionActivationListener) attribute).sessionWillPassivate(event);
                    }
                }
                objectData.put(sessionId, new PersistentSession(new Date(session.getLastAccessedTime() + (session.getMaxInactiveInterval() * 1000)), sessionData));
            }
        }
        sessionPersistenceManager.persistSessions(deploymentName, objectData);
        this.data.clear();
    } finally {
        setTccl(old);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession) HttpSessionActivationListener(javax.servlet.http.HttpSessionActivationListener) Date(java.util.Date) Session(io.undertow.server.session.Session) PersistentSession(io.undertow.servlet.api.SessionPersistenceManager.PersistentSession)

Example 18 with Session

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

the class InMemorySessionTestCase method inMemorySessionTest.

@Test
public void inMemorySessionTest() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setCookieStore(new BasicCookieStore());
    try {
        final SessionCookieConfig sessionConfig = new SessionCookieConfig();
        final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager(""), sessionConfig);
        handler.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.setRootHandler(handler);
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/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.getDefaultServerURL() + "/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.getDefaultServerURL() + "/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());
    } finally {
        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) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Header(org.apache.http.Header) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) Test(org.junit.Test)

Example 19 with Session

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

the class URLRewritingSessionTestCase method setup.

@BeforeClass
public static void setup() {
    final PathParameterSessionConfig sessionConfig = new PathParameterSessionConfig();
    final SessionAttachmentHandler handler = new SessionAttachmentHandler(new InMemorySessionManager(""), sessionConfig);
    handler.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);
            } else {
                Assert.assertEquals("/notamatchingpath;jsessionid=" + session.getId(), exchange.getRequestURI());
            }
            Integer count = (Integer) session.getAttribute(COUNT);
            exchange.getResponseHeaders().add(new HttpString(COUNT), count.toString());
            session.setAttribute(COUNT, ++count);
            for (Map.Entry<String, Deque<String>> qp : exchange.getQueryParameters().entrySet()) {
                exchange.getResponseHeaders().add(new HttpString(qp.getKey()), qp.getValue().getFirst());
            }
            if (exchange.getQueryString().isEmpty()) {
                exchange.getResponseSender().send(sessionConfig.rewriteUrl(DefaultServer.getDefaultServerURL() + "/notamatchingpath", session.getId()));
            } else {
                exchange.getResponseSender().send(sessionConfig.rewriteUrl(DefaultServer.getDefaultServerURL() + "/notamatchingpath?" + exchange.getQueryString(), session.getId()));
            }
        }
    });
    DefaultServer.setRootHandler(handler);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) PathParameterSessionConfig(io.undertow.server.session.PathParameterSessionConfig) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) Session(io.undertow.server.session.Session) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 20 with Session

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

the class ServletFormAuthenticationMechanism method storeInitialLocation.

@Override
protected void storeInitialLocation(final HttpServerExchange exchange) {
    if (!saveOriginalRequest) {
        return;
    }
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    HttpSessionImpl httpSession = servletRequestContext.getCurrentServletContext().getSession(exchange, true);
    Session session;
    if (System.getSecurityManager() == null) {
        session = httpSession.getSession();
    } else {
        session = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(httpSession));
    }
    SessionManager manager = session.getSessionManager();
    if (seenSessionManagers.add(manager)) {
        manager.registerSessionListener(LISTENER);
    }
    session.setAttribute(SESSION_KEY, RedirectBuilder.redirect(exchange, exchange.getRelativePath()));
    SavedRequest.trySaveRequest(exchange);
}
Also used : HttpSessionImpl(io.undertow.servlet.spec.HttpSessionImpl) SessionManager(io.undertow.server.session.SessionManager) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) Session(io.undertow.server.session.Session)

Aggregations

Session (io.undertow.server.session.Session)33 SessionManager (io.undertow.server.session.SessionManager)19 Test (org.junit.Test)10 HttpServerExchange (io.undertow.server.HttpServerExchange)9 AuthenticatedSession (io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession)7 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)7 HttpSessionImpl (io.undertow.servlet.spec.HttpSessionImpl)7 HttpString (io.undertow.util.HttpString)7 HttpHandler (io.undertow.server.HttpHandler)6 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)6 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)6 IOException (java.io.IOException)6 BatchContext (org.wildfly.clustering.ee.BatchContext)6 SessionConfig (io.undertow.server.session.SessionConfig)5 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)4 TestHttpClient (io.undertow.testutils.TestHttpClient)4 Map (java.util.Map)4 Header (org.apache.http.Header)4 HttpResponse (org.apache.http.HttpResponse)4 HeaderMap (io.undertow.util.HeaderMap)3