Search in sources :

Example 1 with IDataPolicy

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

Aggregations

IAsyncResult (io.apiman.gateway.engine.async.IAsyncResult)1 Policy (io.apiman.gateway.engine.beans.Policy)1 IDataPolicy (io.apiman.gateway.engine.policy.IDataPolicy)1 IPolicy (io.apiman.gateway.engine.policy.IPolicy)1 PolicyWithConfiguration (io.apiman.gateway.engine.policy.PolicyWithConfiguration)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1