Skip to content

Commit

Permalink
Null check API, when generating API key
Browse files Browse the repository at this point in the history
  • Loading branch information
PasanT9 committed Oct 10, 2023
1 parent ce42230 commit ed71fcc
Show file tree
Hide file tree
Showing 5 changed files with 46 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);
}

Check warning on line 28 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java#L27-L28

Added lines #L27 - L28 were not covered by tests

public DataNotFoundException(String message, Throwable cause) {
super(message, cause);
}

Check warning on line 32 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/exception/DataNotFoundException.java#L31-L32

Added lines #L31 - L32 were not covered by tests
}
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) {

Check warning on line 77 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/DeliveryStatusUpdater.java#L77

Added line #L77 was not covered by tests
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) {

Check warning on line 52 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersLoader.java#L52

Added line #L52 was not covered by tests
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) {

Check warning on line 92 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/webhooks/SubscribersPersistMediator.java#L92

Added line #L92 was not covered by tests
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");

Check warning on line 132 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/utils/WebhooksUtils.java#L132

Added line #L132 was not covered by tests
}
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

0 comments on commit ed71fcc

Please sign in to comment.