Now operations are auto generated and supports query params, path params and form params.

revert-70aa11f8
Rasika Perera 9 years ago
parent bb33b4ea12
commit 2d44ec234b

@ -35,6 +35,9 @@ import javax.ws.rs.HttpMethod;
import javax.ws.rs.OPTIONS; import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
@ -61,7 +64,8 @@ public class AnnotationUtil {
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"; public static final String STRING_ARR = "string_arr";
public static final String STRING = "string"; public static final String STRING = "string";
private Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature> featureClazz; private Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature>
featureAnnotationClazz;
private ClassLoader classLoader; private ClassLoader classLoader;
private ServletContext servletContext; private ServletContext servletContext;
@ -108,7 +112,7 @@ public class AnnotationUtil {
if (deviceTypeAnno != null) { if (deviceTypeAnno != null) {
Method[] deviceTypeMethod = deviceTypeClazz.getMethods(); Method[] deviceTypeMethod = deviceTypeClazz.getMethods();
String deviceType = invokeMethod(deviceTypeMethod[0], deviceTypeAnno, STRING); String deviceType = invokeMethod(deviceTypeMethod[0], deviceTypeAnno, STRING);
featureClazz = featureAnnotationClazz =
(Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations (Class<org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations
.Feature>) classLoader.loadClass( .Feature>) classLoader.loadClass(
org.wso2.carbon.device.mgt.extensions.feature.mgt org.wso2.carbon.device.mgt.extensions.feature.mgt
@ -130,19 +134,87 @@ public class AnnotationUtil {
return features; return features;
} }
private List<Feature> getFeatures(Method[] annotatedMethods) throws Throwable { private List<Feature> getFeatures(Method[] methodsList) throws Throwable {
List<Feature> featureList = new ArrayList<>(); List<Feature> featureList = new ArrayList<>();
for (Method method : annotatedMethods) { for (Method currentMethod : methodsList) {
Annotation methodAnnotation = method.getAnnotation(featureClazz); Annotation featureAnnotation = currentMethod.getAnnotation(featureAnnotationClazz);
if (methodAnnotation != null) { if (featureAnnotation != null) {
Annotation[] annotations = method.getDeclaredAnnotations();
for (int i = 0; i < annotations.length; i++) {
if (annotations[i].annotationType().getName().equals(
org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature.class.getName())) {
Feature feature = new Feature(); Feature feature = new Feature();
Method[] featureAnnoMethods = featureClazz.getMethods(); feature = processFeatureAnnotation(feature, currentMethod);
Annotation featureAnno = method.getAnnotation(featureClazz); Annotation[] annotations = currentMethod.getDeclaredAnnotations();
Feature.MetadataEntry metadataEntry = new Feature.MetadataEntry();
metadataEntry.setId(-1);
Map<String, Object> apiParams = new HashMap<>();
for (int i = 0; i < annotations.length; i++) {
Annotation currentAnnotation = annotations[i];
feature = processHttpMethodAnnotation(feature, currentAnnotation);
if (currentAnnotation.annotationType().getName().equals(Path.class.getName())) {
String uri = getPathAnnotationValue(currentMethod);
apiParams.put("uri", uri);
}
apiParams = processParamAnnotations(apiParams, currentMethod);
}
metadataEntry.setValue(apiParams);
List<Feature.MetadataEntry> metaInfoList = new ArrayList<>();
metaInfoList.add(metadataEntry);
feature.setMetadataEntries(metaInfoList);
featureList.add(feature);
}
}
return featureList;
}
private Map<String, Object> processParamAnnotations(Map<String, Object> apiParams, Method currentMethod) throws Throwable{
try {
apiParams.put("pathParams", processParamAnnotations(currentMethod, PathParam.class));
apiParams.put("queryParams", processParamAnnotations(currentMethod, QueryParam.class));
apiParams.put("formParams", processParamAnnotations(currentMethod, FormParam.class));
} catch (ClassNotFoundException e) {
log.debug("No Form Param found for class " + featureAnnotationClazz.getName());
}
return apiParams;
}
private List<String> processParamAnnotations(Method currentMethod, Class<?> clazz) throws Throwable{
List<String> params = new ArrayList<>();
try {
Class<?> paramClazz = (Class<?>) classLoader.loadClass(clazz.getName());
Method[] formMethods = paramClazz.getMethods();
//Extract method parameter information and store same as feature meta info
Annotation[][] paramAnnotations = currentMethod.getParameterAnnotations();
Method valueMethod = formMethods[0];
for (int j = 0; j < paramAnnotations.length; j++) {
for (Annotation anno : paramAnnotations[j]) {
if (anno.annotationType().getName().equals(clazz.getName())) {
params.add(invokeMethod(valueMethod, anno, STRING));
}
}
}
} catch (ClassNotFoundException e) {
log.debug("No "+ clazz.getName() +" Param found for class " + featureAnnotationClazz.getName());
}
return params;
}
private Feature processHttpMethodAnnotation(Feature feature, Annotation currentAnnotation) {
//Extracting method with which feature is exposed
if (currentAnnotation.annotationType().getName().equals(GET.class.getName())) {
feature.setMethod(HttpMethod.GET);
} else if (currentAnnotation.annotationType().getName().equals(POST.class.getName())) {
feature.setMethod(HttpMethod.POST);
} else if (currentAnnotation.annotationType().getName().equals(OPTIONS.class.getName())) {
feature.setMethod(HttpMethod.OPTIONS);
} else if (currentAnnotation.annotationType().getName().equals(DELETE.class.getName())) {
feature.setMethod(HttpMethod.DELETE);
} else if (currentAnnotation.annotationType().getName().equals(PUT.class.getName())) {
feature.setMethod(HttpMethod.PUT);
}
return feature;
}
private Feature processFeatureAnnotation(Feature feature, Method currentMethod) throws Throwable{
Method[] featureAnnoMethods = featureAnnotationClazz.getMethods();
Annotation featureAnno = currentMethod.getAnnotation(featureAnnotationClazz);
for (int k = 0; k < featureAnnoMethods.length; k++) { for (int k = 0; k < featureAnnoMethods.length; k++) {
switch (featureAnnoMethods[k].getName()) { switch (featureAnnoMethods[k].getName()) {
case "name": case "name":
@ -159,49 +231,21 @@ public class AnnotationUtil {
break; break;
} }
} }
//Extracting method with which feature is exposed return feature;
if (annotations[i].annotationType().getName().equals(GET.class.getName())) {
feature.setMethod(HttpMethod.GET);
}
if (annotations[i].annotationType().getName().equals(POST.class.getName())) {
feature.setMethod(HttpMethod.POST);
}
if (annotations[i].annotationType().getName().equals(OPTIONS.class.getName())) {
feature.setMethod(HttpMethod.OPTIONS);
}
if (annotations[i].annotationType().getName().equals(DELETE.class.getName())) {
feature.setMethod(HttpMethod.DELETE);
}
if (annotations[i].annotationType().getName().equals(PUT.class.getName())) {
feature.setMethod(HttpMethod.PUT);
} }
public String getPathAnnotationValue(Method currentMethod) throws Throwable{
String uri = "";
try { try {
Class<FormParam> formParamClazz = (Class<FormParam>) classLoader.loadClass( Class<Path> pathClazz = (Class<Path>) classLoader.loadClass(Path.class.getName());
FormParam.class.getName()); Annotation pathAnnno = currentMethod.getAnnotation(pathClazz);
Method[] formMethods = formParamClazz.getMethods(); Method[] pathMethods = pathClazz.getMethods();
//Extract method parameter information and store same as feature meta info Method valueMethod = pathMethods[0];
List<Feature.MetadataEntry> metaInfoList = new ArrayList<>(); uri = invokeMethod(valueMethod, pathAnnno, STRING);
Annotation[][] paramAnnotations = method.getParameterAnnotations();
for (int j = 0; j < paramAnnotations.length; j++) {
for (Annotation anno : paramAnnotations[j]) {
if (anno.annotationType().getName().equals(FormParam.class.getName())) {
Feature.MetadataEntry metadataEntry = new Feature.MetadataEntry();
metadataEntry.setId(j);
metadataEntry.setValue(invokeMethod(formMethods[0], anno, STRING));
metaInfoList.add(metadataEntry);
}
}
}
feature.setMetadataEntries(metaInfoList);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
log.debug("No Form Param found for class " + featureClazz.getName()); log.debug("No Path Param found for class " + featureAnnotationClazz.getName());
}
featureList.add(feature);
} }
} return uri;
}
}
return featureList;
} }
/** /**

Loading…
Cancel
Save