Search in sources :

Example 36 with FullHttpResponse

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) RandomAccessFile(java.io.RandomAccessFile) ChunkedFile(io.netty.handler.stream.ChunkedFile) File(java.io.File)

Example 37 with FullHttpResponse

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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 38 with FullHttpResponse

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);
}
Also used : YarMessageRouter(com.weibo.api.motan.protocol.yar.YarMessageRouter) YarRequest(com.weibo.yar.YarRequest) Channel(com.weibo.api.motan.transport.Channel) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) YarResponse(com.weibo.yar.YarResponse) AttachmentRequest(com.weibo.api.motan.protocol.yar.AttachmentRequest) Test(org.junit.Test)

Example 39 with FullHttpResponse

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);
}
Also used : FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 40 with FullHttpResponse

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);
}
Also used : Expectations(org.jmock.Expectations) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Channel(com.weibo.api.motan.transport.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Test(org.junit.Test)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)83 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)50 ByteBuf (io.netty.buffer.ByteBuf)24 Test (org.junit.Test)16 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)11 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)7 HttpRequest (io.netty.handler.codec.http.HttpRequest)6 ChannelFuture (io.netty.channel.ChannelFuture)5 ChannelPipeline (io.netty.channel.ChannelPipeline)5 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)5 HashMap (java.util.HashMap)5 Channel (com.weibo.api.motan.transport.Channel)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Map (java.util.Map)4 Pair (com.nike.internal.util.Pair)3