use of org.eclipse.jetty.server.handler.AbstractHandler in project httpx by EricEdens.
the class JettyPathTest method testParameterDecoding.
@Test
public void testParameterDecoding() throws Exception {
Server server = run(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print(request.getParameter("hi"));
baseRequest.setHandled(true);
}
});
try {
for (char c = 0; c < 1024; c++) {
String queryParam = String.valueOf(c);
String url = Url.httpBuilder("localhost", "/").port(8080).addQueryParam("hi", queryParam).toString();
try (InputStream stream = new URL(url).openStream()) {
byte[] bytes = ByteStreams.toByteArray(stream);
assertEquals("char c = " + c, queryParam, new String(bytes));
}
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.
the class AsyncStreamLifecycleTest method configureHandler.
@Override
public AbstractHandler configureHandler() throws Exception {
return new AbstractHandler() {
public void handle(String s, Request request, HttpServletRequest req, final HttpServletResponse resp) throws IOException, ServletException {
resp.setContentType("text/plain;charset=utf-8");
resp.setStatus(200);
final Continuation continuation = ContinuationSupport.getContinuation(req);
continuation.suspend();
final PrintWriter writer = resp.getWriter();
executorService.submit(new Runnable() {
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
logger.error("Failed to sleep for 100 ms.", e);
}
logger.info("Delivering part1.");
writer.write("part1");
writer.flush();
}
});
executorService.submit(new Runnable() {
public void run() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
logger.error("Failed to sleep for 200 ms.", e);
}
logger.info("Delivering part2.");
writer.write("part2");
writer.flush();
continuation.complete();
}
});
request.setHandled(true);
}
};
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.
the class BasicHttpTest method postUnboundedInputStreamAsBodyStream.
@Test
public void postUnboundedInputStreamAsBodyStream() throws Throwable {
withClient().run(client -> {
withServer(server).run(server -> {
HttpHeaders h = new DefaultHttpHeaders();
h.add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
server.enqueue(new AbstractHandler() {
EchoHandler chain = new EchoHandler();
@Override
public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
assertEquals(request.getHeader(TRANSFER_ENCODING.toString()), HttpHeaderValues.CHUNKED.toString());
assertNull(request.getHeader(CONTENT_LENGTH.toString()));
chain.handle(target, request, httpServletRequest, httpServletResponse);
}
});
server.enqueueEcho();
client.preparePost(getTargetUrl()).setHeaders(h).setBody(new ByteArrayInputStream("{}".getBytes(StandardCharsets.ISO_8859_1))).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getResponseBody(), "{}");
return response;
}
}).get(TIMEOUT, SECONDS);
});
});
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.
the class NonAsciiContentLengthTest method setUpGlobal.
@BeforeClass(alwaysRun = true)
public void setUpGlobal() throws Exception {
server = new Server();
ServerConnector connector = addHttpConnector(server);
server.setHandler(new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// Can only handle bodies of up to 1024 bytes.
int MAX_BODY_SIZE = 1024;
byte[] b = new byte[MAX_BODY_SIZE];
int offset = 0;
int numBytesRead;
try (ServletInputStream is = request.getInputStream()) {
while ((numBytesRead = is.read(b, offset, MAX_BODY_SIZE - offset)) != -1) {
offset += numBytesRead;
}
}
assertEquals(request.getContentLength(), offset);
response.setStatus(200);
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentLength(request.getContentLength());
try (ServletOutputStream os = response.getOutputStream()) {
os.write(b, 0, offset);
}
}
});
server.start();
port1 = connector.getLocalPort();
}
use of org.eclipse.jetty.server.handler.AbstractHandler in project async-http-client by AsyncHttpClient.
the class RedirectTest method setUpGlobal.
@BeforeClass
@Override
public void setUpGlobal() throws Exception {
server = new Server();
ServerConnector connector1 = addHttpConnector(server);
ServerConnector connector2 = addHttpConnector(server);
HandlerList list = new HandlerList();
list.addHandler(new AbstractHandler() {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
if (request.getLocalPort() == port2) {
httpServletResponse.sendRedirect(getTargetUrl());
}
}
});
list.addHandler(getWebSocketHandler());
server.setHandler(list);
server.start();
port1 = connector1.getLocalPort();
port2 = connector2.getLocalPort();
logger.info("Local HTTP server started successfully");
}
Aggregations