use of com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent 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();
}
}
}
use of com.alipay.sofa.rpc.event.rest.RestServerReceiveEvent 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());
}
}
Aggregations