commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joc...@apache.org
Subject commons-fileupload git commit: FILEUPLOAD-286: Added the default character set to the DiskFileItem.
Date Fri, 06 Oct 2017 19:48:16 GMT
Repository: commons-fileupload
Updated Branches:
  refs/heads/master 85104ae61 -> 2434446fb


FILEUPLOAD-286: Added the default character set to the DiskFileItem.


Project: http://git-wip-us.apache.org/repos/asf/commons-fileupload/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-fileupload/commit/2434446f
Tree: http://git-wip-us.apache.org/repos/asf/commons-fileupload/tree/2434446f
Diff: http://git-wip-us.apache.org/repos/asf/commons-fileupload/diff/2434446f

Branch: refs/heads/master
Commit: 2434446fbaff5ee8d90ec884e32e08aa75492841
Parents: 85104ae
Author: Jochen Wiedmann <jochen@apache.org>
Authored: Fri Oct 6 21:48:13 2017 +0200
Committer: Jochen Wiedmann <jochen@apache.org>
Committed: Fri Oct 6 21:48:13 2017 +0200

----------------------------------------------------------------------
 pom.xml                                         |  4 +++
 src/changes/changes.xml                         |  1 +
 .../commons/fileupload/disk/DiskFileItem.java   | 27 +++++++++++++++--
 .../fileupload/disk/DiskFileItemFactory.java    | 24 +++++++++++++++
 .../fileupload/DiskFileItemSerializeTest.java   |  8 ++---
 .../servlet/ServletFileUploadTest.java          | 31 +++++++++++++++-----
 6 files changed, 81 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index be553d8..b097ac2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -154,6 +154,10 @@
       <email>mailsurfie@gmail.com</email>
     </contributor>
     <contributor>
+      <name>maxxedev</name>
+      <email>maxxedev@gmail.com</email>
+    </contributor>
+    <contributor>
       <name>Rafal Krzewski</name>
       <email>Rafal.Krzewski@e-point.pl</email>
     </contributor>

http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8b2391c..55798e0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -60,6 +60,7 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="FILEUPLOAD-243" dev="sebb" type="update" due-to="Ville Skyttä">Make
some MultipartStream private fields final</action>
       <action                        dev="ecki" type="add">Site: added security report</action>
       <action                        dev="markt" due-to="Felix Schumacher">Improve
performance for large multi-part boundaries</action>
+      <action issue="FILEUPLOAD-286" dev="jochen" due-to="maxxedev" due-to-email="maxxedev@gmail.com">Added
the default character set to the DiskFileItem.</action>
     </release>
 
     <release version="1.3.3" description="Bugfix release for 1.3.3" date="2017-06-13">

http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/src/main/java/org/apache/commons/fileupload/disk/DiskFileItem.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/fileupload/disk/DiskFileItem.java b/src/main/java/org/apache/commons/fileupload/disk/DiskFileItem.java
index 2c6698a..48004f3 100644
--- a/src/main/java/org/apache/commons/fileupload/disk/DiskFileItem.java
+++ b/src/main/java/org/apache/commons/fileupload/disk/DiskFileItem.java
@@ -152,6 +152,12 @@ public class DiskFileItem
      */
     private FileItemHeaders headers;
 
