Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
This file is part of Graylog. Graylog is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Graylog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Graylog. If not, see <http://www.gnu.org/licenses/>.
 
This file is part of Graylog. Graylog is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Graylog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Graylog. If not, see <http://www.gnu.org/licenses/>.
 
 package org.graylog2.restroutes.internal;
 
 
 import javax.ws.rs.*;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;

Author(s):
Dennis Oelkers <dennis@torch.sh>
 
 public class ResourceRoutesParser {
 
     private final String sourcePackage;
 
     public ResourceRoutesParser(String sourcePackage) {
         this. = sourcePackage;
     }
 
     private Set<Class<?>> httpMethods() {
         Set<Class<?>> methods = Sets.newHashSet();
         methods.add(GET.class);
         methods.add(POST.class);
         methods.add(PUT.class);
         methods.add(DELETE.class);
 
         return methods;
     }
 
     private Class<?> httpMethodOfMethod(Method method) {
         for (Class methodAnn : httpMethods()) {
             if (method.getAnnotation(methodAnn) != null)
                 return methodAnn;
         }
 
         return null;
     }
 
 
     public List<RouteClassbuildClasses() {
         Reflections ref = new Reflections();
         Set<Class<?>> classes = ref.getTypesAnnotatedWith(Path.class);
 
         List<RouteClassrouteClassList = Lists.newArrayList();
 
         for (Class<?> klazz : classes) {
             RouteClass routeClass = buildRouteClass(klazz);
             if (routeClass == nullcontinue;
 
             routeClassList.add(routeClass);
         }
 
         return routeClassList;
     }
 
     private RouteClass buildRouteClass(Class<?> klazz) {
         Path pathAnn = klazz.getAnnotation(Path.class);
         if (pathAnn == null)
             return null;
 
         String path = pathAnn.value();
        RouteClass routeClass = new RouteClass(klazzpath);
        for (Method method : klazz.getMethods()) {
            Route route = buildRouteForMethod(klazzpathmethod);
            if (route == nullcontinue;
            routeClass.addRoute(route);
        }
        return routeClass;
    }
    private Route buildRouteForMethod(Class<?> klazzString pathPrefixMethod method) {
        Set<Annotationanns = Sets.newHashSet(method.getAnnotations());
        Class<?> httpMethod = httpMethodOfMethod(method);
        if (httpMethod == null)
            return null;
        Path ann = method.getAnnotation(Path.class);
        String absolutePath = getAbsolutePath(pathPrefixann);
        //System.out.println(httpMethod.getSimpleName() + "\t\t" + buildPath + ": " + klazz.getSimpleName() + "." + method.getName());
        Map<PathParamClass<?>> pathParamTypeMap = getPathParamMap(method);
        return new Route(httpMethod.getSimpleName(), absolutePathklazzmethodpathParamTypeMap);
    }
    private Map<PathParamClass<?>> getPathParamMap(Method method) {
        Map<PathParamClass<?>> pathParamTypeMap = Maps.newLinkedHashMap(); // Using a linked hash map due to its preservation insertion order
        int i = 0;
        for (Annotation[] annotations : method.getParameterAnnotations()) {
            for (Annotation annotation :annotations)
                if (annotation instanceof PathParam) {
                    final Class<?> type = method.getParameterTypes()[i];
                    pathParamTypeMap.put((PathParamannotationtype);
                }
            i++;
        }
        return pathParamTypeMap;
    }
    private String getAbsolutePath(String pathPrefixPath ann) {
        StringBuilder buildPath = new StringBuilder(pathPrefix);
        if (ann != null) {
            String methodPath = ann.value();
            if (!pathPrefix.endsWith("/") && !methodPath.startsWith("/"))
                buildPath.append("/");
            buildPath.append(methodPath);
        }
        return buildPath.toString();
    }
New to GrepCode? Check out our FAQ X