Search in sources :

Example 1 with RestServerSendEvent

use of com.alipay.sofa.rpc.event.rest.RestServerSendEvent 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)

Example 2 with RestServerSendEvent

use of com.alipay.sofa.rpc.event.rest.RestServerSendEvent in project sofa-rpc by sofastack.

the class RestLookoutAdapter method sendRestServerSendEvent.

public static void sendRestServerSendEvent(RestServerSendEvent restServerSendEvent) {
    // this is special for rest
    if (EventBus.isEnable(ServerSendEvent.class)) {
        SofaRequest request = new SofaRequest();
        String appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
        request.setTargetAppName(appName);
        request.addRequestProp(RemotingConstants.HEAD_APP_NAME, restServerSendEvent.getRequest().getHttpHeaders().getHeaderString(RemotingConstants.HEAD_APP_NAME));
        RpcInternalContext context = RpcInternalContext.getContext();
        request.setTargetServiceUniqueName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_SERVICE_KEY));
        request.setMethodName((String) context.getAttachment(INTERNAL_KEY_PREFIX + RestConstants.REST_METHODNAME_KEY));
        request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
        request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
        SofaResponse response = new SofaResponse();
        if (restServerSendEvent.getThrowable() != null) {
            response.setErrorMsg(restServerSendEvent.getThrowable().getMessage());
        }
        final ServerSendEvent event = new ServerSendEvent(request, response, restServerSendEvent.getThrowable());
        EventBus.post(event);
    }
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ServerSendEvent(com.alipay.sofa.rpc.event.ServerSendEvent) RestServerSendEvent(com.alipay.sofa.rpc.event.rest.RestServerSendEvent) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 3 with RestServerSendEvent

use of com.alipay.sofa.rpc.event.rest.RestServerSendEvent in project sofa-rpc by sofastack.

the class RestTracerSubscriber method onEvent.

@Override
public void onEvent(Event originEvent) {
    if (!Tracers.isEnable()) {
        return;
    }
    Class eventClass = originEvent.getClass();
    if (eventClass == RestServerReceiveEvent.class) {
        RestServerReceiveEvent event = (RestServerReceiveEvent) originEvent;
        RestTracerAdapter.serverReceived(event.getRequest());
    } else if (eventClass == RestServerSendEvent.class) {
        RestServerSendEvent event = (RestServerSendEvent) originEvent;
        RestTracerAdapter.serverSend(event.getResponse(), event.getThrowable());
    }
}
Also used : RestServerReceiveEvent(com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent) RestServerSendEvent(com.alipay.sofa.rpc.event.rest.RestServerSendEvent)

Aggregations

RestServerSendEvent (com.alipay.sofa.rpc.event.rest.RestServerSendEvent)3 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)2 RestServerReceiveEvent (com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent)2 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 ServerEndHandleEvent (com.alipay.sofa.rpc.event.ServerEndHandleEvent)1 ServerSendEvent (com.alipay.sofa.rpc.event.ServerSendEvent)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