Search in sources :

Example 1 with RateBucketPeriod

use of io.apiman.gateway.engine.rates.RateBucketPeriod 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 RateBucketPeriod

use of io.apiman.gateway.engine.rates.RateBucketPeriod 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 RateBucketPeriod

use of io.apiman.gateway.engine.rates.RateBucketPeriod in project apiman by apiman.

the class TransferQuotaPolicy method doFinalApply.

/**
 * Called when everything is done (the last byte is written).  This is used to
 * record the # of bytes downloaded.
 */
protected void doFinalApply(IPolicyContext context, TransferQuotaConfig config, long downloadedBytes) {
    if (config.getDirection() == TransferDirectionType.download || config.getDirection() == TransferDirectionType.both) {
        final String bucketId = context.getAttribute(BUCKET_ID_ATTR, (String) null);
        final RateBucketPeriod period = context.getAttribute(PERIOD_ATTR, (RateBucketPeriod) null);
        IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
        rateLimiter.accept(bucketId, period, config.getLimit(), downloadedBytes, new IAsyncResultHandler<RateLimitResponse>() {

            @Override
            public void handle(IAsyncResult<RateLimitResponse> result) {
            // No need to handle the response - it's too late to do anything meaningful with the result.
            // TODO log any error that might have ocurred
            }
        });
    }
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse)

Example 4 with RateBucketPeriod

use of io.apiman.gateway.engine.rates.RateBucketPeriod in project apiman by apiman.

the class TransferQuotaPolicy method doApply.

@Override
protected void doApply(final ApiResponse response, final IPolicyContext context, final TransferQuotaConfig config, final IPolicyChain<ApiResponse> chain) {
    if (config.getDirection() == TransferDirectionType.upload || config.getDirection() == TransferDirectionType.both) {
        final String bucketId = context.getAttribute(BUCKET_ID_ATTR, (String) null);
        final RateBucketPeriod period = context.getAttribute(PERIOD_ATTR, (RateBucketPeriod) null);
        final long uploadedBytes = context.getAttribute(BYTES_UPLOADED_ATTR, (Long) null);
        IRateLimiterComponent rateLimiter = context.getComponent(IRateLimiterComponent.class);
        rateLimiter.accept(bucketId, period, config.getLimit(), uploadedBytes, new IAsyncResultHandler<RateLimitResponse>() {

            @Override
            public void handle(IAsyncResult<RateLimitResponse> result) {
                if (result.isError()) {
                    chain.throwError(result.getError());
                } else {
                    RateLimitResponse rtr = result.getResult();
                    if (!rtr.isAccepted()) {
                        doQuotaExceededFailure(context, config, chain, rtr);
                    } else {
                        Map<String, String> responseHeaders = RateLimitingPolicy.responseHeaders(config, rtr, defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader());
                        response.getHeaders().putAll(responseHeaders);
                        chain.doApply(response);
                    }
                }
            }
        });
    } else {
        Map<String, String> responseHeaders = RateLimitingPolicy.responseHeaders(config, context.getAttribute(RATE_LIMIT_RESPONSE_ATTR, (RateLimitResponse) null), defaultLimitHeader(), defaultRemainingHeader(), defaultResetHeader());
        response.getHeaders().putAll(responseHeaders);
        chain.doApply(response);
    }
}
Also used : IRateLimiterComponent(io.apiman.gateway.engine.components.IRateLimiterComponent) RateBucketPeriod(io.apiman.gateway.engine.rates.RateBucketPeriod) RateLimitResponse(io.apiman.gateway.engine.components.rate.RateLimitResponse) Map(java.util.Map)

Aggregations

IRateLimiterComponent (io.apiman.gateway.engine.components.IRateLimiterComponent)4 RateLimitResponse (io.apiman.gateway.engine.components.rate.RateLimitResponse)4 RateBucketPeriod (io.apiman.gateway.engine.rates.RateBucketPeriod)4 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)2 IPolicyFailureFactoryComponent (io.apiman.gateway.engine.components.IPolicyFailureFactoryComponent)2 Map (java.util.Map)2 HashMap (java.util.HashMap)1