diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java new file mode 100644 index 000000000000..a6767506645a --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023, WSO2 LLC (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.apimgt.gateway.exception; + +/** + * Exception class to handle data not found cases. + */ +public class DataNotFoundException extends Exception { + + public DataNotFoundException(String message) { + super(message); + } + + public DataNotFoundException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java index 13160ed6a489..ac84bc37fab9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java @@ -26,6 +26,7 @@ import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsDataProvider; import org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector; import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; +import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException; import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants; import org.wso2.carbon.apimgt.gateway.utils.WebhooksUtils; import org.wso2.carbon.apimgt.impl.APIConstants; @@ -73,7 +74,7 @@ public boolean mediate(MessageContext messageContext) { WebhooksUtils.publishAnalyticsData(messageContext); } WebhooksUtils.persistData(requestBody, deliveryDataPersisRetries, APIConstants.Webhooks.DELIVERY_EVENT_TYPE); - } catch (InterruptedException | IOException e) { + } catch (InterruptedException | IOException | DataNotFoundException e) { log.error("Error while persisting delivery status", e); } return true; diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java index 203dd1ea3c6f..b2ea7cc081eb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java @@ -20,6 +20,7 @@ import org.apache.synapse.MessageContext; import org.apache.synapse.core.axis2.Axis2MessageContext; import org.apache.synapse.mediators.AbstractMediator; +import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException; import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants; import org.wso2.carbon.apimgt.gateway.utils.WebhooksUtils; import org.wso2.carbon.apimgt.impl.APIConstants; @@ -48,7 +49,7 @@ public boolean mediate(MessageContext messageContext) { } else { messageContext.setProperty(APIConstants.Webhooks.SUBSCRIBERS_COUNT_PROPERTY, 0); } - } catch (URISyntaxException e) { + } catch (URISyntaxException | DataNotFoundException e) { handleException("Error while getting subscribers count", e, messageContext); } return true; diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java index c87bb27bf825..4318b6b70da5 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java @@ -31,6 +31,7 @@ import org.apache.synapse.rest.RESTConstants; import org.apache.synapse.transport.passthru.PassThroughConstants; import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; +import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException; import org.wso2.carbon.apimgt.gateway.handlers.analytics.Constants; import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityConstants; import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityUtils; @@ -88,7 +89,7 @@ public boolean mediate(MessageContext messageContext) { HttpResponse httpResponse = WebhooksUtils.persistData(jsonString, subscriptionDataPersisRetries, APIConstants.Webhooks.SUBSCRIPTION_EVENT_TYPE); handleResponse(httpResponse, messageContext); - } catch (InterruptedException | IOException e) { + } catch (InterruptedException | IOException | DataNotFoundException e) { messageContext.setProperty(SynapseConstants.ERROR_CODE, HttpStatus.SC_INTERNAL_SERVER_ERROR); messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, "Error while persisting request"); messageContext.setProperty(SynapseConstants.ERROR_DETAIL, "Error while persisting request"); diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java index 00ef6f054344..fb65dfae349a 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java @@ -37,6 +37,7 @@ import org.wso2.carbon.apimgt.common.analytics.collectors.impl.GenericRequestDataCollector; import org.wso2.carbon.apimgt.common.analytics.exceptions.AnalyticsException; import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; +import org.wso2.carbon.apimgt.gateway.exception.DataNotFoundException; import org.wso2.carbon.apimgt.gateway.handlers.Utils; import org.wso2.carbon.apimgt.gateway.handlers.streaming.webhook.WebhooksAnalyticsDataProvider; import org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleConstants; @@ -121,11 +122,15 @@ public static EventHubConfigurationDto getEventHubConfiguration() { * @param messageContext the message context. * @return the generated API Key. */ - public static String generateAPIKey(MessageContext messageContext, String tenantDomain) { + public static String generateAPIKey(MessageContext messageContext, String tenantDomain) + throws DataNotFoundException { String context = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT); String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION); API api = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain). getApiByContextAndVersion(context, apiVersion); + if (api == null) { + throw new DataNotFoundException("Error occurred when getting API information"); + } return api.getUuid(); } @@ -136,7 +141,7 @@ public static String generateAPIKey(MessageContext messageContext, String tenant * @return the list of subscribers. */ public static List getSubscribersListFromInMemoryMap(MessageContext messageContext) - throws URISyntaxException { + throws URISyntaxException, DataNotFoundException { String tenantDomain = (String) messageContext.getProperty(APIConstants.TENANT_DOMAIN_INFO_PROPERTY); String apiKey = WebhooksUtils.generateAPIKey(messageContext, tenantDomain); String urlQueryParams = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext(). diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/utils/GatewayUtilsTestCase.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/utils/GatewayUtilsTestCase.java index 8d3ccec3f99d..36d79fb82ba8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/utils/GatewayUtilsTestCase.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/test/java/org/wso2/carbon/apimgt/gateway/utils/GatewayUtilsTestCase.java @@ -231,6 +231,20 @@ public void testGetAPIasNull() { Mockito.verify(subscriptionDataStore, Mockito.times(3)).getApiByContextAndVersion("/abc1", "1.0.0"); } + @Test + public void testGetAPIWithNullContext() { + API api = new API(); + api.setApiName("api1"); + api.setApiVersion("1.0.0"); + api.setApiProvider("admin"); + SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class); + Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder); + SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class); + Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn( + subscriptionDataStore); + Mockito.when(subscriptionDataStore.getApiByContextAndVersion(null, "1.0.0")).thenReturn(null); + } + @Test public void testGetAPIasNullWhenTenantSubscriptionStoreNull() { MessageContext messageContext = Mockito.mock(MessageContext.class); diff --git a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java index b51bf9632e0c..d4ebef898fa1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java @@ -182,6 +182,12 @@ public ApplicationKeyMapping getKeyMappingByKeyAndKeyManager(String key, String @Override public API getApiByContextAndVersion(String context, String version) { + if (context == null) { + if (log.isDebugEnabled()) { + log.debug("Cannot retrieve API information with null context"); + } + return null; + } String key = context + DELEM_PERIOD + version; String synchronizeKey = "SubscriptionDataStoreImpl-API-" + key; API api = apiMap.get(key);