Search in sources :

Example 1 with RequestChain

use of io.apiman.gateway.engine.policy.RequestChain 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;
}
Also used : Date(java.util.Date) IPayloadIO(io.apiman.gateway.engine.io.IPayloadIO) JsonPayloadIO(io.apiman.gateway.engine.io.JsonPayloadIO) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) RequestChain(io.apiman.gateway.engine.policy.RequestChain) BytesPayloadIO(io.apiman.gateway.engine.io.BytesPayloadIO) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) Map(java.util.Map) IApiConnector(io.apiman.gateway.engine.IApiConnector) GatewayConfigProperties(io.apiman.gateway.engine.GatewayConfigProperties) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) RequestAbortedException(io.apiman.gateway.engine.beans.exceptions.RequestAbortedException) RequestMetric(io.apiman.gateway.engine.metrics.RequestMetric) IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) Policy(io.apiman.gateway.engine.beans.Policy) Set(java.util.Set) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) IConnectorInterceptor(io.apiman.gateway.engine.policy.IConnectorInterceptor) List(java.util.List) IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) InvalidApiException(io.apiman.gateway.engine.beans.exceptions.InvalidApiException) Messages(io.apiman.gateway.engine.i18n.Messages) IRegistry(io.apiman.gateway.engine.IRegistry) Entry(java.util.Map.Entry) IApiConnection(io.apiman.gateway.engine.IApiConnection) Chain(io.apiman.gateway.engine.policy.Chain) IPolicy(io.apiman.gateway.engine.policy.IPolicy) IApiConnectionResponse(io.apiman.gateway.engine.IApiConnectionResponse) PolicyContextKeys(io.apiman.gateway.engine.policy.PolicyContextKeys) AsyncResultImpl(io.apiman.gateway.engine.async.AsyncResultImpl) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) HashSet(java.util.HashSet) ApimanStrLookup(io.apiman.common.util.ApimanStrLookup) SoapPayloadIO(io.apiman.gateway.engine.io.SoapPayloadIO) IConnectorConfig(io.apiman.gateway.engine.IConnectorConfig) IApiRequestExecutor(io.apiman.gateway.engine.IApiRequestExecutor) RequiredAuthType(io.apiman.gateway.engine.auth.RequiredAuthType) IBufferFactoryComponent(io.apiman.gateway.engine.components.IBufferFactoryComponent) StrLookup(org.apache.commons.lang3.text.StrLookup) IEngineResult(io.apiman.gateway.engine.IEngineResult) ByteBuffer(io.apiman.gateway.engine.io.ByteBuffer) InvalidContractException(io.apiman.gateway.engine.beans.exceptions.InvalidContractException) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) Api(io.apiman.gateway.engine.beans.Api) ResponseChain(io.apiman.gateway.engine.policy.ResponseChain) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) IMetrics(io.apiman.gateway.engine.IMetrics) IConnectorFactory(io.apiman.gateway.engine.IConnectorFactory) IAsyncHandler(io.apiman.gateway.engine.async.IAsyncHandler) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ApiContract(io.apiman.gateway.engine.beans.ApiContract) IPolicyFactory(io.apiman.gateway.engine.policy.IPolicyFactory) XmlPayloadIO(io.apiman.gateway.engine.io.XmlPayloadIO) RequestChain(io.apiman.gateway.engine.policy.RequestChain)

Example 2 with RequestChain

use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.

the class DataPolicyChainTest method shouldEnsureNonNullRequestReceivedByHandlers.

@Test
public void shouldEnsureNonNullRequestReceivedByHandlers() {
    policies.add(pwcOne);
    requestChain = new RequestChain(policies, mockContext);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.write(mockBuffer);
    requestChain.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).getRequestDataHandler(mockRequest, mockContext, configuration);
}
Also used : RequestChain(io.apiman.gateway.engine.policy.RequestChain) Test(org.junit.Test)

Example 3 with RequestChain

use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.

the class PolicyChainTest method shouldPreserveBufferOrder.

