[FILEUPLOAD-199] #comment as suggested by Thomas, shaded the MimeUtility class from javax.mail APIs in order to decode RFC2047 header values #resolve

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/fileupload/trunk@1455470 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/LICENSE.txt b/LICENSE.txt
index d645695..9412abc 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -200,3 +200,53 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+APACHE COMMONS FILEUPLOAD SUBCOMPONENTS:
+
+The Apache Commons FileUpload jar includes a number of subcomponents with
+separate copyright notices and license terms. Your use of the source
+code for the these subcomponents is subject to the terms and
+conditions of the following licenses.
+
+=
+
+For the javax.mail component (http://www.oracle.com/technetwork/java/javamail/index.html)
+This is licensed under the Common Development and Distribution License("CDDL"), see below
+
+Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
+ 
+The contents of this file are subject to the terms of either the GNU
+General Public License Version 2 only ("GPL") or the Common Development
+and Distribution License("CDDL") (collectively, the "License").  You
+may not use this file except in compliance with the License.  You can
+obtain a copy of the License at
+https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
+or packager/legal/LICENSE.txt.  See the License for the specific
+language governing permissions and limitations under the License.
+
+When distributing the software, include this License Header Notice in each
+file and include the License file at packager/legal/LICENSE.txt.
+
+GPL Classpath Exception:
+Oracle designates this particular file as subject to the "Classpath"
+exception as provided by Oracle in the GPL Version 2 section of the License
+file that accompanied this code.
+
+Modifications:
+If applicable, add the following below the License Header, with the fields
+enclosed by brackets [] replaced by your own identifying information:
+"Portions Copyright [year] [name of copyright owner]"
+
+Contributor(s):
+If you wish your version of this file to be governed by only the CDDL or
+only the GPL Version 2, indicate your decision by adding "[Contributor]
+elects to include this software in this distribution under the [CDDL or GPL
+Version 2] license."  If you don't indicate a single choice of license, a
+recipient has the option to distribute your version of this file under
+either the CDDL, the GPL Version 2 or to extend the choice of license to
+its licensees as provided above.  However, if you add GPL Version 2 code
+and therefore, elected the GPL Version 2 license, then the option applies
+only if the new code is made subject to such option by the copyright
+holder.
+
+=
\ No newline at end of file
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 4450cd9..bb0894b 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -63,6 +63,7 @@
  * [FILEUPLOAD-189] - DiskFileItemFactory use of FileCleaningTracker is documented or coded wrong
  * [FILEUPLOAD-195] - Error reading the file size larger than 2 gb
  * [FILEUPLOAD-197] - ServletFileUpload isMultipartContent method does not support HTTP PUT
+ * [FILEUPLOAD-199] - Uploads have unexpected results for files with non-ASCII names - support RFC2047
  * [FILEUPLOAD-204] - FileItem.getHeaders() returns always null.
  * [FILEUPLOAD-212] - Insecure request size checking
  * [FILEUPLOAD-214] - ServletFileUpload only accepts POST requests
diff --git a/pom.xml b/pom.xml
index 9bc01aa..568c514 100644
--- a/pom.xml
+++ b/pom.xml
@@ -210,6 +210,12 @@
       <artifactId>commons-io</artifactId>
       <version>2.2</version>
     </dependency>
+    <dependency>
+      <groupId>javax.mail</groupId>
+      <artifactId>mail</artifactId>
+      <version>1.4.5</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>
@@ -231,6 +237,41 @@
           <goals>deploy</goals>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>2.0</version>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <artifactSet>
+                <includes>
+                  <include>javax.mail:mail</include>
+                </includes>
+              </artifactSet>
+              <filters>
+                <filter>
+                  <artifact>javax.mail:mail</artifact>
+                  <includes>
+                    <include>javax/mail/MessagingException*</include>
+                    <include>javax/mail/internet/MimeUtility*</include>
+                  </includes>
+                </filter>
+              </filters>
+              <relocations>
+                <relocation>
+                  <pattern>javax.mail</pattern>
+                  <shadedPattern>org.apache.commons.fileupload.util.javax.mail</shadedPattern>
+                </relocation>
+              </relocations>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
     </plugins>
   </build>
 
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6fdfe0f..95430d5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -97,6 +97,9 @@
       <action issue="FILEUPLOAD-197" dev="simonetripodi" type="fix" due-to="David Wolverton">
         ServletFileUpload isMultipartContent method does not support HTTP PUT - thanks Roy T. Fielding and Jochen  Wiedmann
       </action>
+      <action issue="FILEUPLOAD-199" dev="simonetripodi" type="fix" due-to="Mark Thomas">
+        Uploads have unexpected results for files with non-ASCII names - support RFC2047 - thanks Thomas Neidhart
+      </action>
       <action issue="FILEUPLOAD-204" dev="jochen" type="fix" due-to="Hakju Oh">
         FileItem.getHeaders() returns always null.
       </action>
diff --git a/src/main/java/org/apache/commons/fileupload/ParameterParser.java b/src/main/java/org/apache/commons/fileupload/ParameterParser.java
index ca43a68..4386585 100644
--- a/src/main/java/org/apache/commons/fileupload/ParameterParser.java
+++ b/src/main/java/org/apache/commons/fileupload/ParameterParser.java
@@ -16,9 +16,12 @@
  */
 package org.apache.commons.fileupload;
 
+import java.io.UnsupportedEncodingException;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.mail.internet.MimeUtility;
+
 /**
  * A simple parser intended to parse sequences of name/value pairs.
  *
@@ -311,6 +314,14 @@
                 pos++; // skip '='
                 paramValue = parseQuotedToken(new char[] {
                         separator });
+
+                if (paramValue != null) {
+                    try {
+                        paramValue = MimeUtility.decodeText(paramValue);
+                    } catch (UnsupportedEncodingException e) {
+                        // let's keep the original value in this case
+                    }
+                }
             }
             if (hasChar() && (chars[pos] == separator)) {
                 pos++; // skip separator
@@ -319,6 +330,7 @@
                 if (this.lowerCaseNames) {
                     paramName = paramName.toLowerCase();
                 }
+
                 params.put(paramName, paramValue);
             }
         }
diff --git a/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java b/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java
index f77985c..176e3d9 100644
--- a/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java
+++ b/src/test/java/org/apache/commons/fileupload/ParameterParserTest.java
@@ -108,4 +108,15 @@
         assertEquals("BbC04y", params.get("boundary"));
     }
 
+    /**
+     * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-199">FILEUPLOAD-199</a>
+     */
+    @Test
+    public void fileUpload199() {
+        ParameterParser parser = new ParameterParser();
+        String s = "Content-Disposition: form-data; name=\"file\"; filename=\"=?UTF-8?Q?=D1=82=D0=B5=D1=81=D1=82.txt?=\"\r\n";
+        Map<String, String> params = parser.parse(s, new char[] { ',', ';' });
+        assertEquals("ั‚ะตัั‚.txt", params.get("filename"));
+    }
+
 }