From 21c50c9a557c784b485bf4e449938b1beff672fd Mon Sep 17 00:00:00 2001 From: Milan Perera Date: Thu, 12 Jan 2017 19:01:57 +0530 Subject: [PATCH] Fixed NPE occurred due to scope mapping --- .../lifecycle/util/AnnotationProcessor.java | 43 +++++++++++++------ .../permission/AnnotationProcessor.java | 35 +++++++++------ .../devicemgt/app/conf/config.json | 3 +- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java index 8c95252db89..0e7aa9cb0ea 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java @@ -73,6 +73,9 @@ public class AnnotationProcessor { private static final String SWAGGER_ANNOTATIONS_PROPERTIES_CONTEXT = "context"; private static final String SWAGGER_ANNOTATIONS_PROPERTIES_VALUE = "value"; private static final String ANNOTATIONS_SCOPES = "scopes"; + private static final String ANNOTATIONS_SCOPE = "scope"; + private static final String DEFAULT_SCOPE_KEY = "perm:admin"; + private static final String DEFAULT_SCOPE_PERMISSION = "/permision/device-mgt"; private static final String PERMISSION_PREFIX = "/permission/admin"; @@ -273,7 +276,16 @@ public class AnnotationProcessor { resource.setProduces(invokeMethod(producesClassMethods[0], producesAnno, STRING_ARR)); } if (annotations[i].annotationType().getName().equals(ApiOperation.class.getName())) { - resource.setScope(this.getScope(annotations[i])); + Scope scope = this.getScope(annotations[i]); + if (scope != null) { + resource.setScope(scope); + } else { + log.error("Scope is not defined for '" + makeContextURLReady(resourceRootContext) + + makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope"); + scope.setKey(DEFAULT_SCOPE_KEY); + scope.setRoles(DEFAULT_SCOPE_PERMISSION); + resource.setScope(scope); + } } } resourceList.add(resource); @@ -444,18 +456,25 @@ public class AnnotationProcessor { InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod); Annotation[] extensions = (Annotation[]) methodHandler.invoke(currentMethod, apiOperation.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null); - methodHandler = Proxy.getInvocationHandler(extensions[0]); - Annotation[] properties = (Annotation[])methodHandler.invoke(extensions[0], extensionClass - .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES,null), null); - - for (Annotation property : properties) { - methodHandler = Proxy.getInvocationHandler(property); - String scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass - .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null),null); - if (scopeKey.isEmpty()) { - return null; + if (extensions != null) { + methodHandler = Proxy.getInvocationHandler(extensions[0]); + Annotation[] properties = (Annotation[]) methodHandler.invoke(extensions[0], extensionClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES, null), null); + String scopeKey; + String propertyName; + for (Annotation property : properties) { + methodHandler = Proxy.getInvocationHandler(property); + propertyName = (String) methodHandler.invoke(property, extensionPropertyClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null); + if (ANNOTATIONS_SCOPE.equals(propertyName)) { + scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null), null); + if (scopeKey.isEmpty()) { + return null; + } + return apiScopes.get(scopeKey); + } } - return apiScopes.get(scopeKey); } return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/AnnotationProcessor.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/AnnotationProcessor.java index 01532e259e0..ec4c9442439 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/AnnotationProcessor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/AnnotationProcessor.java @@ -70,6 +70,7 @@ public class AnnotationProcessor { private static final String SWAGGER_ANNOTATIONS_PROPERTIES_KEY = "key"; private static final String SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS = "permissions"; private static final String ANNOTATIONS_SCOPES = "scopes"; + private static final String ANNOTATIONS_SCOPE = "scope"; private static final String PERMISSION_PREFIX = "/permission/admin"; @@ -375,19 +376,27 @@ public class AnnotationProcessor { InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod); Annotation[] extensions = (Annotation[]) methodHandler.invoke(currentMethod, apiOperation.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null); - methodHandler = Proxy.getInvocationHandler(extensions[0]); - Annotation[] properties = (Annotation[])methodHandler.invoke(extensions[0], extensionClass - .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES,null), null); - Scope scope; - for (Annotation property : properties) { - methodHandler = Proxy.getInvocationHandler(property); - String scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass - .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null),null); - if (!scopeKey.isEmpty()) { - scope = apiScopes.get(scopeKey); - permission.setName(scope.getName()); - //TODO: currently permission tree supports only adding one permission per API point. - permission.setPath(scope.getRoles().split(" ")[0]); + if (extensions != null) { + methodHandler = Proxy.getInvocationHandler(extensions[0]); + Annotation[] properties = (Annotation[]) methodHandler.invoke(extensions[0], extensionClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES, null), null); + Scope scope; + String scopeKey; + String propertyName; + for (Annotation property : properties) { + methodHandler = Proxy.getInvocationHandler(property); + propertyName = (String) methodHandler.invoke(property, extensionPropertyClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME, null), null); + if (ANNOTATIONS_SCOPE.equals(propertyName)) { + scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass + .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null), null); + if (!scopeKey.isEmpty()) { + scope = apiScopes.get(scopeKey); + permission.setName(scope.getName()); + //TODO: currently permission tree supports only adding one permission per API point. + permission.setPath(scope.getRoles().split(" ")[0]); + } + } } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json index 83671e61bef..686dafbe2e2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json @@ -139,7 +139,8 @@ "perm:admin:certificates:delete", "perm:admin:certificates:details", "perm:admin:certificates:view", - "perm:admin:certificates:add" + "perm:admin:certificates:add", + "perm:admin" ], "isOAuthEnabled" : true, "backendRestEndpoints" : {