From 6072f21b93feac9377e42ad5c41b58c539da6dfa Mon Sep 17 00:00:00 2001 From: Avishka-Shamendra Date: Thu, 29 Feb 2024 17:21:27 +0530 Subject: [PATCH 1/2] Improve SubscriptionBlockedException error handling --- .../store/v1/impl/SubscriptionsApiServiceImpl.java | 7 +++++++ .../apimgt/rest/api/util/utils/RestApiUtil.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/SubscriptionsApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/SubscriptionsApiServiceImpl.java index fafe3bc6e754..3dcc119c6acf 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/SubscriptionsApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/SubscriptionsApiServiceImpl.java @@ -28,6 +28,7 @@ import org.wso2.carbon.apimgt.api.APIMgtAuthorizationFailedException; import org.wso2.carbon.apimgt.api.MonetizationException; import org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException; +import org.wso2.carbon.apimgt.api.SubscriptionBlockedException; import org.wso2.carbon.apimgt.api.WorkflowResponse; import org.wso2.carbon.apimgt.api.WorkflowStatus; import org.wso2.carbon.apimgt.api.model.APIIdentifier; @@ -247,6 +248,9 @@ public Response subscriptionsPost(SubscriptionDTO body, String xWSO2Tenant, Mess RestApiUtil.handleResourceAlreadyExistsError( "Specified subscription already exists for API " + body.getApiId() + ", for application " + body.getApplicationId(), e, log); + } catch (SubscriptionBlockedException e) { + RestApiUtil.handleOperationBlockedError("Subscription blocked. " + e.getMessage() + + ". Please contact the API publisher.", e, log); } catch (URISyntaxException e) { if (RestApiUtil.isDueToResourceNotFound(e)) { //this happens when the specified API identifier does not exist @@ -350,6 +354,9 @@ public Response subscriptionsSubscriptionIdPut(String subscriptionId, Subscripti RestApiUtil.handleResourceAlreadyExistsError( "Specified subscription already exists for API " + body.getApiId() + ", for application " + body.getApplicationId(), e, log); + } catch (SubscriptionBlockedException e) { + RestApiUtil.handleOperationBlockedError("Subscription blocked. " + e.getMessage() + + ". Please contact the API publisher.", e, log); } catch (APIManagementException | URISyntaxException e) { if (RestApiUtil.isDueToResourceNotFound(e)) { //this happens when the specified API identifier does not exist diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java index dd09e8f717f9..dbc223298a20 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java @@ -938,6 +938,20 @@ public static void handleInternalServerError(String msg, Log log) throw internalServerErrorException; } + /** + * Logs the error, builds a ForbiddenException with specified details and throws it + * @param msg error message + * @param t Throwable instance + * @param log Log instance + * @throws ForbiddenException + */ + public static void handleOperationBlockedError(String msg, Throwable t, Log log) + throws ForbiddenException{ + ForbiddenException forbiddenException = buildForbiddenException(msg); + log.error(msg,t); + throw forbiddenException; + } + /** * Check whether the HTTP method is allowed for given resources * From 922dcab4488c20289e0e477ae0e3f4ffb7d79b6f Mon Sep 17 00:00:00 2001 From: Avishka-Shamendra Date: Mon, 11 Mar 2024 16:53:26 +0530 Subject: [PATCH 2/2] Fix code styling issue --- .../org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java index dbc223298a20..fa88f0e30ea1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/utils/RestApiUtil.java @@ -946,7 +946,7 @@ public static void handleInternalServerError(String msg, Log log) * @throws ForbiddenException */ public static void handleOperationBlockedError(String msg, Throwable t, Log log) - throws ForbiddenException{ + throws ForbiddenException { ForbiddenException forbiddenException = buildForbiddenException(msg); log.error(msg,t); throw forbiddenException;