use of io.gravitee.gateway.api.Invoker in project gravitee-gateway by gravitee-io.
the class ApiReactorHandler method handleClientRequest.
private void handleClientRequest(Request serverRequest, Response serverResponse, ExecutionContext executionContext, Handler<Response> handler) {
serverRequest.pause();
// Apply request policies
RequestPolicyChainProcessor requestPolicyChain = new RequestPolicyChainProcessor(policyResolvers);
requestPolicyChain.setResultHandler(requestPolicyChainResult -> {
if (requestPolicyChainResult.isFailure()) {
sendPolicyFailure(requestPolicyChainResult.getPolicyResult(), serverResponse);
handler.handle(serverResponse);
} else {
// Call an invoker to get a proxy connection (connection to an underlying backend, mainly HTTP)
Invoker upstreamInvoker = (Invoker) executionContext.getAttribute(ExecutionContext.ATTR_INVOKER);
long serviceInvocationStart = System.currentTimeMillis();
AtomicReference<ProxyConnection> proxyConnection = new AtomicReference<>();
Request invokeRequest = upstreamInvoker.invoke(executionContext, serverRequest, requestPolicyChainResult.getPolicyChain(), connection -> {
proxyConnection.set(connection);
connection.responseHandler(proxyResponse -> handleProxyResponse(serverRequest, serverResponse, executionContext, proxyResponse, serviceInvocationStart, handler));
requestPolicyChain.setStreamErrorHandler(result -> {
connection.cancel();
sendPolicyFailure(result.getPolicyResult(), serverResponse);
handler.handle(serverResponse);
});
});
// Plug server request stream to request policy chain stream
invokeRequest.bodyHandler(chunk -> requestPolicyChainResult.getPolicyChain().write(chunk)).endHandler(aVoid -> requestPolicyChainResult.getPolicyChain().end());
}
});
requestPolicyChain.execute(serverRequest, serverResponse, executionContext);
}
Aggregations