Search in sources :

Example 1 with PolicyWithConfiguration

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

the class ApiRequestExecutorImpl method loadPolicies.

/**
 * Get/resolve the list of policies into a list of policies with config.  This operation is
 * done asynchronously so that plugins can be downloaded if needed.  Any errors in resolving
 * the policies will be reported back via the policyErrorHandler.
 */
private void loadPolicies(final IAsyncHandler<List<PolicyWithConfiguration>> handler) {
    final Set<Integer> totalCounter = new HashSet<>();
    final Set<Integer> errorCounter = new TreeSet<>();
    final List<PolicyWithConfiguration> rval = new ArrayList<>(policies.size());
    final List<Throwable> errors = new ArrayList<>(policies.size());
    final int numPolicies = policies.size();
    int index = 0;
    // If there aren't any policies, then no need to asynchronously load them!
    if (policies.isEmpty()) {
        handler.handle(policyImpls);
        return;
    }
    for (final Policy policy : policies) {
        rval.add(null);
        errors.add(null);
        final int localIdx = index++;
        policyFactory.loadPolicy(policy.getPolicyImpl(), (IAsyncResult<IPolicy> result) -> {
            if (result.isSuccess()) {
                IPolicy policyImpl = result.getResult();
                // Test whether pipeline contains any data policies. Connectors can use this for Content-Length pass-through.
                if (policyImpl instanceof IDataPolicy) {
                    hasDataPolicy = true;
                }
                try {
                    Object policyConfig = policyFactory.loadConfig(policyImpl, policy.getPolicyImpl(), policy.getPolicyJsonConfig());
                    PolicyWithConfiguration pwc = new PolicyWithConfiguration(policyImpl, policyConfig);
                    rval.set(localIdx, pwc);
                } catch (Throwable t) {
                    errors.set(localIdx, t);
                    errorCounter.add(localIdx);
                }
            } else {
                Throwable error = result.getError();
                errors.set(localIdx, error);
                errorCounter.add(localIdx);
            }
            totalCounter.add(localIdx);
            // Have we done them all?
            if (totalCounter.size() == numPolicies) {
                // the fully resolved list of policies.
                if (!errorCounter.isEmpty()) {
                    int errorIdx = errorCounter.iterator().next();
                    Throwable error = errors.get(errorIdx);
                    // TODO add some logging here to indicate which policy error'd out
                    // Policy errorPolicy = policies.get(errorIdx);
                    policyErrorHandler.handle(error);
                } else {
                    handler.handle(rval);
                }
            }
        });
    }
}
Also used : IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) Policy(io.apiman.gateway.engine.beans.Policy) IPolicy(io.apiman.gateway.engine.policy.IPolicy) IPolicy(io.apiman.gateway.engine.policy.IPolicy) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) IDataPolicy(io.apiman.gateway.engine.policy.IDataPolicy) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) HashSet(java.util.HashSet)

Example 2 with PolicyWithConfiguration

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

the class DataPolicyChainTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    policies = new ArrayList<>();
    policyOne = spy(new PassthroughDataPolicy("1"));
    pwcOne = new PolicyWithConfiguration(policyOne, configuration);
    mockContext = mock(IPolicyContext.class);
    mockRequest = mock(ApiRequest.class);
    given(mockRequest.getApiKey()).willReturn("bacon");
    given(mockRequest.getDestination()).willReturn("mars");
    given(mockRequest.getType()).willReturn("request");
    mockResponse = mock(ApiResponse.class);
    given(mockRequest.getApiKey()).willReturn("bacon");
    given(mockRequest.getDestination()).willReturn("mars");
    given(mockRequest.getType()).willReturn("response");
    mockBuffer = mock(IApimanBuffer.class);
    given(mockBuffer.toString()).willReturn("bananas");
    mockRequestHandler = mock(IReadWriteStream.class);
    mockResponseHandler = mock(IReadWriteStream.class);
    mockBodyHandler = mock(IAsyncHandler.class);
    mockEndHandler = mock(IAsyncHandler.class);
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IReadWriteStream(io.apiman.gateway.engine.io.IReadWriteStream) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PassthroughDataPolicy(io.apiman.gateway.engine.util.PassthroughDataPolicy) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) IAsyncHandler(io.apiman.gateway.engine.async.IAsyncHandler) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Before(org.junit.Before)

