Search in sources :

Example 1 with NettyHttpRequest

use of org.jboss.resteasy.plugins.server.netty.NettyHttpRequest in project sofa-rpc by sofastack.

the class SofaRestRequestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof NettyHttpRequest) {
        NettyHttpRequest request = (NettyHttpRequest) msg;
        try {
            if (EventBus.isEnable(RestServerReceiveEvent.class)) {
                EventBus.post(new RestServerReceiveEvent(request));
            }
            if (request.getUri().getPath().endsWith(RemotingConstants.IGNORE_WEB_BROWSER)) {
                HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
                ctx.writeAndFlush(response);
                return;
            }
            if (request.is100ContinueExpected()) {
                send100Continue(ctx);
            }
            NettyHttpResponse response = request.getResponse();
            Exception exception = null;
            try {
                RpcInternalContext context = RpcInternalContext.getContext();
                context.setProviderSide(true);
                // 获取远程ip 兼容 nignx 转发和 vip 等
                HttpHeaders httpHeaders = request.getHttpHeaders();
                String remoteIP = httpHeaders.getHeaderString("X-Forwarded-For");
                if (remoteIP == null) {
                    remoteIP = httpHeaders.getHeaderString("X-Real-IP");
                }
                if (remoteIP != null) {
                    context.setRemoteAddress(remoteIP, 0);
                } else {
                    // request取不到就从channel里取
                    context.setRemoteAddress((InetSocketAddress) ctx.channel().remoteAddress());
                }
                // 设置本地地址
                context.setLocalAddress((InetSocketAddress) ctx.channel().localAddress());
                dispatcher.service(ctx, request, response, true);
            } catch (Failure e1) {
                response.reset();
                response.setStatus(e1.getErrorCode());
                exception = e1;
            } catch (Exception ex) {
                response.reset();
                response.setStatus(500);
                // todo 异常带给用户?
                logger.error(LogCodes.getLog(LogCodes.ERROR_PROCESS_UNKNOWN), ex);
                exception = ex;
            } finally {
                if (EventBus.isEnable(RestServerSendEvent.class)) {
                    EventBus.post(new RestServerSendEvent(request, response, exception));
                }
                RestLookoutAdapter.sendRestServerSendEvent(new RestServerSendEvent(request, response, exception));
            }
            if (!request.getAsyncContext().isSuspended()) {
                response.finish();
            }
        } finally {
            /**
             * issue: https://github.com/sofastack/sofa-rpc/issues/592
             */
            request.releaseContentBuffer();
            if (EventBus.isEnable(ServerEndHandleEvent.class)) {
                EventBus.post(new ServerEndHandleEvent());
            }
            RpcInvokeContext.removeContext();
            RpcInternalContext.removeAllContext();
        }
    }
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) NettyHttpResponse(org.jboss.resteasy.plugins.server.netty.NettyHttpResponse) RestServerReceiveEvent(com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) RestServerSendEvent(com.alipay.sofa.rpc.event.rest.RestServerSendEvent) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) NettyHttpResponse(org.jboss.resteasy.plugins.server.netty.NettyHttpResponse) ServerEndHandleEvent(com.alipay.sofa.rpc.event.ServerEndHandleEvent) NettyHttpRequest(org.jboss.resteasy.plugins.server.netty.NettyHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Failure(org.jboss.resteasy.spi.Failure)

Aggregations

RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 ServerEndHandleEvent (com.alipay.sofa.rpc.event.ServerEndHandleEvent)1 RestServerReceiveEvent (com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent)1 RestServerSendEvent (com.alipay.sofa.rpc.event.rest.RestServerSendEvent)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 NettyHttpRequest (org.jboss.resteasy.plugins.server.netty.NettyHttpRequest)1 NettyHttpResponse (org.jboss.resteasy.plugins.server.netty.NettyHttpResponse)1 Failure (org.jboss.resteasy.spi.Failure)1