Search in sources :

Example 1 with FormParam

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

the class HomeService method saveAxeProperties.

public String saveAxeProperties(Param param) {
    URL resource = Thread.currentThread().getContextClassLoader().getResource(ConfigConstant.CONFIG_FILE);
    File configFile = null;
    if (resource != null) {
        configFile = FileUtil.backupAndCreateNewFile(resource.getFile());
    } else {
        configFile = new File(Thread.currentThread().getContextClassLoader().getClass().getResource("/").getPath() + ConfigConstant.CONFIG_FILE);
    }
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(configFile));
        //#保存需要保存的配置
        for (List<FormParam> formParamList : param.getFieldMap().values()) {
            String fieldName = null;
            String fieldValue = null;
            for (FormParam formParam : formParamList) {
                fieldName = formParam.getFieldName();
                if (StringUtil.isNotEmpty(formParam.getFieldValue()) && !"null".equalsIgnoreCase(formParam.getFieldValue())) {
                    fieldValue = fieldValue == null ? formParam.getFieldValue() : fieldValue + "," + formParam.getFieldValue();
                }
            }
            if (StringUtil.isNotEmpty(fieldValue)) {
                if (!fieldName.startsWith("NOSAVE.")) {
                    writer.write(fieldName + "=" + fieldValue);
                    writer.newLine();
                }
            }
        }
        //#登录ID和密码需要生成TOKEN后保存
        List<FormParam> signIdList = param.getFieldMap().get(ConfigConstant.AXE_SIGN_IN);
        if (CollectionUtil.isNotEmpty(signIdList)) {
            if ("true".equalsIgnoreCase(signIdList.get(0).getFieldValue())) {
                //#需要登录才保存
                List<FormParam> idList = param.getFieldMap().get("NOSAVE.axe.signin.id");
                String id = "";
                if (CollectionUtil.isNotEmpty(idList)) {
                    id = idList.get(0).getFieldValue();
                }
                List<FormParam> passwordList = param.getFieldMap().get("NOSAVE.axe.signin.password");
                String password = "";
                if (CollectionUtil.isNotEmpty(passwordList)) {
                    password = passwordList.get(0).getFieldValue();
                }
                id = id == null ? "" : id;
                password = password == null ? "" : password;
                String fieldName = ConfigConstant.AXE_SIGN_IN_TOKEN;
                String fieldValue = MD5Util.getMD5Code(id + ":" + password);
                writer.write(fieldName + "=" + fieldValue);
                writer.newLine();
            }
        }
        writer.close();
    } catch (Exception e) {
        LOGGER.error("home error", e);
    }
    return configFile.getAbsolutePath();
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) FormParam(org.axe.bean.mvc.FormParam) URL(java.net.URL) BufferedWriter(java.io.BufferedWriter)

Example 2 with FormParam

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

the class FormRequestHelper method createParam.

/**
     * 创建请求对象
     */
public static Param createParam(HttpServletRequest request, String requestPath, String mappingPath) {
    List<FormParam> formParamList = new ArrayList<>();
    List<FileParam> fileParamList = new ArrayList<>();
    try {
        //解析表单请求参数、文件
        Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
        if (CollectionUtil.isNotEmpty(fileItemListMap)) {
            for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
                String fieldName = fileItemListEntry.getKey();
                List<FileItem> fileItemList = fileItemListEntry.getValue();
                if (CollectionUtil.isNotEmpty(fileItemList)) {
                    for (FileItem fileItem : fileItemList) {
                        if (fileItem.isFormField()) {
                            String fieldValue = fileItem.getString("UTF-8");
                            formParamList.add(new FormParam(fieldName, fieldValue));
                        } else {
                            String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
                            if (StringUtil.isNotEmpty(fileName)) {
                                long fileSize = fileItem.getSize();
                                String contentType = fileItem.getContentType();
                                InputStream inputStream = fileItem.getInputStream();
                                fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputStream));
                            }
                        }
                    }
                }
            }
        }
        //解析url请求参数
        formParamList.addAll(RequestUtil.parseParameter(request, requestPath, mappingPath));
    } catch (Exception e) {
        LOGGER.error("create param failed", e);
        throw new RuntimeException(e);
    }
    return new Param(null, formParamList, fileParamList, null);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileParam(org.axe.bean.mvc.FileParam) FileItem(org.apache.commons.fileupload.FileItem) FormParam(org.axe.bean.mvc.FormParam) Param(org.axe.bean.mvc.Param) FileParam(org.axe.bean.mvc.FileParam) ArrayList(java.util.ArrayList) List(java.util.List) FormParam(org.axe.bean.mvc.FormParam) Map(java.util.Map)

