Search in sources :

Example 1 with SessionCookieConfig

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

the class FormAuthTestCase method setRootHandler.

@Override
protected void setRootHandler(HttpHandler current) {
    final PredicateHandler handler = new PredicateHandler(Predicates.path("/login"), new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("Login Page");
        }
    }, current);
    super.setRootHandler(new SessionAttachmentHandler(handler, new InMemorySessionManager("test"), new SessionCookieConfig()));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) PredicateHandler(io.undertow.server.handlers.PredicateHandler) ProtocolException(org.apache.http.ProtocolException) IOException(java.io.IOException) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Example 2 with SessionCookieConfig

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

the class HTTP2ViaUpgradeTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    int port = DefaultServer.getHostPort("default");
    server = Undertow.builder().addHttpListener(port + 1, DefaultServer.getHostAddress("default")).setServerOption(UndertowOptions.ENABLE_HTTP2, true).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(Handlers.header(new Http2UpgradeHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (!(exchange.getConnection() instanceof Http2ServerConnection)) {
                throw new RuntimeException("Not HTTP2");
            }
            exchange.getResponseHeaders().add(new HttpString("X-Custom-Header"), "foo");
            exchange.getResponseSender().send(message);
        }
    }, "h2c", "h2c-17"), Headers.SEC_WEB_SOCKET_ACCEPT_STRING, //work around Netty bug, it assumes that every upgrade request that does not have this header is an old style websocket upgrade
    "fake")).build();
    server.start();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) URISyntaxException(java.net.URISyntaxException) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 3 with SessionCookieConfig

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

the class AbstractLoadBalancingProxyTestCase method getRootHandler.

protected static HttpHandler getRootHandler(String s1, String server1) {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    return jvmRoute("JSESSIONID", s1, path().addPrefixPath("/session", new SessionAttachmentHandler(new SessionTestHandler(sessionConfig), new InMemorySessionManager(""), sessionConfig)).addPrefixPath("/name", new StringSendHandler(server1)).addPrefixPath("/url", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send(exchange.getRequestURI());
        }
    }).addPrefixPath("/path", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send(exchange.getRequestURI());
        }
    }).addPrefixPath("/fail", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (firstFail) {
                firstFail = false;
                IoUtils.safeClose(exchange.getConnection());
            }
            exchange.getResponseSender().send(exchange.getRequestURI() + ":" + firstFail);
        }
    }).addPrefixPath("/timeout", new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            if (exchange.getConnection().getAttachment(EXISTING) == null) {
                exchange.getConnection().putAttachment(EXISTING, true);
                exchange.getResponseSender().send("false");
            } else {
                exchange.getResponseSender().send("true");
            }
        }
    }));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) HttpHandler(io.undertow.server.HttpHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) IOException(java.io.IOException) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager)

Example 4 with SessionCookieConfig

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

the class LoadBalancingProxyWithCustomHostSelectorTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    final SessionCookieConfig sessionConfig = new SessionCookieConfig();
    int port = DefaultServer.getHostPort("default");
    server1 = Undertow.builder().addHttpListener(port + 1, DefaultServer.getHostAddress("default")).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(jvmRoute("JSESSIONID", "s1", path().addPrefixPath("/session", new SessionAttachmentHandler(new AbstractLoadBalancingProxyTestCase.SessionTestHandler(sessionConfig), new InMemorySessionManager(""), sessionConfig)).addPrefixPath("/name", new AbstractLoadBalancingProxyTestCase.StringSendHandler("server1")))).build();
    server2 = Undertow.builder().addHttpListener(port + 2, DefaultServer.getHostAddress("default")).setSocketOption(Options.REUSE_ADDRESSES, true).setHandler(jvmRoute("JSESSIONID", "s2", path().addPrefixPath("/session", new SessionAttachmentHandler(new AbstractLoadBalancingProxyTestCase.SessionTestHandler(sessionConfig), new InMemorySessionManager(""), sessionConfig)).addPrefixPath("/name", new AbstractLoadBalancingProxyTestCase.StringSendHandler("server2")))).build();
    server1.start();
    server2.start();
    LoadBalancingProxyClient.HostSelector hostSelector = new LoadBalancingProxyClient.HostSelector() {

        @Override
        public int selectHost(LoadBalancingProxyClient.Host[] availableHosts) {
            return 0;
        }
    };
    DefaultServer.setRootHandler(new ProxyHandler(new LoadBalancingProxyClient(UndertowClient.getInstance(), null, hostSelector).setConnectionsPerThread(4).addHost(new URI("http", null, DefaultServer.getHostAddress("default"), port + 1, null, null, null), "s1").addHost(new URI("http", null, DefaultServer.getHostAddress("default"), port + 2, null, null, null), "s2"), 10000, ResponseCodeHandler.HANDLE_404));
}
Also used : SessionAttachmentHandler(io.undertow.server.session.SessionAttachmentHandler) SessionCookieConfig(io.undertow.server.session.SessionCookieConfig) URI(java.net.URI) InMemorySessionManager(io.undertow.server.session.InMemorySessionManager) BeforeClass(org.junit.BeforeClass)

Example 5 with SessionCookieConfig

use of io.undertow.server.session.SessionCookieConfig 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)

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