Ejemplo n.º 1
0
 public Map<?, ?> resolve(Invocation inv, ParamMetaData metaData) {
   if (StringUtils.isNotEmpty(metaData.getParamName())) {
     Class<?>[] genericTypes = (Class[]) metaData.getUserObject(this);
     Class<?> keyType = genericTypes[0];
     Class<?> valueType = genericTypes[1];
     Map<?, ?> toConvert =
         WebUtils.getParametersStartingWith(
             inv.getRequest(), metaData.getParamName() + MAP_SEPARATOR);
     if (toConvert != null) {
       if (keyType != String.class || valueType != String.class) {
         Map<Object, Object> ret = new HashMap<Object, Object>();
         for (Map.Entry<?, ?> entry : toConvert.entrySet()) {
           Object key = entry.getKey();
           Object value = entry.getValue();
           TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
           if (keyType != String.class) {
             key = typeConverter.convertIfNecessary(key, keyType);
           }
           if (valueType != String.class) {
             value = typeConverter.convertIfNecessary(value, valueType);
           }
           ret.put(key, value);
         }
         return ret;
       }
       return toConvert;
     }
   }
   return new HashMap<Object, Object>(2);
 }
Ejemplo n.º 2
0
    public Object resolve(Invocation inv, ParamMetaData metaData) {
      String toConvert = null;
      //
      FlashParam flashParam = metaData.getAnnotation(FlashParam.class);
      if (flashParam != null) {
        toConvert = inv.getFlash().get(flashParam.value());
      }

      for (String paramName : metaData.getParamNames()) {
        if (paramName != null) {
          toConvert = inv.getRequest().getParameter(paramName);
          if (toConvert != null) {
            break;
          }
        }
      }
      if (toConvert == null) {
        DefValue defValudeAnnotation = metaData.getAnnotation(DefValue.class);
        if (defValudeAnnotation != null
            && !DefValue.NATIVE_DEFAULT.equals(defValudeAnnotation.value())) {
          toConvert = defValudeAnnotation.value();
        }
      }
      if (toConvert != null) {
        SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
        return typeConverter.convertIfNecessary(toConvert, metaData.getParamType());
      }
      if (metaData.getParamType().isPrimitive()) {
        // 对这最常用的类型做一下if-else判断,其他类型就简单使用converter来做吧
        if (metaData.getParamType() == int.class) {
          return Integer.valueOf(0);
        } else if (metaData.getParamType() == long.class) {
          return Long.valueOf(0);
        } else if (metaData.getParamType() == boolean.class) {
          return Boolean.FALSE;
        } else if (metaData.getParamType() == double.class) {
          return Double.valueOf(0);
        } else if (metaData.getParamType() == float.class) {
          return Float.valueOf(0);
        } else {
          SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
          return typeConverter.convertIfNecessary("0", metaData.getParamType());
        }
      }
      return null;
    }
Ejemplo n.º 3
0
 public boolean supports(ParamMetaData metaData) {
   if (ClassUtils.isPrimitiveOrWrapper(metaData.getParamType())) {
     return true;
   }
   SimpleTypeConverter simpleTypeConverter = SafedTypeConverterFactory.getCurrentConverter();
   return simpleTypeConverter.findCustomEditor(metaData.getParamType(), null) != null
       || simpleTypeConverter.getDefaultEditor(metaData.getParamType()) != null;
 }