Example 3 with PolicyWithConfiguration

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

the class PolicyChainTest method setup.

@Before
public void setup() {
    policies = new ArrayList<>();
    policyOne = spy(new PassthroughPolicy("1"));
    policyTwo = spy(new PassthroughPolicy("2"));
    pwcOne = new PolicyWithConfiguration(policyOne, new Object());
    pwcTwo = new PolicyWithConfiguration(policyTwo, new Object());
    // mockChain = mock(IPolicyChain.class);
    mockContext = mock(IPolicyContext.class);
    mockRequest = mock(ApiRequest.class);
    given(mockRequest.getApiKey()).willReturn("bacon");
    given(mockRequest.getDestination()).willReturn("mars");
    given(mockRequest.getType()).willReturn("request");
    mockResponse = mock(ApiResponse.class);
    given(mockRequest.getApiKey()).willReturn("bacon");
    given(mockRequest.getDestination()).willReturn("mars");
    given(mockRequest.getType()).willReturn("response");
    mockBuffer = mock(IApimanBuffer.class);
    given(mockBuffer.toString()).willReturn("bananas");
    mockBodyHandler = mock(IAsyncHandler.class);
    mockEndHandler = mock(IAsyncHandler.class);
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PassthroughPolicy(io.apiman.gateway.engine.util.PassthroughPolicy) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) IAsyncHandler(io.apiman.gateway.engine.async.IAsyncHandler) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Before(org.junit.Before)

Example 4 with PolicyWithConfiguration

use of io.apiman.gateway.engine.policy.PolicyWithConfiguration 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 PolicyWithConfiguration

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

the class ApiRequestExecutorImpl method execute.

/**
 * @see io.apiman.gateway.engine.IApiRequestExecutor#execute()
 */
