Skip to content

Commit

Permalink
Merge pull request #12172 from PasanT9/skip-getAPI-fix
Browse files Browse the repository at this point in the history
Skip returning API when context is null
  • Loading branch information
PasanT9 authored Oct 19, 2023
2 parents 7db9013 + ed71fcc commit 1193a47
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -136,7 +141,7 @@ public static String generateAPIKey(MessageContext messageContext, String tenant
* @return the list of subscribers.
*/
public static List<WebhooksDTO> 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().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1193a47

Please sign in to comment.