Merge pull request #226 from ruwany/master

refactoring and bug fixes
4.x.x
Ruwan 9 years ago
commit 1486e4ab8c

@ -72,10 +72,12 @@ public class APIPublisherUtil {
} }
public static API getAPI(APIConfig config) throws APIManagementException { public static API getAPI(APIConfig config) throws APIManagementException {
APIProvider provider = config.getProvider(); APIProvider provider = config.getProvider();
String apiVersion = config.getVersion(); String apiVersion = config.getVersion();
APIIdentifier id = new APIIdentifier(replaceEmailDomain(config.getOwner()), config.getName(), apiVersion); APIIdentifier id = new APIIdentifier(replaceEmailDomain(config.getOwner()), config.getName(), apiVersion);
API api = new API(id); API api = new API(id);
api.setApiOwner(config.getOwner()); api.setApiOwner(config.getOwner());
String context = config.getContext(); String context = config.getContext();
context = context.startsWith("/") ? context : ("/" + context); context = context.startsWith("/") ? context : ("/" + context);
@ -84,12 +86,14 @@ public class APIPublisherUtil {
//Create tenant aware context for API //Create tenant aware context for API
context = "/t/" + providerDomain + context; context = "/t/" + providerDomain + context;
} }
// This is to support the new Pluggable version strategy // This is to support the new Pluggable version strategy
// if the context does not contain any {version} segment, we use the default version strategy. // if the context does not contain any {version} segment, we use the default version strategy.
context = checkAndSetVersionParam(context); context = checkAndSetVersionParam(context);
api.setContextTemplate(context); api.setContextTemplate(context);
context = updateContextWithVersion(config.getVersion(), context); context = updateContextWithVersion(config.getVersion(), context);
api.setContext(context); api.setContext(context);
api.setUrl(config.getEndpoint()); api.setUrl(config.getEndpoint());
api.addAvailableTiers(provider.getTiers()); api.addAvailableTiers(provider.getTiers());
api.setEndpointSecured(true); api.setEndpointSecured(true);
@ -97,12 +101,15 @@ public class APIPublisherUtil {
api.setTransports(config.getTransports()); api.setTransports(config.getTransports());
api.setContextTemplate(config.getContextTemplate()); api.setContextTemplate(config.getContextTemplate());
api.setUriTemplates(config.getUriTemplates()); api.setUriTemplates(config.getUriTemplates());
Set<String> environements = new HashSet<>();
environements.add(API_PUBLISH_ENVIRONEMENT); Set<String> environments = new HashSet<>();
api.setEnvironments(environements); environments.add(API_PUBLISH_ENVIRONEMENT);
api.setEnvironments(environments);
Set<Tier> tiers = new HashSet<Tier>(); Set<Tier> tiers = new HashSet<Tier>();
tiers.add(new Tier(APIConstants.UNLIMITED_TIER)); tiers.add(new Tier(APIConstants.UNLIMITED_TIER));
api.addAvailableTiers(tiers); api.addAvailableTiers(tiers);
if (config.isSharedWithAllTenants()) { if (config.isSharedWithAllTenants()) {
api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS); api.setSubscriptionAvailability(APIConstants.SUBSCRIPTION_TO_ALL_TENANTS);
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);

@ -24,8 +24,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.scannotation.AnnotationDB; import org.scannotation.AnnotationDB;
import org.scannotation.WarUrlFinder; 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.APIResource;
import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration; import org.wso2.carbon.apimgt.webapp.publisher.config.APIResourceConfiguration;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.ws.rs.*; import javax.ws.rs.*;
import java.io.IOException; 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_APACHE = "org.apache";
private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus"; private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus";
private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework"; private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework";
private static final String AUTH_TYPE = "Any"; private static final String AUTH_TYPE = "Any";
private static final String PROTOCOL_HTTP = "http"; private static final String PROTOCOL_HTTP = "http";
private static final String SERVER_HOST = "carbon.local.ip"; private static final String SERVER_HOST = "carbon.local.ip";
private static final String HTTP_PORT = "httpPort"; private static final String HTTP_PORT = "httpPort";
public static final String DIR_WEB_INF_LIB = "/WEB-INF/lib"; private static final String STRING_ARR = "string_arr";
public static final String STRING_ARR = "string_arr"; private static final String STRING = "string";
public static final String STRING = "string";
private StandardContext context; private StandardContext context;
private Method[] pathClazzMethods; private Method[] pathClazzMethods;
private Class<Path> pathClazz; private Class<Path> pathClazz;
Class<API> apiClazz;
private ClassLoader classLoader; private ClassLoader classLoader;
private ServletContext servletContext; private ServletContext servletContext;
@ -68,6 +71,7 @@ public class AnnotationUtil {
/** /**
* Scan the context for classes with annotations * Scan the context for classes with annotations
*
* @return * @return
* @throws IOException * @throws IOException
*/ */
@ -89,6 +93,7 @@ public class AnnotationUtil {
/** /**
* Method identifies the URL templates and context by reading the annotations of a class * Method identifies the URL templates and context by reading the annotations of a class
*
* @param entityClasses * @param entityClasses
* @return * @return
*/ */
@ -107,38 +112,23 @@ public class AnnotationUtil {
APIResourceConfiguration apiResourceConfig = null; APIResourceConfiguration apiResourceConfig = null;
try { try {
clazz = classLoader.loadClass(className); clazz = classLoader.loadClass(className);
Class<Path> apiClazz = (Class<Path>)
classLoader.loadClass(org.wso2.carbon.apimgt.annotations.api.API.class.getName()); apiClazz = (Class<API>)
classLoader.loadClass(org.wso2.carbon.apimgt.annotations.api.API
.class.getName());
Annotation apiAnno = clazz.getAnnotation(apiClazz); Annotation apiAnno = clazz.getAnnotation(apiClazz);
List<APIResource> resourceList; List<APIResource> resourceList;
apiResourceConfig = new APIResourceConfiguration();
if (apiAnno != null) { if (apiAnno != null) {
Method[] apiClazzMethods = apiClazz.getMethods();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Application Context root = " + servletContext.getContextPath()); log.debug("Application Context root = " + servletContext.getContextPath());
} }
try { try {
for(int k=0;k<apiClazzMethods.length;k++){ apiResourceConfig = processAPIAnnotation(apiAnno);
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;
}
}
// All the apis should map to same root "/" // All the apis should map to same root "/"
String rootContext = servletContext.getContextPath(); String rootContext = servletContext.getContextPath();
pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName()); pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName());
@ -177,7 +167,45 @@ public class AnnotationUtil {
return apiResourceConfigs; return apiResourceConfigs;
} }
private List<APIResource> 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<APIResource> getApiResources(String resourceRootContext, String apiRootContext,
Method[] annotatedMethods) throws Throwable {
List<APIResource> resourceList; List<APIResource> resourceList;
resourceList = new ArrayList<APIResource>(); resourceList = new ArrayList<APIResource>();
for (Method method : annotatedMethods) { for (Method method : annotatedMethods) {
@ -195,31 +223,19 @@ public class AnnotationUtil {
resource.setAuthType(AUTH_TYPE); resource.setAuthType(AUTH_TYPE);
Annotation[] annotations = method.getDeclaredAnnotations(); Annotation[] annotations = method.getDeclaredAnnotations();
for(int i=0; i<annotations.length; i++){ for (int i = 0; i < annotations.length; i++) {
if(annotations[i].annotationType().getName().equals(GET.class.getName())){ processHTTPMethodAnnotation(resource, annotations[i]);
resource.setHttpVerb(HttpMethod.GET); if (annotations[i].annotationType().getName().equals(Consumes.class.getName())) {
} Class<Consumes> consumesClass = (Class<Consumes>) classLoader.loadClass(
if(annotations[i].annotationType().getName().equals(POST.class.getName())){ Consumes.class.getName());
resource.setHttpVerb(HttpMethod.POST);
}
if(annotations[i].annotationType().getName().equals(OPTIONS.class.getName())){
resource.setHttpVerb(HttpMethod.OPTIONS);
}
if(annotations[i].annotationType().getName().equals(DELETE.class.getName())){
resource.setHttpVerb(HttpMethod.DELETE);
}
if(annotations[i].annotationType().getName().equals(PUT.class.getName())){
resource.setHttpVerb(HttpMethod.PUT);
}
if(annotations[i].annotationType().getName().equals(Consumes.class.getName())){
Class<Consumes> consumesClass = (Class<Consumes>) classLoader.loadClass(Consumes.class.getName());
Method[] consumesClassMethods = consumesClass.getMethods(); Method[] consumesClassMethods = consumesClass.getMethods();
Annotation consumesAnno = method.getAnnotation(consumesClass); Annotation consumesAnno = method.getAnnotation(consumesClass);
resource.setConsumes(invokeMethod(consumesClassMethods[0], consumesAnno, STRING_ARR)); resource.setConsumes(invokeMethod(consumesClassMethods[0], consumesAnno, STRING_ARR));
} }
if(annotations[i].annotationType().getName().equals(Produces.class.getName())){ if (annotations[i].annotationType().getName().equals(Produces.class.getName())) {
Class<Produces> producesClass = (Class<Produces>) classLoader.loadClass(Produces.class.getName()); Class<Produces> producesClass = (Class<Produces>) classLoader.loadClass(
Produces.class.getName());
Method[] producesClassMethods = producesClass.getMethods(); Method[] producesClassMethods = producesClass.getMethods();
Annotation producesAnno = method.getAnnotation(producesClass); Annotation producesAnno = method.getAnnotation(producesClass);
resource.setProduces(invokeMethod(producesClassMethods[0], producesAnno, STRING_ARR)); resource.setProduces(invokeMethod(producesClassMethods[0], producesAnno, STRING_ARR));
@ -231,12 +247,40 @@ public class AnnotationUtil {
return resourceList; return resourceList;
} }
private String makeContextURLReady(String context){ /**
if(context != null && !context.equalsIgnoreCase("")){ * Read Method annotations indicating HTTP Methods
if(context.startsWith("/")){ * @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; return context;
}else{ } else {
return "/"+context; return "/" + context;
} }
} }
return ""; 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 * When an annotation and method is passed, this method invokes that executes said method against the annotation
*
* @param method * @param method
* @param annotation * @param annotation
* @param returnType * @param returnType
@ -252,11 +297,11 @@ public class AnnotationUtil {
*/ */
private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable { private String invokeMethod(Method method, Annotation annotation, String returnType) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation); InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
switch (returnType){ switch (returnType) {
case STRING: case STRING:
return (String) methodHandler.invoke(annotation, method, null); return (String) methodHandler.invoke(annotation, method, null);
case STRING_ARR: case STRING_ARR:
return ((String[])methodHandler.invoke(annotation, method, null))[0]; return ((String[]) methodHandler.invoke(annotation, method, null))[0];
default: default:
return null; return null;
} }
@ -267,6 +312,6 @@ public class AnnotationUtil {
*/ */
private String[] invokeMethod(Method method, Annotation annotation) throws Throwable { private String[] invokeMethod(Method method, Annotation annotation) throws Throwable {
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation); InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
return ((String[])methodHandler.invoke(annotation, method, null)); return ((String[]) methodHandler.invoke(annotation, method, null));
} }
} }

@ -62,8 +62,8 @@ public class AnnotationUtil {
private static final String PACKAGE_ORG_APACHE = "org.apache"; private static final String PACKAGE_ORG_APACHE = "org.apache";
private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus"; private static final String PACKAGE_ORG_CODEHAUS = "org.codehaus";
private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework"; private static final String PACKAGE_ORG_SPRINGFRAMEWORK = "org.springframework";
public static final String STRING_ARR = "string_arr"; private static final String STRING_ARR = "string_arr";
public static final String STRING = "string"; private static final String STRING = "string";
private Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature> private Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature>
featureAnnotationClazz; featureAnnotationClazz;
private ClassLoader classLoader; private ClassLoader classLoader;
@ -164,7 +164,8 @@ public class AnnotationUtil {
return featureList; return featureList;
} }
private Map<String, Object> processParamAnnotations(Map<String, Object> apiParams, Method currentMethod) throws Throwable{ private Map<String, Object> processParamAnnotations(Map<String, Object> apiParams, Method currentMethod)
throws Throwable{
try { try {
apiParams.put("pathParams", processParamAnnotations(currentMethod, PathParam.class)); apiParams.put("pathParams", processParamAnnotations(currentMethod, PathParam.class));
apiParams.put("queryParams", processParamAnnotations(currentMethod, QueryParam.class)); apiParams.put("queryParams", processParamAnnotations(currentMethod, QueryParam.class));
@ -196,6 +197,12 @@ public class AnnotationUtil {
return params; return params;
} }
/**
* Read Method annotations indicating HTTP Methods
* @param feature
* @param currentAnnotation
* @return
*/
private Feature processHttpMethodAnnotation(Feature feature, Annotation currentAnnotation) { private Feature processHttpMethodAnnotation(Feature feature, Annotation currentAnnotation) {
//Extracting method with which feature is exposed //Extracting method with which feature is exposed
if (currentAnnotation.annotationType().getName().equals(GET.class.getName())) { if (currentAnnotation.annotationType().getName().equals(GET.class.getName())) {
@ -212,6 +219,13 @@ public class AnnotationUtil {
return feature; return feature;
} }
/**
* Read Feature annotation and Identify Features
* @param feature
* @param currentMethod
* @return
* @throws Throwable
*/
private Feature processFeatureAnnotation(Feature feature, Method currentMethod) throws Throwable{ private Feature processFeatureAnnotation(Feature feature, Method currentMethod) throws Throwable{
Method[] featureAnnoMethods = featureAnnotationClazz.getMethods(); Method[] featureAnnoMethods = featureAnnotationClazz.getMethods();
Annotation featureAnno = currentMethod.getAnnotation(featureAnnotationClazz); Annotation featureAnno = currentMethod.getAnnotation(featureAnnotationClazz);
@ -234,6 +248,12 @@ public class AnnotationUtil {
return feature; return feature;
} }
/**
* Get value depicted by Path Annotation
* @param currentMethod
* @return
* @throws Throwable
*/
public String getPathAnnotationValue(Method currentMethod) throws Throwable{ public String getPathAnnotationValue(Method currentMethod) throws Throwable{
String uri = ""; String uri = "";
try { try {

Loading…
Cancel
Save