From 7807ed77da7706528a85acd042ca282b5b4b24bc Mon Sep 17 00:00:00 2001 From: GPrathap Date: Mon, 5 Dec 2016 09:50:10 +0530 Subject: [PATCH] removed hardcoded validity period while creating application in APIM --- ...ApiApplicationRegistrationServiceImpl.java | 20 ++++++++++--------- .../api/util/RegistrationProfile.java | 6 +++--- .../APIManagementProviderService.java | 5 ++++- .../APIManagementProviderServiceImpl.java | 3 +-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java index d0fe4e516da..a69ae339f22 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java @@ -65,7 +65,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( applicationName, APIUtil.getAllowedApisTags().toArray(new String[APIUtil.getAllowedApisTags().size()]), - ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false); + ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false, + ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } catch (APIManagerException e) { String msg = "Error occurred while registering an application '" + applicationName + "'"; @@ -97,6 +98,12 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi } String username = APIUtil.getAuthenticatedUser() + "@" + APIUtil.getTenantDomainOftheUser(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); + String validityPeriod; + if (registrationProfile.getValidityPeriod() == null) { + validityPeriod = ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD; + } else { + validityPeriod = registrationProfile.getValidityPeriod(); + } if (registrationProfile.isMappingAnExistingOAuthApp()) { JSONObject jsonStringObject = new JSONObject(); jsonStringObject.put(ApiApplicationConstants.JSONSTRING_USERNAME_TAG, username); @@ -105,13 +112,7 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi jsonStringObject.put(ApiApplicationConstants.OAUTH_CLIENT_ID, registrationProfile.getConsumerKey()); jsonStringObject.put(ApiApplicationConstants.OAUTH_CLIENT_SECRET, registrationProfile.getConsumerSecret()); - if (registrationProfile.getValidityPeriod() == 0) { - jsonStringObject.put(ApiApplicationConstants.JSONSTRING_VALIDITY_PERIOD_TAG, - ApiApplicationConstants.DEFAULT_VALIDITY_PERIOD); - } else { - jsonStringObject.put(ApiApplicationConstants.JSONSTRING_VALIDITY_PERIOD_TAG, - registrationProfile.getValidityPeriod()); - } + jsonStringObject.put(ApiApplicationConstants.JSONSTRING_VALIDITY_PERIOD_TAG, validityPeriod); apiManagementProviderService.registerExistingOAuthApplicationToAPIApplication( jsonStringObject.toJSONString(), registrationProfile.getApplicationName(), registrationProfile.getConsumerKey(), username, registrationProfile.isAllowedToAllDomains(), @@ -120,7 +121,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi } else { ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( registrationProfile.getApplicationName(), registrationProfile.getTags(), - ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, false); + ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, + registrationProfile.isAllowedToAllDomains(), validityPeriod); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } } catch (APIManagerException e) { diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java index 3efd856cd4b..b1634923f35 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java @@ -42,7 +42,7 @@ public class RegistrationProfile { private String consumerKey; private String consumerSecret; @XmlElement(required = false) - private int validityPeriod; + private String validityPeriod; public String getApplicationName() { return applicationName; @@ -92,11 +92,11 @@ public class RegistrationProfile { this.consumerSecret = consumerSecret; } - public int getValidityPeriod() { + public String getValidityPeriod() { return validityPeriod; } - public void setValidityPeriod(int validityPeriod) { + public void setValidityPeriod(String validityPeriod) { this.validityPeriod = validityPeriod; } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java index e931c28358d..7e04924dc4c 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderService.java @@ -34,11 +34,14 @@ public interface APIManagementProviderService { * @param tags tags of the apis that application needs to be subscribed. * @param keyType of the application. * @param username to whom the application is created + * @param isAllowedAllDomains application is allowed to all the tenants + * @param validityTime validity period of the application * @return consumerkey and secrete of the created application. * @throws APIManagerException */ ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String tags[], - String keyType, String username, boolean isAllowedAllDomains) + String keyType, String username, boolean isAllowedAllDomains, + String validityTime) throws APIManagerException; /** diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java index ce92eba32a0..913541e5334 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -133,7 +133,7 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe @Override public ApiApplicationKey generateAndRetrieveApplicationKeys(String apiApplicationName, String tags[], String keyType, String username, - boolean isAllowedAllDomains) + boolean isAllowedAllDomains, String validityTime) throws APIManagerException { try { APIManagerUtil.loadTenantRegistry(); @@ -172,7 +172,6 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe } else { allowedDomains[0] = APIManagerUtil.getTenantDomain(); } - String validityTime = "3600"; String ownerJsonString = "{\"username\":\"" + username + "\"}"; Map keyDetails = apiConsumer.requestApprovalForApplicationRegistration(username, apiApplicationName,