private static String getTypeRequestMapping(Class<?> controllerType) {
   Assert.notNull(controllerType, "'controllerType' must not be null");
   RequestMapping annot = AnnotationUtils.findAnnotation(controllerType, RequestMapping.class);
   if (annot == null
       || ObjectUtils.isEmpty(annot.value())
       || StringUtils.isEmpty(annot.value()[0])) {
     return "/";
   }
   if (annot.value().length > 1 && logger.isWarnEnabled()) {
     logger.warn("Multiple paths on controller " + controllerType.getName() + ", using first one");
   }
   return annot.value()[0];
 }
 private static String getMethodRequestMapping(Method method) {
   RequestMapping annot = AnnotationUtils.findAnnotation(method, RequestMapping.class);
   if (annot == null) {
     throw new IllegalArgumentException("No @RequestMapping on: " + method.toGenericString());
   }
   if (ObjectUtils.isEmpty(annot.value()) || StringUtils.isEmpty(annot.value()[0])) {
     return "/";
   }
   if (annot.value().length > 1 && logger.isWarnEnabled()) {
     logger.warn("Multiple paths on method " + method.toGenericString() + ", using first one");
   }
   return annot.value()[0];
 }
 private String addMethodPathComponent(
     ExecutableElement executableElement, TypeElement cls, String path, RequestMapping anno) {
   if (anno == null) {
     throw new IllegalArgumentException(
         String.format(
             "Method %s should have Request mapping annotation",
             executableElement.getSimpleName()));
   }
   if (anno.value() != null && anno.value().length > 0) {
     return Utils.joinPaths(path, anno.value()[0]);
   }
   return path;
 }
Beispiel #4
0
  protected String defaultViewPrefix() {
    String currentViewPrefix = "";
    RequestMapping requestMapping =
        AnnotationUtils.findAnnotation(getClass(), RequestMapping.class);
    if (requestMapping != null && requestMapping.value().length > 0) {
      currentViewPrefix = requestMapping.value()[0];
    }

    if (StringUtils.isEmpty(currentViewPrefix)) {
      currentViewPrefix = this.entityClass.getSimpleName();
    }

    return currentViewPrefix;
  }
