in dubbo-rpc-extensions/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/filter/ServiceInvokeRestFilter.java [77:160]
private void doHandler(
HttpRequest nettyHttpRequest,
NettyHttpResponse nettyHttpResponse,
RequestFacade request,
URL url,
Object originRequest, // resteasy request
ServiceDeployer serviceDeployer)
throws Exception {
PathMatcher pathMatcher = RestRPCInvocationUtil.createPathMatcher(request);
// path NoFound 404
if (!serviceDeployer.hashRestMethod(pathMatcher)) {
throw new PathNoFoundException("rest service Path no found, current path info:" + pathMatcher);
}
// method disallowed
if (!serviceDeployer.isMethodAllowed(pathMatcher)) {
nettyHttpResponse.sendError(
405,
"service require request method is : "
+ serviceDeployer.pathHttpMethods(pathMatcher)
+ ", but current request method is: " + request.getMethod());
return;
}
// compare http method and acquire metadata by request
InvokerAndRestMethodMetadataPair restMethodMetadataPair =
RestRPCInvocationUtil.getRestMethodMetadataAndInvokerPair(
pathMatcher.compareHttpMethod(true), serviceDeployer);
Invoker invoker = restMethodMetadataPair.getInvoker();
RestMethodMetadata restMethodMetadata = restMethodMetadataPair.getRestMethodMetadata();
// content-type support judge,throw unSupportException
acceptSupportJudge(request, restMethodMetadata.getReflectMethod().getReturnType());
// build RpcInvocation
RpcInvocation rpcInvocation = RestRPCInvocationUtil.createBaseRpcInvocation(request, restMethodMetadata);
// parse method real args
RestRPCInvocationUtil.parseMethodArgs(
rpcInvocation, request, nettyHttpRequest, nettyHttpResponse, restMethodMetadata);
// execute business method invoke
Result result = invoker.invoke(rpcInvocation);
// set raw response
nettyHttpResponse.setResponseBody(result.getValue());
if (result.hasException()) {
Throwable exception = result.getException();
logger.error(
"", exception.getMessage(), "", "dubbo rest protocol provider Invoker invoke error", exception);
if (serviceDeployer.getExceptionMapper().hasExceptionMapper(exception)) {
ExceptionHandlerResult exceptionToResult =
serviceDeployer.getExceptionMapper().exceptionToResult(result.getException());
writeResult(
nettyHttpResponse, request, url, exceptionToResult.getEntity(), rpcInvocation.getReturnType());
nettyHttpResponse.setStatus(exceptionToResult.getStatus());
} else {
nettyHttpResponse.sendError(
500,
"\n dubbo rest business exception, error cause is: "
+ result.getException().getCause()
+ "\n message is: " + result.getException().getMessage()
+ "\n stacktrace is: " + stackTraceToString(exception));
}
}
try {
RestInterceptContext restFilterContext = new RestInterceptContext(
url, request, nettyHttpResponse, serviceDeployer, result.getValue(), rpcInvocation);
// set filter request
restFilterContext.setOriginRequest(originRequest);
// invoke the intercept chain before Result write to response
executeResponseIntercepts(restFilterContext);
} catch (Exception exception) {
logger.error(
"", exception.getMessage(), "", "dubbo rest protocol execute ResponseIntercepts error", exception);
throw exception;
}
}