Search in sources :

Example 1 with Param

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

the class DispatcherServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) {
    String contentType = ContentType.APPLICATION_JSON.CONTENT_TYPE;
    String characterEncoding = CharacterEncoding.UTF_8.CHARACTER_ENCODING;
    List<Filter> doEndFilterList = null;
    try {
        //获取请求方法与请求路径
        String requestMethod = RequestUtil.getRequestMethod(request);
        String requestPath = RequestUtil.getRequestPath(request);
        if (requestPath != null && requestPath.equals("/favicon.ico")) {
            return;
        }
        //获取 Action 处理器
        Handler handler = ControllerHelper.getHandler(requestMethod, requestPath);
        if (handler != null) {
            //获取 Controller  类和 Bean 实例
            Class<?> controllerClass = handler.getControllerClass();
            Method actionMethod = handler.getActionMethod();
            Object controllerBean = BeanHelper.getBean(controllerClass);
            contentType = handler.getContentType();
            characterEncoding = handler.getCharacterEncoding();
            //##1.创建你请求参数对象
            Param param;
            if (FormRequestHelper.isMultipart(request)) {
                //如果是文件上传
                param = FormRequestHelper.createParam(request, requestPath, handler.getMappingPath());
            } else {
                //如果不是
                param = AjaxRequestHelper.createParam(request, requestPath, handler.getMappingPath());
            }
            //                List<Object> actionParamList = this.convertRequest2RequestParam(actionMethod, param, request, response);
            //                param.setActionParamList(actionParamList);
            //##2.先执行Filter链
            List<Filter> filterList = handler.getFilterList();
            boolean doFilterSuccess = true;
            if (CollectionUtil.isNotEmpty(filterList)) {
                for (Filter filter : filterList) {
                    //被执行的Filter,都添加到end任务里
                    if (doEndFilterList == null) {
                        doEndFilterList = new ArrayList<>();
                    }
                    doEndFilterList.add(filter);
                    doFilterSuccess = filter.doFilter(request, response, param, handler);
                    //执行失败则跳出,不再往下进行
                    if (!doFilterSuccess)
                        break;
                }
            }
            //##3.执行Interceptor 列表
            List<Interceptor> interceptorList = handler.getInterceptorList();
            boolean doInterceptorSuccess = true;
            if (CollectionUtil.isNotEmpty(interceptorList)) {
                for (Interceptor interceptor : interceptorList) {
                    doInterceptorSuccess = interceptor.doInterceptor(request, response, param, handler);
                    if (!doInterceptorSuccess)
                        break;
                }
            }
            //##4.执行action
            if (doFilterSuccess && doInterceptorSuccess) {
                //调用 Action方法
                List<Object> actionParamList = this.convertRequest2RequestParam(actionMethod, param, request, response);
                Object result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, actionParamList.toArray());
                if (result != null) {
                    if (result instanceof View) {
                        handleViewResult((View) result, request, response);
                    } else if (result instanceof Data) {
                        handleDataResult((Data) result, response, handler);
                    } else {
                        Data data = new Data(result);
                        handleDataResult(data, response, handler);
                    }
                }
            }
        } else {
            //404
            throw new RestException(RestException.SC_NOT_FOUND, "404 Not Found");
        }
    } catch (RedirectorInterrupt e) {
        //被中断,跳转
        View view = new View(e.getPath());
        try {
            handleViewResult(view, request, response);
        } catch (Exception e1) {
            LOGGER.error("中断,跳转 error", e);
        }
    } catch (RestException e) {
        //需要返回前台信息的异常
        writeError(e.getStatus(), e.getMessage(), response, contentType, characterEncoding);
    } catch (Exception e) {
        LOGGER.error("server error", e);
        //500
        writeError(RestException.SC_INTERNAL_SERVER_ERROR, "500 server error", response, contentType, characterEncoding);
        try {
            //邮件通知
            MailHelper.errorMail(e);
        } catch (Exception e1) {
            LOGGER.error("mail error", e1);
        }
    } finally {
        //##5.执行Filter链各个节点的收尾工作
        if (CollectionUtil.isNotEmpty(doEndFilterList)) {
            for (Filter filter : doEndFilterList) {
                try {
                    filter.doEnd();
                } catch (Exception endEx) {
                    LOGGER.error("filter doEnd failed", endEx);
                }
            }
        }
    }
}
Also used : RestException(org.axe.exception.RestException) Handler(org.axe.bean.mvc.Handler) Data(org.axe.bean.mvc.Data) Method(java.lang.reflect.Method) View(org.axe.bean.mvc.View) ServletException(javax.servlet.ServletException) RestException(org.axe.exception.RestException) IOException(java.io.IOException) Filter(org.axe.interface_.mvc.Filter) RequestParam(org.axe.annotation.mvc.RequestParam) Param(org.axe.bean.mvc.Param) Interceptor(org.axe.interface_.mvc.Interceptor) RedirectorInterrupt(org.axe.exception.RedirectorInterrupt)

Example 2 with Param

use of org.axe.bean.mvc.Param 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)

Aggregations

Param (org.axe.bean.mvc.Param)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 FileItem (org.apache.commons.fileupload.FileItem)1 RequestParam (org.axe.annotation.mvc.RequestParam)1 Data (org.axe.bean.mvc.Data)1 FileParam (org.axe.bean.mvc.FileParam)1 FormParam (org.axe.bean.mvc.FormParam)1 Handler (org.axe.bean.mvc.Handler)1 View (org.axe.bean.mvc.View)1 RedirectorInterrupt (org.axe.exception.RedirectorInterrupt)1 RestException (org.axe.exception.RestException)1 Filter (org.axe.interface_.mvc.Filter)1 Interceptor (org.axe.interface_.mvc.Interceptor)1