use of io.apiman.gateway.engine.policy.ResponseChain in project apiman by apiman.
the class ApiRequestExecutorImpl method createRequestChain.
/**
* Creates the chain used to apply policies in order to the api request.
*/
private Chain<ApiRequest> createRequestChain(IAsyncHandler<ApiRequest> requestHandler) {
RequestChain chain = new RequestChain(policyImpls, context);
chain.headHandler(requestHandler);
chain.policyFailureHandler(failure -> {
// It will likely not have been initialised, so create one.
if (responseChain == null) {
// Its response will not be used as we take the failure path only, so we just use an empty lambda.
responseChain = createResponseChain((ignored) -> {
});
}
responseChain.doFailure(failure);
});
chain.policyErrorHandler(policyErrorHandler);
return chain;
}
use of io.apiman.gateway.engine.policy.ResponseChain in project apiman by apiman.
the class DataPolicyChainTest method shouldEnsureNonNullResponseReceivedByHandlers.
@Test
public void shouldEnsureNonNullResponseReceivedByHandlers() {
policies.add(pwcOne);
responseChain = new ResponseChain(policies, mockContext);
responseChain.bodyHandler(mockBodyHandler);
responseChain.endHandler(mockEndHandler);
responseChain.doApply(mockResponse);
responseChain.write(mockBuffer);
responseChain.end();
verify(mockBodyHandler, times(1)).handle(mockBuffer);
verify(mockEndHandler, times(1)).handle((Void) null);
// At this point we must ensure that the request and responses are NOT null.
verify(policyOne).getResponseDataHandler(mockResponse, mockContext, configuration);
}
use of io.apiman.gateway.engine.policy.ResponseChain in project apiman by apiman.
the class PolicyChainTest method shouldExecuteResponseChainTwice.
@Test
public void shouldExecuteResponseChainTwice() {
policies.add(pwcOne);
policies.add(pwcTwo);
responseChain = new ResponseChain(policies, mockContext);
responseChain.bodyHandler(mockBodyHandler);
responseChain.endHandler(mockEndHandler);
responseChain.doApply(mockResponse);
responseChain.write(mockBuffer);
responseChain.write(mockBuffer);
responseChain.end();
verify(mockBodyHandler, times(2)).handle(mockBuffer);
verify(mockEndHandler, times(1)).handle((Void) null);
InOrder order = inOrder(policyTwo, policyOne);
order.verify(policyTwo).apply(mockResponse, mockContext, pwcTwo.getConfiguration(), responseChain);
order.verify(policyOne).apply(mockResponse, mockContext, pwcOne.getConfiguration(), responseChain);
}
use of io.apiman.gateway.engine.policy.ResponseChain in project apiman by apiman.
the class ApiRequestExecutorImpl method createResponseChain.
/**
* Creates the chain used to apply policies in reverse order to the api response.
*/
private Chain<ApiResponse> createResponseChain(IAsyncHandler<ApiResponse> responseHandler) {
ResponseChain chain = new ResponseChain(policyImpls, context);
chain.headHandler(responseHandler);
chain.policyFailureHandler(result -> {
if (apiConnectionResponse != null) {
apiConnectionResponse.abort();
}
policyFailureHandler.handle(result);
});
chain.policyErrorHandler(result -> {
if (apiConnectionResponse != null) {
apiConnectionResponse.abort();
}
policyErrorHandler.handle(result);
});
return chain;
}
Aggregations