private void addMethodParameters(JaxrsResourceMethodDescription jaxrsRes, Method method) {
   for (Parameter param : method.getParameters()) {
     ParamInfo paramInfo = new ParamInfo();
     paramInfo.cls = param.getType();
     paramInfo.defaultValue = null;
     paramInfo.name = null;
     paramInfo.type = null;
     Annotation annotation;
     if ((annotation = param.getAnnotation(PathParam.class)) != null) {
       PathParam pathParam = (PathParam) annotation;
       paramInfo.name = pathParam.value();
       paramInfo.type = "@" + PathParam.class.getSimpleName();
     } else if ((annotation = param.getAnnotation(QueryParam.class)) != null) {
       QueryParam queryParam = (QueryParam) annotation;
       paramInfo.name = queryParam.value();
       paramInfo.type = "@" + QueryParam.class.getSimpleName();
     } else if ((annotation = param.getAnnotation(HeaderParam.class)) != null) {
       HeaderParam headerParam = (HeaderParam) annotation;
       paramInfo.name = headerParam.value();
       paramInfo.type = "@" + HeaderParam.class.getSimpleName();
     } else if ((annotation = param.getAnnotation(CookieParam.class)) != null) {
       CookieParam cookieParam = (CookieParam) annotation;
       paramInfo.name = cookieParam.value();
       paramInfo.type = "@" + CookieParam.class.getSimpleName();
     } else if ((annotation = param.getAnnotation(MatrixParam.class)) != null) {
       MatrixParam matrixParam = (MatrixParam) annotation;
       paramInfo.name = matrixParam.value();
       paramInfo.type = "@" + MatrixParam.class.getSimpleName();
     } else if ((annotation = param.getAnnotation(FormParam.class)) != null) {
       FormParam formParam = (FormParam) annotation;
       paramInfo.name = formParam.value();
       paramInfo.type = "@" + FormParam.class.getSimpleName();
     }
     if (paramInfo.name == null) {
       paramInfo.name = param.getName();
     }
     if ((annotation = param.getAnnotation(DefaultValue.class)) != null) {
       DefaultValue defaultValue = (DefaultValue) annotation;
       paramInfo.defaultValue = defaultValue.value();
     }
     jaxrsRes.parameters.add(paramInfo);
   }
 }
Пример #2
0
 private static String getValue(DefaultValue ann) {
   return (ann != null) ? ann.value() : null;
 }
