james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r769884 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java main/java/org/apache/james/mime4j/parser/MimeStreamParser.java test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
Date Wed, 29 Apr 2009 19:15:05 GMT
Author: rdonkin
Date: Wed Apr 29 19:15:04 2009
New Revision: 769884

URL: http://svn.apache.org/viewvc?rev=769884&view=rev
Log:
Allow headless parsing from Message MIME4J-128 https://issues.apache.org/jira/browse/MIME4J-128

Added:
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
  (with props)
Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeStreamParser.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java?rev=769884&r1=769883&r2=769884&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeEntityConfig.java
Wed Apr 29 19:15:04 2009
@@ -32,6 +32,7 @@
     private int maxHeaderCount;
     private long maxContentLen;
     private boolean countLineNumbers;
+    private String defaultContentType;
     
     public MimeEntityConfig() {
         this.maximalBodyDescriptor = false;
@@ -159,6 +160,31 @@
         return countLineNumbers;
     }
 
+    /**
+     * Returns the value of the default content type. 
+     * When not null, indicates that the parsing should be headless.
+     * 
+     * @return default content type when parsing headless,
+     * null otherwise
+     * @see MimeStreamParser#parse(java.io.InputStream)
+     */
+    public String getDefaultContentType() {
+        return defaultContentType;
+    }
+
+    /**
+     * Defines a default content type. 
+     * When not null, indicates that the parsing should be headless.
+     * 
+     * @param defaultContentType
+     *            value of the default content type when parsing headless,
+     *            null otherwise
+     * @see MimeStreamParser#parse(java.io.InputStream)
+     */
+    public void setDefaultContentType(String defaultContentType) {
+        this.defaultContentType = defaultContentType;
+    }
+
     @Override
     public MimeEntityConfig clone() {
         try {

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeStreamParser.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeStreamParser.java?rev=769884&r1=769883&r2=769884&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeStreamParser.java
(original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/parser/MimeStreamParser.java
Wed Apr 29 19:15:04 2009
@@ -19,11 +19,14 @@
 
 package org.apache.james.mime4j.parser;
 
-import org.apache.james.mime4j.MimeException;
-import org.apache.james.mime4j.descriptor.BodyDescriptor;
-
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.SequenceInputStream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.descriptor.BodyDescriptor;
+import org.apache.james.mime4j.field.FieldName;
 
 /**
  * <p>
@@ -43,18 +46,18 @@
 
     private ContentHandler handler = null;
     private boolean contentDecoding;
+    private final MimeEntityConfig mimeEntityConfig;
     
     private final MimeTokenStream mimeTokenStream;
 
     public MimeStreamParser(final MimeEntityConfig config) {
         super();
-        MimeEntityConfig localConfig;
         if (config != null) {
-            localConfig = config.clone();
+            mimeEntityConfig = config.clone();
         } else {
-            localConfig = new MimeEntityConfig();
+            mimeEntityConfig = new MimeEntityConfig();
         }
-        this.mimeTokenStream = new MimeTokenStream(localConfig);
+        this.mimeTokenStream = new MimeTokenStream(mimeEntityConfig);
         this.contentDecoding = false;
     }
     
@@ -79,14 +82,24 @@
     }
 
     /**
-     * Parses a stream of bytes containing a MIME message.
+     * Parses a stream of bytes containing a MIME message. If the mime config of this
+     * object contains a not null defaultContentType
+     * ({@link MimeEntityConfig#getDefaultContentType()}) a headless parsing is performed.
      * 
      * @param is the stream to parse.
      * @throws MimeException if the message can not be processed
      * @throws IOException on I/O errors.
      */
     public void parse(InputStream is) throws MimeException, IOException {
-        mimeTokenStream.parse(is);
+        boolean headless = mimeEntityConfig.getDefaultContentType() != null;
+        InputStream inputStream = is;
+        if (headless) {
+            ByteArrayInputStream headerInputStream = new ByteArrayInputStream(
+                    (FieldName.CONTENT_TYPE + ": " + mimeEntityConfig.getDefaultContentType()
+                            + "\r\n\r\n").getBytes("iso-8859-1"));
+            inputStream = new SequenceInputStream(headerInputStream, is);
+        }
+        mimeTokenStream.parse(inputStream);
         OUTER: for (;;) {
             int state = mimeTokenStream.getState();
             switch (state) {

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java?rev=769884&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
Wed Apr 29 19:15:04 2009
@@ -0,0 +1,60 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, 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.                                           *
+ ****************************************************************/
+
+package org.apache.james.mime4j.message;
+
+import java.io.ByteArrayInputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.james.mime4j.field.ContentTypeField;
+import org.apache.james.mime4j.field.FieldName;
+import org.apache.james.mime4j.parser.MimeEntityConfig;
+
+public class MessageHeadlessParserTest extends TestCase {
+
+	public void testMultipartFormContent() throws Exception {
+		String contentType = "multipart/form-data; boundary=foo";
+		String headlessContent = "\r\n"
+				+ "--foo\r\nContent-Disposition: form-data; name=\"field01\""
+				+ "\r\n"
+				+ "\r\n"
+				+ "this stuff\r\n"
+				+ "--foo\r\n"
+				+ "Content-Disposition: form-data; name=\"field02\"\r\n"
+				+ "\r\n"
+				+ "that stuff\r\n"
+				+ "--foo\r\n"
+				+ "Content-Disposition: form-data; name=\"field03\"; filename=\"mypic.jpg\"\r\n"
+				+ "Content-Type: image/jpeg\r\n" + "\r\n"
+				+ "all kind of stuff\r\n" + "--foo--\r\n";
+
+		MimeEntityConfig mimeEntityConfig = new MimeEntityConfig();
+		mimeEntityConfig.setDefaultContentType(contentType);
+		Message message = new Message(new ByteArrayInputStream(headlessContent
+				.getBytes("UTF-8")), mimeEntityConfig);
+		assertEquals("multipart/form-data", message.getMimeType());
+		assertEquals(1, message.getHeader().getFields().size());
+		ContentTypeField contentTypeField = ((ContentTypeField) message
+				.getHeader().getField(FieldName.CONTENT_TYPE));
+		assertEquals("foo", contentTypeField.getBoundary());
+		Multipart multipart = (Multipart) message.getBody();
+		assertEquals(3, multipart.getCount());
+	}
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/message/MessageHeadlessParserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message