throw new GatewayException(GatewayExceptionCode.RATEORQUOTA, "The property " + ExchangeConstantKeys.E3_AUTH_IDENTITY.toString() + " is required to check the rate limits.");
}
boolean isTDREnabled = exchange.getProperty(ExchangeConstantKeys.E3_TDR_ENABLED.toString(), boolean.class);
AuthIdentity authIdentity = (AuthIdentity) property;
LimitCheckResult limitCheckResult = rateManager.isAllowed(authIdentity, isTDREnabled);
if(isTDREnabled){
/**
* Put some TDR data into into service
*/
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA, limitCheckResult.isOverQuota());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_SEC, limitCheckResult.isOverSecond());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_MIN, limitCheckResult.isOverMinute());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_DAY, limitCheckResult.isOverDay());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_WEEK, limitCheckResult.isOverWeek());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_MONTH, limitCheckResult.isOverMonth());
for(String tdrName : limitCheckResult.getTdrValues().keySet()){
List<TdrGenerationRule> genRules = limitCheckResult.getTdrValues().get(tdrName);
for(TdrGenerationRule genRule : genRules){
TDRDataService.addNewTdrGenerationRule(exchange, genRule, tdrName);
}
}
}
// Route allowed if null - no action error defined
if (limitCheckResult != null && limitCheckResult.getActionType() != null) {
if(isTDREnabled) {
exchange.setProperty(ExchangeConstantKeys.E3_RATELIMIT_ACTION.toString(), limitCheckResult.getActionType().toString());
TDRDataService.addTxTDRProperty(exchange, TDRConstant.OVER_QUOTA_ACTION, limitCheckResult.getActionType().toString());
}
// Log - Will be based on the Logging Framework developed internally
//log.debug("Route rate limit for API " + (authIdentity.getApi()==null ? "(no API)" : authIdentity.getApi().getId()) + " exeeded" + ((authIdentity.getAuth()==null) ? "": "by " + authIdentity.getAuth().getId()));
// TDR Notification - not yet implemented
// HTTP Status Code 429 - to be return by the error processor of the route
if(limitCheckResult.getActionType().equals(ActionType.REJECT)) {
throw new GatewayException(GatewayExceptionCode.RATEORQUOTA, "Rate limit exceeded. " + limitCheckResult.getActionTypeMessage(), limitCheckResult.getActionType().toString());
}
}
}