diff --git a/nb-configuration.xml b/nb-configuration.xml index 7bef924..14cc29e 100644 --- a/nb-configuration.xml +++ b/nb-configuration.xml @@ -1,19 +1,18 @@ - - + + - +--> + - JDK_1.5 - mit - - +--> + mit + + diff --git a/src/main/java/com/fingled/jersey/annotations/RestAnnotationProcessor.java b/src/main/java/com/fingled/jersey/annotations/RestAnnotationProcessor.java index c577a22..de10657 100644 --- a/src/main/java/com/fingled/jersey/annotations/RestAnnotationProcessor.java +++ b/src/main/java/com/fingled/jersey/annotations/RestAnnotationProcessor.java @@ -1,219 +1,243 @@ -/* - * The MIT License - * - * Copyright 2014 Robert Peralta. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.fingled.jersey.annotations; - -import com.sun.mirror.apt.AnnotationProcessor; -import com.sun.mirror.apt.AnnotationProcessorEnvironment; -import com.sun.mirror.declaration.*; - -import java.util.*; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Process annotations for {@link RestClass} y {@link RestMethod}. - * - * @author Robert Peralta - */ -public class RestAnnotationProcessor implements AnnotationProcessor -{ - - private final AnnotationProcessorEnvironment environment; - private final AnnotationTypeDeclaration methodDeclaration; - private final AnnotationTypeDeclaration classDeclaration; - - /** - * Map of classes to be generated. - */ - HashMap toGenerate; - - public RestAnnotationProcessor(AnnotationProcessorEnvironment env) - { - environment = env; - - methodDeclaration = (AnnotationTypeDeclaration) environment - .getTypeDeclaration(RestAnnotationProcessorFactory.RestMethodName); - classDeclaration = (AnnotationTypeDeclaration) environment - .getTypeDeclaration(RestAnnotationProcessorFactory.RestClassName); - - toGenerate = new HashMap(); - } - - public void process() - { - Collection classDeclarations = environment. - getDeclarationsAnnotatedWith(classDeclaration); - - Collection methodDeclarations = environment. - getDeclarationsAnnotatedWith(methodDeclaration); - - for (Declaration d : classDeclarations) - processClassDeclaration(d); - - for (Declaration d : methodDeclarations) - processMethodDeclaration(d); - - for (Map.Entry pair : toGenerate.entrySet()) - { - RestObject rest = pair.getValue(); - processRestObject(rest); - } - } - - /** - * Handles creation of each generated class - * - * @param rest Object containing necessary information to - * generate the class. - */ - private void processRestObject(RestObject rest) - { - - try - { - - String path = rest.filePath.substring(0, rest.filePath.lastIndexOf("\\") + 1); - String filename; - - if (rest.createPath == null) - { - filename = rest.filePath.substring(path.length(), rest.filePath.length()); - filename = filename.replace(".java", "Impl.java"); - - } - else - filename = rest.createPath.replaceAll("\"", ""); - - path += "generated\\"; - rest.createPath = path + filename; - System.out.println("FullPath: " + rest.createPath); - - RestWriter.writeClass(rest.createPath, rest, environment); - - } - catch (Exception ex) - { - System.err.println("Error: " + ex.getMessage()); - environment.getMessager().printError(ex.getMessage()); - Logger.getLogger(RestAnnotationProcessor.class.getSimpleName()).log(Level.SEVERE, ex.getMessage(), ex); - } - } - - private void processClassDeclaration(Declaration d) - { - addToRestObject(d, true); - } - - private void processMethodDeclaration(Declaration d) - { - addToRestObject(d, false); - } - - /** - * Adds a new entry to the RestObject map. If already exists, - * adds information of methods which are annotated by any - * RestMethod - * - * @param d - * @param isClass - */ - public void addToRestObject(Declaration d, boolean isClass) - { - Collection annotations = d.getAnnotationMirrors(); - - for (AnnotationMirror mirror : annotations) - { - String path = mirror.getPosition().file().getPath(); - - RestObject object = toGenerate.get(path); - - if (isClass) - addRestClassProperties(object, mirror); - else - addRestMethodProperties(object, mirror); - - } - } - - private void addRestMethodProperties(RestObject object, AnnotationMirror mirror) - { - if (object != null) - { - - Map values = mirror.getElementValues(); - - String path = null; - String[] queryParams = null; - - for (Map.Entry entry : values.entrySet()) - { - AnnotationTypeElementDeclaration key = entry.getKey(); - AnnotationValue value = entry.getValue(); - - if (key.getSimpleName().contains("path")) - path = (String) value.getValue(); - else if (key.getSimpleName().contains("queryParams")) - { - - List list = (List) value.getValue(); - - queryParams = new String[list.size()]; - Iterator it = list.iterator(); - for (int i = 0; it.hasNext(); i++) - queryParams[i] = ((AnnotationValue) it.next()).toString(); - } - - } - - if (path != null) - object.addMethod(path, queryParams); - } - } - - private void addRestClassProperties(RestObject object, AnnotationMirror mirror) - { - String path = mirror.getPosition().file().getPath(); - if (object == null || !toGenerate.containsKey(path)) - { - object = new RestObject(); - object.filePath = path; - toGenerate.put(path, object); - } - - Map values = mirror.getElementValues(); - - for (Map.Entry entry : values.entrySet()) - { - AnnotationTypeElementDeclaration key = entry.getKey(); - AnnotationValue value = entry.getValue(); - - if (key.getSimpleName().contains("compiledName")) - object.createPath = value.toString(); - else if (key.getSimpleName().contains("path")) - object.servicePath = value.toString(); - - } - } - -} +/* + * The MIT License + * + * Copyright 2014 Robert Peralta. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.fingled.jersey.annotations; + +import com.sun.mirror.apt.AnnotationProcessor; +import com.sun.mirror.apt.AnnotationProcessorEnvironment; +import com.sun.mirror.declaration.*; + +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Process annotations for {@link RestClass} y {@link RestMethod}. + * + * @author Robert Peralta + */ +public class RestAnnotationProcessor implements AnnotationProcessor +{ + + private final AnnotationProcessorEnvironment environment; + private final AnnotationTypeDeclaration methodDeclaration; + private final AnnotationTypeDeclaration classDeclaration; + + /** + * Map of classes to be generated. + */ + HashMap toGenerate; + + public RestAnnotationProcessor(AnnotationProcessorEnvironment env) + { + environment = env; + + methodDeclaration = (AnnotationTypeDeclaration) environment + .getTypeDeclaration(RestAnnotationProcessorFactory.RestMethodName); + classDeclaration = (AnnotationTypeDeclaration) environment + .getTypeDeclaration(RestAnnotationProcessorFactory.RestClassName); + + toGenerate = new HashMap(); + } + + public void process() + { + Collection classDeclarations = environment. + getDeclarationsAnnotatedWith(classDeclaration); + + Collection methodDeclarations = environment. + getDeclarationsAnnotatedWith(methodDeclaration); + + for (Declaration d : classDeclarations) + { + processClassDeclaration(d); + } + + for (Declaration d : methodDeclarations) + { + processMethodDeclaration(d); + } + + for (Map.Entry pair : toGenerate.entrySet()) + { + RestObject rest = pair.getValue(); + processRestObject(rest); + } + } + + /** + * Handles creation of each generated class + * + * @param rest Object containing necessary information to generate the + * class. + */ + private void processRestObject(RestObject rest) + { + + try + { + + String path = rest.filePath.substring(0, rest.filePath.lastIndexOf("\\") + 1); + String filename; + + if (rest.createPath == null) + { + filename = rest.filePath.substring(path.length(), rest.filePath.length()); + filename = filename.replace(".java", "Impl.java"); + + } + else + { + filename = rest.createPath.replaceAll("\"", ""); + } + + path += "generated\\"; + rest.createPath = path + filename; + System.out.println("FullPath: " + rest.createPath); + + RestWriter.writeClass(rest.createPath, rest, environment); + + } + catch (Exception ex) + { + System.err.println("Error: " + ex.getMessage()); + environment.getMessager().printError(ex.getMessage()); + Logger.getLogger(RestAnnotationProcessor.class.getSimpleName()).log(Level.SEVERE, ex.getMessage(), ex); + } + } + + private void processClassDeclaration(Declaration d) + { + addToRestObject(d, true); + } + + private void processMethodDeclaration(Declaration d) + { + addToRestObject(d, false); + } + + /** + * Adds a new entry to the RestObject map. If already exists, adds + * information of methods which are annotated by any + * RestMethod + * + * @param d + * @param isClass + */ + public void addToRestObject(Declaration d, boolean isClass) + { + Collection annotations = d.getAnnotationMirrors(); + + for (AnnotationMirror mirror : annotations) + { + String path = mirror.getPosition().file().getPath(); + + RestObject object = toGenerate.get(path); + + if (isClass) + { + addRestClassProperties(object, mirror); + } + else + { + addRestMethodProperties(object, mirror); + } + + } + } + + private void addRestMethodProperties(RestObject object, AnnotationMirror mirror) + { + if (object != null) + { + + Map values = mirror.getElementValues(); + + String path = null; + String[] queryParams = null; + + for (Map.Entry entry : values.entrySet()) + { + AnnotationTypeElementDeclaration key = entry.getKey(); + AnnotationValue value = entry.getValue(); + + if (key.getSimpleName().contains("path")) + { + path = (String) value.getValue(); + } + else if (key.getSimpleName().contains("queryParams")) + { + + List list = (List) value.getValue(); + + queryParams = new String[list.size()]; + Iterator it = list.iterator(); + for (int i = 0; it.hasNext(); i++) + { + queryParams[i] = ((AnnotationValue) it.next()).toString(); + } + } + + } + + if (path != null) + { + object.addMethod(path, queryParams); + } + } + } + + private void addRestClassProperties(RestObject object, AnnotationMirror mirror) + { + String path = mirror.getPosition().file().getPath(); + if (object == null || !toGenerate.containsKey(path)) + { + object = new RestObject(); + object.filePath = path; + toGenerate.put(path, object); + } + + Map values = mirror.getElementValues(); + + for (Map.Entry entry : values.entrySet()) + { + AnnotationTypeElementDeclaration key = entry.getKey(); + AnnotationValue value = entry.getValue(); + + if (key.getSimpleName().contains("compiledName")) + { + object.createPath = value.toString(); + } + else if (key.getSimpleName().contains("path")) + { + object.servicePath = value.toString(); + } + else if (key.getSimpleName().contains("packageName")) + { + object.packageName = value.toString(); + } + + } + } + +} diff --git a/src/main/java/com/fingled/jersey/annotations/RestClass.java b/src/main/java/com/fingled/jersey/annotations/RestClass.java index f7d2e7b..1d8a049 100644 --- a/src/main/java/com/fingled/jersey/annotations/RestClass.java +++ b/src/main/java/com/fingled/jersey/annotations/RestClass.java @@ -1,51 +1,55 @@ -/* - * The MIT License - * - * Copyright 2014 Robert Peralta. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.fingled.jersey.annotations; - -import java.lang.annotation.*; - -/** - * Classes annotated with this, will cause a new - * class(that implements a REST service using jersey) - * to be automatically generated. - * - * @author Robert Peralta - */ -@Target(ElementType.TYPE) -@Inherited -@Documented -@Retention(RetentionPolicy.RUNTIME) -public @interface RestClass -{ - - /** - * Jersey Service {@link javax.ws.rs.Path} to be accessed by.. - */ - String path(); - - /** - * .java File to be generated. - */ - String compiledName() default ""; -} +/* + * The MIT License + * + * Copyright 2014 Robert Peralta. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.fingled.jersey.annotations; + +import java.lang.annotation.*; + +/** + * Classes annotated with this, will cause a new class(that implements a REST + * service using jersey) to be automatically generated. + * + * @author Robert Peralta + */ +@Target(ElementType.TYPE) +@Inherited +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface RestClass +{ + + /** + * Jersey Service {@link javax.ws.rs.Path} to be accessed by.. + */ + String path(); + + /** + * Package name for generated files. + */ + String packageName() default ""; + + /** + * .java File to be generated. + */ + String compiledName() default ""; +} diff --git a/src/main/java/com/fingled/jersey/annotations/RestObject.java b/src/main/java/com/fingled/jersey/annotations/RestObject.java index a4598f1..cea210e 100644 --- a/src/main/java/com/fingled/jersey/annotations/RestObject.java +++ b/src/main/java/com/fingled/jersey/annotations/RestObject.java @@ -1,73 +1,76 @@ -/* - * The MIT License - * - * Copyright 2014 Robert Peralta. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.fingled.jersey.annotations; - -import java.util.ArrayList; -import java.util.List; - -/** - * To handle basic structure for generated classes. - * - * @author Robert Peralta - */ -class RestObject -{ - - class Method - { - - String path; - String[] queryParams; - } - - String filePath; - String createPath; - String servicePath; - List methods = new ArrayList(); - - public void addMethod(String path, String[] queryParams) - { - Method m = new Method(); - - m.path = path; - m.queryParams = queryParams; - - methods.add(m); - } - - public void print() - { - System.out.println("createPath: " + this.createPath); - System.out.println("servicePath: " + this.servicePath); - System.out.println("filePath: " + this.filePath); - - for (RestObject.Method m : this.methods) - { - System.out.println("methodPath: " + m.path); - - for (String s : m.queryParams) - System.out.println("\tmethodQueryParams: " + s); - } - } -} +/* + * The MIT License + * + * Copyright 2014 Robert Peralta. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.fingled.jersey.annotations; + +import java.util.ArrayList; +import java.util.List; + +/** + * To handle basic structure for generated classes. + * + * @author Robert Peralta + */ +class RestObject +{ + + class Method + { + + String path; + String[] queryParams; + } + + String filePath; + String createPath; + String servicePath; + String packageName; + List methods = new ArrayList(); + + public void addMethod(String path, String[] queryParams) + { + Method m = new Method(); + + m.path = path; + m.queryParams = queryParams; + + methods.add(m); + } + + public void print() + { + System.out.println("createPath: " + this.createPath); + System.out.println("servicePath: " + this.servicePath); + System.out.println("filePath: " + this.filePath); + + for (RestObject.Method m : this.methods) + { + System.out.println("methodPath: " + m.path); + + for (String s : m.queryParams) + { + System.out.println("\tmethodQueryParams: " + s); + } + } + } +} diff --git a/src/main/java/com/fingled/jersey/annotations/RestWriter.java b/src/main/java/com/fingled/jersey/annotations/RestWriter.java index 02edd61..a501973 100644 --- a/src/main/java/com/fingled/jersey/annotations/RestWriter.java +++ b/src/main/java/com/fingled/jersey/annotations/RestWriter.java @@ -1,210 +1,220 @@ -/* - * The MIT License - * - * Copyright 2014 Robert Peralta. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.fingled.jersey.annotations; - -import com.fingled.util.ClassUtil; -import com.sun.mirror.apt.AnnotationProcessorEnvironment; - -import java.io.*; -import java.lang.reflect.Method; - -/** - * Handles formatting and I/O for generated class containing - * resulting Jersey REST Service - * - * @author Robert Peralta - */ -class RestWriter -{ - - public static boolean writeClass(String filename, RestObject rest, AnnotationProcessorEnvironment environment) throws Exception - { - BufferedWriter writer; - if (!filename.endsWith(".java")) - { - String error = "[RestWriter.writeClass] RestClass Path must end with '.java' (" + filename + ") !!"; - System.err.println(error); - environment.getMessager().printError(error); - return false; - } - System.out.println("[RestWriter.writeClass] Generating Jersey Service file: " + filename); - - File file = new File(filename); - - File genFolder = file.getParentFile(); - deleteFolder(genFolder); - genFolder.mkdir(); - - if (file.exists()) - { - file.createNewFile(); - - } - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); - String className = classNameForFilePath(rest.filePath); - String classNameToCreate = classNameForFilePath(rest.createPath); - classNameToCreate - = classNameToCreate.substring(1 + classNameToCreate.lastIndexOf(".")); - Class c = Class.forName(className); - writeLine(RestStrings.LICENSE, writer); - writer.newLine(); - writeLine(c.getPackage().toString() + ";", writer); - writer.newLine(); - writeLine("import javax.ws.rs.*;", writer); - writeLine("import javax.ws.rs.core.MediaType;", writer); - writer.newLine(); - writeLine(RestStrings.GENERATED_COMMENT, writer); - writeLine("@Path(" + rest.servicePath + ")", writer); - writeLine("public class " + classNameToCreate + "\n{", writer); - writer.newLine(); - for (RestObject.Method m : rest.methods) - { - writeGetMethod(m, c, writer); - writePostMethod(m, c, writer); - } - newLine(3, writer); - writeLine("}", writer); - writer.close(); - return true; - } - - public static String classNameForFilePath(String filepath) - { - String className = filepath.substring(filepath.lastIndexOf("java\\") + 5); - className = className.replaceAll("[\\\\]", "."); - className = className.replace(".java", ""); - return className; - } - - private static void writeGetMethod(RestObject.Method m, Class c, BufferedWriter writer) throws IOException - { - writeMethod("Get", m, c, writer); - } - - private static void writePostMethod(RestObject.Method m, Class c, BufferedWriter writer) throws IOException - { - writeMethod("Post", m, c, writer); - } - - private static void writeMethod(String method, RestObject.Method m, Class c, BufferedWriter writer) throws IOException - { - boolean isGet = method.equalsIgnoreCase("Get"); - String operationName = m.path.substring(1); - String params = ""; - String invokeParams = ""; - String paramType = isGet ? "Query" : "Form"; - - Method classMethod = ClassUtil.getMethodAnnotatedWith( - c, RestMethod.class, "path", m.path); - - for (int i = 0; m.queryParams!=null && i < m.queryParams.length; i++) - { - String param = m.queryParams[i]; - String attr = param.replaceAll("\"", ""); - - String paramDecl = "@" + paramType + "Param(" + param + ") String " + attr; - - if (i + 1 < m.queryParams.length) - { - invokeParams += attr + ", "; - params += paramDecl + ", "; - } else - { - params += paramDecl; - invokeParams += attr; - } - } - - newLine(writer); - - writeLine("@" + method.toUpperCase(), writer, 1); - writeLine("@Path(\"" + m.path + "\")", writer, 1); - - if (!isGet) - { - writeLine("@Consumes(MediaType.WILDCARD)", writer, 1); - } - - writeLine("@Produces(MediaType.APPLICATION_XML)", writer, 1); - writeLine("public String " + operationName + method - + "(" + params + ")", writer, 1); - writeLine("{", writer, 1); - writeLine("return " - + c.getName() + "." + classMethod.getName() - + "(" + invokeParams + ");", writer, 2); - writeLine("}", writer, 1); - - newLine(writer); - } - - private static void writeLine(String str, BufferedWriter writer) throws IOException - { - writeLine(str, writer, 0); - } - - private static void writeLine(String str, BufferedWriter writer, int tabs) throws IOException - { - String tabstr = ""; - - for (int i = 0; i < tabs; i++) - { - tabstr += "\t"; - } - - writer.append(tabstr + str); - writer.newLine(); - } - - private static void newLine(BufferedWriter writer) throws IOException - { - newLine(1, writer); - } - - private static void newLine(int times, BufferedWriter writer) throws IOException - { - for (int i = 0; i < times; i++) - { - writer.newLine(); - } - } - - private static void deleteFolder(File folder) - { - File[] files = folder.listFiles(); - if (files != null) - { //some JVMs return null for empty dirs - for (File f : files) - { - if (f.isDirectory()) - { - deleteFolder(f); - } else - { - f.delete(); - } - } - } - folder.delete(); - } -} +/* + * The MIT License + * + * Copyright 2014 Robert Peralta. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.fingled.jersey.annotations; + +import com.fingled.util.ClassUtil; +import com.sun.mirror.apt.AnnotationProcessorEnvironment; + +import java.io.*; +import java.lang.reflect.Method; + +/** + * Handles formatting and I/O for generated class containing resulting Jersey + * REST Service + * + * @author Robert Peralta + */ +class RestWriter +{ + + public static boolean writeClass(String filename, RestObject rest, AnnotationProcessorEnvironment environment) throws Exception + { + BufferedWriter writer; + if (!filename.endsWith(".java")) + { + String error = "[RestWriter.writeClass] RestClass Path must end with '.java' (" + filename + ") !!"; + System.err.println(error); + environment.getMessager().printError(error); + return false; + } + System.out.println("[RestWriter.writeClass] Generating Jersey Service file: " + filename); + + File file = new File(filename); + + File genFolder = file.getParentFile(); + deleteFolder(genFolder); + genFolder.mkdir(); + + if (file.exists()) + { + file.createNewFile(); + + } + writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + String className = classNameForFilePath(rest.filePath); + String classNameToCreate = classNameForFilePath(rest.createPath); + classNameToCreate = classNameToCreate + .substring(1 + classNameToCreate.lastIndexOf(".")); + Class c = Class.forName(className); + String pkgName = rest.packageName != null && rest.packageName.length() > 0 + ? "package " + rest.packageName + : c.getPackage().toString(); + pkgName += ";"; + pkgName = pkgName.replaceAll("\"", ""); + + writeLine(RestStrings.LICENSE, writer); + writer.newLine(); + + writeLine(pkgName, writer); + + writer.newLine(); + writeLine("import javax.ws.rs.*;", writer); + writeLine("import javax.ws.rs.core.MediaType;", writer); + writer.newLine(); + writeLine(RestStrings.GENERATED_COMMENT, writer); + writeLine("@Path(" + rest.servicePath + ")", writer); + writeLine("public class " + classNameToCreate + "\n{", writer); + writer.newLine(); + for (RestObject.Method m : rest.methods) + { + writeGetMethod(m, c, writer); + writePostMethod(m, c, writer); + } + newLine(3, writer); + writeLine("}", writer); + writer.close(); + return true; + } + + public static String classNameForFilePath(String filepath) + { + String className = filepath.substring(filepath.lastIndexOf("java\\") + 5); + className = className.replaceAll("[\\\\]", "."); + className = className.replace(".java", ""); + return className; + } + + private static void writeGetMethod(RestObject.Method m, Class c, BufferedWriter writer) throws IOException + { + writeMethod("Get", m, c, writer); + } + + private static void writePostMethod(RestObject.Method m, Class c, BufferedWriter writer) throws IOException + { + writeMethod("Post", m, c, writer); + } + + private static void writeMethod(String method, RestObject.Method m, Class c, BufferedWriter writer) throws IOException + { + boolean isGet = method.equalsIgnoreCase("Get"); + String operationName = m.path.substring(1); + String params = ""; + String invokeParams = ""; + String paramType = isGet ? "Query" : "Form"; + + Method classMethod = ClassUtil.getMethodAnnotatedWith( + c, RestMethod.class, "path", m.path); + + for (int i = 0; m.queryParams != null && i < m.queryParams.length; i++) + { + String param = m.queryParams[i]; + String attr = param.replaceAll("\"", ""); + + String paramDecl = "@" + paramType + "Param(" + param + ") String " + attr; + + if (i + 1 < m.queryParams.length) + { + invokeParams += attr + ", "; + params += paramDecl + ", "; + } + else + { + params += paramDecl; + invokeParams += attr; + } + } + + newLine(writer); + + writeLine("@" + method.toUpperCase(), writer, 1); + writeLine("@Path(\"" + m.path + "\")", writer, 1); + + if (!isGet) + { + writeLine("@Consumes(MediaType.WILDCARD)", writer, 1); + } + + writeLine("@Produces(MediaType.APPLICATION_XML)", writer, 1); + writeLine("public String " + operationName + method + + "(" + params + ")", writer, 1); + writeLine("{", writer, 1); + writeLine("return " + + c.getName() + "." + classMethod.getName() + + "(" + invokeParams + ");", writer, 2); + writeLine("}", writer, 1); + + newLine(writer); + } + + private static void writeLine(String str, BufferedWriter writer) throws IOException + { + writeLine(str, writer, 0); + } + + private static void writeLine(String str, BufferedWriter writer, int tabs) throws IOException + { + String tabstr = ""; + + for (int i = 0; i < tabs; i++) + { + tabstr += "\t"; + } + + writer.append(tabstr + str); + writer.newLine(); + } + + private static void newLine(BufferedWriter writer) throws IOException + { + newLine(1, writer); + } + + private static void newLine(int times, BufferedWriter writer) throws IOException + { + for (int i = 0; i < times; i++) + { + writer.newLine(); + } + } + + private static void deleteFolder(File folder) + { + File[] files = folder.listFiles(); + if (files != null) + { //some JVMs return null for empty dirs + for (File f : files) + { + if (f.isDirectory()) + { + deleteFolder(f); + } + else + { + f.delete(); + } + } + } + folder.delete(); + } +} diff --git a/src/main/java/com/fingled/jersey/services/TestService.java b/src/main/java/com/fingled/jersey/services/TestService.java index 14227d0..8b79082 100644 --- a/src/main/java/com/fingled/jersey/services/TestService.java +++ b/src/main/java/com/fingled/jersey/services/TestService.java @@ -1,67 +1,67 @@ -/* - * The MIT License - * - * Copyright 2014 Robert Peralta. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.fingled.jersey.services; - -import com.fingled.jersey.annotations.RestClass; -import com.fingled.jersey.annotations.RestMethod; - -/** - * A simple test service class using {@link RestClass} and {@link RestMethod} - * Annotations - * - * @author Robert Peralta - */ -@RestClass(path = "/wsdl") -public class TestService -{ - - // - @RestMethod(path = "/helloGest", queryParams - = - { - "name" - }) - public static String helloGest(String name) - { - return "Hello " + name + ""; - } - - @RestMethod(path = "/welcomeGest") - public static String welcomeGest() - { - return "Welcome Gest"; - } - - @RestMethod(path = "/byeGest", queryParams - = - { - "msg", "name" - }) - public static String byeGest(String msg, String name) - { - return "" + msg + " " + name + ""; - } - // - -} +/* + * The MIT License + * + * Copyright 2014 Robert Peralta. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.fingled.jersey.services; + +import com.fingled.jersey.annotations.RestClass; +import com.fingled.jersey.annotations.RestMethod; + +/** + * A simple test service class using {@link RestClass} and {@link RestMethod} + * Annotations + * + * @author Robert Peralta + */ +@RestClass(path = "/wsdl", packageName = "my.services") +public class TestService +{ + + // + @RestMethod(path = "/helloGest", queryParams + = + { + "name" + }) + public static String helloGest(String name) + { + return "Hello " + name + ""; + } + + @RestMethod(path = "/welcomeGest") + public static String welcomeGest() + { + return "Welcome Gest"; + } + + @RestMethod(path = "/byeGest", queryParams + = + { + "msg", "name" + }) + public static String byeGest(String msg, String name) + { + return "" + msg + " " + name + ""; + } + // + +} diff --git a/src/main/java/com/fingled/jersey/services/generated/TestServiceImpl.java b/src/main/java/com/fingled/jersey/services/generated/TestServiceImpl.java index 1ab41b2..ab1e1dc 100644 --- a/src/main/java/com/fingled/jersey/services/generated/TestServiceImpl.java +++ b/src/main/java/com/fingled/jersey/services/generated/TestServiceImpl.java @@ -20,69 +20,79 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - */ -package com.fingled.jersey.services; - -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; - + */ + +package my.services; + +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; + /** - * This class has been generated by RestAnnotationProcessor by Robert Peralta + * This class has been generated by RestAnnotationProcessor by Robert Peralta * to create a basic Jersey service for POST/GET operations. - */ -@Path("/wsdl") + */ +@Path("/wsdl") public class TestServiceImpl -{ - - @GET - @Path("/helloGest") - @Produces(MediaType.APPLICATION_XML) - public String helloGestGet(@QueryParam("name") String name) - { - return com.fingled.jersey.services.TestService.helloGest(name); - } - - @POST - @Path("/helloGest") - @Consumes(MediaType.WILDCARD) - @Produces(MediaType.APPLICATION_XML) - public String helloGestPost(@FormParam("name") String name) - { - return com.fingled.jersey.services.TestService.helloGest(name); - } - - @GET - @Path("/welcomeGest") - @Produces(MediaType.APPLICATION_XML) - public String welcomeGestGet() - { - return com.fingled.jersey.services.TestService.welcomeGest(); - } - - @POST - @Path("/welcomeGest") - @Consumes(MediaType.WILDCARD) - @Produces(MediaType.APPLICATION_XML) - public String welcomeGestPost() - { - return com.fingled.jersey.services.TestService.welcomeGest(); - } - - @GET - @Path("/byeGest") - @Produces(MediaType.APPLICATION_XML) - public String byeGestGet(@QueryParam("msg") String msg, @QueryParam("name") String name) - { - return com.fingled.jersey.services.TestService.byeGest(msg, name); - } - - @POST - @Path("/byeGest") - @Consumes(MediaType.WILDCARD) - @Produces(MediaType.APPLICATION_XML) - public String byeGestPost(@FormParam("msg") String msg, @FormParam("name") String name) - { - return com.fingled.jersey.services.TestService.byeGest(msg, name); - } - -} +{ + + + @GET + @Path("/helloGest") + @Produces(MediaType.APPLICATION_XML) + public String helloGestGet(@QueryParam("name") String name) + { + return com.fingled.jersey.services.TestService.helloGest(name); + } + + + @POST + @Path("/helloGest") + @Consumes(MediaType.WILDCARD) + @Produces(MediaType.APPLICATION_XML) + public String helloGestPost(@FormParam("name") String name) + { + return com.fingled.jersey.services.TestService.helloGest(name); + } + + + @GET + @Path("/byeGest") + @Produces(MediaType.APPLICATION_XML) + public String byeGestGet(@QueryParam("msg") String msg, @QueryParam("name") String name) + { + return com.fingled.jersey.services.TestService.byeGest(msg, name); + } + + + @POST + @Path("/byeGest") + @Consumes(MediaType.WILDCARD) + @Produces(MediaType.APPLICATION_XML) + public String byeGestPost(@FormParam("msg") String msg, @FormParam("name") String name) + { + return com.fingled.jersey.services.TestService.byeGest(msg, name); + } + + + @GET + @Path("/welcomeGest") + @Produces(MediaType.APPLICATION_XML) + public String welcomeGestGet() + { + return com.fingled.jersey.services.TestService.welcomeGest(); + } + + + @POST + @Path("/welcomeGest") + @Consumes(MediaType.WILDCARD) + @Produces(MediaType.APPLICATION_XML) + public String welcomeGestPost() + { + return com.fingled.jersey.services.TestService.welcomeGest(); + } + + + + +}