struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r941368 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
Date Wed, 05 May 2010 16:25:09 GMT
Author: lukaszlenart
Date: Wed May  5 16:25:09 2010
New Revision: 941368

URL: http://svn.apache.org/viewvc?rev=941368&view=rev
Log:
Solved WW-3250 - added wildcard support for Content type and test case

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java?rev=941368&r1=941367&r2=941368&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
Wed May  5 16:25:09 2010
@@ -226,6 +226,7 @@ public class FileUploadInterceptor exten
     /* (non-Javadoc)
      * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
      */
+
     public String intercept(ActionInvocation invocation) throws Exception {
         ActionContext ac = invocation.getInvocationContext();
 
@@ -243,7 +244,7 @@ public class FileUploadInterceptor exten
         ValidationAware validation = null;
 
         Object action = invocation.getAction();
-        
+
         if (action instanceof ValidationAware) {
             validation = (ValidationAware) action;
         }
@@ -323,7 +324,7 @@ public class FileUploadInterceptor exten
 
                 if ((currentFile != null) && currentFile.isFile()) {
                     if (currentFile.delete() == false) {
-                        LOG.warn("Resource Leaking:  Could not remove uploaded file '"+currentFile.getCanonicalPath()+"'.");
+                        LOG.warn("Resource Leaking:  Could not remove uploaded file '" +
currentFile.getCanonicalPath() + "'.");
                     }
                 }
             }
@@ -369,7 +370,7 @@ public class FileUploadInterceptor exten
             }
 
             LOG.warn(errMsg);
-        } else if ((! allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet,
filename))) {
+        } else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet,
filename))) {
             String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed",
new Object[]{inputName, filename, file.getName(), contentType}, locale);
             if (validation != null) {
                 validation.addFieldError(inputName, errMsg);
@@ -409,7 +410,43 @@ public class FileUploadInterceptor exten
      * @return true if itemCollection contains the item, false otherwise.
      */
     private boolean containsItem(Collection<String> itemCollection, String item) {
-        return itemCollection.contains(item.toLowerCase());
+        for (String pattern : itemCollection)
+            if (matchesWildcard(pattern, item))
+                return true;
+        return false;
+    }
+
+    private boolean matchesWildcard(String pattern, String text) {
+        text += '\0';
+        pattern += '\0';
+
+        int N = pattern.length();
+
+        boolean[] states = new boolean[N + 1];
+        boolean[] old = new boolean[N + 1];
+        old[0] = true;
+
+        for (int i = 0; i < text.length(); i++) {
+            char c = text.charAt(i);
+            states = new boolean[N + 1];
+            for (int j = 0; j < N; j++) {
+                char p = pattern.charAt(j);
+
+                if (old[j] && (p == '*'))
+                    old[j + 1] = true;
+
+                if (old[j] && (p == c))
+                    states[j + 1] = true;
+                if (old[j] && (p == '?'))
+                    states[j + 1] = true;
+                if (old[j] && (p == '*'))
+                    states[j] = true;
+                if (old[j] && (p == '*'))
+                    states[j + 1] = true;
+            }
+            old = states;
+        }
+        return states[N];
     }
 
     private boolean isNonEmpty(Object[] objArray) {
@@ -425,15 +462,15 @@ public class FileUploadInterceptor exten
     private String getTextMessage(String messageKey, Object[] args, Locale locale) {
         return getTextMessage(null, messageKey, args, locale);
     }
-    
+
     private String getTextMessage(Object action, String messageKey, Object[] args, Locale
locale) {
         if (args == null || args.length == 0) {
-            if ( action != null && useActionMessageBundle) {
+            if (action != null && useActionMessageBundle) {
                 return LocalizedTextUtil.findText(action.getClass(), messageKey, locale);
             }
-            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale);     
                  
+            return LocalizedTextUtil.findText(this.getClass(), messageKey, locale);
         } else {
-            if ( action != null && useActionMessageBundle) {
+            if (action != null && useActionMessageBundle) {
                 return LocalizedTextUtil.findText(action.getClass(), messageKey, locale,
DEFAULT_MESSAGE, args);
             }
             return LocalizedTextUtil.findText(this.getClass(), messageKey, locale, DEFAULT_MESSAGE,
args);

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java?rev=941368&r1=941367&r2=941368&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
(original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
Wed May  5 16:25:09 2010
@@ -82,6 +82,26 @@ public class FileUploadInterceptorTest e
         assertTrue(validation.hasErrors());
     }
 
+
+    public void testAcceptFileWithWildcardContent() throws Exception {
+        interceptor.setAllowedTypes("text/*");
+
+        ValidationAwareSupport validation = new ValidationAwareSupport();
+        boolean ok = interceptor.acceptFile(null, new File(""), "filename.txt", "text/plain",
"inputName", validation, Locale.getDefault());
+
+        assertTrue(ok);
+        assertTrue(validation.getFieldErrors().isEmpty());
+        assertFalse(validation.hasErrors());
+
+        interceptor.setAllowedTypes("text/h*");
+        validation = new ValidationAwareSupport();
+        boolean notOk = interceptor.acceptFile(null, new File(""), "filename.html", "text/plain",
"inputName", validation, Locale.getDefault());
+
+        assertFalse(notOk);
+        assertFalse(validation.getFieldErrors().isEmpty());
+        assertTrue(validation.hasErrors());
+    }
+
     public void testAcceptFileWithoutEmptyExtensions() throws Exception {
         interceptor.setAllowedExtensions(".txt");
 



Mime
View raw message