Search in sources :

Example 1 with PolicyChain

use of io.gravitee.gateway.policy.impl.PolicyChain in project gravitee-gateway by gravitee-io.

the class ApiReactorHandler method handleProxyResponse.

private void handleProxyResponse(Request serverRequest, final Response serverResponse, ExecutionContext executionContext, ProxyResponse proxyResponse, long serviceInvocationStart, Handler<Response> handler) {
    if (proxyResponse == null || proxyResponse instanceof DirectProxyConnection.DirectResponse) {
        serverResponse.status((proxyResponse == null) ? HttpStatusCode.SERVICE_UNAVAILABLE_503 : proxyResponse.status());
        serverResponse.end();
        handler.handle(serverResponse);
    } else {
        // Set the status
        serverResponse.status(proxyResponse.status());
        // Copy HTTP headers
        proxyResponse.headers().forEach((headerName, headerValues) -> serverResponse.headers().put(headerName, headerValues));
        // Calculate response policies
        PolicyChain responsePolicyChain = apiPolicyResolver.resolve(StreamType.ON_RESPONSE, serverRequest, serverResponse, executionContext);
        responsePolicyChain.setResultHandler(responsePolicyResult -> {
            if (responsePolicyResult.isFailure()) {
                sendPolicyFailure(responsePolicyResult, serverResponse);
                handler.handle(serverResponse);
            } else {
                responsePolicyChain.bodyHandler(chunk -> {
                    serverResponse.write(chunk);
                    serverRequest.metrics().setResponseContentLength(serverRequest.metrics().getResponseContentLength() + chunk.length());
                });
                responsePolicyChain.endHandler(responseEndResult -> {
                    serverRequest.metrics().setApiResponseTimeMs(System.currentTimeMillis() - serviceInvocationStart);
                    serverResponse.end();
                    // Transfer proxy response to the initial consumer
                    handler.handle(serverResponse);
                });
                proxyResponse.bodyHandler(buffer -> {
                    if (serverResponse.writeQueueFull()) {
                        proxyResponse.pause();
                        serverResponse.drainHandler(aVoid -> proxyResponse.resume());
                    }
                    responsePolicyChain.write(buffer);
                });
                proxyResponse.endHandler(aVoid -> responsePolicyChain.end());
            }
        });
        responsePolicyChain.setStreamErrorHandler(result -> {
            sendPolicyFailure(result, serverResponse);
            handler.handle(serverResponse);
        });
        // Execute response policy chain
        responsePolicyChain.doNext(serverRequest, serverResponse);
        // Resume response read
        proxyResponse.resume();
    }
}
Also used : DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain)

Example 2 with PolicyChain

use of io.gravitee.gateway.policy.impl.PolicyChain in project gravitee-gateway by gravitee-io.

the class GlobalPolicyChainTest method doNext_multiplePolicyOrder.

@Test
public void doNext_multiplePolicyOrder() throws Exception {
    List<Policy> policies = policies2();
    PolicyChain requestChain = RequestPolicyChain.create(policies, mock(ExecutionContext.class));
    requestChain.setResultHandler(result -> {
    });
    PolicyChain responseChain = ResponsePolicyChain.create(policies, mock(ExecutionContext.class));
    responseChain.setResultHandler(result -> {
    });
    InOrder requestOrder = inOrder(policy, policy2);
    InOrder responseOrder = inOrder(policy, policy2);
    requestChain.doNext(null, null);
    responseChain.doNext(null, null);
    requestOrder.verify(policy).onRequest(anyVararg());
    requestOrder.verify(policy2).onRequest(anyVararg());
    responseOrder.verify(policy2).onResponse(anyVararg());
    responseOrder.verify(policy).onResponse(anyVararg());
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 3 with PolicyChain

use of io.gravitee.gateway.policy.impl.PolicyChain in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_multiplePolicy_throwError.

@Test
public void doNext_multiplePolicy_throwError() throws Exception {
    ExecutionContext executionContext = mock(ExecutionContext.class);
    Request request = mock(Request.class);
    Metrics metrics = Metrics.on(System.currentTimeMillis()).build();
    when(request.metrics()).thenReturn(metrics);
    PolicyChain chain = RequestPolicyChain.create(policies3(), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(request, null);
    verify(request, atLeastOnce()).metrics();
    verify(policy3, atLeastOnce()).onRequest(request, null, chain, executionContext);
    verify(policy2, never()).onRequest(request, null, chain);
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) Metrics(io.gravitee.reporter.api.http.Metrics) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 4 with PolicyChain

use of io.gravitee.gateway.policy.impl.PolicyChain in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_multiplePolicyOrder.

@Test
public void doNext_multiplePolicyOrder() throws Exception {
    PolicyChain chain = RequestPolicyChain.create(policies2(), mock(ExecutionContext.class));
    chain.setResultHandler(result -> {
    });
    InOrder inOrder = inOrder(policy, policy2);
    chain.doNext(null, null);
    inOrder.verify(policy).onRequest(anyVararg());
    inOrder.verify(policy2).onRequest(anyVararg());
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) InOrder(org.mockito.InOrder) Test(org.junit.Test)

Example 5 with PolicyChain

use of io.gravitee.gateway.policy.impl.PolicyChain in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_multiplePolicy.

@Test
public void doNext_multiplePolicy() throws Exception {
    ExecutionContext executionContext = mock(ExecutionContext.class);
    PolicyChain chain = RequestPolicyChain.create(policies2(), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(null, null);
    verify(policy, atLeastOnce()).onRequest(null, null, chain, executionContext);
    verify(policy2, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Test(org.junit.Test)

Aggregations

PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)21 Test (org.junit.Test)20 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)17 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)12 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)9 Request (io.gravitee.gateway.api.Request)8 InOrder (org.mockito.InOrder)7 Response (io.gravitee.gateway.api.Response)6 Handler (io.gravitee.gateway.api.handler.Handler)6 BufferedReadWriteStream (io.gravitee.gateway.api.stream.BufferedReadWriteStream)6 ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)6 Metrics (io.gravitee.reporter.api.http.Metrics)2 DirectProxyConnection (io.gravitee.gateway.http.core.direct.DirectProxyConnection)1