use of io.netty.handler.codec.http.FullHttpResponse in project async-http-client by AsyncHttpClient.
the class HttpStaticFileServerHandler method sendListing.
private static void sendListing(ChannelHandlerContext ctx, File dir) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
String dirPath = dir.getPath();
StringBuilder buf = new StringBuilder().append("<!DOCTYPE html>\r\n").append("<html><head><title>").append("Listing of: ").append(dirPath).append("</title></head><body>\r\n").append("<h3>Listing of: ").append(dirPath).append("</h3>\r\n").append("<ul>").append("<li><a href=\"../\">..</a></li>\r\n");
for (File f : dir.listFiles()) {
if (f.isHidden() || !f.canRead()) {
continue;
}
String name = f.getName();
if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
continue;
}
buf.append("<li><a href=\"").append(name).append("\">").append(name).append("</a></li>\r\n");
}
buf.append("</ul></body></html>\r\n");
ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8);
response.content().writeBytes(buffer);
buffer.release();
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.http.FullHttpResponse in project async-http-client by AsyncHttpClient.
the class HttpStaticFileServerHandler method sendError.
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class YarMessageHandlerWarpperTest method testHandle.
@Test
public void testHandle() throws Exception {
YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
final YarResponse yarResponse = YarProtocolUtil.buildDefaultErrorResponse("test err", "JSON");
YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {
@Override
public Object handle(Channel channel, Object message) {
AttachmentRequest request = (AttachmentRequest) message;
verifyAttachments(request.getAttachments());
return yarResponse;
}
});
FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
assertNotNull(httpResponse);
assertNotNull(httpResponse.content());
YarResponse retYarResponse = getYarResponse(httpResponse);
assertNotNull(retYarResponse);
assertEquals(yarResponse, retYarResponse);
}
use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandler method processHttpRequest.
protected void processHttpRequest(ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
FullHttpResponse httpResponse = null;
try {
httpResponse = (FullHttpResponse) messageHandler.handle(serverChannel, httpRequest);
} catch (Exception e) {
LoggerUtil.error("NettyHttpHandler process http request fail.", e);
httpResponse = buildErrorResponse(e.getMessage());
} finally {
httpRequest.content().release();
}
sendResponse(ctx, httpResponse);
}
use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandlerTest method testChannelRead0.
@Test
public void testChannelRead0() throws Exception {
final MessageHandler messageHandler = mockery.mock(MessageHandler.class);
final ChannelHandlerContext ctx = mockery.mock(ChannelHandlerContext.class);
final FullHttpResponse response = mockery.mock(FullHttpResponse.class);
mockery.checking(new Expectations() {
{
allowing(ctx).write(with(any(FullHttpResponse.class)));
will(new CustomAction("verify") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
FullHttpResponse actualResponse = (FullHttpResponse) invocation.getParameter(0);
assertNotNull(actualResponse);
assertEquals(response, actualResponse);
return null;
}
});
allowing(ctx).flush();
will(returnValue(null));
allowing(ctx).close();
will(returnValue(null));
atLeast(1).of(messageHandler).handle(with(any(Channel.class)), with(anything()));
will(returnValue(response));
allowing(response).headers();
will(returnValue(new DefaultHttpHeaders()));
}
});
FullHttpRequest httpRequest = buildHttpRequest("anyPath");
NettyHttpRequestHandler handler = new NettyHttpRequestHandler(null, messageHandler);
handler.channelRead0(ctx, httpRequest);
}
Aggregations