Search in sources :

Example 26 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class SimpleSSLTestCase method simpleSSLTestCase.

@Test
public void simpleSSLTestCase() throws IOException, GeneralSecurityException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
            exchange.endExchange();
        }
    });
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders("scheme");
        Assert.assertEquals("https", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
        DefaultServer.stopSSLServer();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 27 with HttpHandler

use of io.undertow.server.HttpHandler 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 28 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class AuthenticationTestBase method setAuthenticationChain.

@Before
public void setAuthenticationChain() {
    List<AuthenticationMechanism> testMechanisms = getTestMechanisms();
    if (testMechanisms == null) {
        return;
    }
    HttpHandler current = new ResponseHandler();
    current = new AuthenticationCallHandler(current);
    current = new AuthenticationConstraintHandler(current);
    current = new AuthenticationMechanismsHandler(current, testMechanisms);
    // Ensure empty on initialisation.
    auditReceiver.takeNotifications();
    current = new NotificationReceiverHandler(current, Collections.<NotificationReceiver>singleton(auditReceiver));
    if (cachingRequired()) {
        current = new CachedAuthenticatedSessionHandler(current);
    }
    current = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, current);
    setRootHandler(current);
}
Also used : HttpHandler(io.undertow.server.HttpHandler) AuthenticationConstraintHandler(io.undertow.security.handlers.AuthenticationConstraintHandler) CachedAuthenticatedSessionHandler(io.undertow.security.handlers.CachedAuthenticatedSessionHandler) NotificationReceiverHandler(io.undertow.security.handlers.NotificationReceiverHandler) SecurityInitialHandler(io.undertow.security.handlers.SecurityInitialHandler) AuthenticationMechanismsHandler(io.undertow.security.handlers.AuthenticationMechanismsHandler) NotificationReceiver(io.undertow.security.api.NotificationReceiver) AuthenticationMechanism(io.undertow.security.api.AuthenticationMechanism) AuthenticationCallHandler(io.undertow.security.handlers.AuthenticationCallHandler) Before(org.junit.Before)

Example 29 with HttpHandler

use of io.undertow.server.HttpHandler 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 30 with HttpHandler

use of io.undertow.server.HttpHandler in project undertow by undertow-io.

the class NameVirtualHostHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final String hostHeader = exchange.getRequestHeaders().getFirst(Headers.HOST);
    if (hostHeader != null) {
        String host;
        if (hostHeader.contains(":")) {
            //header can be in host:port format
            host = hostHeader.substring(0, hostHeader.lastIndexOf(":"));
        } else {
            host = hostHeader;
        }
        //most hosts will be lowercase, so we do the host
        HttpHandler handler = hosts.get(host);
        if (handler != null) {
            handler.handleRequest(exchange);
            return;
        }
        //do a cache insensitive match
        handler = hosts.get(host.toLowerCase(Locale.ENGLISH));
        if (handler != null) {
            handler.handleRequest(exchange);
            return;
        }
    }
    defaultHandler.handleRequest(exchange);
}
Also used : HttpHandler(io.undertow.server.HttpHandler)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)117 HttpServerExchange (io.undertow.server.HttpServerExchange)71 IOException (java.io.IOException)54 BeforeClass (org.junit.BeforeClass)35 Test (org.junit.Test)25 TestHttpClient (io.undertow.testutils.TestHttpClient)20 HttpResponse (org.apache.http.HttpResponse)20 HttpGet (org.apache.http.client.methods.HttpGet)19 PathHandler (io.undertow.server.handlers.PathHandler)16 HttpString (io.undertow.util.HttpString)14 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)11 ArrayList (java.util.ArrayList)11 Undertow (io.undertow.Undertow)10 HandlerWrapper (io.undertow.server.HandlerWrapper)9 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)9 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)9 Header (org.apache.http.Header)9 SessionCookieConfig (io.undertow.server.session.SessionCookieConfig)8 OutputStream (java.io.OutputStream)7 ServiceController (org.jboss.msc.service.ServiceController)7