Created
February 9, 2014 23:44
-
-
Save jeanouii/8907902 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Tomitribe Confidential | |
| * | |
| * Copyright Tomitribe Corporation. 2014 | |
| * | |
| * The source code for this program is not published or otherwise divested | |
| * of its trade secrets, irrespective of what has been deposited with the | |
| * U.S. Copyright Office. | |
| */ | |
| package com.tomitribe.wadler.twitter; | |
| import com.tomitribe.wadler.codegen.service.AccountResource; | |
| import com.tomitribe.wadler.codegen.service.ApplicationResource; | |
| import com.tomitribe.wadler.xml.Jaxb; | |
| import com.tomitribe.wadler.xml.StaxCompare; | |
| import com.tomitribe.wadler.xml.fluent.Application; | |
| import com.tomitribe.wadler.xml.fluent.Resource; | |
| import com.tomitribe.wadler.xml.fluent.Resources; | |
| import org.apache.openejb.loader.IO; | |
| import org.apache.xbean.finder.AnnotationFinder; | |
| import org.apache.xbean.finder.archive.ClassesArchive; | |
| import javax.ws.rs.*; | |
| import java.io.File; | |
| import java.lang.annotation.Annotation; | |
| import java.lang.reflect.Method; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| public class GenerateWadl { | |
| private static List<Class<? extends Annotation>> httpMethods = Arrays.asList(GET.class, POST.class, DELETE.class, POST.class, DELETE.class); | |
| public static void main(String[] args) throws Exception { | |
| final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(AccountResource.class, ApplicationResource.class)); | |
| List<Class<?>> classes = finder.findAnnotatedClasses(Path.class); | |
| // we should find also the applications, to create REST Application and all resources | |
| // let's assume we have only one application and one resource | |
| Application application = new Application(); | |
| Resources resources = new Resources(); | |
| for (Class<?> aClass : classes) { | |
| addResourceForClass(resources, aClass); | |
| } | |
| application.withResources(resources); | |
| String sourceApplication = IO.readString(classLoader().getResource("twitter/wadl/1.1/_docs_api_1_1_get_account_settings.xml")); | |
| String targetApplication = Jaxb.toString(Application.class, application); | |
| System.out.println(targetApplication); | |
| StaxCompare.compare(sourceApplication, targetApplication); | |
| } | |
| private static void addResourceForClass(final Resources resources, final Class<?> aClass) { | |
| Resource resource = new Resource(); | |
| resources.withResource(resource); | |
| Path path = aClass.getAnnotation(Path.class); | |
| resource.withPath(path.value()); | |
| // maybe we should find any HTTP methods, but let's just iterate over all declared methods | |
| for (Method method : aClass.getMethods()) { | |
| Resource methodResource = new Resource(); | |
| // Todo Doc | |
| Path methodPath = method.getAnnotation(Path.class); | |
| if (methodPath != null) methodResource.withPath(methodPath.value()); | |
| com.tomitribe.wadler.xml.fluent.Method methodMethod = new com.tomitribe.wadler.xml.fluent.Method(); | |
| methodResource.withMethodOrResource(methodMethod); | |
| // Todo Doc | |
| for (Annotation annotation : method.getAnnotations()) { | |
| if (httpMethods.contains(annotation.annotationType())) { | |
| methodMethod.setName(annotation.annotationType().getAnnotation(HttpMethod.class).value()); | |
| // at least a HTTP method so we can add the resource | |
| resource.withMethodOrResource(methodResource); | |
| } | |
| // Todo Doc | |
| } | |
| } | |
| } | |
| private static ClassLoader classLoader() { | |
| ClassLoader loader = Thread.currentThread().getContextClassLoader(); | |
| if (loader == null) { | |
| loader = GenerateWadl.class.getClassLoader(); | |
| } | |
| return loader; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment