From a06327b731da38bc15f60b39c303d6d486eea3c7 Mon Sep 17 00:00:00 2001 From: Ace Date: Wed, 27 Apr 2016 20:43:13 +0530 Subject: [PATCH] refactoring and bug fixes --- .../webapp/publisher/APIPublisherUtil.java | 13 +- .../lifecycle/util/AnnotationUtil.java | 151 ++++++++++++------ .../feature/mgt/util/AnnotationUtil.java | 26 ++- 3 files changed, 131 insertions(+), 59 deletions(-) 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 f927bb5c84..877f6bb788 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 @@ -72,10 +72,12 @@ public class APIPublisherUtil { } public static API getAPI(APIConfig config) throws APIManagementException { + APIProvider provider = config.getProvider(); String apiVersion = config.getVersion(); APIIdentifier id = new APIIdentifier(replaceEmailDomain(config.getOwner()), config.getName(), apiVersion); API api = new API(id); + api.setApiOwner(config.getOwner()); String context = config.getContext(); context = context.startsWith("/") ? context : ("/" + context); @@ -84,12 +86,14 @@ public class APIPublisherUtil { //Create tenant aware context for API context = "/t/" + providerDomain + context; } + // This is to support the new Pluggable version strategy // if the context does not contain any {version} segment, we use the default version strategy. context = checkAndSetVersionParam(context); api.setContextTemplate(context); context = updateContextWithVersion(config.getVersion(), context); api.setContext(context); + api.setUrl(config.getEndpoint()); api.addAvailableTiers(provider.getTiers()); api.setEndpointSecured(true); @@ -97,12 +101,15 @@ public class APIPublisherUtil { api.setTransports(config.getTransports()); api.setContextTemplate(config.getContextTemplate()); api.setUriTemplates(config.getUriTemplates()); - Set environements = new HashSet<>(); - environements.add(API_PUBLISH_ENVIRONEMENT); - api.setEnvironments(environements); + + Set environments = new HashSet<>(); + environments.add(API_PUBLISH_ENVIRONEMENT); + api.setEnvironments(environments); + Set tiers = new HashSet(); tiers.add(new Tier(APIConstants.UNLIMITED_TIER)); api.addAvailableTiers(tiers); + if (config.isSharedWithAllTenants()) { api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS); api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); 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 a26c2042f1..8120a9d60e 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 @@ -24,8 +24,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.scannotation.AnnotationDB; import org.scannotation.WarUrlFinder; +import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.webapp.publisher.config.APIResource; import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration; + import javax.servlet.ServletContext; import javax.ws.rs.*; import java.io.IOException; @@ -45,17 +47,18 @@ public class AnnotationUtil { private static final String PACKAGE_ORG_APACHE = "org.apache"; private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus"; private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework"; + private static final String AUTH_TYPE = "Any"; private static final String PROTOCOL_HTTP = "http"; private static final String SERVER_HOST = "carbon.local.ip"; private static final String HTTP_PORT = "httpPort"; - public static final String DIR_WEB_INF_LIB = "/WEB-INF/lib"; - public static final String STRING_ARR = "string_arr"; - public static final String STRING = "string"; + private static final String STRING_ARR = "string_arr"; + private static final String STRING = "string"; private StandardContext context; private Method[] pathClazzMethods; private Class pathClazz; + Class apiClazz; private ClassLoader classLoader; private ServletContext servletContext; @@ -68,6 +71,7 @@ public class AnnotationUtil { /** * Scan the context for classes with annotations + * * @return * @throws IOException */ @@ -89,6 +93,7 @@ public class AnnotationUtil { /** * Method identifies the URL templates and context by reading the annotations of a class + * * @param entityClasses * @return */ @@ -107,38 +112,23 @@ public class AnnotationUtil { APIResourceConfiguration apiResourceConfig = null; try { clazz = classLoader.loadClass(className); - Class apiClazz = (Class) - classLoader.loadClass(org.wso2.carbon.apimgt.annotations.api.API.class.getName()); + + apiClazz = (Class) + classLoader.loadClass(org.wso2.carbon.apimgt.annotations.api.API + .class.getName()); + Annotation apiAnno = clazz.getAnnotation(apiClazz); List resourceList; - apiResourceConfig = new APIResourceConfiguration(); if (apiAnno != null) { - Method[] apiClazzMethods = apiClazz.getMethods(); - if (log.isDebugEnabled()) { log.debug("Application Context root = " + servletContext.getContextPath()); } try { - for(int k=0;k) classLoader.loadClass(Path.class.getName()); @@ -177,7 +167,45 @@ public class AnnotationUtil { return apiResourceConfigs; } - private List getApiResources(String resourceRootContext, String apiRootContext, Method[] annotatedMethods) throws Throwable { + /** + * Iterate API annotation and build API Configuration + * @param apiAnno + * @return + * @throws Throwable + */ + private APIResourceConfiguration processAPIAnnotation(Annotation apiAnno) throws Throwable { + Method[] apiClazzMethods = apiClazz.getMethods(); + APIResourceConfiguration apiResourceConfig = new APIResourceConfiguration(); + for (int k = 0; k < apiClazzMethods.length; k++) { + switch (apiClazzMethods[k].getName()) { + case "name": + apiResourceConfig.setName(invokeMethod(apiClazzMethods[k], apiAnno, STRING)); + break; + case "version": + apiResourceConfig.setVersion(invokeMethod(apiClazzMethods[k], apiAnno, STRING)); + break; + case "context": + apiResourceConfig.setContext(invokeMethod(apiClazzMethods[k], apiAnno, STRING)); + break; + case "tags": + apiResourceConfig.setTags(invokeMethod(apiClazzMethods[k], apiAnno)); + break; + } + } + return apiResourceConfig; + } + + + /** + * Get Resources for each API + * @param resourceRootContext + * @param apiRootContext + * @param annotatedMethods + * @return + * @throws Throwable + */ + private List getApiResources(String resourceRootContext, String apiRootContext, + Method[] annotatedMethods) throws Throwable { List resourceList; resourceList = new ArrayList(); for (Method method : annotatedMethods) { @@ -195,31 +223,19 @@ public class AnnotationUtil { resource.setAuthType(AUTH_TYPE); Annotation[] annotations = method.getDeclaredAnnotations(); - for(int i=0; i consumesClass = (Class) classLoader.loadClass(Consumes.class.getName()); + processHTTPMethodAnnotation(resource, annotations[i]); + if (annotations[i].annotationType().getName().equals(Consumes.class.getName())) { + Class consumesClass = (Class) classLoader.loadClass( + Consumes.class.getName()); Method[] consumesClassMethods = consumesClass.getMethods(); Annotation consumesAnno = method.getAnnotation(consumesClass); resource.setConsumes(invokeMethod(consumesClassMethods[0], consumesAnno, STRING_ARR)); } - if(annotations[i].annotationType().getName().equals(Produces.class.getName())){ - Class producesClass = (Class) classLoader.loadClass(Produces.class.getName()); + if (annotations[i].annotationType().getName().equals(Produces.class.getName())) { + Class producesClass = (Class) classLoader.loadClass( + Produces.class.getName()); Method[] producesClassMethods = producesClass.getMethods(); Annotation producesAnno = method.getAnnotation(producesClass); resource.setProduces(invokeMethod(producesClassMethods[0], producesAnno, STRING_ARR)); @@ -231,12 +247,40 @@ public class AnnotationUtil { return resourceList; } - private String makeContextURLReady(String context){ - if(context != null && !context.equalsIgnoreCase("")){ - if(context.startsWith("/")){ + /** + * Read Method annotations indicating HTTP Methods + * @param resource + * @param annotation + */ + private void processHTTPMethodAnnotation(APIResource resource, Annotation annotation) { + if (annotation.annotationType().getName().equals(GET.class.getName())) { + resource.setHttpVerb(HttpMethod.GET); + } + if (annotation.annotationType().getName().equals(POST.class.getName())) { + resource.setHttpVerb(HttpMethod.POST); + } + if (annotation.annotationType().getName().equals(OPTIONS.class.getName())) { + resource.setHttpVerb(HttpMethod.OPTIONS); + } + if (annotation.annotationType().getName().equals(DELETE.class.getName())) { + resource.setHttpVerb(HttpMethod.DELETE); + } + if (annotation.annotationType().getName().equals(PUT.class.getName())) { + resource.setHttpVerb(HttpMethod.PUT); + } + } + + /** + * Append '/' to the context and make it URL ready + * @param context + * @return + */ + private String makeContextURLReady(String context) { + if (context != null && !context.equalsIgnoreCase("")) { + if (context.startsWith("/")) { return context; - }else{ - return "/"+context; + } else { + return "/" + context; } } return ""; @@ -244,6 +288,7 @@ public class AnnotationUtil { /** * When an annotation and method is passed, this method invokes that executes said method against the annotation + * * @param method * @param annotation * @param returnType @@ -252,11 +297,11 @@ public class AnnotationUtil { */ private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable { InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation); - switch (returnType){ + switch (returnType) { case STRING: return (String) methodHandler.invoke(annotation, method, null); case STRING_ARR: - return ((String[])methodHandler.invoke(annotation, method, null))[0]; + return ((String[]) methodHandler.invoke(annotation, method, null))[0]; default: return null; } @@ -267,6 +312,6 @@ public class AnnotationUtil { */ private String[] invokeMethod(Method method, Annotation annotation) throws Throwable { InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation); - return ((String[])methodHandler.invoke(annotation, method, null)); + return ((String[]) methodHandler.invoke(annotation, method, null)); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationUtil.java index 8941ad0312..dbca91f10e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationUtil.java @@ -62,8 +62,8 @@ public class AnnotationUtil { private static final String PACKAGE_ORG_APACHE = "org.apache"; private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus"; private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework"; - public static final String STRING_ARR = "string_arr"; - public static final String STRING = "string"; + private static final String STRING_ARR = "string_arr"; + private static final String STRING = "string"; private Class featureAnnotationClazz; private ClassLoader classLoader; @@ -164,7 +164,8 @@ public class AnnotationUtil { return featureList; } - private Map processParamAnnotations(Map apiParams, Method currentMethod) throws Throwable{ + private Map processParamAnnotations(Map apiParams, Method currentMethod) + throws Throwable{ try { apiParams.put("pathParams", processParamAnnotations(currentMethod, PathParam.class)); apiParams.put("queryParams", processParamAnnotations(currentMethod, QueryParam.class)); @@ -196,6 +197,12 @@ public class AnnotationUtil { return params; } + /** + * Read Method annotations indicating HTTP Methods + * @param feature + * @param currentAnnotation + * @return + */ private Feature processHttpMethodAnnotation(Feature feature, Annotation currentAnnotation) { //Extracting method with which feature is exposed if (currentAnnotation.annotationType().getName().equals(GET.class.getName())) { @@ -212,6 +219,13 @@ public class AnnotationUtil { return feature; } + /** + * Read Feature annotation and Identify Features + * @param feature + * @param currentMethod + * @return + * @throws Throwable + */ private Feature processFeatureAnnotation(Feature feature, Method currentMethod) throws Throwable{ Method[] featureAnnoMethods = featureAnnotationClazz.getMethods(); Annotation featureAnno = currentMethod.getAnnotation(featureAnnotationClazz); @@ -234,6 +248,12 @@ public class AnnotationUtil { return feature; } + /** + * Get value depicted by Path Annotation + * @param currentMethod + * @return + * @throws Throwable + */ public String getPathAnnotationValue(Method currentMethod) throws Throwable{ String uri = ""; try {