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);
}
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);
}
});
}
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();
}
}
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());
}
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);
}
Aggregations