struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ralf Hauser (JIRA)" <j...@apache.org>
Subject [jira] Commented: (STR-1394) [upload] File upload maximum size validator
Date Wed, 14 Mar 2007 13:06:51 GMT

    [ https://issues.apache.org/struts/browse/STR-1394?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_40553
] 

Ralf Hauser commented on STR-1394:
----------------------------------

see also https://issues.apache.org/jira/browse/FILEUPLOAD-88 

> [upload] File upload maximum size validator
> -------------------------------------------
>
>                 Key: STR-1394
>                 URL: https://issues.apache.org/struts/browse/STR-1394
>             Project: Struts 1
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: Future
>         Environment: Operating System: All
> Platform: All
>            Reporter: Jerzy Puchala
>         Assigned To: Struts Developers
>            Priority: Minor
>
> There is a class which is checking if the size of uploaded file is not bigger 
> then allowed in validator.xml. Right now it exists as separate Validor class, 
> but the idea is to incorporate it into the Struts. In this version it works 
> checking both DynaForm and "static" forms. I was able to build DynaForm, wchich 
> contains fields org.apache.struts.upload.FormFile without the problem.
> There is the code of the class with incorporated documentation:
> <code>
> /*
>  * Created on Apr 3, 2003
>  *
>  */
> package org.yourek.struts.validator;
> import java.lang.reflect.InvocationTargetException;
> import java.lang.reflect.Method;
> import javax.servlet.http.HttpServletRequest;
> import org.apache.commons.validator.Field;
> import org.apache.commons.validator.ValidatorAction;
> import org.apache.struts.action.ActionErrors;
> import org.apache.struts.upload.FormFile;
> import org.apache.struts.validator.Resources;
> import org.apache.commons.beanutils.DynaBean;
> /**
>  * @author Jerzy Puchala
>  *
>  */
> public class FileSizeValidator {
> 	/**
> 	 * <p>Checks if the file uploaded from this field is smaller than value 
> in var attribute.
> 	 * You can use pure Byte number in &lt;var-value&gt; or you may use B - 
> for Bytes, 
> 	 * KB - for Kiloytes, MB - for Megabytes and GB - for Gigabytes. (I 
> hope you will not 
> 	 * upload this way Terabytes)</p>
> 	 * 
> 	 * To make this class work you have to:<br>
> 	 * 1. Add something simmilar to your validate.xml
> 	 * <pre>
> &lt;validator name="maxfilesize" 
> 	classname="org.yourek.struts.validate.FileSizeValidator" 
> 	method="validateMaxFileSize"
> 	methodParams="java.lang.Object,
> 				
> 	org.apache.commons.validator.ValidatorAction,
> 					org.apache.commons.validator.Field,
> 					org.apache.struts.action.ActionErrors,
> 					javax.servlet.http.HttpServletRequest" 
> 	msg="errors.maxfilesize" /&gt;
> 				
> &lt;field property="smallPictFile" depends="maxfilesize"&gt;
> 	&lt;arg0 name="maxfilesize" key="${var:maxFileSize}" 
> resource="false" /&gt;
> 	&lt;var&gt;
>     	&lt;var-name&gt;maxFileSize&lt;/var-name&gt;
>     	&lt;var-value&gt;100KB&lt;/var-value&gt;
>     		&lt;/var&gt;
> &lt;/field&gt;
> </pre>
> 	 * <br>
> 	 * 2. Add to the validator-rules.xml as follow:<br>
> 	 * <pre>      
> &lt;validator name="maxfilesize"
> 	classname="org.yourek.struts.validator.FileSizeValidator"
> 	method="validateMaxFileSize"
> 	methodParams="java.lang.Object,
> 				
> 	org.apache.commons.validator.ValidatorAction,
> 					org.apache.commons.validator.Field,
> 					org.apache.struts.action.ActionErrors,
> 					javax.servlet.http.HttpServletRequest"
> 	depends=""
> 	msg="errors.maxfilesize"&gt;
>       
> 	&lt;javascript&gt;&lt;![CDATA[
> 			function validateMaxFileSize(form) {
> 				return true;
> 			}]]&gt;
> 	&lt;/javascript&gt;
> &lt;/validator&gt;
> </pre>
> 	 * 
> 	 * @param bean The bean validation is being performed on.
> 	 * @param va The <code>ValidatorAction</code> that is currently being 
> performed.
> 	 * @param field The <code>Field</code> object associated with the 
> current field being validated.
> 	 * @param errors The <code>ActionErrors</code> object to add errors to 
> if any validation errors occur.
> 	 * @param request Current request object.
> 	 * @return True if smaller or equall, false otherwise.
> 	 */
> 	public static boolean validateMaxFileSize(
> 		Object bean,
> 		ValidatorAction va,
> 		Field field,
> 		ActionErrors errors,
> 		HttpServletRequest request) {
> 		String fieldProperty = field.getProperty();
> 		String maxFileSizeStr = field.getVarValue("maxFileSize");
> 		int maxFileSize = 0;
> 		if (maxFileSizeStr.charAt(maxFileSizeStr.length() - 1) == 'B') {
> 			if (maxFileSizeStr.charAt(maxFileSizeStr.length() - 2) 
> == 'K') {
> 				maxFileSize =
> 					Integer.parseInt(
> 						maxFileSizeStr.substring(
> 							0,
> 							maxFileSizeStr.length
> () - 2))
> 						* 1024;
> 			}
> 			else if (
> 				maxFileSizeStr.charAt(maxFileSizeStr.length() - 
> 2) == 'M') {
> 				maxFileSize =
> 					Integer.parseInt(
> 						maxFileSizeStr.substring(
> 							0,
> 							maxFileSizeStr.length
> () - 2))
> 						* 1048576;
> 			}
> 			else if (
> 				maxFileSizeStr.charAt(maxFileSizeStr.length() - 
> 2) == 'G') {
> 				maxFileSize =
> 					Integer.parseInt(
> 						maxFileSizeStr.substring(
> 							0,
> 							maxFileSizeStr.length
> () - 2))
> 						* 1073741824;
> 			}
> 			else {
> 				maxFileSize =
> 					Integer.parseInt(
> 						maxFileSizeStr.substring(0, 
> maxFileSizeStr.length() - 1));
> 			}
> 		}
> 		else {
> 			maxFileSize = Integer.parseInt(maxFileSizeStr);
> 		}
> 		FormFile file = (FormFile) getProperty(bean, fieldProperty);
> 		// check if (file != null) - there is no requirement that field 
> is required. 
> 		if (file != null && file.getFileSize() > maxFileSize) {
> 			errors.add(
> 				field.getKey(),
> 				Resources.getActionError(request, va, field));
> 			return false;
> 		}
> 		return true;
> 	}
> 	/**
> 	 * Return the property value from the page. It is naroving to have the 
> ability to
> 	 * return only java.lang.String like in <a 
> href="http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanuti
> ls/BeanUtils.html#getProperty(java.lang.Object,%20java.lang.String)">
> 	 * 
> http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/Bea
> nUtils.html#getProperty(java.lang.Object,%20java.lang.String)</a>
> 	 * To have flexibility of handling in DynaForm more classes and be able 
> to validate the input
> 	 * we have to return java.lang.Object 
> 	 * 
> 	 * @param bean
> 	 * @param property
> 	 * @param application
> 	 * @return java.lang.Object
> 	 */
> 	private static Object getProperty(Object bean, String property) {
> 		if (bean == null || property == null) {
> 			return null;
> 		}
> 		Class beanClass = bean.getClass();
> 		try {
> 			if (bean instanceof DynaBean) {
> 				return ((DynaBean) bean).get(property);
> 			}
> 			else {
> 				String methodName =
> 					new StringBuffer("get")
> 						.append(property.toUpperCase
> ().charAt(0))
> 						.append(property.substring(1))
> 						.toString();
> 				Method method = beanClass.getMethod(methodName, 
> null);
> 				return method.invoke(bean, null);
> 			}
> 		}
> 		catch (NoSuchMethodException e) {
> 			e.printStackTrace();
> 			return null;
> 		}
> 		catch (IllegalArgumentException e) {
> 			e.printStackTrace();
> 			return null;
> 		}
> 		catch (IllegalAccessException e) {
> 			e.printStackTrace();
> 			return null;
> 		}
> 		catch (InvocationTargetException e) {
> 			e.printStackTrace();
> 			return null;
> 		}
> 	}
> }
> </code>

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message