From 6db9b2162358eadfa5db2d00925d8da1072e2ca6 Mon Sep 17 00:00:00 2001 From: mharindu Date: Thu, 28 Apr 2016 14:49:34 +0530 Subject: [PATCH] Code cleanup in webapp publisher --- .../apimgt/annotations/api/Permission.java | 9 +++++ .../publisher/APIPublisherServiceImpl.java | 8 +++-- .../webapp/publisher/APIPublisherUtil.java | 35 ++++++++++--------- .../lifecycle/util/AnnotationUtil.java | 1 - 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/src/main/java/org/wso2/carbon/apimgt/annotations/api/Permission.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/src/main/java/org/wso2/carbon/apimgt/annotations/api/Permission.java index 5cb590d3a5..65ade5cc3a 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/src/main/java/org/wso2/carbon/apimgt/annotations/api/Permission.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/src/main/java/org/wso2/carbon/apimgt/annotations/api/Permission.java @@ -30,7 +30,16 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) public @interface Permission { + /** + * Represents the scope name. + * @return Returns scope name. + */ String scope(); + + /** + * Represents the associated permissions. + * @return Returns list of permissions. + */ String[] permissions(); } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java index 1d29863f86..fcb27541db 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -107,8 +107,6 @@ public class APIPublisherServiceImpl implements APIPublisherService { List scopes = new ArrayList<>(); for (URITemplate uriTemplate : api.getUriTemplates()) { - JsonObject authType = new JsonObject(); - authType.addProperty("x-auth-type", "Application%20%26%20Application%20User"); JsonObject response = new JsonObject(); response.addProperty("200", ""); @@ -148,7 +146,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { swaggerDefinition.addProperty("swagger", "2.0"); swaggerDefinition.add("info", info); - // adding scopes to definition + // adding scopes to swagger definition if (!api.getScopes().isEmpty()) { Gson gson = new Gson(); JsonElement element = gson.toJsonTree(api.getScopes(), new TypeToken>() { @@ -162,6 +160,9 @@ public class APIPublisherServiceImpl implements APIPublisherService { swaggerDefinition.add("x-wso2-security", wso2Security); } } + if (log.isDebugEnabled()) { + log.debug("API swagger definition: " + swaggerDefinition.toString()); + } return swaggerDefinition.toString(); } @@ -193,4 +194,5 @@ public class APIPublisherServiceImpl implements APIPublisherService { log.debug("End of publishing the batch of APIs"); } } + } 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 d684e12562..2e7c8caac7 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 @@ -42,14 +42,10 @@ public class APIPublisherUtil { public static final String API_VERSION_PARAM = "{version}"; public static final String API_PUBLISH_ENVIRONMENT = "Production and Sandbox"; private static final String API_CONFIG_DEFAULT_VERSION = "1.0.0"; - private static final String PARAM_MANAGED_API_NAME = "managed-api-name"; - private static final String PARAM_MANAGED_API_VERSION = "managed-api-version"; - private static final String PARAM_MANAGED_API_CONTEXT = "managed-api-context"; private static final String PARAM_MANAGED_API_ENDPOINT = "managed-api-endpoint"; private static final String PARAM_MANAGED_API_OWNER = "managed-api-owner"; private static final String PARAM_MANAGED_API_TRANSPORTS = "managed-api-transports"; private static final String PARAM_MANAGED_API_IS_SECURED = "managed-api-isSecured"; - private static final String PARAM_MANAGED_API_APPLICATION = "managed-api-application"; private static final String PARAM_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants"; private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain"; @@ -125,16 +121,22 @@ public class APIPublisherUtil { if (scope != null) { if (apiScopes.get(scope.getKey()) == null) { apiScopes.put(scope.getKey(), scope); - } else { - // this has to be done because of the use of pass by reference - // where same object reference of scope should be available for both - // api scope and uri template scope - template.setScope(apiScopes.get(scope.getKey())); } } } Set scopes = new HashSet<>(apiScopes.values()); api.setScopes(scopes); + + // this has to be done because of the use of pass by reference + // where same object reference of scope should be available for both + // api scope and uri template scope + for (Scope scope : scopes) { + for (URITemplate template : uriTemplates) { + if (scope.getKey().equals(template.getScope().getKey())) { + template.setScope(scope); + } + } + } api.setUriTemplates(uriTemplates); } return api; @@ -202,12 +204,13 @@ public class APIPublisherUtil { * Build the API Configuration to be passed to APIM, from a given list of URL templates * * @param servletContext + * @param apiDef * @return */ - public static APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apidef) { + public static APIConfig buildApiConfig(ServletContext servletContext, APIResourceConfiguration apiDef) { APIConfig apiConfig = new APIConfig(); - String name = apidef.getName(); + String name = apiDef.getName(); if (name == null || name.isEmpty()) { if (log.isDebugEnabled()) { log.debug("API Name not set in @API Annotation"); @@ -216,7 +219,7 @@ public class APIPublisherUtil { } apiConfig.setName(name); - String version = apidef.getVersion(); + String version = apiDef.getVersion(); if (version == null || version.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'API Version not set in @API Annotation'"); @@ -226,7 +229,7 @@ public class APIPublisherUtil { apiConfig.setVersion(version); - String context = apidef.getContext(); + String context = apiDef.getContext(); if (context == null || context.isEmpty()) { if (log.isDebugEnabled()) { log.debug("'API Context not set in @API Annotation'"); @@ -235,7 +238,7 @@ public class APIPublisherUtil { } apiConfig.setContext(context); - String[] tags = apidef.getTags(); + String[] tags = apiDef.getTags(); if (tags == null || tags.length == 0) { if (log.isDebugEnabled()) { log.debug("'API tag not set in @API Annotation'"); @@ -300,8 +303,8 @@ public class APIPublisherUtil { && Boolean.parseBoolean(sharingValueParam)); apiConfig.setSharedWithAllTenants(isSharedWithAllTenants); - Set uriTemplates = new LinkedHashSet(); - for (APIResource apiResource : apidef.getResources()) { + Set uriTemplates = new LinkedHashSet<>(); + for (APIResource apiResource : apiDef.getResources()) { URITemplate template = new URITemplate(); template.setAuthType(apiResource.getAuthType()); template.setHTTPVerb(apiResource.getHttpVerb()); diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationUtil.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationUtil.java index 070924bef6..4974a54c0e 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationUtil.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/util/AnnotationUtil.java @@ -125,7 +125,6 @@ public class AnnotationUtil { .class.getName()); Annotation apiAnno = clazz.getAnnotation(apiClazz); - List resourceList; if (apiAnno != null) {