Ejemplo n.º 4
0
 @SuppressWarnings("unchecked")
 private static Object resolveArray(
     Invocation inv, ParamMetaData metaData, Class<?> compnentType) {
   if (compnentType == MultipartFile.class) {
     String filterName = metaData.getParamName();
     if (filterName == null) {
       filterName = "";
     }
     if (inv.getRequest() instanceof MultipartRequest) {
       List<MultipartFile> files = new LinkedList<MultipartFile>();
       MultipartRequest multipartRequest = (MultipartRequest) inv.getRequest();
       Iterator<String> names = multipartRequest.getFileNames();
       while (names.hasNext()) {
         String name = names.next();
         if (name.startsWith(filterName)) {
           files.add(multipartRequest.getFile(name));
         }
       }
       return files.toArray(new MultipartFile[0]);
     } else {
       if (logger.isDebugEnabled()) {
         logger.debug(
             "cann't "
                 + "set MultipartFile param to method "
                 + ", the request is not a MultipartRequest");
       }
     }
   } else {
     Object toConvert = null;
     for (String paramName : metaData.getParamNames()) {
       if (paramName != null) {
         toConvert = inv.getRequest().getParameterValues(paramName);
         if (toConvert != null) {
           break;
         }
       }
     }
     if (toConvert != null) {
       if (((String[]) toConvert).length == 1) {
         toConvert = ((String[]) toConvert)[0].split(",");
       }
       Class<?> arrayType;
       if (metaData.getParamType().isArray()) {
         arrayType = metaData.getParamType();
       } else {
         arrayType = arrayTypeMap.get(compnentType);
         if (arrayType == null) {
           arrayType = Array.newInstance((Class<?>) compnentType, 0).getClass();
         }
       }
       TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
       Object array = typeConverter.convertIfNecessary(toConvert, arrayType);
       return array;
     }
   }
   return Array.newInstance((Class<?>) compnentType, 0);
 }
  public Object[] resolve(final Invocation inv, final ParameterBindingResult parameterBindingResult)
      throws Exception {
    Object[] parameters = new Object[paramMetaDatas.length];
    for (int i = 0; i < resolvers.length; i++) {
      if (resolvers[i] == null) {
        continue;
      }
      try {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Resolves parameter "
                  + paramMetaDatas[i].getParamType().getSimpleName()
                  + " using "
                  + resolvers[i].getClass().getName());
        }
        parameters[i] = resolvers[i].resolve(inv, paramMetaDatas[i]);
        // afterPropertiesSet
        if (parameters[i] instanceof InitializingBean) {
          ((InitializingBean) parameters[i]).afterPropertiesSet();
        }
        if (parameters[i] == null) {
          DefValue defValudeAnnotation = paramMetaDatas[i].getAnnotation(DefValue.class);
          if (defValudeAnnotation != null && paramMetaDatas[i].getParamType() == String.class) {
            parameters[i] = defValudeAnnotation.value();
          }
        }
      } catch (TypeMismatchException e) {
        // 出现这个错误肯定是解析一般参数失败导致的,而非bean里面的某个属性值的解析失败

        logger.debug("", e);

        // 对简单类型的参数,设置一个默认值给它以支持对该方法的继续调用
        if (paramMetaDatas[i].getParamType().isPrimitive()) {
          DefValue defValudeAnnotation = paramMetaDatas[i].getAnnotation(DefValue.class);
          if (defValudeAnnotation == null
              || DefValue.NATIVE_DEFAULT.equals(defValudeAnnotation.value())) {
            // 对这最常用的类型做一下if-else判断,其他类型就简单使用converter来做吧
            if (paramMetaDatas[i].getParamType() == int.class) {
              parameters[i] = Integer.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == long.class) {
              parameters[i] = Long.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == boolean.class) {
              parameters[i] = Boolean.FALSE;
            } else if (paramMetaDatas[i].getParamType() == double.class) {
              parameters[i] = Double.valueOf(0);
            } else if (paramMetaDatas[i].getParamType() == float.class) {
              parameters[i] = Float.valueOf(0);
            } else {
              TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
              parameters[i] =
                  typeConverter.convertIfNecessary("0", paramMetaDatas[i].getParamType());
            }
          } else {
            TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
            parameters[i] =
                typeConverter.convertIfNecessary(
                    defValudeAnnotation.value(), paramMetaDatas[i].getParamType());
          }
        }
        //
        String paramName = parameterNames[i];
        if (paramName == null) {
          for (String name : paramMetaDatas[i].getParamNames()) {
            if ((paramName = name) != null) {
              break;
            }
          }
        }
        Assert.isTrue(paramName != null);
        FieldError fieldError =
            new FieldError( //
                "method", // 该出错字段所在的对象的名字;对于这类异常我们统一规定名字为method
                paramName, // 出错的字段的名字;取其参数名
                inv.getParameter(paramName), // 被拒绝的值
                true, // whether this error represents a binding failure (like a type mismatch);
                // else, it is a validation failure
                new String[] {e.getErrorCode()}, // "typeMismatch"
                new String[] {
                  inv.getParameter(paramName)
                }, // the array of arguments to be used to resolve this message
                null // the default message to be used to resolve this message
                );
        parameterBindingResult.addError(fieldError);
      } catch (Exception e) {
        // 什么错误呢?比如很有可能是构造对象不能成功导致的错误,没有默认构造函数、构造函数执行失败等等
        logger.error("", e);
        throw e;
      }
    }
    return parameters;
  }