// 验证器 private void handleValidator() throws Exception { this.context.setValidation( ValidateExecution.checkValidate( context.getActionConfigBean().getValidator(), context.getQueryParamMap(), context.getRequest())); }
public static void handleActionRedirect(Context context, String path, String baseUrl) throws IOException { String method; String location; if (!path.contains("@")) { method = HttpMethod.GET; location = path; } else { int lastIndex = path.indexOf("@") + 1; method = path.substring(lastIndex); location = path.substring(0, lastIndex - 1); } StringBuilder sb = new StringBuilder(""); String param = null; if (path.contains("?")) { int lastIndex2 = path.indexOf("?") + 1; param = path.substring(lastIndex2); String[] params = param.split("&"); for (String para : params) { String[] p = para.split("="); sb.append(String.format("<input name='%s' value='%s' />", p[0], p[1])); } method = method.substring(0, method.indexOf("?")); } if (HttpMethod.GET.equalsIgnoreCase(method)) { String pa = param == null ? "" : "?" + CommonUtil.replaceChinese2Utf8(param); context.getResponse().sendRedirect(baseUrl + location + pa); return; } String _method = ""; if (HttpMethod.PUT.equalsIgnoreCase(method) || HttpMethod.DELETE.equalsIgnoreCase(method)) { _method = new String(method); method = HttpMethod.POST; } String action = baseUrl + location; // 构造一个Form表单模拟客户端发送新的Action请求 String format = "<form id='ACTION_REDIRECT_FORM' action='%s' method='%s' ><input name='_method' value='%s' />%s<input type='submit' /></form>"; String form = String.format(format, action, method, _method, sb.toString()); String js = "<script>document.getElementById('ACTION_REDIRECT_FORM').submit();</script>"; context.getWriter().print(form + js); }
public Validation validate(ValidatorConfigBean val, Context context) { Map<String, String> errMap = new HashMap<String, String>(); for (FieldConfigBean fcb : val.getField()) { // 比如我现在验证 "name"参数值不能出现 "中国" 字眼 String key = fcb.getName(); String value = context.getQueryParamMap().get(key)[0]; // 这个是封装了request请求传过来的参数map if (value.indexOf("中国") != -1) errMap.put(key, fcb.getMessage()); } Validation validation = new Validation(); validation.getErrors().put(val.getName(), errMap); return validation; }
public void validateUpload() throws Exception { String tmpDir = null; long memoryMax = 0; long allSizeMax = 0; long oneSizeMax = 0; String[] suffixArray = null; Upload _upload = method.getAnnotation(Upload.class); if (_upload != null) { if (_upload.tmpDir().trim().length() > 0) tmpDir = _upload.tmpDir(); if (_upload.maxMemorySize().trim().length() > 0) memoryMax = CommonUtil.strToInt(CommonUtil.parseFileSize(_upload.maxMemorySize()) + ""); if (_upload.maxRequestSize().trim().length() > 0) allSizeMax = CommonUtil.parseFileSize(_upload.maxRequestSize()); if (_upload.maxFileSize().trim().length() > 0) oneSizeMax = CommonUtil.parseFileSize(_upload.maxFileSize()); if (_upload.suffix().length > 0) suffixArray = _upload.suffix(); } long countAllSize = 0; for (Iterator<Entry<String, List<UploadFile>>> it = this.context.getUploadMap().entrySet().iterator(); it.hasNext(); ) { Entry<String, List<UploadFile>> e = it.next(); String fieldName = e.getKey(); List<UploadFile> files = e.getValue(); for (UploadFile file : files) { String fileName = file.getFileName(); String fileContentType = file.getContentType(); if (suffixArray != null && suffixArray.length > 0) { boolean isOk = false; for (String suffix : suffixArray) { if (fileName.endsWith("." + suffix)) { isOk = true; break; } } if (!isOk) { String err = "your upload file " + fileName + " type invalid ! only allow " + Arrays.asList(suffixArray); Map<String, String> errMap = new HashMap<String, String>(); errMap.put(fieldName, err); context.getValidation().getErrors().put("upload", errMap); return; } } long fileSize = file.getSize(); if (fileSize > oneSizeMax) { Map<String, String> errMap = new HashMap<String, String>(); String err = "your upload file " + fileName + " size overflow, only allow less than " + oneSizeMax; errMap.put(fieldName, err); context.getValidation().getErrors().put("upload", errMap); return; } countAllSize += fileSize; if (countAllSize > allSizeMax) { Map<String, String> errMap = new HashMap<String, String>(); String err = "your upload files all size overflow, only allow less than " + allSizeMax; errMap.put(fieldName, err); context.getValidation().getErrors().put("upload", errMap); return; } } } }