Index: lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorSpringUtil.java =================================================================== diff -u -r7090e391bfb2b94be0a2ff7ffcbfa4f3e9eb5743 -r0c57311e088d2090b702155b3cf78027c8462e0c --- lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorSpringUtil.java (.../FileValidatorSpringUtil.java) (revision 7090e391bfb2b94be0a2ff7ffcbfa4f3e9eb5743) +++ lams_common/src/java/org/lamsfoundation/lams/util/FileValidatorSpringUtil.java (.../FileValidatorSpringUtil.java) (revision 0c57311e088d2090b702155b3cf78027c8462e0c) @@ -34,6 +34,8 @@ import org.apache.commons.validator.util.ValidatorUtils; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.validation.Errors; import org.springframework.web.multipart.MultipartFile; @@ -46,7 +48,7 @@ public class FileValidatorSpringUtil { public static final String LARGE_FILE = "largeFile"; - private static final String MSG_KEY = "errors.maxfilesize"; + public static final String MSG_KEY = "errors.maxfilesize"; /** * To enable this validator copy the XML entry below to validator-rules.xml @@ -77,25 +79,26 @@ * the message as arg0. So the following message or something similar could be used. * errors.maxfilesize=The uploaded file has exceeded the maximum file size limit of {0} bytes */ -// public static boolean validateFileSize(Object bean, ValidatorAction va, Field field, Errors errors, -// Validator validator, HttpServletRequest request) { -// -// long fileSize = 0; -// try { -// String fileSizeStr = ValidatorUtils.getValueAsString(bean, field.getProperty()); -// fileSize = Long.valueOf(fileSizeStr); -// } catch (Exception e) { -// //catch null point exception: e.g., offlineFile.fileSize, if offlineFile is null, -// //ValidatorUtils.getValueAsString() will throw null. -// //skip, do nothing -// return true; -// } -// -// boolean largeFile = Boolean.valueOf(field.getVarValue(LARGE_FILE)).booleanValue(); -// //so far put message into GLOABLE_MESSAGE rather than special key -// return FileValidatorSpringUtil.validateFileSize(fileSize, largeFile, errors); -// } + public static boolean validateFileSize(Object bean, ValidatorAction va, Field field, MultiValueMap errorMap, + Validator validator, HttpServletRequest request) { + Long fileSize = 0L; + try { + String fileSizeStr = ValidatorUtils.getValueAsString(bean, field.getProperty()); + fileSize = Long.valueOf(fileSizeStr); + } catch (Exception e) { + //catch null point exception: e.g., offlineFile.fileSize, if offlineFile is null, + //ValidatorUtils.getValueAsString() will throw null. + //skip, do nothing + return true; + } + + boolean largeFile = Boolean.valueOf(field.getVarValue(LARGE_FILE)).booleanValue(); + //so far put message into GLOABLE_MESSAGE rather than special key + return FileValidatorSpringUtil.validateFileSize(fileSize, largeFile, errorMap); + // validateFileSize(fileSize, largeFile, errorMap); + } + /** * * @param file @@ -104,8 +107,8 @@ * @return Be careful, if the file size is under maximum size, return TRUE. Otherwise, return false. */ - public static boolean validateFileSize(MultipartFile file, boolean largeFile) { - long fileSize = 0; + public static boolean validateFileSize(MultipartFile file, boolean largeFile, MultiValueMap errorMap) { + Long fileSize = 0L; try { fileSize = file.getSize(); } catch (Exception e) { @@ -119,6 +122,7 @@ String maxSize = FileValidatorSpringUtil.formatSize(maxFileSize); // set error message + errorMap.add(MSG_KEY, maxSize); return false; } return true; @@ -133,45 +137,47 @@ * @param errors * @return Be careful, if the file size is under maximum size, return TRUE. Otherwise, return false. */ - public static boolean validateFileSize(MultipartFile file, Long fileSize, boolean largeFile, String errorKey, - Errors errors) { - - try { - fileSize = file.getSize(); - } catch (Exception e) { - //skip, do nothing - return true; - } +// public static boolean validateFileSize(MultipartFile file, Long fileSize, boolean largeFile) { +// +// try { +// fileSize = file.getSize(); +// } catch (Exception e) { +// //skip, do nothing +// return true; +// } +// +// return FileValidatorSpringUtil.validateFileSize(file, fileSize, largeFile); +// } - return FileValidatorSpringUtil.validateFileSize(file, fileSize, largeFile, errorKey, errors); - } - /** * * @param file * @param largeFile * @return return null if file size is below max filesize, otherwise, return error message */ - public static Errors validateFileSize(MultipartFile file, Long fileSize, boolean largeFile) { + public static MultiValueMap validateFileSize(MultipartFile file, Long fileSize, boolean largeFile) { fileSize = file.getSize(); - Errors errorMessage = null; - boolean isMaxFilesizeExceeded = !FileValidatorSpringUtil.validateFileSize(file, largeFile); + MultiValueMap errorMap = new LinkedMultiValueMap(); + + MultiValueMap errorMessages = new LinkedMultiValueMap(); + + boolean isMaxFilesizeExceeded = !FileValidatorSpringUtil.validateFileSize(file, largeFile, errorMap); if (isMaxFilesizeExceeded) { -// errorMessage = (Errors) errors.getAllErrors(); + errorMessages.add(MSG_KEY, errorMap.toString()); } - return errorMessage; + return errorMessages; } - private static boolean validateFileSize(int fileSize, boolean largeFile, String errorKey, Errors errors) { + public static boolean validateFileSize(Long fileSize, boolean largeFile, MultiValueMap errorMap) { float maxFileSize = largeFile ? Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_LARGE_MAX_SIZE) : Configuration.getAsInt(ConfigurationKeys.UPLOAD_FILE_MAX_SIZE); if (fileSize > maxFileSize) { String maxSize = FileValidatorSpringUtil.formatSize(maxFileSize); // set error message - errors.reject(MSG_KEY, maxSize); + errorMap.add(MSG_KEY, maxSize); return false; } return true;