You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2016/11/25 18:17:58 UTC

[03/14] struts git commit: Adjusts interceptor to use the new class

Adjusts interceptor to use the new class


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/407cd582
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/407cd582
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/407cd582

Branch: refs/heads/master
Commit: 407cd58279c61a8c9487aae3e42a99fcfe8352b9
Parents: 45edbcb
Author: Lukasz Lenart <lu...@apache.org>
Authored: Mon Nov 21 11:05:47 2016 +0100
Committer: Lukasz Lenart <lu...@apache.org>
Committed: Mon Nov 21 11:05:47 2016 +0100

----------------------------------------------------------------------
 .../interceptor/FileUploadInterceptor.java      |  9 ++--
 .../interceptor/FileUploadInterceptorTest.java  | 56 ++++++++++++++++----
 2 files changed, 50 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/407cd582/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java b/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
index be97a31..b9f5cb6 100644
--- a/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
+++ b/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java
@@ -34,6 +34,7 @@ import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.dispatcher.LocalizedMessage;
 import org.apache.struts2.dispatcher.Parameter;
 import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
+import org.apache.struts2.dispatcher.multipart.UploadedFile;
 import org.apache.struts2.util.ContentTypeMatcher;
 
 import javax.servlet.http.HttpServletRequest;
@@ -280,9 +281,9 @@ public class FileUploadInterceptor extends AbstractInterceptor {
 
                 if (isNonEmpty(fileName)) {
                     // get a File object for the uploaded File
-                    File[] files = multiWrapper.getFiles(inputName);
+                    UploadedFile[] files = multiWrapper.getFiles(inputName);
                     if (files != null && files.length > 0) {
-                        List<File> acceptedFiles = new ArrayList<>(files.length);
+                        List<UploadedFile> acceptedFiles = new ArrayList<>(files.length);
                         List<String> acceptedContentTypes = new ArrayList<>(files.length);
                         List<String> acceptedFileNames = new ArrayList<>(files.length);
                         String contentTypeName = inputName + "ContentType";
@@ -298,7 +299,7 @@ public class FileUploadInterceptor extends AbstractInterceptor {
 
                         if (!acceptedFiles.isEmpty()) {
                             Map<String, Parameter> newParams = new HashMap<>();
-                            newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()])));
+                            newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new UploadedFile[acceptedFiles.size()])));
                             newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])));
                             newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])));
                             ac.getParameters().appendAll(newParams);
