Search in sources :

Example 6 with FormParam

use of org.axe.bean.mvc.FormParam in project Axe by DongyuCai.

the class RequestUtil method getParamList.

/**
     * List<?>、List<Object>、List<String>
     * 三个参数形式,都是一样的取请求参数的逻辑
     */
public static List<?> getParamList(String fieldName, Param param) {
    List<?> parameterValue = null;
    Map<String, List<FormParam>> fieldMap = param.getFieldMap();
    Map<String, List<FileParam>> fileMap = param.getFileMap();
    if (fieldMap.containsKey(fieldName)) {
        List<FormParam> formParamList = fieldMap.get(fieldName);
        List<Object> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(formParamList)) {
            for (FormParam formParam : formParamList) {
                list.add(formParam.getFieldValue());
            }
        }
        parameterValue = list.size() > 0 ? list : null;
    } else if (fileMap.containsKey(fieldName)) {
        parameterValue = fileMap.get(fieldName);
    }
    return parameterValue;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FormParam(org.axe.bean.mvc.FormParam)

Example 7 with FormParam

use of org.axe.bean.mvc.FormParam in project Axe by DongyuCai.

the class RequestUtil method getRequestParam.

public static Object getRequestParam(Param param, String fieldName, Type parameterType) {
    Object parameterValue = null;
    try {
        if (parameterType instanceof ParameterizedType) {
            //* 泛型,只支持List这样,但是不支持元素还是泛型了,比如List<Map<String,Object>这样就不支持
            Type[] actualTypes = ((ParameterizedType) parameterType).getActualTypeArguments();
            if (ReflectionUtil.compareType(List.class, (Class<?>) ((ParameterizedType) parameterType).getRawType())) {
                if (actualTypes.length > 0) {
                    Type listParamType = actualTypes[0];
                    if (listParamType instanceof WildcardType) {
                        //List<?>
                        parameterValue = RequestUtil.getParamList(fieldName, param);
                    } else {
                        List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                        if (formParamList == null) {
                            formParamList = new ArrayList<>();
                        }
                        Class<?> listParamClass = (Class<?>) listParamType;
                        if (ReflectionUtil.compareType(Object.class, listParamClass)) {
                            //List<Object>
                            parameterValue = RequestUtil.getParamList(fieldName, param);
                        } else if (ReflectionUtil.compareType(String.class, listParamClass)) {
                            //List<String>
                            parameterValue = RequestUtil.getFormParamList(fieldName, param.getFieldMap());
                        } else if (ReflectionUtil.compareType(FileParam.class, listParamClass)) {
                            //List<FileParam>
                            parameterValue = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                        } else if (ReflectionUtil.compareType(Byte.class, listParamClass)) {
                            //List<Byte>
                            List<Byte> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Boolean.class, listParamClass)) {
                            //List<Boolean>
                            List<Boolean> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Short.class, listParamClass)) {
                            //List<Short>
                            List<Short> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Character.class, listParamClass)) {
                            //List<Character>
                            List<Character> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Integer.class, listParamClass)) {
                            //List<Integer>
                            List<Integer> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Long.class, listParamClass)) {
                            //List<Long>
                            List<Long> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Float.class, listParamClass)) {
                            //List<Float>
                            List<Float> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Double.class, listParamClass)) {
                            //List<double>
                            List<Double> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        }
                    }
                }
            }
        } else if (parameterType instanceof Class) {
            Class<?> parameterClass = (Class<?>) parameterType;
            if (parameterClass.isArray()) {
                List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                if (formParamList == null) {
                    formParamList = new ArrayList<>();
                }
                if (ReflectionUtil.compareType(parameterClass.getComponentType(), Object.class)) {
                    //Object
                    List<?> paramList = RequestUtil.getParamList(fieldName, param);
                    parameterValue = CollectionUtil.isNotEmpty(paramList) ? paramList.toArray() : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), String.class)) {
                    //String[]
                    List<String> paramList = RequestUtil.getFormParamList(fieldName, param.getFieldMap());
                    if (CollectionUtil.isNotEmpty(paramList)) {
                        String[] paramAry = new String[paramList.size()];
                        parameterValue = paramList.toArray(paramAry);
                    }
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), FileParam.class)) {
                    //FileParam[]
                    List<FileParam> paramList = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                    if (CollectionUtil.isNotEmpty(paramList)) {
                        FileParam[] paramAry = new FileParam[paramList.size()];
                        parameterValue = paramList.toArray(paramAry);
                    }
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Byte.class)) {
                    //Byte[]
                    Byte[] list = new Byte[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Boolean.class)) {
                    //Boolean[]
                    Boolean[] list = new Boolean[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Short.class)) {
                    //Short[]
                    Short[] list = new Short[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Character.class)) {
                    //Character[]
                    Character[] list = new Character[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Integer.class)) {
                    //Integer[]
                    Integer[] list = new Integer[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Long.class)) {
                    //Long[]
                    Long[] list = new Long[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Float.class)) {
                    //Float[]
                    Float[] list = new Float[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Double.class)) {
                    //Double[]
                    Double[] list = new Double[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                }
            } else {
                List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                if (formParamList == null) {
                    formParamList = new ArrayList<>();
                }
                if (ReflectionUtil.compareType(List.class, parameterClass)) {
                    //List
                    parameterValue = RequestUtil.getParamList(fieldName, param);
                } else if (ReflectionUtil.compareType(Object.class, parameterClass)) {
                    //Object
                    parameterValue = RequestUtil.getParamList(fieldName, param);
                } else if (ReflectionUtil.compareType(String.class, parameterClass)) {
                    //String
                    Map<String, List<FormParam>> fieldMap = param.getFieldMap();
                    if (fieldMap.containsKey(fieldName)) {
                        StringBuilder buffer = new StringBuilder("");
                        for (int j = 0; j < formParamList.size(); j++) {
                            String value = CastUtil.castString(formParamList.get(j).getFieldValue(), null);
                            if (value != null) {
                                buffer.append(value).append(",");
                            }
                        }
                        parameterValue = buffer.length() > 0 ? buffer.substring(0, buffer.length() - 1) : null;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, FileParam.class)) {
                    //FileParam[]
                    List<FileParam> paramList = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                    parameterValue = CollectionUtil.isNotEmpty(paramList) ? paramList.get(paramList.size() - 1) : null;
                } else if (ReflectionUtil.compareType(parameterClass, Byte.class)) {
                    //Byte
                    for (int i = 0; i < formParamList.size(); i++) {
                        Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Boolean.class)) {
                    //Boolean
                    for (int i = 0; i < formParamList.size(); i++) {
                        Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Short.class)) {
                    //Short
                    for (int i = 0; i < formParamList.size(); i++) {
                        Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Character.class)) {
                    //Character
                    for (int i = 0; i < formParamList.size(); i++) {
                        Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Integer.class)) {
                    //Integer
                    for (int i = 0; i < formParamList.size(); i++) {
                        Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Long.class)) {
                    //Long
                    for (int i = 0; i < formParamList.size(); i++) {
                        Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Float.class)) {
                    //Float
                    for (int i = 0; i < formParamList.size(); i++) {
                        Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Double.class)) {
                    //Double
                    for (int i = 0; i < formParamList.size(); i++) {
                        Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("request analyze error", e);
    }
    return parameterValue;
}
Also used : ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) ArrayList(java.util.ArrayList) List(java.util.List) FileParam(org.axe.bean.mvc.FileParam) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) FormParam(org.axe.bean.mvc.FormParam) Map(java.util.Map)

Aggregations

FormParam (org.axe.bean.mvc.FormParam)7 ArrayList (java.util.ArrayList)6 List (java.util.List)4 Map (java.util.Map)3 FileParam (org.axe.bean.mvc.FileParam)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Param (org.axe.bean.mvc.Param)1