+    /**
+     * Default content charset to be used when no explicit charset
+     * parameter is provided by the sender.
+     */
+    private String defaultCharset = DEFAULT_CHARSET;
+
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -338,7 +344,7 @@ public class DiskFileItem
         byte[] rawdata = get();
         String charset = getCharSet();
         if (charset == null) {
-            charset = DEFAULT_CHARSET;
+            charset = defaultCharset;
         }
         try {
             return new String(rawdata, charset);
@@ -476,7 +482,7 @@ public class DiskFileItem
      *
      * @throws IOException if an error occurs.
      */
-    public OutputStream getOutputStream()
+    public OutputStream getOutputStream() 
         throws IOException {
         if (dfos == null) {
             File outputFile = getTempFile();
@@ -601,4 +607,21 @@ public class DiskFileItem
         headers = pHeaders;
     }
 
+    /**
+     * Returns the default charset for use when no explicit charset
+     * parameter is provided by the sender.
+     * @return the default charset
+     */
+    public String getDefaultCharset() {
+        return defaultCharset;
+    }
+
+    /**
+     * Sets the default charset for use when no explicit charset
+     * parameter is provided by the sender.
+     * @param charset the default charset
+     */
+    public void setDefaultCharset(String charset) {
+        defaultCharset = charset;
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/src/main/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java b/src/main/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java
index 5768260..6a00a0c 100644
--- a/src/main/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java
+++ b/src/main/java/org/apache/commons/fileupload/disk/DiskFileItemFactory.java
@@ -95,6 +95,12 @@ public class DiskFileItemFactory implements FileItemFactory {
      */
     private FileCleaningTracker fileCleaningTracker;
 
+    /**
+     * Default content charset to be used when no explicit charset
+     * parameter is provided by the sender.
+     */
+    private String defaultCharset = DiskFileItem.DEFAULT_CHARSET;
+
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -192,6 +198,7 @@ public class DiskFileItemFactory implements FileItemFactory {
             boolean isFormField, String fileName) {
         DiskFileItem result = new DiskFileItem(fieldName, contentType,
                 isFormField, fileName, sizeThreshold, repository);
+        result.setDefaultCharset(defaultCharset);
         FileCleaningTracker tracker = getFileCleaningTracker();
         if (tracker != null) {
             tracker.track(result.getTempFile(), result);
@@ -222,4 +229,21 @@ public class DiskFileItemFactory implements FileItemFactory {
         fileCleaningTracker = pTracker;
     }
 
+    /**
+     * Returns the default charset for use when no explicit charset
+     * parameter is provided by the sender.
+     * @return the default charset
+     */
+    public String getDefaultCharset() {
+        return defaultCharset;
+    }
+
+    /**
+     * Sets the default charset for use when no explicit charset
+     * parameter is provided by the sender.
+     * @param pCsharset the default charset
+     */
+    public void setDefaultCharset(String pCharset) {
+        defaultCharset = pCharset;
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
index fa7e5a5..c773aed 100644
--- a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
+++ b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
@@ -98,7 +98,7 @@ public class DiskFileItemSerializeTest {
      * configured threshold.
      */
     @Test
-    public void testBelowThreshold() throws Exception {
+    public void testBelowThreshold() {
         // Create the FileItem
         byte[] testFieldValueBytes = createContentBytes(threshold - 1);
         testInMemoryObject(testFieldValueBytes);
@@ -109,7 +109,7 @@ public class DiskFileItemSerializeTest {
      * configured threshold.
      */
     @Test
-    public void testThreshold() throws Exception {
+    public void testThreshold() {
         // Create the FileItem
         byte[] testFieldValueBytes = createContentBytes(threshold);
         testInMemoryObject(testFieldValueBytes);
@@ -120,7 +120,7 @@ public class DiskFileItemSerializeTest {
      * configured threshold.
      */
     @Test
-    public void testAboveThreshold() throws Exception {
+    public void testAboveThreshold() {
         // Create the FileItem
         byte[] testFieldValueBytes = createContentBytes(threshold + 1);
         FileItem item = createFileItem(testFieldValueBytes);
@@ -137,7 +137,7 @@ public class DiskFileItemSerializeTest {
      * Test serialization and deserialization when repository is not null.
      */
     @Test
-    public void testValidRepository() throws Exception {
+    public void testValidRepository() {
         // Create the FileItem
         byte[] testFieldValueBytes = createContentBytes(threshold);
         testInMemoryObject(testFieldValueBytes, REPO);

http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/2434446f/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
index 3519a2f..5513ea6 100644
--- a/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
+++ b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
@@ -28,7 +28,6 @@ import org.apache.commons.fileupload.Constants;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.MockHttpServletRequest;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.junit.Before;
 import org.junit.Test;
 
 /**
@@ -39,13 +38,6 @@ import org.junit.Test;
  */
 public class ServletFileUploadTest {
 
-    private ServletFileUpload upload;
-
-    @Before
-    public void setUp() {
-        upload = new ServletFileUpload(new DiskFileItemFactory());
-    }
-
     /**
      * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">
      */
@@ -74,6 +66,7 @@ public class ServletFileUploadTest {
         byte[] bytes = text.getBytes("US-ASCII");
         HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
 
+        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
         Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request);
         assertTrue(mappedParameters.containsKey("file"));
         assertEquals(1, mappedParameters.get("file").size());
@@ -85,4 +78,26 @@ public class ServletFileUploadTest {
         assertEquals(2, mappedParameters.get("multi").size());
     }
 
+
+    @Test
+    public void parseImpliedUtf8()
+	    throws Exception {
+        // utf8 encoded form-data without explicit content-type encoding
+        String text = "-----1234\r\n" +
+                "Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
+                "\r\n" +
+                "Th�s �s the co�te�t of the f�le\n" +
+                "\r\n" +
+                "-----1234--\r\n";
+
+        byte[] bytes = text.getBytes("UTF-8");
+        HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
+
+        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
+        fileItemFactory.setDefaultCharset("UTF-8");
+        ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
+        List<FileItem> fileItems = upload.parseRequest(request);
+        FileItem fileItem = fileItems.get(0);
+        assertTrue(fileItem.getString(), fileItem.getString().contains("co�te�t"));
+    }
 }


Mime
View raw message