@@ -332,7 +333,7 @@ public class FileUploadInterceptor extends AbstractInterceptor {
      *                    logging.
      * @return true if the proposed file is acceptable by contentType and size.
      */
-    protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation) {
+    protected boolean acceptFile(Object action, UploadedFile file, String filename, String contentType, String inputName, ValidationAware validation) {
         boolean fileIsAcceptable = false;
 
         // If it's null the upload failed

http://git-wip-us.apache.org/repos/asf/struts/blob/407cd582/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java b/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
index 6668b4c..87251cd 100644
--- a/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
+++ b/core/src/test/java/org/apache/struts2/interceptor/FileUploadInterceptorTest.java
@@ -33,7 +33,9 @@ import org.apache.struts2.StrutsInternalTestCase;
 import org.apache.struts2.TestAction;
 import org.apache.struts2.dispatcher.HttpParameters;
 import org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest;
+import org.apache.struts2.dispatcher.multipart.JakartaUploadedFile;
 import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
+import org.apache.struts2.dispatcher.multipart.UploadedFile;
 import org.springframework.mock.web.MockHttpServletRequest;
 
 import javax.servlet.http.HttpServletRequest;
@@ -53,6 +55,38 @@ import java.util.Map;
  */
 public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
+    public static final UploadedFile EMPTY_FILE = new UploadedFile() {
+        @Override
+        public Long length() {
+            return 0L;
+        }
+
+        @Override
+        public String getName() {
+            return "";
+        }
+
+        @Override
+        public boolean isFile() {
+            return false;
+        }
+
+        @Override
+        public boolean delete() {
+            return false;
+        }
+
+        @Override
+        public String getAbsolutePath() {
+            return null;
+        }
+
+        @Override
+        public byte[] getContent() {
+            return new byte[0];
+        }
+    };
+
     private FileUploadInterceptor interceptor;
     private File tempDir;
     private TestAction action;
@@ -60,7 +94,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
     public void testAcceptFileWithEmptyAllowedTypesAndExtensions() throws Exception {
         // when allowed type is empty
         ValidationAwareSupport validation = new ValidationAwareSupport();
-        boolean ok = interceptor.acceptFile(action, new File(""), "filename", "text/plain", "inputName", validation);
+        boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename", "text/plain", "inputName", validation);
 
         assertTrue(ok);
         assertTrue(validation.getFieldErrors().isEmpty());
@@ -72,7 +106,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         // when file is of allowed types
         ValidationAwareSupport validation = new ValidationAwareSupport();
-        boolean ok = interceptor.acceptFile(action, new File(""), "filename.txt", "text/plain", "inputName", validation);
+        boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
 
         assertTrue(ok);
         assertTrue(validation.getFieldErrors().isEmpty());
@@ -80,7 +114,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         // when file is not of allowed types
         validation = new ValidationAwareSupport();
-        boolean notOk = interceptor.acceptFile(action, new File(""), "filename.html", "text/html", "inputName", validation);
+        boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/html", "inputName", validation);
 
         assertFalse(notOk);
         assertFalse(validation.getFieldErrors().isEmpty());
@@ -92,7 +126,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
         interceptor.setAllowedTypes("text/*");
 
         ValidationAwareSupport validation = new ValidationAwareSupport();
-        boolean ok = interceptor.acceptFile(action, new File(""), "filename.txt", "text/plain", "inputName", validation);
+        boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
 
         assertTrue(ok);
         assertTrue(validation.getFieldErrors().isEmpty());
@@ -100,7 +134,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         interceptor.setAllowedTypes("text/h*");
         validation = new ValidationAwareSupport();
-        boolean notOk = interceptor.acceptFile(action, new File(""), "filename.html", "text/plain", "inputName", validation);
+        boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/plain", "inputName", validation);
 
         assertFalse(notOk);
         assertFalse(validation.getFieldErrors().isEmpty());
@@ -112,7 +146,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         // when file is of allowed extensions
         ValidationAwareSupport validation = new ValidationAwareSupport();
-        boolean ok = interceptor.acceptFile(action, new File(""), "filename.txt", "text/plain", "inputName", validation);
+        boolean ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.txt", "text/plain", "inputName", validation);
 
         assertTrue(ok);
         assertTrue(validation.getFieldErrors().isEmpty());
@@ -120,7 +154,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         // when file is not of allowed extensions
         validation = new ValidationAwareSupport();
-        boolean notOk = interceptor.acceptFile(action, new File(""), "filename.html", "text/html", "inputName", validation);
+        boolean notOk = interceptor.acceptFile(action, EMPTY_FILE, "filename.html", "text/html", "inputName", validation);
 
         assertFalse(notOk);
         assertFalse(validation.getFieldErrors().isEmpty());
@@ -129,7 +163,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
         //test with multiple extensions
         interceptor.setAllowedExtensions(".txt,.lol");
         validation = new ValidationAwareSupport();
-        ok = interceptor.acceptFile(action, new File(""), "filename.lol", "text/plain", "inputName", validation);
+        ok = interceptor.acceptFile(action, EMPTY_FILE, "filename.lol", "text/plain", "inputName", validation);
 
         assertTrue(ok);
         assertTrue(validation.getFieldErrors().isEmpty());
@@ -164,7 +198,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
         URL url = ClassLoaderUtil.getResource("log4j2.xml", FileUploadInterceptorTest.class);
         File file = new File(new URI(url.toString()));
         assertTrue("log4j2.xml should be in src/test folder", file.exists());
-        boolean notOk = interceptor.acceptFile(action, file, "filename", "text/html", "inputName", validation);
+        boolean notOk = interceptor.acceptFile(action, new JakartaUploadedFile(file), "filename", "text/html", "inputName", validation);
 
         assertFalse(notOk);
         assertFalse(validation.getFieldErrors().isEmpty());
@@ -263,7 +297,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         HttpParameters parameters = mai.getInvocationContext().getParameters();
         assertTrue(parameters.getNames().size() == 3);
-        File[] files = (File[]) parameters.get("file").getObject();
+        UploadedFile[] files = (UploadedFile[]) parameters.get("file").getObject();
         String[] fileContentTypes = parameters.get("fileContentType").getMultipleValues();
         String[] fileRealFilenames = parameters.get("fileFileName").getMultipleValues();
 
@@ -323,7 +357,7 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
 
         HttpParameters parameters = mai.getInvocationContext().getParameters();
         assertEquals(3, parameters.getNames().size());
-        File[] files = (File[]) parameters.get("file").getObject();
+        UploadedFile[] files = (UploadedFile[]) parameters.get("file").getObject();
         String[] fileContentTypes = parameters.get("fileContentType").getMultipleValues();
         String[] fileRealFilenames = parameters.get("fileFileName").getMultipleValues();