Пример #3
0
    public T fromAnnotations() {
      Annotation[] annotations = parameter.getAnnotations();
      AccessibleObject injectTarget = parameter.getAccessibleObject();
      Class<?> type = parameter.getResourceClass().getClazz();

      parameter.encoded =
          findAnnotation(annotations, Encoded.class) != null
              || injectTarget.isAnnotationPresent(Encoded.class)
              || type.isAnnotationPresent(Encoded.class);
      DefaultValue defaultValue = findAnnotation(annotations, DefaultValue.class);
      if (defaultValue != null) parameter.defaultValue = defaultValue.value();

      QueryParam query;
      HeaderParam header;
      MatrixParam matrix;
      PathParam uriParam;
      CookieParam cookie;
      FormParam formParam;
      Form form;
      Suspend suspend;
      Suspended suspended;

      if ((query = findAnnotation(annotations, QueryParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.QUERY_PARAM;
        parameter.paramName = query.value();
      } else if ((header = findAnnotation(annotations, HeaderParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.HEADER_PARAM;
        parameter.paramName = header.value();
      } else if ((formParam = findAnnotation(annotations, FormParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.FORM_PARAM;
        parameter.paramName = formParam.value();
      } else if ((cookie = findAnnotation(annotations, CookieParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.COOKIE_PARAM;
        parameter.paramName = cookie.value();
      } else if ((uriParam = findAnnotation(annotations, PathParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.PATH_PARAM;
        parameter.paramName = uriParam.value();
      } else if ((form = findAnnotation(annotations, Form.class)) != null) {
        parameter.paramType = Parameter.ParamType.FORM;
        parameter.paramName = form.prefix();
      } else if (findAnnotation(annotations, BeanParam.class) != null) {
        parameter.paramType = Parameter.ParamType.BEAN_PARAM;
      } else if ((matrix = findAnnotation(annotations, MatrixParam.class)) != null) {
        parameter.paramType = Parameter.ParamType.MATRIX_PARAM;
        parameter.paramName = matrix.value();
      } else if ((suspend = findAnnotation(annotations, Suspend.class)) != null) {
        parameter.paramType = Parameter.ParamType.SUSPEND;
        parameter.suspendTimeout = suspend.value();
      } else if (findAnnotation(annotations, Context.class) != null) {
        parameter.paramType = Parameter.ParamType.CONTEXT;
      } else if ((suspended = findAnnotation(annotations, Suspended.class)) != null) {
        parameter.paramType = Parameter.ParamType.SUSPENDED;
      } else if (javax.ws.rs.container.AsyncResponse.class.isAssignableFrom(type)) {
        parameter.paramType = Parameter.ParamType.SUSPENDED;
      } else if (findAnnotation(annotations, Body.class) != null) {
        parameter.paramType = Parameter.ParamType.MESSAGE_BODY;
      } else {
        parameter.paramType = Parameter.ParamType.UNKNOWN;
      }
      return (T) this;
    }
  private Object[] assemParams(Class<?>[] paramTypes, Annotation[][] paramAnns) throws Exception {
    Object[] params = new Object[paramTypes.length];
    int pathParamIndex = 0;
    for (int i = 0; i < paramTypes.length; ++i) {
      Annotation[] anns = paramAnns[i];
      Class<?> paramClass = paramTypes[i];
      String[] paramValue = null;
      // ------------------------------------------------------
      // 通过给定class 获取对应的ActionContextObj
      if (Context.class.isAssignableFrom(paramClass)) {
        params[i] = this.context;
        continue;
      }

      if (HttpServletRequest.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getRequest();
        continue;
      }

      if (HttpServletResponse.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getResponse();
        continue;
      }

      if (PrintWriter.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getWriter();
        continue;
      }

      if (ServletOutputStream.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getOut();
        continue;
      }

      if (HttpSession.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getSession();
        continue;
      }

      if (ActionProp.class.isAssignableFrom(paramClass)) {
        if (this.context.getActionProp() == null)
          this.context.setActionProp(new ActionProp(this.actionObject.getClass().getName()));

        params[i] = this.context.getActionProp();
        continue;
      }

      if (Validation.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getValidation();
        continue;
      }

      if (QueryParams.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getQueryParams();
        continue;
      }

      if (DAO.class.isAssignableFrom(paramClass)) {
        params[i] = new DAOImpl("");
        continue;
      }

      if (InsertDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getInsertDAO();
        continue;
      }

      if (DeleteDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getDeleteDAO();
        continue;
      }

      if (UpdateDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getUpdateDAO();
        continue;
      }

      if (SelectDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getSelectDAO();
        continue;
      }

      if (DivPageDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getDivPageDAO();
        continue;
      }

      if (SearchDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getSearchDAO();
        continue;
      }

      if (CascadeDAO.class.isAssignableFrom(paramClass)) {
        params[i] = DAOFactory.getCascadeDAO();
        continue;
      }

      PathParam pathParamAnn = this.getPathParamAnn(anns);
      if (pathParamAnn != null) {
        paramValue = this.getPathParamValue(pathParamAnn.value());
        params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
        continue;
      }

      QueryParam queryParamAnn = this.getQueryParamAnn(anns);

      // 视图模型
      if (queryParamAnn == null && Map.class.isAssignableFrom(paramClass)) {
        params[i] = this.context.getModel();
        continue;
      }

      if (queryParamAnn != null) {
        final String fieldName = queryParamAnn.value();
        if (File.class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          UploadFile uploadFile = list.get(0);
          File file = uploadFile.getTmpFile();
          params[i] = file;

          continue;
        }

        if (File[].class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;
          File[] files = new File[list.size()];
          for (int j = 0; j < files.length; j++) files[j] = list.get(j).getTmpFile();

          params[i] = files;
        }

        if (UploadFile.class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          UploadFile uploadFile = list.get(0);
          params[i] = uploadFile;

          continue;
        }

        if (UploadFile[].class.isAssignableFrom(paramClass)) {
          if (!this.context.getUploadMap().containsKey(fieldName)) continue;
          List<UploadFile> list = this.context.getUploadMap().get(fieldName);
          if (list == null || list.isEmpty()) continue;

          params[i] = list.toArray(new UploadFile[] {});
        }

        String defaultValue = null;
        DefaultValue defaultValueAnn = this.getDefaultValueAnn(anns);
        if (defaultValueAnn != null) defaultValue = defaultValueAnn.value();

        paramValue = this.getQueryParamValue(fieldName, defaultValue);

        if (java.util.Date.class.isAssignableFrom(paramClass)) {
          params[i] = this.getDateParam(anns, paramValue[0]);
          continue;
        }

        String startName = fieldName;
        if (ClassUtil.isPojo(paramClass)) {
          params[i] = this.injectParam2Pojo(paramClass, startName);
          continue;
        }

        if (Map.class.isAssignableFrom(paramClass)) {
          params[i] = this.injectParam2Map(startName);
          continue;
        }

        if (paramClass.isArray()) params[i] = ClassUtil.getParamVals(paramClass, paramValue);
        else params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
      } else {
        // 如果是基本数据类型,则按照排序进行注入
        String[] pathParams = this.context.getActionConfigBean().getPathParams();
        if (pathParams == null) {
          log.warn("QueryParam not found and PathParam not found too");
          continue;
        }

        paramValue = this.getPathParamValue(pathParams[pathParamIndex]);
        params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
        pathParamIndex++;
        continue;
      }
    }

    return params;
  }