use of io.undertow.server.session.SessionAttachmentHandler 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()));
}
use of io.undertow.server.session.SessionAttachmentHandler 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();
}
}
use of io.undertow.server.session.SessionAttachmentHandler 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);
}
use of io.undertow.server.session.SessionAttachmentHandler 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());
return;
}
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");
}
}
}).addPrefixPath("/close", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
IoUtils.safeClose(exchange.getConnection());
}
}).addPrefixPath("/old", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
exchange.startBlocking();
exchange.setProtocol(Protocols.HTTP_1_0);
exchange.getOutputStream().write(RESPONSE_BODY.getBytes(StandardCharsets.US_ASCII));
exchange.getOutputStream().flush();
}
}));
}
use of io.undertow.server.session.SessionAttachmentHandler 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(ProxyHandler.builder().setProxyClient(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")).setMaxRequestTime(10000).setMaxConnectionRetries(2).build());
}
Aggregations