Search in sources :

Example 81 with HttpHandler

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

the class RangeRequestTestCase method setup.

@BeforeClass
public static void setup() throws URISyntaxException {
    Path rootPath = Paths.get(RangeRequestTestCase.class.getResource("range.txt").toURI()).getParent();
    PathHandler path = Handlers.path();
    path.addPrefixPath("/path", new ByteRangeHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(new Date(10000)));
            exchange.getResponseHeaders().put(Headers.ETAG, "\"someetag\"");
            exchange.getResponseSender().send("0123456789");
        }
    }, true));
    path.addPrefixPath("/resource", new ResourceHandler(new PathResourceManager(rootPath, 10485760)).setDirectoryListingEnabled(true));
    path.addPrefixPath("/cachedresource", new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)).setDirectoryListingEnabled(true));
    DefaultServer.setRootHandler(path);
}
Also used : Path(java.nio.file.Path) HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) DirectBufferCache(io.undertow.server.handlers.cache.DirectBufferCache) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) Date(java.util.Date) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 82 with HttpHandler

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

the class ReceiverTestCase method setup.

@BeforeClass
public static void setup() {
    HttpHandler testFullString = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {

                @Override
                public void handle(HttpServerExchange exchange, String message) {
                    exchange.getResponseSender().send(message);
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testPartialString = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            final StringBuilder sb = new StringBuilder();
            exchange.getRequestReceiver().receivePartialString(new Receiver.PartialStringCallback() {

                @Override
                public void handle(HttpServerExchange exchange, String message, boolean last) {
                    sb.append(message);
                    if (last) {
                        exchange.getResponseSender().send(sb.toString());
                    }
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testFullBytes = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullBytes(new Receiver.FullBytesCallback() {

                @Override
                public void handle(HttpServerExchange exchange, byte[] message) {
                    exchange.getResponseSender().send(ByteBuffer.wrap(message));
                }
            }, ERROR_CALLBACK);
        }
    };
    HttpHandler testPartialBytes = new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            class CB implements Receiver.PartialBytesCallback, IoCallback {

                final Receiver receiver;

                final Sender sender;

                CB(Receiver receiver, Sender sender) {
                    this.receiver = receiver;
                    this.sender = sender;
                }

                @Override
                public void onComplete(HttpServerExchange exchange, Sender sender) {
                    receiver.resume();
                }

                @Override
                public void onException(HttpServerExchange exchange, Sender sender, IOException exception) {
                    exception.printStackTrace();
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    exchange.endExchange();
                }

                @Override
                public void handle(HttpServerExchange exchange, byte[] message, boolean last) {
                    receiver.pause();
                    sender.send(ByteBuffer.wrap(message), last ? IoCallback.END_EXCHANGE : this);
                }
            }
            CB callback = new CB(exchange.getRequestReceiver(), exchange.getResponseSender());
            exchange.getRequestReceiver().receivePartialBytes(callback);
        }
    };
    final PathHandler handler = new PathHandler().addPrefixPath("/fullstring", testFullString).addPrefixPath("/partialstring", testPartialString).addPrefixPath("/fullbytes", testFullBytes).addPrefixPath("/partialbytes", testPartialBytes);
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            Deque<String> block = exchange.getQueryParameters().get("blocking");
            if (block != null) {
                exchange.startBlocking();
                exchange.dispatch(handler);
                return;
            }
            handler.handleRequest(exchange);
        }
    });
}
Also used : HttpHandler(io.undertow.server.HttpHandler) Receiver(io.undertow.io.Receiver) IOException(java.io.IOException) Deque(java.util.Deque) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) IoCallback(io.undertow.io.IoCallback) BeforeClass(org.junit.BeforeClass)

Example 83 with HttpHandler

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

the class ResumeWritesTestCase method testResumeWritesHttp10.

@Test
public void testResumeWritesHttp10() throws IOException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.addResponseWrapper(new ReturnZeroWrapper());
            exchange.getResponseSender().send(HELLO_WORLD);
        }
    });
    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
        get.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(HELLO_WORLD, HttpClientUtils.readResponse(result));
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 84 with HttpHandler

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

the class PredicatedHandlersParserTestCase method testParsedPredicatedHandler1.

@Test
public void testParsedPredicatedHandler1() {
    String value = "contains(value='a', search=b) -> dump-request";
    List<PredicatedHandler> ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    HttpHandler handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    ContainsPredicate predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b" }, predicate.getValues());
    value = "contains(value='a', search={b}) -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b" }, predicate.getValues());
    value = "contains[value='a', search={b, c}] -> dump-request";
    ret = PredicatedHandlersParser.parse(value, getClass().getClassLoader());
    Assert.assertEquals(1, ret.size());
    handler = ret.get(0).getHandler().wrap(ResponseCodeHandler.HANDLE_200);
    Assert.assertTrue(handler instanceof RequestDumpingHandler);
    predicate = (ContainsPredicate) ret.get(0).getPredicate();
    Assert.assertEquals("a", predicate.getAttribute().readAttribute(null));
    Assert.assertArrayEquals(new String[] { "b", "c" }, predicate.getValues());
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ContainsPredicate(io.undertow.predicate.ContainsPredicate) RequestDumpingHandler(io.undertow.server.handlers.RequestDumpingHandler) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 85 with HttpHandler

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

the class DeflateContentEncodingTestCase method setup.

@BeforeClass
public static void setup() {
    final EncodingHandler handler = new EncodingHandler(new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50, Predicates.maxContentSize(5))).setNext(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, message.length() + "");
            exchange.getResponseSender().send(message, IoCallback.END_EXCHANGE);
        }
    });
    DefaultServer.setRootHandler(handler);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpHandler (io.undertow.server.HttpHandler)126 HttpServerExchange (io.undertow.server.HttpServerExchange)72 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)17 HttpString (io.undertow.util.HttpString)15 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)13 Undertow (io.undertow.Undertow)12 ArrayList (java.util.ArrayList)11 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 AuthenticationMechanism (io.undertow.security.api.AuthenticationMechanism)7 AuthenticationCallHandler (io.undertow.security.handlers.AuthenticationCallHandler)7