Search in sources :

Example 1 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile in project eweb4j-framework by laiweiwei.

the class RequriedValidator method validate.

public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> valError = new HashMap<String, String>();
    for (FieldConfigBean f : val.getField()) {
        String[] value = context.getQueryParamMap().get(f.getName());
        List<UploadFile> files = context.getUploadMap().get(f.getName());
        String mess = f.getMessage();
        if ((value == null || value.length == 0) && (files == null || files.isEmpty()))
            valError.put(f.getName(), mess);
        else {
            boolean isBlank = false;
            if (value != null)
                for (String v : value) {
                    if (v == null || v.trim().length() == 0) {
                        isBlank = true;
                        break;
                    }
                }
            if (!isBlank) {
                if (files != null)
                    for (UploadFile file : files) {
                        if (file == null || file.getFileName() == null || file.getFileName().trim().length() == 0 || file.getTmpFile() == null) {
                            isBlank = true;
                            break;
                        }
                    }
            }
            if (isBlank)
                valError.put(f.getName(), mess);
        }
        context.getRequest().setAttribute(f.getName(), value);
    }
    Validation validation = new Validation();
    if (!valError.isEmpty())
        validation.getErrors().put(val.getName(), valError);
    return validation;
}
Also used : Validation(org.eweb4j.mvc.action.Validation) UploadFile(org.eweb4j.mvc.upload.UploadFile) HashMap(java.util.HashMap) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean)

Example 2 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile in project eweb4j-framework by laiweiwei.

the class ParamUtil method injectFile.

