From 1c8bb7551dd59ef869f105f8aba7b560b35756ce Mon Sep 17 00:00:00 2001 From: harshanl Date: Wed, 7 Oct 2015 23:15:17 +0530 Subject: [PATCH] Refactored Dynamic-client registration service to use AppMgt osgi service --- .../webapp/publisher/APIPublisherUtil.java | 3 +- .../pom.xml | 5 +- .../impl/DynamicClientRegistrationImpl.java | 17 ++-- ...amicClientRegistrationBundleActivator.java | 48 ----------- .../DynamicClientRegistrationDataHolder.java | 51 +++++++++++ ...micClientRegistrationServiceComponent.java | 85 +++++++++++++++++++ ...ava => PermissionBasedScopeValidator.java} | 8 +- 7 files changed, 157 insertions(+), 60 deletions(-) delete mode 100644 components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationBundleActivator.java create mode 100644 components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationDataHolder.java create mode 100644 components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationServiceComponent.java rename components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/{ScopeValidator.java => PermissionBasedScopeValidator.java} (92%) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java index 8a2f782da5..533a7fc2dc 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherUtil.java @@ -67,7 +67,8 @@ public class APIPublisherUtil { api.setEndpointSecured(true); api.setStatus(APIStatus.PUBLISHED); api.setTransports(config.getTransports()); - + api.setAsDefaultVersion(true); + api.setAsPublishedDefaultVersion(true); return api; } diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml index 51bce89c19..b2d8ec58b9 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/pom.xml @@ -36,6 +36,10 @@ + + org.apache.felix + maven-scr-plugin + org.apache.felix maven-bundle-plugin @@ -47,7 +51,6 @@ ${project.artifactId} ${carbon.device.mgt.version} Dynamic Client Registration Bundle - org.wso2.carbon.dynamic.client.registration.internal.DynamicClientRegistrationBundleActivator org.wso2.carbon.dynamic.client.registration.internal !org.wso2.carbon.dynamic.client.registration.internal, diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationImpl.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationImpl.java index fa8c10d1c6..339a0d32f2 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationImpl.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/impl/DynamicClientRegistrationImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.dynamic.client.registration.*; +import org.wso2.carbon.dynamic.client.registration.internal.DynamicClientRegistrationDataHolder; import org.wso2.carbon.dynamic.client.registration.profile.RegistrationProfile; import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException; import org.wso2.carbon.identity.application.common.model.*; @@ -153,7 +154,8 @@ public class DynamicClientRegistrationImpl implements DynamicClientRegistrationS serviceProvider.setDescription("Service Provider for application " + applicationName); - ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + ApplicationManagementService appMgtService = DynamicClientRegistrationDataHolder. + getInstance().getApplicationManagementService(); if (appMgtService == null) { throw new IllegalStateException( "Error occurred while retrieving Application Management" + @@ -164,7 +166,7 @@ public class DynamicClientRegistrationImpl implements DynamicClientRegistrationS applicationName, tenantDomain); if (existingServiceProvider == null) { - appMgtService.createApplication(serviceProvider, userName, tenantDomain); + appMgtService.createApplication(serviceProvider, tenantDomain, userName); } ServiceProvider createdServiceProvider = appMgtService.getServiceProvider( @@ -324,7 +326,8 @@ public class DynamicClientRegistrationImpl implements DynamicClientRegistrationS try { oAuthAdminService.removeOAuthApplicationData(consumerKey); - ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + ApplicationManagementService appMgtService = DynamicClientRegistrationDataHolder. + getInstance().getApplicationManagementService(); if (appMgtService == null) { throw new IllegalStateException( @@ -357,15 +360,17 @@ public class DynamicClientRegistrationImpl implements DynamicClientRegistrationS @Override public boolean isOAuthApplicationExists(String applicationName) throws DynamicClientRegistrationException { - ApplicationManagementService appMgtService = ApplicationManagementService.getInstance(); + ApplicationManagementService appMgtService = DynamicClientRegistrationDataHolder. + getInstance().getApplicationManagementService(); if (appMgtService == null) { throw new IllegalStateException( "Error occurred while retrieving Application Management" + "Service"); } try { - if (ApplicationManagementService.getInstance().getServiceProvider(applicationName, - CarbonContext.getThreadLocalCarbonContext().getTenantDomain()) != null) { + if (appMgtService.getServiceProvider(applicationName, + CarbonContext.getThreadLocalCarbonContext() + .getTenantDomain()) != null) { return true; } } catch (IdentityApplicationManagementException e) { diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationBundleActivator.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationBundleActivator.java deleted file mode 100644 index a5ba8ecb2b..0000000000 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationBundleActivator.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.dynamic.client.registration.internal; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; -import org.wso2.carbon.dynamic.client.registration.impl.DynamicClientRegistrationImpl; - -/** - * BundleActivator class of DynamicClientRegistration component. - */ -public class DynamicClientRegistrationBundleActivator implements BundleActivator{ - - private static final Log log = LogFactory.getLog(DynamicClientRegistrationBundleActivator.class); - - @Override - public void start(BundleContext bundleContext) throws Exception { - DynamicClientRegistrationService dynamicClientRegistrationService = - new DynamicClientRegistrationImpl(); - bundleContext.registerService(DynamicClientRegistrationService.class.getName(), - dynamicClientRegistrationService, null); - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - - } - -} diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationDataHolder.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationDataHolder.java new file mode 100644 index 0000000000..63bda3e337 --- /dev/null +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationDataHolder.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.dynamic.client.registration.internal; + +import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; + +/** + * DataHolder class of DynamicClientRegistration bundle. This hold a reference to + * ApplicationManagementService. + */ +public class DynamicClientRegistrationDataHolder { + + private ApplicationManagementService applicationManagementService; + + private static DynamicClientRegistrationDataHolder thisInstance = + new DynamicClientRegistrationDataHolder(); + + private DynamicClientRegistrationDataHolder() { + } + + public static DynamicClientRegistrationDataHolder getInstance() { + return thisInstance; + } + + public ApplicationManagementService getApplicationManagementService() { + if (applicationManagementService == null) { + throw new IllegalStateException("ApplicationManagementService is not initialized properly"); + } + return applicationManagementService; + } + + public void setApplicationManagementService(ApplicationManagementService realmService) { + this.applicationManagementService = realmService; + } +} diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationServiceComponent.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationServiceComponent.java new file mode 100644 index 0000000000..daaa7366e5 --- /dev/null +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.registration/src/main/java/org/wso2/carbon/dynamic/client/registration/internal/DynamicClientRegistrationServiceComponent.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.dynamic.client.registration.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.dynamic.client.registration.DynamicClientRegistrationService; +import org.wso2.carbon.dynamic.client.registration.impl.DynamicClientRegistrationImpl; +import org.wso2.carbon.identity.application.mgt.ApplicationManagementService; + +/** + * @scr.component name="org.wso2.carbon.dynamic.client.registration" immediate="true" + * @scr.reference name="identity.application.management.service" + * interface="org.wso2.carbon.identity.application.mgt.ApplicationManagementService" + * cardinality="1..1" + * policy="dynamic" + * bind="setApplicationManagementService" + * unbind="unsetApplicationManagementService" + */ +public class DynamicClientRegistrationServiceComponent { + + private static final Log log = LogFactory.getLog(DynamicClientRegistrationServiceComponent.class); + + @SuppressWarnings("unused") + protected void activate(ComponentContext componentContext) { + if(log.isDebugEnabled()){ + log.debug("Starting DynamicClientRegistrationServiceComponent"); + } + DynamicClientRegistrationService dynamicClientRegistrationService = + new DynamicClientRegistrationImpl(); + componentContext.getBundleContext().registerService(DynamicClientRegistrationService.class.getName(), + dynamicClientRegistrationService, null); + } + + @SuppressWarnings("unused") + protected void deactivate(ComponentContext componentContext) { + if(log.isDebugEnabled()){ + log.debug("Stopping DynamicClientRegistrationServiceComponent"); + } + } + + /** + * Sets ApplicationManagement Service. + * + * @param applicationManagementService An instance of ApplicationManagementService + */ + protected void setApplicationManagementService(ApplicationManagementService + applicationManagementService) { + if (log.isDebugEnabled()) { + log.debug("Setting ApplicationManagement Service"); + } + DynamicClientRegistrationDataHolder.getInstance(). + setApplicationManagementService(applicationManagementService); + } + + /** + * Unsets ApplicationManagement Service. + * + * @param applicationManagementService An instance of ApplicationManagementService + */ + protected void unsetApplicationManagementService(ApplicationManagementService + applicationManagementService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting ApplicationManagement Service"); + } + DynamicClientRegistrationDataHolder.getInstance().setApplicationManagementService(null); + } +} diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/ScopeValidator.java b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java similarity index 92% rename from components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/ScopeValidator.java rename to components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java index 0432238fac..39d62f0063 100644 --- a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/ScopeValidator.java +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java @@ -37,7 +37,7 @@ import java.util.Properties; * Custom OAuth2Token Scope validation implementation for DeviceManagement. This will validate the * user permissions before dispatching the HTTP request to the actual endpoint. */ -public class ScopeValidator extends OAuth2ScopeValidator { +public class PermissionBasedScopeValidator extends OAuth2ScopeValidator { private static final String URL_PROPERTY = "URL"; private static final String HTTP_METHOD_PROPERTY = "HTTP_METHOD"; @@ -52,7 +52,7 @@ public class ScopeValidator extends OAuth2ScopeValidator { public static final String ACTION = "action"; } - private static final Log log = LogFactory.getLog(ScopeValidator.class); + private static final Log log = LogFactory.getLog(PermissionBasedScopeValidator.class); @Override public boolean validateScope(AccessTokenDO accessTokenDO, String resource) @@ -64,8 +64,8 @@ public class ScopeValidator extends OAuth2ScopeValidator { String method = resource.substring(++idx, resource.length()); Properties properties = new Properties(); - properties.put(ScopeValidator.URL_PROPERTY, url); - properties.put(ScopeValidator.HTTP_METHOD_PROPERTY, method); + properties.put(PermissionBasedScopeValidator.URL_PROPERTY, url); + properties.put(PermissionBasedScopeValidator.HTTP_METHOD_PROPERTY, method); PermissionManagerService permissionManagerService = OAuthExtensionsDataHolder.getInstance(). getPermissionManagerService(); try {