Search in sources :

Example 1 with InterExecution

use of org.eweb4j.mvc.interceptor.InterExecution in project eweb4j-framework by laiweiwei.

the class ActionExecution method execute.

/**
	 * 执行Action
	 * 
	 * @throws Exception
	 */
public void execute() throws Exception {
    // 实例化pojo
    initPojo();
    // IOC注入对象到pojo中
    injectIocBean();
    String methodName = this.context.getActionConfigBean().getMethod();
    Method[] methods = ru.getMethods(methodName);
    if (methods == null || methods.length == 0)
        return;
    method = this.getFirstMethd(methods);
    if (method == null)
        return;
    //		Consumes cons = method.getAnnotation(Consumes.class);
    boolean hasConsumes = JAXWSUtil.hasConsumes(method);
    if (hasConsumes) {
        String[] cts = ConsumesUtil.getConsumesValue(method);
        for (String ct : cts) {
            if ("json".equals(ct) || MIMEType.JSON.equals(ct)) {
                String[] jss = this.context.getQueryParamMap().get("_json_data_");
                if (jss != null) {
                    for (String json : jss) {
                        Map<String, Object> map;
                        try {
                            map = CommonUtil.parse(json, Map.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                            break;
                        }
                        for (Iterator<Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext(); ) {
                            Entry<String, Object> e = it.next();
                            String key = e.getKey();
                            String val = String.valueOf(e.getValue());
                            //继续解析,如果解析成功说明是多级的json格式,最后将其平展开来
                            try {
                                Map<String, Object> subMap = CommonUtil.parse(val, Map.class);
                                for (Iterator<Entry<String, Object>> subIt = subMap.entrySet().iterator(); subIt.hasNext(); ) {
                                    Entry<String, Object> subE = subIt.next();
                                    String subKey = subE.getKey();
                                    String subVal = String.valueOf(subE.getValue());
                                    String newKey = key + "." + subKey;
                                    try {
                                        Map<String, Object> subSubMap = CommonUtil.parse(subVal, Map.class);
                                        for (Iterator<Entry<String, Object>> subSubIt = subSubMap.entrySet().iterator(); subSubIt.hasNext(); ) {
                                            Entry<String, Object> subSubE = subSubIt.next();
                                            String subSubKey = subSubE.getKey();
                                            String subSubVal = String.valueOf(subSubE.getValue());
                                            String newSubKey = newKey + "." + subSubKey;
                                            assemJsonData(newSubKey, subSubVal);
                                        }
                                    } catch (Exception ex2) {
                                        assemJsonData(newKey, subVal);
                                    }
                                }
                            } catch (Exception ex) {
                                assemJsonData(key, val);
                            }
                        }
                    }
                }
            }
        }
    }
    // 注入框架mvc action 上下文环境
    this.injectActionCxt2Pojo(this.ru);
    //		if (IAction.class.isAssignableFrom(this.actionObject.getClass())) {
    //			// struts2风格
    //			IAction action = (IAction) actionObject;
    //			action.init(this.context);
    //			retn = action.execute();
    //			// 对Action执行返回结果的处理
    //			this.handleResult();
    //			
    //			return ;
    //		}
    // 执行验证器
    this.handleValidator();
    try {
        // 注入mvc action 请求参数
        ParamUtil.injectParam(this.context, this.ru, null);
        /* 方法体内的前置拦截器执行  */
        Before before = method.getAnnotation(Before.class);
        if (before != null) {
            InterExecution before_interExe = new InterExecution("before", context);
            before_interExe.execute(before.value());
            if (before_interExe.getError() != null) {
                before_interExe.showErr();
                return;
            }
        }
        // execute the action method
        excuteMethod(methodName);
        /* 方法体内的后置拦截器执行  */
        After after = method.getAnnotation(After.class);
        if (after != null) {
            // 后置拦截器
            InterExecution after_interExe = new InterExecution("after", context);
            after_interExe.execute(after.value());
            if (after_interExe.getError() != null) {
                after_interExe.showErr();
                return;
            }
        }
        /* 外部配置的后置拦截器后执行 */
        // 7.后置拦截器
        InterExecution after_interExe = new InterExecution("after", this.context);
        if (after_interExe.findAndExecuteInter()) {
            after_interExe.showErr();
            return;
        }
        // 对Action执行返回结果的处理
        this.handleResult();
    } catch (Exception e) {
        throw e;
    }
}
Also used : Before(org.eweb4j.mvc.interceptor.Before) Method(java.lang.reflect.Method) IOException(java.io.IOException) ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) After(org.eweb4j.mvc.interceptor.After) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with InterExecution

use of org.eweb4j.mvc.interceptor.InterExecution in project eweb4j-framework by laiweiwei.

