Search in sources :

Example 81 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.

the class NettyHttpRequestHandler method buildHttpResponse.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected FullHttpResponse buildHttpResponse(Response response, boolean keepAlive) throws Exception {
    Object value = response.getValue();
    byte[] responseBytes = null;
    if (value instanceof Message) {
        Marshaller marshaller = ProtoUtils.jsonMarshaller((Message) value.getClass().getMethod("getDefaultInstance", null).invoke(null, null));
        InputStream is = marshaller.stream(value);
        responseBytes = new byte[is.available()];
        is.read(responseBytes);
    } else {
    // TODO not pb
    }
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
    httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded");
    httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());
    if (keepAlive) {
        httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    } else {
        httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    }
    return httpResponse;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Marshaller(io.grpc.MethodDescriptor.Marshaller) Message(com.google.protobuf.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 82 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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 83 with FullHttpResponse

use of org.apache.flink.shaded.netty4.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 84 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.

the class YarMessageHandlerWarpperTest method testAbnormal.

@Test
public void testAbnormal() throws Exception {
    final String errmsg = "rpc process error";
    YarMessageHandlerWarpper handler = new YarMessageHandlerWarpper(new YarMessageRouter() {

        @Override
        public Object handle(Channel channel, Object message) {
            throw new RuntimeException(errmsg);
        }
    });
    // yar协议无法解析
    FullHttpResponse httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(null, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    YarResponse retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertNotNull(retYarResponse.getError());
    // yar协议可以正常解析,但后续处理异常
    YarRequest yarRequest = new YarRequest(123, "JSON", "testmethod", new Object[] { "params", 456 });
    httpResponse = (FullHttpResponse) handler.handle(new MockChannel(), buildHttpRequest(yarRequest, uri));
    assertNotNull(httpResponse);
    assertEquals(HttpResponseStatus.OK, httpResponse.getStatus());
    retYarResponse = getYarResponse(httpResponse);
    assertNotNull(retYarResponse);
    assertEquals(errmsg, retYarResponse.getError());
}
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) Test(org.junit.Test)

Example 85 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project netty by netty.

the class WebSocketClientHandshaker method processHandshake.

/**
 * Process the opening handshake initiated by {@link #handshake}}.
 *
 * @param channel
 *            Channel
 * @param response
 *            HTTP response containing the closing handshake details
 * @param promise
 *            the {@link ChannelPromise} to notify once the handshake completes.
 * @return future
 *            the {@link ChannelFuture} which is notified once the handshake completes.
 */
public final ChannelFuture processHandshake(final Channel channel, HttpResponse response, final ChannelPromise promise) {
    if (response instanceof FullHttpResponse) {
        try {
            finishHandshake(channel, (FullHttpResponse) response);
            promise.setSuccess();
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    } else {
        ChannelPipeline p = channel.pipeline();
        ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
        if (ctx == null) {
            ctx = p.context(HttpClientCodec.class);
            if (ctx == null) {
                return promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpResponseDecoder or HttpClientCodec"));
            }
        }
        // Add aggregator and ensure we feed the HttpResponse so it is aggregated. A limit of 8192 should be more
        // then enough for the websockets handshake payload.
        // 
        // TODO: Make handshake work without HttpObjectAggregator at all.
        String aggregatorName = "httpAggregator";
        p.addAfter(ctx.name(), aggregatorName, new HttpObjectAggregator(8192));
        p.addAfter(aggregatorName, "handshaker", new SimpleChannelInboundHandler<FullHttpResponse>() {

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
                // Remove ourself and do the actual handshake
                ctx.pipeline().remove(this);
                try {
                    finishHandshake(channel, msg);
                    promise.setSuccess();
                } catch (Throwable cause) {
                    promise.setFailure(cause);
                }
            }

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                // Remove ourself and fail the handshake promise.
                ctx.pipeline().remove(this);
                promise.setFailure(cause);
            }

            @Override
            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                // Fail promise if Channel was closed
                if (!promise.isDone()) {
                    promise.tryFailure(new ClosedChannelException());
                }
                ctx.fireChannelInactive();
            }
        });
        try {
            ctx.fireChannelRead(ReferenceCountUtil.retain(response));
        } catch (Throwable cause) {
            promise.setFailure(cause);
        }
    }
    return promise;
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ClosedChannelException(java.nio.channels.ClosedChannelException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19