Beispiel #5
0
  private ApiInfo addApiInfo(HandlerMethod method) throws Exception {
    String key = getKey(method);
    ApiInfo apiInfo = new ApiInfo();

    if (method.getMethodAnnotation(Api.class) != null) {
      RequestMapping requestMapping = method.getMethodAnnotation(RequestMapping.class);
      String pattern = requestMapping.value()[0];

      if (pattern.contains(PathVariables.USER_KEY)) {
        apiInfo.setHasUserKeyInPath(true);
        pattern = pattern.replace(PathVariables.USER_KEY, WILD_CARD_CHAR);
      } else {
        apiInfo.setHasUserKeyInPath(false);
      }

      apiInfo.setLevel(method.getMethodAnnotation(Api.class).level());
      apiInfo.setRequestMappingPattern(pattern);
    } else {
      logger.debug("Invalid api identity!");
      throw new UnexpectedErrorException();
    }

    this.apiInfoCache.put(key, apiInfo);

    logger.debug("ApiInfoMapper.addApiInfo: key=" + key);
    logger.debug("ApiInfoMapper.addApiInfo: apiInfo=" + apiInfo.toString());
    return apiInfo;
  }
  private String getClassLevelUrlPath(TypeElement cls) {
    RestApiMountPoint mountPoint = cls.getAnnotation(RestApiMountPoint.class);
    String path = mountPoint == null ? "/" : mountPoint.value();

    RequestMapping clsAnno = cls.getAnnotation(RequestMapping.class);
    if (clsAnno == null || clsAnno.value().length == 0) {
      return path;
    } else if (clsAnno.value().length == 1) {
      return Utils.joinPaths(path, clsAnno.value()[0]);
    } else {
      throw new IllegalStateException(
          String.format(
              "The RequestMapping annotation of class %s has multiple value strings. Only zero or one value is supported",
              cls.getQualifiedName()));
    }
  }
  @Override
  protected RequestMappingInfo createRequestMappingInfo(
      RequestMapping annotation, RequestCondition<?> customCondition) {
    String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value());
    RequestMethod[] methods = annotation.method();

    // BPC的默认方法为POST
    if (methods == null || methods.length <= 0) {
      methods = new RequestMethod[] {RequestMethod.POST};
    }

    return new RequestMappingInfo(
        annotation.name(),
        new PatternsRequestCondition(
            patterns,
            getUrlPathHelper(),
            getPathMatcher(),
            this.useSuffixPatternMatch(),
            this.useTrailingSlashMatch(),
            this.getFileExtensions()),
        new RequestMethodsRequestCondition(methods),
        new ParamsRequestCondition(annotation.params()),
        new HeadersRequestCondition(annotation.headers()),
        new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
        new ProducesRequestCondition(
            annotation.produces(), annotation.headers(), this.getContentNegotiationManager()),
        customCondition);
  }
 @PostConstruct
 void init() {
   rootType =
       (Class<T>)
           ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
   kendoUiGridService.getSqlRoot().select().target("p").getRoot().from().target(rootType, "p");
   RequestMapping rm = AnnotationUtils.findAnnotation(this.getClass(), RequestMapping.class);
   String[] modulePaths = rm.value();
   moduleName = modulePaths[0].substring(1);
 }
 /**
  * Check for an @RequestMapping annotation on the class and, if present, store the RequestMethod
  * and the URL mapping
  */
 public void initialize() {
   Annotation ann = clazz.getAnnotation(RequestMapping.class);
   if (ann != null && (ann instanceof RequestMapping)) {
     RequestMapping rm = (RequestMapping) ann;
     RequestMethod[] reqMethod = rm.method();
     if (reqMethod.length > 0) {
       this.methodName = reqMethod[0].name();
     }
     String[] classValue = rm.value();
     if (classValue.length > 0) {
       this.path = classValue[0];
     }
   }
 }
  private Map<String, List<Method>> collectApisByRequestMapping(List<Method> methods) {
    Map<String, List<Method>> apiMethodMap = new HashMap<String, List<Method>>();
    for (Method method : methods) {
      if (method.isAnnotationPresent(RequestMapping.class)) {
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        String path = "";
        if (requestMapping.value() != null && requestMapping.value().length != 0) {
          path = generateFullPath(requestMapping.value()[0]);
        } else {
          path = resourcePath;
        }
        if (apiMethodMap.containsKey(path)) {
          apiMethodMap.get(path).add(method);
        } else {
          List<Method> ms = new ArrayList<Method>();
          ms.add(method);
          apiMethodMap.put(path, ms);
        }
      }
    }

    return apiMethodMap;
  }
 /** Created a RequestMappingInfo from a RequestMapping annotation. */
 private RequestMappingInfo createRequestMappingInfo(
     RequestMapping annotation, RequestCondition<?> customCondition) {
   return new RequestMappingInfo(
       new PatternsRequestCondition(
           annotation.value(),
           getUrlPathHelper(),
           getPathMatcher(),
           this.useSuffixPatternMatch,
           this.useTrailingSlashMatch),
       new RequestMethodsRequestCondition(annotation.method()),
       new ParamsRequestCondition(annotation.params()),
       new HeadersRequestCondition(annotation.headers()),
       new ConsumesRequestCondition(annotation.consumes(), annotation.headers()),
       new ProducesRequestCondition(annotation.produces(), annotation.headers()),
       customCondition);
 }
 private String getPath(RequestMapping classMapping) {
   if (classMapping != null && isNotEmpty(classMapping.value())) {
     return classMapping.value()[0];
   }
   return null;
 }
  // Helper method for loadDocuments()
  private Map<String, SpringResource> analyzeController(
      Class<?> clazz, Map<String, SpringResource> resourceMap, String description)
      throws ClassNotFoundException {
    String controllerCanonicalName = clazz.getCanonicalName();
    String[] controllerRequestMappingValues = null;

    // Determine if we will use class-level requestmapping or dummy string
    if (clazz.getAnnotation(RequestMapping.class) != null
        && clazz.getAnnotation(RequestMapping.class).value() != null) {
      controllerRequestMappingValues = clazz.getAnnotation(RequestMapping.class).value();
    } else {
      controllerRequestMappingValues = new String[1];
      controllerRequestMappingValues[0] = "";
    }

    // Iterate over all value attributes of the class-level RequestMapping annotation
    for (int i = 0; i < controllerRequestMappingValues.length; i++) {

      // Iterate over all methods inside the controller
      Method[] methods = clazz.getMethods();
      for (Method method : methods) {
        RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class);

        // Look for method-level @RequestMapping annotation
        if (methodRequestMapping instanceof RequestMapping) {
          RequestMethod[] requestMappingRequestMethods = methodRequestMapping.method();

          // For each method-level @RequestMapping annotation, iterate over HTTP Verb
          for (RequestMethod requestMappingRequestMethod : requestMappingRequestMethods) {
            String[] methodRequestMappingValues = methodRequestMapping.value();

            // Check for cases where method-level @RequestMapping#value is not set, and use the
            // controllers @RequestMapping
            if (methodRequestMappingValues == null || methodRequestMappingValues.length == 0) {
              // The map key is a concat of the following:
              //   1. The controller package
              //   2. The controller class name
              //   3. The controller-level @RequestMapping#value
              String resourceKey =
                  controllerCanonicalName
                      + controllerRequestMappingValues[i]
                      + requestMappingRequestMethod;
              if ((!(resourceMap.containsKey(resourceKey)))) {
                resourceMap.put(
                    resourceKey,
                    new SpringResource(
                        clazz, controllerRequestMappingValues[i], resourceKey, description));
              }
              resourceMap.get(resourceKey).addMethod(method);
            } else {
              // Here we know that method-level @RequestMapping#value is populated, so
              // iterate over all the @RequestMapping#value attributes, and add them to the resource
              // map.
              for (String methodRequestMappingValue : methodRequestMappingValues) {
                String resourceName = methodRequestMappingValue;
                // The map key is a concat of the following:
                //   1. The controller package
                //   2. The controller class name
                //   3. The controller-level @RequestMapping#value
                //   4. The method-level @RequestMapping#value
                //   5. The method-level @RequestMapping#method
                String resourceKey =
                    controllerCanonicalName
                        + controllerRequestMappingValues[i]
                        + resourceName
                        + requestMappingRequestMethod;
                if (!(resourceName.equals(""))) {
                  if ((!(resourceMap.containsKey(resourceKey)))) {
                    resourceMap.put(
                        resourceKey,
                        new SpringResource(clazz, resourceName, resourceKey, description));
                  }
                  resourceMap.get(resourceKey).addMethod(method);
                }
              }
            }
          }
        }
      }
    }
    clazz.getFields();
    clazz
        .getDeclaredFields(); // <--In case developer declares a field without an associated
                              // getter/setter.
    // this will allow NoClassDefFoundError to be caught before it triggers bamboo failure.

    return resourceMap;
  }
  private List<ResourceInfo> findMethods(Map<String, Object> handlerMap, Set<String> urls) {

    SortedSet<ResourceInfo> result = new TreeSet<ResourceInfo>();

    for (String key : urls) {

      Object handler = handlerMap.get(key);
      Class<?> handlerType = ClassUtils.getUserClass(handler);
      HandlerMethodResolver resolver = new HandlerMethodResolver();
      resolver.init(handlerType);

      String[] typeMappings = null;
      RequestMapping typeMapping =
          AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
      if (typeMapping != null) {
        typeMappings = typeMapping.value();
      }

      Set<Method> handlerMethods = resolver.getHandlerMethods();
      for (Method method : handlerMethods) {

        RequestMapping mapping = method.getAnnotation(RequestMapping.class);

        Collection<String> computedMappings = new HashSet<String>();
        if (typeMappings != null) {
          computedMappings.addAll(Arrays.asList(typeMappings));
        }

        for (String path : mapping.value()) {
          if (typeMappings != null) {
            for (String parent : computedMappings) {
              if (parent.endsWith("/")) {
                parent = parent.substring(0, parent.length() - 1);
              }
              computedMappings.add(parent + path);
            }
          } else {
            computedMappings.add(path);
          }
        }

        logger.debug(
            "Analysing mappings for method:"
                + method.getName()
                + ", key:"
                + key
                + ", computed mappings: "
                + computedMappings);
        if (computedMappings.contains(key)) {
          RequestMethod[] methods = mapping.method();
          if (methods != null && methods.length > 0) {
            for (RequestMethod requestMethod : methods) {
              logger.debug(
                  "Added explicit mapping for path=" + key + "to RequestMethod=" + requestMethod);
              result.add(new ResourceInfo(key, requestMethod));
            }
          } else {
            logger.debug("Added implicit mapping for path=" + key + "to RequestMethod=GET");
            result.add(new ResourceInfo(key, RequestMethod.GET));
          }
        }
      }

      if (handlerMethods.isEmpty()) {
        result.add(new ResourceInfo(key, RequestMethod.GET));
      }
    }

    return new ArrayList<ResourceInfo>(result);
  }