From dd291b1cfbbd2e8c33a623e2b8e9b182f231009f Mon Sep 17 00:00:00 2001 From: Akram Azarm Date: Thu, 4 May 2023 03:24:50 +0530 Subject: [PATCH] fix issue for custom property throttling key throttling --- .../gateway/handlers/throttling/ThrottleHandler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java index 839148c142e1..858af5346f66 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java @@ -78,6 +78,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TimeZone; @@ -102,6 +103,7 @@ public class ThrottleHandler extends AbstractHandler implements ManagedLifecycle private static final String RESOURCE_THROTTLE = "RESOURCE_THROTTLE"; private static final String BLOCKED_TEST = "BLOCKED_TEST"; private final String type = ExtensionType.THROTTLING.toString(); + private static final String CUSTOM_PROPERTY = "customProperty"; /** * The key for getting the throttling policy - key refers to a/an [registry] Resource entry @@ -992,6 +994,7 @@ public boolean validateCustomPolicy(String userID, String appKey, String resourc String apiTenant, String appId, String clientIp, Map keyTemplateMap, MessageContext messageContext) { + HashMap propertyFromMap = (HashMap) messageContext.getProperty(CUSTOM_PROPERTY); if (keyTemplateMap != null && keyTemplateMap.size() > 0) { for (String key : keyTemplateMap.keySet()) { key = key.replaceAll("\\$resourceKey", resourceKey); @@ -1004,6 +1007,11 @@ public boolean validateCustomPolicy(String userID, String appKey, String resourc if (clientIp != null) { key = key.replaceAll("\\$clientIp", APIUtil.ipToBigInteger(clientIp).toString()); } + if (propertyFromMap != null) { + for (String mapKey : propertyFromMap.keySet()) { + key = key.replaceAll("\\$customProperty." + mapKey, (String) propertyFromMap.get(mapKey)); + } + } if (getThrottleDataHolder().isThrottled(key)) { long timestamp = getThrottleDataHolder().getThrottleNextAccessTimestamp(key); messageContext.setProperty(APIThrottleConstants.THROTTLED_NEXT_ACCESS_TIMESTAMP, timestamp);