public static void injectFile(Context context, ReflectUtil ru, String startName) throws Exception {
    Hashtable<String, Object> hasPojo = new Hashtable<String, Object>();
    paramForeach: for (Entry<String, List<UploadFile>> en : context.getUploadMap().entrySet()) {
        String paramName = en.getKey();
        List<UploadFile> paramValue = en.getValue();
        if (paramValue == null || paramValue.size() == 0)
            continue;
        Method setter = null;
        String[] pojoParamNames = paramName.split("\\.");
        if (pojoParamNames.length > 1) {
            Object lastPojo = ru.getObject();
            if (startName != null && startName.trim().length() > 0) {
                if (!pojoParamNames[0].equals(startName))
                    continue;
            }
            int lastIndex = pojoParamNames.length - 1;
            for (int i = 0; i < lastIndex; i++) {
                if (startName != null && startName.trim().length() > 0) {
                    if ((i + 1) == lastIndex)
                        break;
                    lastPojo = getLastPojo(lastPojo, pojoParamNames[i + 1], hasPojo);
                } else {
                    lastPojo = getLastPojo(lastPojo, pojoParamNames[i], hasPojo);
                }
                if (lastPojo == null)
                    continue paramForeach;
            }
            String _paramName = pojoParamNames[lastIndex];
            if (Map.class.isAssignableFrom(lastPojo.getClass())) {
                Map<String, Object> map = (HashMap<String, Object>) lastPojo;
                if (paramValue.size() <= 1)
                    map.put(_paramName, paramValue.get(0));
                else
                    map.put(_paramName, paramValue);
                lastPojo = map;
            }
            ReflectUtil lpRu = new ReflectUtil(lastPojo);
            setter = lpRu.getSetter(_paramName);
            if (setter == null)
                continue;
            Class<?> clazz = setter.getParameterTypes()[0];
            if (File.class.isAssignableFrom(clazz)) {
                setter.invoke(lastPojo, paramValue.get(0).getTmpFile());
            } else if (File[].class.isAssignableFrom(clazz)) {
                File[] files = new File[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
                setter.invoke(lastPojo, new Object[] { files });
            }
            if (UploadFile.class.isAssignableFrom(clazz)) {
                setter.invoke(lastPojo, paramValue.get(0));
            } else if (UploadFile[].class.isAssignableFrom(clazz)) {
                UploadFile[] files = new UploadFile[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
                setter.invoke(lastPojo, new Object[] { files });
            }
        } else {
            setter = ru.getSetter(paramName);
            if (setter == null)
                continue;
            Class<?> clazz = setter.getParameterTypes()[0];
            if (File.class.isAssignableFrom(clazz)) {
                setter.invoke(ru.getObject(), paramValue.get(0).getTmpFile());
            } else if (File[].class.isAssignableFrom(clazz)) {
                File[] files = new File[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j).getTmpFile();
                setter.invoke(ru.getObject(), new Object[] { files });
            }
            if (UploadFile.class.isAssignableFrom(clazz)) {
                setter.invoke(ru.getObject(), paramValue.get(0));
            } else if (UploadFile[].class.isAssignableFrom(clazz)) {
                UploadFile[] files = new UploadFile[paramValue.size()];
                for (int j = 0; j < files.length; j++) files[j] = paramValue.get(j);
                setter.invoke(ru.getObject(), new Object[] { files });
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) Method(java.lang.reflect.Method) ReflectUtil(org.eweb4j.util.ReflectUtil) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) UploadFile(org.eweb4j.mvc.upload.UploadFile)

Example 3 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile in project eweb4j-framework by laiweiwei.

the class ActionExecution method assemParams.

private Object[] assemParams(Class<?>[] paramTypes, Annotation[][] paramAnns) throws Exception {
    Object[] params = new Object[paramTypes.length];
    int pathParamIndex = 0;
    for (int i = 0; i < paramTypes.length; ++i) {
        Annotation[] anns = paramAnns[i];
        Class<?> paramClass = paramTypes[i];
        String[] paramValue = null;
        // 通过给定class 获取对应的ActionContextObj
        if (Context.class.isAssignableFrom(paramClass)) {
            params[i] = this.context;
            continue;
        }
        if (HttpServletRequest.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getRequest();
            continue;
        }
        if (HttpServletResponse.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getResponse();
            continue;
        }
        if (PrintWriter.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getWriter();
            continue;
        }
        if (ServletOutputStream.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getOut();
            continue;
        }
        if (HttpSession.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getSession();
            continue;
        }
        if (ActionProp.class.isAssignableFrom(paramClass)) {
            if (this.context.getActionProp() == null)
                this.context.setActionProp(new ActionProp(this.actionObject.getClass().getName()));
            params[i] = this.context.getActionProp();
            continue;
        }
        if (Validation.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getValidation();
            continue;
        }
        if (QueryParams.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getQueryParams();
            continue;
        }
        if (DAO.class.isAssignableFrom(paramClass)) {
            params[i] = new DAOImpl("");
            continue;
        }
        if (InsertDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getInsertDAO();
            continue;
        }
        if (DeleteDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getDeleteDAO();
            continue;
        }
        if (UpdateDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getUpdateDAO();
            continue;
        }
        if (SelectDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getSelectDAO();
            continue;
        }
        if (DivPageDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getDivPageDAO();
            continue;
        }
        if (SearchDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getSearchDAO();
            continue;
        }
        if (CascadeDAO.class.isAssignableFrom(paramClass)) {
            params[i] = DAOFactory.getCascadeDAO();
            continue;
        }
        //			PathParam pathParamAnn = this.getPathParamAnn(anns);
        Annotation pathParam = JAXWSUtil.getPathParam(anns);
        if (pathParam != null) {
            paramValue = this.getPathParamValue(PathUtil.getPathParamValue(pathParam));
            params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
            continue;
        }
        //			QueryParam queryParamAnn = this.getQueryParamAnn(anns);
        Annotation queryParamAnn = JAXWSUtil.getQueryParam(anns);
        if (queryParamAnn == null && Map.class.isAssignableFrom(paramClass)) {
            params[i] = this.context.getModel();
            continue;
        }
        //若参数有@QueryParam注解
        if (queryParamAnn != null) {
            final String fieldName = QueryParamUtil.getQueryParamValue(queryParamAnn);
            if (File.class.isAssignableFrom(paramClass)) {
                if (!this.context.getUploadMap().containsKey(fieldName))
                    continue;
                List<UploadFile> list = this.context.getUploadMap().get(fieldName);
                if (list == null || list.isEmpty())
                    continue;
                UploadFile uploadFile = list.get(0);
                File file = uploadFile.getTmpFile();
                params[i] = file;
                continue;
            }
            if (File[].class.isAssignableFrom(paramClass)) {
                if (!this.context.getUploadMap().containsKey(fieldName))
                    continue;
                List<UploadFile> list = this.context.getUploadMap().get(fieldName);
                if (list == null || list.isEmpty())
                    continue;
                File[] files = new File[list.size()];
                for (int j = 0; j < files.length; j++) files[j] = list.get(j).getTmpFile();
                params[i] = files;
            }
            if (UploadFile.class.isAssignableFrom(paramClass)) {
                if (!this.context.getUploadMap().containsKey(fieldName))
                    continue;
                List<UploadFile> list = this.context.getUploadMap().get(fieldName);
                if (list == null || list.isEmpty())
                    continue;
                UploadFile uploadFile = list.get(0);
                params[i] = uploadFile;
                continue;
            }
            if (UploadFile[].class.isAssignableFrom(paramClass)) {
                if (!this.context.getUploadMap().containsKey(fieldName))
                    continue;
                List<UploadFile> list = this.context.getUploadMap().get(fieldName);
                if (list == null || list.isEmpty())
                    continue;
                params[i] = list.toArray(new UploadFile[] {});
            }
            // 根据参数名称获取http对应的参数值
            String defaultValue = null;
            //				DefaultValue defaultValueAnn = this.getDefaultValueAnn(anns);
            Annotation defaultValueAnn = JAXWSUtil.getDefaultValue(anns);
            if (defaultValueAnn != null)
                defaultValue = QueryParamUtil.getDefaultValue(defaultValueAnn);
            paramValue = this.getQueryParamValue(fieldName, defaultValue);
            // 处理Date日期类型的参数
            if (java.util.Date.class.isAssignableFrom(paramClass)) {
                params[i] = this.getDateParam(anns, paramValue[0]);
                continue;
            }
            // 处理POJO类型的参数
            String startName = fieldName;
            if (ClassUtil.isPojo(paramClass)) {
                params[i] = this.injectParam2Pojo(paramClass, startName);
                continue;
            }
            // 处理Map类型的参数
            if (Map.class.isAssignableFrom(paramClass)) {
                params[i] = this.injectParam2Map(startName);
                continue;
            }
            //处理数组类型的参数
            if (paramClass.isArray())
                params[i] = ClassUtil.getParamVals(paramClass, paramValue);
            else
                params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
        } else {
            // 如果是基本数据类型,则按照排序进行注入
            String[] pathParams = this.context.getActionConfigBean().getPathParams();
            if (pathParams == null) {
                log.warn("QueryParam not found and PathParam not found too");
                continue;
            }
            paramValue = this.getPathParamValue(pathParams[pathParamIndex]);
            params[i] = ClassUtil.getParamVal(paramClass, paramValue[0]);
            pathParamIndex++;
            continue;
        }
    }
    return params;
}
Also used : Annotation(java.lang.annotation.Annotation) UploadFile(org.eweb4j.mvc.upload.UploadFile) DAOImpl(org.eweb4j.orm.dao.DAOImpl) Map(java.util.Map) HashMap(java.util.HashMap) UploadFile(org.eweb4j.mvc.upload.UploadFile) File(java.io.File)

Example 4 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile 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 5 with UploadFile

use of org.eweb4j.mvc.upload.UploadFile 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

UploadFile (org.eweb4j.mvc.upload.UploadFile)6 HashMap (java.util.HashMap)4 List (java.util.List)3 Entry (java.util.Map.Entry)3 File (java.io.File)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 ActionExecution (org.eweb4j.mvc.action.ActionExecution)2 Validation (org.eweb4j.mvc.action.Validation)2 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)2 InterExecution (org.eweb4j.mvc.interceptor.InterExecution)2 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1