Search in sources :

Example 11 with SessionCookieConfig

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

the class SessionServer method main.

public static void main(String[] args) {
    PathHandler pathHandler = new PathHandler();
    pathHandler.addPrefixPath("/", new HttpHandler() {

        public void handleRequest(HttpServerExchange exchange) throws Exception {
            StringBuilder sb = new StringBuilder();
            sb.append("<form action='addToSession' >");
            sb.append("<label>Attribute Name</label>");
            sb.append("<input name='attrName' />");
            sb.append("<label>Attribute Value</label>");
            sb.append("<input name='value' />");
            sb.append("<button>Save to Session</button>");
            // To retrive the SessionManager use the attachmentKey
            SessionManager sm = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
            // same goes to SessionConfig
            SessionConfig sessionConfig = exchange.getAttachment(SessionConfig.ATTACHMENT_KEY);
            sb.append("</form>");
            sb.append("<a href='/destroySession'>Destroy Session</a>");
            sb.append("<br/>");
            Session session = sm.getSession(exchange, sessionConfig);
            if (session == null)
                session = sm.createSession(exchange, sessionConfig);
            sb.append("<ul>");
            for (String string : session.getAttributeNames()) {
                sb.append("<li>" + string + " : " + session.getAttribute(string) + "</li>");
            }
            sb.append("</ul>");
            exchange.getResponseHeaders().add(Headers.CONTENT_TYPE, "text/html;");
            exchange.getResponseSender().send(sb.toString());
        }
    });
    pathHandler.addPrefixPath("/addToSession", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            SessionManager sm = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
            SessionConfig sessionConfig = exchange.getAttachment(SessionConfig.ATTACHMENT_KEY);
            Map<String, Deque<String>> reqParams = exchange.getQueryParameters();
            Session session = sm.getSession(exchange, sessionConfig);
            if (session == null)
                session = sm.createSession(exchange, sessionConfig);
            Deque<String> deque = reqParams.get("attrName");
            Deque<String> dequeVal = reqParams.get("value");
            session.setAttribute(deque.getLast(), dequeVal.getLast());
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
            exchange.getResponseHeaders().put(Headers.LOCATION, "/");
            exchange.getResponseSender().close();
        }
    });
    pathHandler.addPrefixPath("/destroySession", new HttpHandler() {

        public void handleRequest(HttpServerExchange exchange) throws Exception {
            SessionManager sm = exchange.getAttachment(SessionManager.ATTACHMENT_KEY);
            SessionConfig sessionConfig = exchange.getAttachment(SessionConfig.ATTACHMENT_KEY);
            Session session = sm.getSession(exchange, sessionConfig);
            if (session == null)
                session = sm.createSession(exchange, sessionConfig);
            session.invalidate(exchange);
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
            exchange.getResponseHeaders().put(Headers.LOCATION, "/");
            exchange.getResponseSender().close();
        }
    });
    SessionManager sessionManager = new InMemorySessionManager("SESSION_MANAGER");
    SessionCookieConfig sessionConfig = new SessionCookieConfig();
    /*
         * Use the sessionAttachmentHandler to add the sessionManager and
         * sessionCofing to the exchange of every request
         */
    SessionAttachmentHandler sessionAttachmentHandler = new SessionAttachmentHandler(sessionManager, sessionConfig);
    // set as next handler your root handler
    sessionAttachmentHandler.setNext(pathHandler);
    System.out.println("Open the url and fill the form to add attributes into the session");
    Undertow server = Undertow.builder().addHttpListener(8080, "localhost").setHandler(sessionAttachmentHandler).build();
    server.start();
}
Also used : HttpHandler(io.undertow.server.HttpHandler) SessionManager(io.undertow.server.session.SessionManager) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) PathHandler(io.undertow.server.handlers.PathHandler) SessionConfig(io.undertow.server.session.SessionConfig) Deque(java.util.Deque) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) Map(java.util.Map) Undertow(io.undertow.Undertow) Session(io.undertow.server.session.Session) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Aggregations

SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)11 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 HttpHandler (io.undertow.server.HttpHandler)8 HttpServerExchange (io.undertow.server.HttpServerExchange)7 IOException (java.io.IOException)4 BeforeClass (org.junit.BeforeClass)4 Undertow (io.undertow.Undertow)3 PathHandler (io.undertow.server.handlers.PathHandler)3 Session (io.undertow.server.session.Session)3 SessionManager (io.undertow.server.session.SessionManager)3 HttpString (io.undertow.util.HttpString)3 URI (java.net.URI)3 UndertowXnioSsl (io.undertow.protocols.ssl.UndertowXnioSsl)2 TestHttpClient (io.undertow.testutils.TestHttpClient)2 Header (org.apache.http.Header)2 HttpResponse (org.apache.http.HttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)2 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)1