Search in sources :

Example 1 with RequestPolicyChainProcessor

use of io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor 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);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) CorsHandler(io.gravitee.gateway.handlers.api.cors.CorsHandler) HttpHeaders(io.gravitee.common.http.HttpHeaders) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) LoggerFactory(org.slf4j.LoggerFactory) HttpHeadersValues(io.gravitee.common.http.HttpHeadersValues) Autowired(org.springframework.beans.factory.annotation.Autowired) Invoker(io.gravitee.gateway.api.Invoker) PolicyManager(io.gravitee.gateway.policy.PolicyManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) LoggableClientResponse(io.gravitee.gateway.handlers.api.logging.LoggableClientResponse) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Response(io.gravitee.gateway.api.Response) StreamType(io.gravitee.gateway.policy.StreamType) Reactable(io.gravitee.gateway.reactor.Reactable) Request(io.gravitee.gateway.api.Request) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) Logger(org.slf4j.Logger) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) PolicyChainResolver(io.gravitee.gateway.policy.PolicyChainResolver) TemplateContext(io.gravitee.gateway.api.expression.TemplateContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) Throwables(com.google.common.base.Throwables) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) Buffer(io.gravitee.gateway.api.buffer.Buffer) Handler(io.gravitee.gateway.api.handler.Handler) CorsResponseHandler(io.gravitee.gateway.handlers.api.cors.CorsResponseHandler) EndpointLifecycleManager(io.gravitee.gateway.http.core.endpoint.EndpointLifecycleManager) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) ProxyResponse(io.gravitee.gateway.api.proxy.ProxyResponse) ExecutionContextFactory(io.gravitee.gateway.handlers.api.context.ExecutionContextFactory) Cors(io.gravitee.definition.model.Cors) TemplateVariableProvider(io.gravitee.gateway.api.expression.TemplateVariableProvider) PlanPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.plan.PlanPolicyChainResolver) Api(io.gravitee.gateway.handlers.api.definition.Api) AbstractReactorHandler(io.gravitee.gateway.reactor.handler.AbstractReactorHandler) PolicyResult(io.gravitee.policy.api.PolicyResult) SecurityPolicyChainResolver(io.gravitee.gateway.security.core.SecurityPolicyChainResolver) ApiPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.api.ApiPolicyChainResolver) Invoker(io.gravitee.gateway.api.Invoker) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Request(io.gravitee.gateway.api.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor)

Aggregations

JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Throwables (com.google.common.base.Throwables)1 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 HttpHeadersValues (io.gravitee.common.http.HttpHeadersValues)1 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 MediaType (io.gravitee.common.http.MediaType)1 Cors (io.gravitee.definition.model.Cors)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Invoker (io.gravitee.gateway.api.Invoker)1 Request (io.gravitee.gateway.api.Request)1 Response (io.gravitee.gateway.api.Response)1 Buffer (io.gravitee.gateway.api.buffer.Buffer)1 TemplateContext (io.gravitee.gateway.api.expression.TemplateContext)1 TemplateVariableProvider (io.gravitee.gateway.api.expression.TemplateVariableProvider)1 Handler (io.gravitee.gateway.api.handler.Handler)1 ProxyConnection (io.gravitee.gateway.api.proxy.ProxyConnection)1 ProxyResponse (io.gravitee.gateway.api.proxy.ProxyResponse)1 ExecutionContextFactory (io.gravitee.gateway.handlers.api.context.ExecutionContextFactory)1