Example 3 with FormParam

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

the class RequestUtil method parseParameter.

public static List<FormParam> parseParameter(HttpServletRequest request, String requestPath, String mappingPath) {
    List<FormParam> formParamList = new ArrayList<>();
    //分析url查询字符串
    Enumeration<String> paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements()) {
        String fieldName = paramNames.nextElement();
        String[] fieldValues = request.getParameterValues(fieldName);
        if (ArrayUtil.isNotEmpty(fieldValues)) {
            for (String fieldValue : fieldValues) {
                formParamList.add(new FormParam(fieldName, fieldValue));
            }
        }
    }
    //分析url路径参数
    //requestPath 客户端过来的servletPath
    //action方法上的@Quest.value注解值
    Pattern reg4requestPath = Pattern.compile(mappingPath.replaceAll("\\{[" + REG_WORD + "]*\\}", "([" + REG_WORD + "]*)"));
    Matcher matcher4requestPath = reg4requestPath.matcher(requestPath);
    Pattern reg4mappingPath = Pattern.compile("\\{([" + REG_WORD + "]*)\\}");
    Matcher matcher4mappingPath = reg4mappingPath.matcher(mappingPath);
    boolean fieldValueFind = matcher4requestPath.find();
    for (int fieldValueGroupIndex = 1; matcher4mappingPath.find(); fieldValueGroupIndex++) {
        String fieldName = matcher4mappingPath.group(1);
        String fieldValue = null;
        if (fieldValueFind && fieldValueGroupIndex <= matcher4requestPath.groupCount()) {
            fieldValue = matcher4requestPath.group(fieldValueGroupIndex);
        }
        formParamList.add(new FormParam(fieldName, fieldValue));
    }
    return formParamList;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) FormParam(org.axe.bean.mvc.FormParam)

Example 4 with FormParam

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

the class RequestUtil method getFormParamList.

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

Example 5 with FormParam

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

the class FormRequestHelper method initParam.

/**
     * 创建请求对象
     */
public static void initParam(Param param, HttpServletRequest request, String requestPath, String mappingPath) {
    List<FormParam> formParamList = new ArrayList<>();
    List<FileParam> fileParamList = new ArrayList<>();
    try {
        //解析表单请求参数、文件
        Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
        if (CollectionUtil.isNotEmpty(fileItemListMap)) {
            for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
                String fieldName = fileItemListEntry.getKey();
                List<FileItem> fileItemList = fileItemListEntry.getValue();
                if (CollectionUtil.isNotEmpty(fileItemList)) {
                    for (FileItem fileItem : fileItemList) {
                        if (fileItem.isFormField()) {
                            String fieldValue = fileItem.getString("UTF-8");
                            formParamList.add(new FormParam(fieldName, fieldValue));
                        } else {
                            String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
                            if (StringUtil.isNotEmpty(fileName)) {
                                long fileSize = fileItem.getSize();
                                String contentType = fileItem.getContentType();
                                InputStream inputStream = fileItem.getInputStream();
                                fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputStream));
                            }
                        }
                    }
                }
            }
        }
        //解析url请求参数
        formParamList.addAll(RequestUtil.parseParameter(request, requestPath, mappingPath));
    } catch (Exception e) {
        LOGGER.error("create param failed", e);
        throw new RuntimeException(e);
    }
    param.init(null, formParamList, fileParamList, null);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileParam(org.axe.bean.mvc.FileParam) FileItem(org.apache.commons.fileupload.FileItem) ArrayList(java.util.ArrayList) List(java.util.List) 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