@Test
public void shouldPreserveBufferOrder() {
    policies.add(pwcOne);
    requestChain = new RequestChain(policies, mockContext);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    IApimanBuffer buffer1 = mock(IApimanBuffer.class);
    IApimanBuffer buffer2 = mock(IApimanBuffer.class);
    IApimanBuffer buffer3 = mock(IApimanBuffer.class);
    requestChain.write(buffer1);
    requestChain.write(buffer2);
    requestChain.write(buffer3);
    requestChain.end();
    InOrder order = inOrder(mockBodyHandler, mockEndHandler);
    order.verify(mockBodyHandler).handle(buffer1);
    order.verify(mockBodyHandler).handle(buffer2);
    order.verify(mockBodyHandler).handle(buffer3);
    order.verify(mockEndHandler).handle((Void) null);
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) InOrder(org.mockito.InOrder) RequestChain(io.apiman.gateway.engine.policy.RequestChain) Test(org.junit.Test)

Example 4 with RequestChain

use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.

the class PolicyChainTest method shouldEndChainImmediatelyWhenSkipCalled.

@Test
public void shouldEndChainImmediatelyWhenSkipCalled() {
    IPolicy skipPolicy = spy(new IPolicy() {

        @Override
        public Object parseConfiguration(String jsonConfiguration) throws ConfigurationParseException {
            return null;
        }

        @Override
        public void apply(ApiRequest request, IPolicyContext context, Object config, IPolicyChain<ApiRequest> chain) {
            chain.doSkip(request);
        }

        @Override
        public void apply(ApiResponse response, IPolicyContext context, Object config, IPolicyChain<ApiResponse> chain) {
            chain.doSkip(response);
        }
    });
    PolicyWithConfiguration pwcSkip = new PolicyWithConfiguration(skipPolicy, null);
    policies.add(pwcSkip);
    policies.add(pwcTwo);
    requestChain = new RequestChain(policies, mockContext);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.end();
    verify(mockEndHandler, times(1)).handle((Void) null);
    // Should only be called once, as the second is skipped
    verify(skipPolicy, times(1)).apply(mockRequest, mockContext, null, requestChain);
    verify(policyOne, never()).apply(mockRequest, mockContext, null, requestChain);
}
Also used : IPolicy(io.apiman.gateway.engine.policy.IPolicy) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) ConfigurationParseException(io.apiman.gateway.engine.beans.exceptions.ConfigurationParseException) RequestChain(io.apiman.gateway.engine.policy.RequestChain) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Test(org.junit.Test)

Example 5 with RequestChain

use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.

the class PolicyChainTest method shouldCallFailureHandlerOnDoFail.

@Test
public void shouldCallFailureHandlerOnDoFail() {
    policies.add(pwcOne);
    policies.add(pwcTwo);
    requestChain = new RequestChain(policies, mockContext);
    IAsyncHandler<PolicyFailure> mPolicyFailureHandler = mock(IAsyncHandler.class);
    PolicyFailure mPolicyFailure = mock(PolicyFailure.class);
    requestChain.policyFailureHandler(mPolicyFailureHandler);
    requestChain.bodyHandler(mockBodyHandler);
    requestChain.endHandler(mockEndHandler);
    requestChain.doApply(mockRequest);
    requestChain.doFailure(mPolicyFailure);
    verify(mPolicyFailureHandler).handle(mPolicyFailure);
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RequestChain(io.apiman.gateway.engine.policy.RequestChain) Test(org.junit.Test)

Aggregations

RequestChain (io.apiman.gateway.engine.policy.RequestChain)8 Test (org.junit.Test)7 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)2 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)2 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)2 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)2 IPolicy (io.apiman.gateway.engine.policy.IPolicy)2 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)2 PolicyWithConfiguration (io.apiman.gateway.engine.policy.PolicyWithConfiguration)2 ApimanStrLookup (io.apiman.common.util.ApimanStrLookup)1 GatewayConfigProperties (io.apiman.gateway.engine.GatewayConfigProperties)1 IApiConnection (io.apiman.gateway.engine.IApiConnection)1 IApiConnectionResponse (io.apiman.gateway.engine.IApiConnectionResponse)1 IApiConnector (io.apiman.gateway.engine.IApiConnector)1 IApiRequestExecutor (io.apiman.gateway.engine.IApiRequestExecutor)1 IConnectorConfig (io.apiman.gateway.engine.IConnectorConfig)1 IConnectorFactory (io.apiman.gateway.engine.IConnectorFactory)1 IEngineResult (io.apiman.gateway.engine.IEngineResult)1 IMetrics (io.apiman.gateway.engine.IMetrics)1 IRegistry (io.apiman.gateway.engine.IRegistry)1