Search in sources :

Example 1 with IPolicyFailureFactoryComponent

use of io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent in project apiman by apiman.

the class RateLimitingPolicy method doApply.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final RateLimitingConfig config, final IPolicyChain<ApiRequest> chain) {
    String bucketId = bucketId(request, config);
    final RateBucketPeriod period = bucketFactory.getPeriod(config);
    if (bucketId.equals(BucketFactory.NO_USER_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_USER_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoUser"));
        chain.doFailure(failure);
        return;
    }
    if (bucketId.equals(BucketFactory.NO_CLIENT_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_APP_FOR_RATE_LIMITING, Messages.i18n.format("RateLimitingPolicy.NoApp"));
        chain.doFailure(failure);
        return;
    }
    IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
    rateLimiter.accept(bucketId, period, config.getLimit(), 1, new IAsyncResultHandler<RateLimitResponse>() {

        @Override
        public void handle(IAsyncResult<RateLimitResponse> result) {
            if (result.isError()) {
                chain.throwError(result.getError());
            } else {
                RateLimitResponse rtr = result.getResult();
                Map<String, String> responseHeaders = responseHeaders(config, rtr, defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader());
                if (rtr.isAccepted()) {
                    // $NON-NLS-1$
                    context.setAttribute("rate-limit-response-headers", responseHeaders);
                    chain.doApply(request);
                } else {
                    IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
                    PolicyFailure failure = limitExceededFailure(failureFactory);
                    failure.getHeaders().putAll(responseHeaders);
                    chain.doFailure(failure);
                }
            }
        }
    });
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with IPolicyFailureFactoryComponent

use of io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent in project apiman by apiman.

the class TransferQuotaPolicy method doApply.

@Override
protected void doApply(final ApiRequest request, final IPolicyContext context, final TransferQuotaConfig config, final IPolicyChain<ApiRequest> chain) {
    // *************************************************************
    // Step 1:  check to see if we're already in violation of this
    // policy.  If so, fail fast.
    // *************************************************************
    // $NON-NLS-1$
    String bucketId = bucketId(request, config);
    final RateBucketPeriod period = bucketFactory.getPeriod(config);
    if (bucketId.equals(BucketFactory.NO_USER_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_USER_FOR_RATE_LIMITING, Messages.i18n.format("TransferQuotaPolicy.NoUser"));
        chain.doFailure(failure);
        return;
    }
    if (bucketId.equals(BucketFactory.NO_CLIENT_AVAILABLE)) {
        IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
        // $NON-NLS-1$
        PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.NO_APP_FOR_RATE_LIMITING, Messages.i18n.format("TransferQuotaPolicy.NoApp"));
        chain.doFailure(failure);
        return;
    }
    context.setAttribute(BUCKET_ID_ATTR, bucketId);
    context.setAttribute(PERIOD_ATTR, period);
    IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
    rateLimiter.accept(bucketId, period, config.getLimit(), 0, new IAsyncResultHandler<RateLimitResponse>() {

        @Override
        public void handle(IAsyncResult<RateLimitResponse> result) {
            if (result.isError()) {
                chain.throwError(result.getError());
            } else {
                RateLimitResponse rtr = result.getResult();
                context.setAttribute(RATE_LIMIT_RESPONSE_ATTR, rtr);
                if (!rtr.isAccepted()) {
                    doQuotaExceededFailure(context, config, chain, rtr);
                } else {
                    chain.doApply(request);
                }
            }
        }
    });
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)

Example 3 with IPolicyFailureFactoryComponent

use of io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent in project apiman by apiman.

the class TransferQuotaPolicy method doQuotaExceededFailure.

/**
 * Called to send a 'quota exceeded' failure.
 */
protected void doQuotaExceededFailure(final IPolicyContext context, final TransferQuotaConfig config, final IPolicyChain<?> chain, RateLimitResponse rtr) {
    Map<String, String> responseHeaders = RateLimitingPolicy.responseHeaders(config, rtr, defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader());
    IPolicyFailureFactoryComponent failureFactory = context.getComponent(IPolicyFailureFactoryComponent.class);
    PolicyFailure failure = limitExceededFailure(failureFactory);
    failure.getHeaders().putAll(responseHeaders);
    chain.doFailure(failure);
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)

Example 4 with IPolicyFailureFactoryComponent

use of io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent in project apiman by apiman.

the class IPBlacklistPolicyTest method testApply.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApply() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"1.2.3.4\"," + "    \"3.4.5.6\"," + "    \"10.0.0.11\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setRemoteAddr("1.2.3.4");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failure
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Success
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Example 5 with IPolicyFailureFactoryComponent

use of io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent in project apiman by apiman.

the class IPBlacklistPolicyTest method testApplyWithWildcards.

/**
 * Test method for {@link io.apiman.gateway.engine.policies.IPBlacklistPolicy#parseConfiguration(java.lang.String)}.
 */
@Test
public void testApplyWithWildcards() {
    IPBlacklistPolicy policy = new IPBlacklistPolicy();
    String json = "{" + "  \"ipList\" : [" + "    \"10.0.*.*\"" + "  ]" + "}";
    IPListConfig config = policy.parseConfiguration(json);
    ApiRequest request = new ApiRequest();
    request.setType("GET");
    request.setApiKey("12345");
    request.setDestination("/");
    IPolicyContext context = Mockito.mock(IPolicyContext.class);
    final PolicyFailure failure = new PolicyFailure();
    Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {

        @Override
        public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
            return failure;
        }
    });
    IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
    // Failures
    request.setRemoteAddr("10.0.123.87");
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    request.setRemoteAddr("10.0.97.1");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doFailure(failure);
    // Successes
    request.setRemoteAddr("9.8.7.6");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
    request.setRemoteAddr("10.10.15.123");
    chain = Mockito.mock(IPolicyChain.class);
    policy.apply(request, context, config, chain);
    Mockito.verify(chain).doApply(request);
}
Also used : IPolicyChain(io.apiman.gateway.engine.policy.IPolicyChain) IPolicyContext(io.apiman.gateway.engine.policy.IPolicyContext) PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyFailureType(io.apiman.gateway.engine.beans.PolicyFailureType) IPListConfig(io.apiman.gateway.engine.policies.config.IPListConfig) ApiRequest(io.apiman.gateway.engine.beans.ApiRequest) IPolicyFailureFactoryComponent(io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent) Test(org.junit.Test)

Aggregations

PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)17 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)17 PolicyFailureType (io.apiman.gateway.engine.beans.PolicyFailureType)8 ApiRequest (io.apiman.gateway.engine.beans.ApiRequest)7 IPolicyContext (io.apiman.gateway.engine.policy.IPolicyContext)7 IPolicyChain (io.apiman.gateway.engine.policy.IPolicyChain)6 Test (org.junit.Test)5 IRateLimiterComponent (io.apiman.gateway.engine.components.IRateLimiterComponent)2 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)2 IPListConfig (io.apiman.gateway.engine.policies.config.IPListConfig)2 RateBucketPeriod (io.apiman.gateway.engine.rates.RateBucketPeriod)2 DefaultJdbcComponent (io.apiman.gateway.engine.impl.DefaultJdbcComponent)1 InMemoryRateLimiterComponent (io.apiman.gateway.engine.impl.InMemoryRateLimiterComponent)1 BasicAuthenticationConfig (io.apiman.gateway.engine.policies.config.BasicAuthenticationConfig)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1