@Override
public void execute() {
    // load the api data based on request
    registry.getApi(request.getApiOrgId(), request.getApiId(), request.getApiVersion(), (IAsyncResult<Api> apiResult) -> {
        if (apiResult.isSuccess()) {
            api = apiResult.getResult();
        } else if (apiResult.isError()) {
            resultHandler.handle(AsyncResultImpl.create(apiResult.getError(), IEngineResult.class));
        }
    });
    // check if api disable key are enabled
    if (api != null && !api.isKeysStrippingDisabled()) {
        // Strip apikey
        stripApiKey();
    }
    // Fill out some of the basic metrics structure.
    requestMetric.setRequestStart(new Date());
    requestMetric.setUrl(request.getUrl());
    requestMetric.setResource(request.getDestination());
    requestMetric.setMethod(request.getType());
    requestMetric.setApiOrgId(request.getApiOrgId());
    requestMetric.setApiId(request.getApiId());
    requestMetric.setApiVersion(request.getApiVersion());
    // Set request metric
    context.setAttribute(PolicyContextKeys.REQUEST_METRIC, requestMetric);
    // Set connector config early (allows mutation of certain connector properties)
    IConnectorConfig connectorConfig = connectorFactory.createConnectorConfig(request, api);
    context.setConnectorConfiguration(connectorConfig);
    // Create the handler that will be called once the policies are asynchronously
    // loaded (can happen this way due to the plugin framework).
    final IAsyncHandler<List<PolicyWithConfiguration>> policiesLoadedHandler = (List<PolicyWithConfiguration> result) -> {
        policyImpls = result;
        // Set up the policy chain request, call #doApply to execute.
        requestChain = createRequestChain((ApiRequest req) -> {
            IConnectorInterceptor connectorInterceptor = context.getConnectorInterceptor();
            IApiConnector connector;
            if (connectorInterceptor == null) {
                connector = connectorFactory.createConnector(req, api, RequiredAuthType.parseType(api), hasDataPolicy, connectorConfig);
            } else {
                connector = connectorInterceptor.createConnector();
            }
            // TODO check for a null connector
            // Open up a connection to the back-end if we're given the OK from the request chain
            requestMetric.setApiStart(new Date());
            // Attach the response handler here.
            apiConnection = connector.connect(req, createApiConnectionResponseHandler());
            // Write the body chunks from the *policy request* into the connector request.
            requestChain.bodyHandler(buffer -> {
                requestMetric.setBytesUploaded(requestMetric.getBytesUploaded() + buffer.length());
                apiConnection.write(buffer);
            });
            // Indicate end from policy chain request to connector request.
            requestChain.endHandler(onEnd -> apiConnection.end());
            // Once we have returned from connector.request, we know it is safe to start
            // writing chunks without buffering. At this point, it is the responsibility
            // of the implementation as to how they should cope with the chunks.
            handleStream();
        });
        requestChain.doApply(request);
    };
    // The handler used when we need to parse the inbound request payload into
    // an object and make it available via the policy context.
    final IAsyncResultHandler<Object> payloadParserHandler = new IAsyncResultHandler<Object>() {

        @Override
        public void handle(IAsyncResult<Object> result) {
            if (result.isSuccess()) {
                final Object payload = result.getResult();
                // Store the parsed object in the policy context.
                context.setAttribute(PolicyContextKeys.REQUEST_PAYLOAD, payload);
                context.setAttribute(PolicyContextKeys.REQUEST_PAYLOAD_IO, payloadIO);
                // Now replace the inbound stream handler with one that uses the payload IO
                // object to re-marshall the (possibly modified) payload object to bytes
                // and sends that (because the *real* inbound stream has already been consumed)
                streamHandler(new IAsyncHandler<ISignalWriteStream>() {

                    @Override
                    public void handle(ISignalWriteStream connectorStream) {
                        try {
                            if (payload == null) {
                                connectorStream.end();
                            } else {
                                payloadIO = context.getAttribute(PolicyContextKeys.REQUEST_PAYLOAD_IO, payloadIO);
                                byte[] data = payloadIO.marshall(payload);
                                IApimanBuffer buffer = bufferFactory.createBuffer(data);
                                connectorStream.write(buffer);
                                connectorStream.end();
                            }
                        } catch (Exception e) {
                            connectorStream.abort(e);
                            throw new RuntimeException(e);
                        }
                    }
                });
                // Load and executes the policies
                loadPolicies(policiesLoadedHandler);
            } else {
                resultHandler.handle(AsyncResultImpl.create(result.getError(), IEngineResult.class));
            }
        }
    };
    // then we lookup the Contract and use that.
    if (request.getApiKey() == null || (api != null && api.isKeysStrippingDisabled())) {
        if (api == null) {
            // $NON-NLS-1$
            ApiNotFoundException error = new ApiNotFoundException(Messages.i18n.format("EngineImpl.ApiNotFound"));
            resultHandler.handle(AsyncResultImpl.create(error, IEngineResult.class));
        } else if (!api.isPublicAPI()) {
            // $NON-NLS-1$
            InvalidApiException error = new InvalidApiException(Messages.i18n.format("EngineImpl.ApiNotPublic"));
            // Forbidden
            error.setStatusCode(403);
            resultHandler.handle(AsyncResultImpl.create(error, IEngineResult.class));
        } else {
            resolvePropertyReplacements(api);
            request.setApi(api);
            policies = api.getApiPolicies();
            policyImpls = new ArrayList<>(policies.size());
            // or a JSON document
            if (api.isParsePayload()) {
                parsePayload(payloadParserHandler);
            } else {
                loadPolicies(policiesLoadedHandler);
            }
        }
    } else {
        String apiOrgId = request.getApiOrgId();
        String apiId = request.getApiId();
        String apiVersion = request.getApiVersion();
        String apiKey = request.getApiKey();
        registry.getContract(apiOrgId, apiId, apiVersion, apiKey, (IAsyncResult<ApiContract> contractResult) -> {
            if (contractResult.isSuccess()) {
                ApiContract apiContract = contractResult.getResult();
                resolvePropertyReplacements(apiContract);
                requestMetric.setClientOrgId(apiContract.getClient().getOrganizationId());
                requestMetric.setClientId(apiContract.getClient().getClientId());
                requestMetric.setClientVersion(apiContract.getClient().getVersion());
                requestMetric.setPlanId(apiContract.getPlan());
                requestMetric.setContractId(request.getApiKey());
                api = apiContract.getApi();
                request.setContract(apiContract);
                request.setApi(api);
                policies = apiContract.getPolicies();
                policyImpls = new ArrayList<>(policies.size());
                if (request.getApiOrgId() != null) {
                    try {
                        validateRequest(request);
                    } catch (InvalidContractException e) {
                        resultHandler.handle(AsyncResultImpl.create(e, IEngineResult.class));
                        return;
                    }
                }
                // or a JSON document
                if (api.isParsePayload()) {
                    parsePayload(payloadParserHandler);
                } else {
                    // Load and executes the policies
                    loadPolicies(policiesLoadedHandler);
                }
            } else {
                resultHandler.handle(AsyncResultImpl.create(contractResult.getError(), IEngineResult.class));
            }
        });
    }
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) IEngineResult(io.apiman.gateway.engine.IEngineResult) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) ArrayList(java.util.ArrayList) IAsyncResultHandler(io.apiman.gateway.engine.async.IAsyncResultHandler) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) InvalidApiException(io.apiman.gateway.engine.beans.exceptions.InvalidApiException) List(java.util.List) ArrayList(java.util.ArrayList) InvalidContractException(io.apiman.gateway.engine.beans.exceptions.InvalidContractException) IConnectorConfig(io.apiman.gateway.engine.IConnectorConfig) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) IApiConnector(io.apiman.gateway.engine.IApiConnector) Date(java.util.Date) RequestAbortedException(io.apiman.gateway.engine.beans.exceptions.RequestAbortedException) InvalidApiException(io.apiman.gateway.engine.beans.exceptions.InvalidApiException) InvalidContractException(io.apiman.gateway.engine.beans.exceptions.InvalidContractException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) IConnectorInterceptor(io.apiman.gateway.engine.policy.IConnectorInterceptor) IAsyncResult(io.apiman.gateway.engine.async.IAsyncResult) PolicyWithConfiguration(io.apiman.gateway.engine.policy.PolicyWithConfiguration) ApiContract(io.apiman.gateway.engine.beans.ApiContract)

Aggregations

PolicyWithConfiguration (io.apiman.gateway.engine.policy.PolicyWithConfiguration)5 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)4 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)3 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)3 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)3 IAsyncHandler (io.apiman.gateway.engine.async.IAsyncHandler)2 IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)2 IPolicy (io.apiman.gateway.engine.policy.IPolicy)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 IApiConnector (io.apiman.gateway.engine.IApiConnector)1 IConnectorConfig (io.apiman.gateway.engine.IConnectorConfig)1 IEngineResult (io.apiman.gateway.engine.IEngineResult)1 IAsyncResultHandler (io.apiman.gateway.engine.async.IAsyncResultHandler)1 ApiContract (io.apiman.gateway.engine.beans.ApiContract)1 Policy (io.apiman.gateway.engine.beans.Policy)1 ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)1 ConfigurationParseException (io.apiman.gateway.engine.beans.exceptions.ConfigurationParseException)1 InvalidApiException (io.apiman.gateway.engine.beans.exceptions.InvalidApiException)1 InvalidContractException (io.apiman.gateway.engine.beans.exceptions.InvalidContractException)1