the class EWebServlet method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Context context = null;
    try {
        // 2
        String err = EWeb4JConfig.start(ConfigConstant.START_FILE_NAME);
        // 启动eweb4j
        if (err != null) {
            this.printHtml(err, response.getWriter());
            return;
        }
        // 1 初始化环境
        context = this.initContext(request, response);
        // 最主要的还是提供给 org.eweb4j.i18n.Lang.java 类使用
        MVC.getThreadLocal().set(context);
        // 设置国际化语言
        Lang.change(request.getLocale());
        // 3.URI解析
        String uri = this.parseURL(request);
        context.setUri(uri);
        // 拿到BaseURL
        parseBaseUrl(context);
        // HTTP Method 解析
        String reqMethod = this.parseMethod(request);
        context.setHttpMethod(reqMethod);
        // 4.外部前置拦截器
        InterExecution before_interExe = new InterExecution("before", context);
        if (before_interExe.findAndExecuteInter()) {
            before_interExe.showErr();
            return;
        }
        // method + uri,用来判断是否有Action与之绑定
        ActionExecution actionExe = new ActionExecution(uri, reqMethod, context);
        if (actionExe.findAction()) {
            // 5.execute the action
            actionExe.execute();
            return;
        }
        // log
        this.normalReqLog(uri);
    } catch (Exception e) {
        e.printStackTrace();
        String info = CommonUtil.getExceptionString(e);
        LogFactory.getMVCLogger(EWebFilter.class).error(info);
        this.printHtml(info, response.getWriter());
    } finally {
        // 清空临时文件
        if (context != null && !context.getUploadMap().isEmpty())
            for (Iterator<Entry<String, List<UploadFile>>> it = context.getUploadMap().entrySet().iterator(); it.hasNext(); ) {
                Entry<String, List<UploadFile>> en = it.next();
                if (en.getValue() == null)
                    continue;
                for (UploadFile f : en.getValue()) {
                    FileUtil.deleteFile(f.getTmpFile());
                }
            }
    }
}
Also used : ServletContext(javax.servlet.ServletContext) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) ActionExecution(org.eweb4j.mvc.action.ActionExecution) Iterator(java.util.Iterator) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with InterExecution

use of org.eweb4j.mvc.interceptor.InterExecution in project eweb4j-framework by laiweiwei.

the class EWebFilter method doFilter.

/**
	 * 执行Filter
	 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    Context context = null;
    try {
        // 2
        String err = EWeb4JConfig.start(ConfigConstant.START_FILE_NAME);
        // 启动eweb4j
        if (err != null) {
            this.printHtml(err, res.getWriter());
            return;
        }
        // 1 初始化环境
        context = this.initContext(request, response);
        // 最主要的还是提供给 org.eweb4j.i18n.Lang.java 类使用
        MVC.getThreadLocal().set(context);
        // 设置国际化语言
        Lang.change(request.getLocale());
        // 3.URI解析
        String uri = this.parseURL(request);
        context.setUri(uri);
        // 拿到BaseURL
        parseBaseUrl(context);
        // HTTP Method 解析
        String reqMethod = this.parseMethod(request);
        context.setHttpMethod(reqMethod);
        // 4.外部前置拦截器
        InterExecution before_interExe = new InterExecution("before", context);
        if (before_interExe.findAndExecuteInter()) {
            before_interExe.showErr();
            return;
        }
        // method + uri,用来判断是否有Action与之绑定
        ActionExecution actionExe = new ActionExecution(uri, reqMethod, context);
        if (actionExe.findAction()) {
            // 5.execute the action
            actionExe.execute();
            return;
        }
        // log
        this.normalReqLog(uri);
        // chain
        chain.doFilter(req, res);
    } catch (Exception e) {
        e.printStackTrace();
        String info = CommonUtil.getExceptionString(e);
        LogFactory.getMVCLogger(EWebFilter.class).error(info);
        this.printHtml(info, res.getWriter());
    } finally {
        // 清空临时文件
        if (context != null && !context.getUploadMap().isEmpty())
            for (Iterator<Entry<String, List<UploadFile>>> it = context.getUploadMap().entrySet().iterator(); it.hasNext(); ) {
                Entry<String, List<UploadFile>> en = it.next();
                if (en.getValue() == null)
                    continue;
                for (UploadFile f : en.getValue()) {
                    FileUtil.deleteFile(f.getTmpFile());
                }
            }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) ActionExecution(org.eweb4j.mvc.action.ActionExecution) Iterator(java.util.Iterator) HttpServletResponse(javax.servlet.http.HttpServletResponse) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 Entry (java.util.Map.Entry)3 InterExecution (org.eweb4j.mvc.interceptor.InterExecution)3 Iterator (java.util.Iterator)2 List (java.util.List)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 ActionExecution (org.eweb4j.mvc.action.ActionExecution)2 UploadFile (org.eweb4j.mvc.upload.UploadFile)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipEntry (java.util.zip.ZipEntry)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 After (org.eweb4j.mvc.interceptor.After)1 Before (org.eweb4j.mvc.interceptor.Before)1