use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class GatewayThreadContext method getPolicyFailure.
/**
* @return the thread-local policy failure
*/
public static final PolicyFailure getPolicyFailure() {
PolicyFailure failure = policyFailure.get();
if (failure == null) {
failure = new PolicyFailure();
policyFailure.set(failure);
}
failure.setResponseCode(0);
failure.setFailureCode(0);
failure.setMessage(null);
failure.setType(null);
failure.getHeaders().clear();
return failure;
}
use of io.apiman.gateway.engine.beans.PolicyFailure 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);
}
}
}
});
}
use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class RateLimitingPolicy method limitExceededFailure.
/**
* Generate a rate limit exceeded policy failure.
*
* @param failureFactory failure factory
* @return a limit exceeded policy failure
*/
protected PolicyFailure limitExceededFailure(IPolicyFailureFactoryComponent failureFactory) {
// $NON-NLS-1$
PolicyFailure failure = failureFactory.createFailure(PolicyFailureType.Other, PolicyFailureCodes.RATE_LIMIT_EXCEEDED, Messages.i18n.format("RateLimitingPolicy.RateExceeded"));
failure.setResponseCode(429);
return failure;
}
use of io.apiman.gateway.engine.beans.PolicyFailure 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);
}
}
}
});
}
use of io.apiman.gateway.engine.beans.PolicyFailure 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);
}
Aggregations