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