From 667b1a523bc62d332d3406ed85fcb1e55b021612 Mon Sep 17 00:00:00 2001
From: Christoph Brill <egore911@egore911.de>
Date: Wed, 15 Aug 2012 16:55:46 +0200
Subject: [PATCH] Port resteasy-multipart-provider to mime4j 0.7 API

---
 pom.xml                                            |    3 +-
 .../multipart/MultipartFormDataInputImpl.java      |   10 +++---
 .../providers/multipart/MultipartInputImpl.java    |   34 +++++++++++---------
 .../multipart/MultipartRelatedInputImpl.java       |    8 ++--
 4 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/pom.xml b/pom.xml
index c3d2495..2a2474e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,7 +34,8 @@
         </dependency>
         <dependency>
             <groupId>org.apache.james</groupId>
-            <artifactId>apache-mime4j</artifactId>
+            <artifactId>apache-mime4j-dom</artifactId>
+            <version>0.7</version>
         </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
diff --git a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartFormDataInputImpl.java b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartFormDataInputImpl.java
index 1a431c8..6b1b80e 100644
--- a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartFormDataInputImpl.java
+++ b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartFormDataInputImpl.java
@@ -10,10 +10,10 @@ import java.util.Map;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.Providers;
 
-import org.apache.james.mime4j.field.ContentDispositionField;
-import org.apache.james.mime4j.field.FieldName;
-import org.apache.james.mime4j.message.BodyPart;
-import org.apache.james.mime4j.parser.Field;
+import org.apache.james.mime4j.dom.field.ContentDispositionField;
+import org.apache.james.mime4j.dom.field.FieldName;
+import org.apache.james.mime4j.dom.Entity;
+import org.apache.james.mime4j.stream.Field;
 import org.jboss.resteasy.util.GenericType;
 
 /**
@@ -61,7 +61,7 @@ public class MultipartFormDataInputImpl extends MultipartInputImpl implements
 	}
 
 	@Override
-	protected InputPart extractPart(BodyPart bodyPart) throws IOException {
+	protected InputPart extractPart(Entity bodyPart) throws IOException {
 		InputPart currPart = super.extractPart(bodyPart);
 		Field disposition = bodyPart.getHeader().getField(
 				FieldName.CONTENT_DISPOSITION);
diff --git a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartInputImpl.java b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartInputImpl.java
index 3e73737..61cd1ab 100644
--- a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartInputImpl.java
+++ b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartInputImpl.java
@@ -1,14 +1,14 @@
 package org.jboss.resteasy.plugins.providers.multipart;
 
-import org.apache.james.mime4j.field.ContentTypeField;
-import org.apache.james.mime4j.message.BinaryBody;
-import org.apache.james.mime4j.message.Body;
-import org.apache.james.mime4j.message.BodyPart;
-import org.apache.james.mime4j.message.Message;
-import org.apache.james.mime4j.message.Multipart;
-import org.apache.james.mime4j.message.TextBody;
-import org.apache.james.mime4j.parser.Field;
-import org.apache.james.mime4j.util.CharsetUtil;
+import org.apache.james.mime4j.dom.field.ContentTypeField;
+import org.apache.james.mime4j.dom.BinaryBody;
+import org.apache.james.mime4j.dom.Body;
+import org.apache.james.mime4j.dom.Entity;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.message.DefaultMessageBuilder;
+import org.apache.james.mime4j.dom.Multipart;
+import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.stream.Field;
 import org.jboss.resteasy.spi.HttpRequest;
 import org.jboss.resteasy.spi.ResteasyProviderFactory;
 import org.jboss.resteasy.util.CaseInsensitiveMap;
@@ -29,6 +29,8 @@ import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -73,7 +75,7 @@ public class MultipartInputImpl implements MultipartInput
 
    public void parse(InputStream is) throws IOException
    {
-      mimeMessage = new Message(addHeaderToHeadlessStream(is));
+      mimeMessage = new DefaultMessageBuilder().parseMessage(addHeaderToHeadlessStream(is));
       extractParts();
    }
 
@@ -104,11 +106,11 @@ public class MultipartInputImpl implements MultipartInput
    protected void extractParts() throws IOException
    {
       Multipart multipart = (Multipart) mimeMessage.getBody();
-      for (BodyPart bodyPart : multipart.getBodyParts())
+      for (Entity bodyPart : multipart.getBodyParts())
          parts.add(extractPart(bodyPart));
    }
 
-   protected InputPart extractPart(BodyPart bodyPart) throws IOException
+   protected InputPart extractPart(Entity bodyPart) throws IOException
    {
       return new PartImpl(bodyPart);
    }
@@ -116,12 +118,12 @@ public class MultipartInputImpl implements MultipartInput
    public class PartImpl implements InputPart
    {
 
-      private BodyPart bodyPart;
+      private Entity bodyPart;
       private MediaType contentType;
       private MultivaluedMap<String, String> headers = new CaseInsensitiveMap<String>();
       private boolean contentTypeFromMessage;
 
-      public PartImpl(BodyPart bodyPart)
+      public PartImpl(Entity bodyPart)
       {
          this.bodyPart = bodyPart;
          for (Field field : bodyPart.getHeader())
@@ -198,7 +200,9 @@ public class MultipartInputImpl implements MultipartInput
             {
                String charset = contentType.getParameters().get("charset");
                if (charset != null)
-                  charset = CharsetUtil.toJavaCharset(charset);
+                 try {
+                   charset = Charset.forName(charset).name();
+                 } catch (UnsupportedCharsetException e) {}
                inputStreamReader = charset == null ? new InputStreamReader(
                        inputStream)
                        : new InputStreamReader(inputStream, charset);
diff --git a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartRelatedInputImpl.java b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartRelatedInputImpl.java
index da7f0b7..3c2d498 100644
--- a/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartRelatedInputImpl.java
+++ b/src/main/java/org/jboss/resteasy/plugins/providers/multipart/MultipartRelatedInputImpl.java
@@ -8,9 +8,9 @@ import java.util.Map;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.Providers;
 
-import org.apache.james.mime4j.field.ContentTypeField;
-import org.apache.james.mime4j.field.FieldName;
-import org.apache.james.mime4j.message.BodyPart;
+import org.apache.james.mime4j.dom.field.ContentTypeField;
+import org.apache.james.mime4j.dom.field.FieldName;
+import org.apache.james.mime4j.dom.Entity;
 
 /**
  * Implements {@link MultipartRelatedInput} by extending
@@ -43,7 +43,7 @@ public class MultipartRelatedInputImpl extends MultipartInputImpl implements
 	}
 
 	@Override
-	protected InputPart extractPart(BodyPart bodyPart) throws IOException {
+	protected InputPart extractPart(Entity bodyPart) throws IOException {
 		InputPart inputPart = super.extractPart(bodyPart);
 		relatedMap
 				.put(inputPart.getHeaders().getFirst("Content-ID"), inputPart);
-- 
1.7.8.6

