From 7d84cf13062ca081bb6c2a42dd5a6178b104273f Mon Sep 17 00:00:00 2001 From: sachinda96 Date: Tue, 22 Dec 2020 02:40:16 +0530 Subject: [PATCH 1/2] EXTGW-579 fix new version email send issue --- .../lifecycleextension/APICustomExecutor.java | 2 +- .../notification/NotificationService.java | 2 + .../impl/NotificationServiceImpl.java | 55 +++++++++++++++++-- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java index dca571ce8..4ad682418 100644 --- a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java +++ b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java @@ -34,7 +34,7 @@ public APICustomExecutor(){ public boolean execute(RequestContext context, String currentState, String targetState) { boolean superExecuted = super.execute(context, currentState, targetState); - if(superExecuted && currentState.equalsIgnoreCase(Constants.STATE_CREATED) && targetState.equalsIgnoreCase(Constants.STATE_PUBLISHED)){ + if(superExecuted && currentState.equalsIgnoreCase(Constants.STATE_CREATED) && targetState.equalsIgnoreCase(Constants.STATE_PUBLISHED) && notificationService.validateIsNotNewApiVersion(context)){ notificationService.sendApiProviderEmail(context); } diff --git a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/NotificationService.java b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/NotificationService.java index 2894204ec..db99e81cb 100644 --- a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/NotificationService.java +++ b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/NotificationService.java @@ -22,4 +22,6 @@ public interface NotificationService { void sendApiProviderEmail(RequestContext context); + boolean validateIsNotNewApiVersion(RequestContext context); + } diff --git a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java index 400633884..a89c56e01 100644 --- a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java +++ b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java @@ -23,9 +23,12 @@ import com.wso2telco.dep.lifecycleextension.util.EmailNotificationUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.api.APIProvider; import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.impl.APIConstants; +import org.wso2.carbon.apimgt.impl.APIManagerFactory; import org.wso2.carbon.apimgt.impl.utils.APIUtil; +import org.wso2.carbon.apimgt.impl.utils.APIVersionComparator; import org.wso2.carbon.governance.api.generic.GenericArtifactManager; import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; import org.wso2.carbon.registry.core.jdbc.handlers.RequestContext; @@ -53,12 +56,8 @@ public void sendApiProviderEmail(RequestContext context) { try { - GenericArtifactManager artifactManager = APIUtil.getArtifactManager(context.getSystemRegistry(), - APIConstants.API_KEY); + API api = getApiDetails(context); log.info("Starting email trigger functionality in API creation"); - String artifactId = context.getResource().getUUID(); - GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId); - API api = APIUtil.getAPI(apiArtifact); List userList = Arrays.asList(remoteUserStoreManager.getUserListOfRole(Constants.ADMIN_ROLE)); @@ -87,4 +86,48 @@ public void sendApiProviderEmail(RequestContext context) { log.error("Failed to validate user details for send email ", e); } } -} + + @Override + public boolean validateIsNotNewApiVersion(RequestContext context) { + + try { + + API api = getApiDetails(context); + + String provider = APIUtil.replaceEmailDomain(api.getId().getProviderName()); + APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(provider); + List oldApiList = apiProvider.getAPIsByProvider(provider); + + if(!oldApiList.isEmpty()){ + APIVersionComparator versionComparator = new APIVersionComparator(); + for (API oldApi : oldApiList) { + if(oldApi.getId().getApiName().equalsIgnoreCase(api.getId().getApiName()) && versionComparator.compare(oldApi, api) < 0 && + (APIConstants.PUBLISHED.equals(oldApi.getStatus()))){ + return false; + } + } + } + + return true; + }catch (Exception e){ + log.error("Failed to validate api version details ", e); + return false; + } + } + + private API getApiDetails(RequestContext context)throws Exception{ + + try { + GenericArtifactManager artifactManager = APIUtil.getArtifactManager(context.getSystemRegistry(), + APIConstants.API_KEY); + String artifactId = context.getResource().getUUID(); + GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId); + return APIUtil.getAPI(apiArtifact); + + }catch (Exception e){ + log.error("Failed to get request context api details ", e); + throw e; + } + + } +} \ No newline at end of file From 9d0318eac1767b80c9df8c300f203d45f06b2866 Mon Sep 17 00:00:00 2001 From: sachinda96 Date: Tue, 22 Dec 2020 18:45:20 +0530 Subject: [PATCH 2/2] EXTGW-579 remove this try-catch completely in getApiDetails method. --- .../dep/lifecycleextension/APICustomExecutor.java | 1 + .../notification/impl/NotificationServiceImpl.java | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java index 4ad682418..b17653d5d 100644 --- a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java +++ b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/APICustomExecutor.java @@ -38,6 +38,7 @@ public boolean execute(RequestContext context, String currentState, String targe notificationService.sendApiProviderEmail(context); } + return superExecuted; } } diff --git a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java index a89c56e01..337d91653 100644 --- a/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java +++ b/components/life-cycle-extension/src/main/java/com/wso2telco/dep/lifecycleextension/notification/impl/NotificationServiceImpl.java @@ -117,17 +117,10 @@ public boolean validateIsNotNewApiVersion(RequestContext context) { private API getApiDetails(RequestContext context)throws Exception{ - try { GenericArtifactManager artifactManager = APIUtil.getArtifactManager(context.getSystemRegistry(), APIConstants.API_KEY); String artifactId = context.getResource().getUUID(); GenericArtifact apiArtifact = artifactManager.getGenericArtifact(artifactId); return APIUtil.getAPI(apiArtifact); - - }catch (Exception e){ - log.error("Failed to get request context api details ", e); - throw e; - } - } } \ No newline at end of file