Example #1
0
  /**
   * Return the param value as "cls" class object. If the value is null (or empty string) return the
   * defaultValue.<br>
   * Note: For the first call, this method will parse the request (in case of a multipart).<br>
   * <div class="issues"> <strong>Issues</strong>
   *
   * <ul>
   *   <li>FIXME: Need to fix the multipart handling. Can be simplified
   * </ul>
   *
   * </div>
   *
   * @param <T> Class of the return element
   * @param name of the parameter
   * @param cls Class of the return element
   * @param defaultValue Default value in case of an error or null/empty value
   * @return
   */
  @SuppressWarnings("unchecked")
  public <T> T getParam(String name, Class<T> cls, T defaultValue) {
    Map<String, Object> paramMap = getParamMap();
    if (paramMap == null) {
      return defaultValue;
    }
    // if we have a primitive type or array, then, just get the single value and convert it to the
    // appropriate type
    if (ObjectUtil.isPrimitive(cls) || cls.isArray() || cls == FileItem.class || cls.isEnum()) {
      // first, try to get it from the paramMap
      Object valueObject = paramMap.get(name);

      if (isMultipart) {
        // HACK
        // if not found, try to get it from the regular HttpServletRequest
        // (in the case of a multiPart post,
        // HttpServletRequest.getParameter still have the URL params)
        if (valueObject == null) {
          valueObject = getReq().getParameter(name);
        }
      }

      if (valueObject == null) {
        return defaultValue;
      } else if (valueObject instanceof String) {
        return (T) ObjectUtil.getValue((String) valueObject, cls, defaultValue);
      } else if (valueObject instanceof String[]) {
        return (T) ObjectUtil.getValue((String[]) valueObject, cls, defaultValue);
      } else {
        // hope for the best (should be a fileItem)
        return (T) valueObject;
      }
    }
    // otherwise, if it is not a primitive type, attempt to create the targeted object with the
    // corresponding paramMap
    else {
      Map subParamMap = getParamMap(name + "."); // i.e., "product."
      if (subParamMap != null) {
        try {
          T value = cls.newInstance();
          ObjectUtil.populate(value, subParamMap);
          return value;
        } catch (Exception e) {
          logger.warn(e.getMessage());
          return defaultValue;
        }
      } else {
        return defaultValue;
      }
    }
  }
Example #2
0
 /**
  * @param <T>
  * @param prefix
  * @param cls
  * @return The list of values given a param prefix
  */
 public <T> List<T> getParamMapValues(String prefix, Class<T> cls) {
   List<T> list = new ArrayList<T>();
   Map<String, Object> prefixParamMap = getParamMap(prefix);
   if (prefixParamMap != null) {
     for (Object valueObj : prefixParamMap.values()) {
       if (valueObj instanceof String) {
         T value = ObjectUtil.getValue((String) valueObj, cls, null);
         if (value != null) {
           list.add(value);
         }
       }
     }
   }
   return list;
 }
Example #3
0
 public <T> T getCurrentPriPathAt(int i, Class<T> cls, T defaultValue) {
   String valueStr = getCurrentPriPathAt(i);
   return ObjectUtil.getValue(valueStr, cls, defaultValue);
 }
Example #4
0
 public <T> T getCookie(String name, Class<T> cls, T defaultValue) {
   return ObjectUtil.getValue(getCookie(name), cls, defaultValue);
 }