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();
}
}
}
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);
}
}
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());
}
}
Aggregations