james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1086697 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/stream/MimeEntity.java test/java/org/apache/james/mime4j/stream/MimeEntityTest.java
Date Tue, 29 Mar 2011 19:33:58 GMT
Author: olegk
Date: Tue Mar 29 19:33:58 2011
New Revision: 1086697

URL: http://svn.apache.org/viewvc?rev=1086697&view=rev
Log:
MIME4J-165: added test case for skipping unwanted fields

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/MimeEntityTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java?rev=1086697&r1=1086696&r2=1086697&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/MimeEntity.java Tue
Mar 29 19:33:58 2011
@@ -38,18 +38,18 @@ class MimeEntity extends AbstractEntity 
 
     private final LineNumberSource lineSource;
     private final BufferedLineReaderInputStream inbuffer;
-    
+
     private RecursionMode recursionMode;
     private MimeBoundaryInputStream currentMimePartStream;
     private LineReaderInputStreamAdaptor dataStream;
-    
+
     private byte[] tmpbuf;
-    
+
     MimeEntity(
             LineNumberSource lineSource,
             InputStream instream,
             MimeEntityConfig config,
-            EntityState startState, 
+            EntityState startState,
             EntityState endState,
             DecodeMonitor monitor,
             FieldBuilder fieldBuilder,
@@ -69,7 +69,7 @@ class MimeEntity extends AbstractEntity 
             LineNumberSource lineSource,
             InputStream instream,
             MimeEntityConfig config,
-            EntityState startState, 
+            EntityState startState,
             EntityState endState,
             MutableBodyDescriptor body) {
         this(lineSource, instream, config, startState, endState,
@@ -82,8 +82,8 @@ class MimeEntity extends AbstractEntity 
             InputStream instream,
             MimeEntityConfig config,
             MutableBodyDescriptor body) {
-        this(lineSource, instream, config, 
-                EntityState.T_START_MESSAGE, EntityState.T_END_MESSAGE, 
+        this(lineSource, instream, config,
+                EntityState.T_START_MESSAGE, EntityState.T_END_MESSAGE,
                 config.isStrictParsing() ? DecodeMonitor.STRICT : DecodeMonitor.SILENT,
                 new DefaultFieldBuilder(config.getMaxHeaderLen()), body);
     }
@@ -91,8 +91,19 @@ class MimeEntity extends AbstractEntity 
     MimeEntity(
             LineNumberSource lineSource,
             InputStream instream,
+            FieldBuilder fieldBuilder,
+            MutableBodyDescriptor body) {
+        this(lineSource, instream, new MimeEntityConfig(),
+                EntityState.T_START_MESSAGE, EntityState.T_END_MESSAGE,
+                DecodeMonitor.SILENT,
+                fieldBuilder, body);
+    }
+
+    MimeEntity(
+            LineNumberSource lineSource,
+            InputStream instream,
             MutableBodyDescriptor body) {
-        this(lineSource, instream, new MimeEntityConfig(), 
+        this(lineSource, instream, new MimeEntityConfig(),
                 EntityState.T_START_MESSAGE, EntityState.T_END_MESSAGE,
                 DecodeMonitor.SILENT,
                 new DefaultFieldBuilder(-1), body);
@@ -107,9 +118,9 @@ class MimeEntity extends AbstractEntity 
     }
 
     public void stop() {
-    	this.inbuffer.truncate();
+        this.inbuffer.truncate();
     }
-    
+
     @Override
     protected int getLineNumber() {
         if (lineSource == null)
@@ -117,12 +128,12 @@ class MimeEntity extends AbstractEntity 
         else
             return lineSource.getLineNumber();
     }
-    
+
     @Override
     protected LineReaderInputStream getDataStream() {
         return dataStream;
     }
-    
+
     public EntityStateMachine advance() throws IOException, MimeException {
         switch (state) {
         case T_START_MESSAGE:
@@ -142,7 +153,7 @@ class MimeEntity extends AbstractEntity 
             } else if (MimeUtil.isMultipart(mimeType)) {
                 state = EntityState.T_START_MULTIPART;
                 clearMimePartStream();
-            } else if (recursionMode != RecursionMode.M_NO_RECURSE 
+            } else if (recursionMode != RecursionMode.M_NO_RECURSE
                     && MimeUtil.isMessage(mimeType)) {
                 state = EntityState.T_BODY;
                 return nextMessage();
@@ -152,20 +163,20 @@ class MimeEntity extends AbstractEntity 
             break;
         case T_START_MULTIPART:
             if (dataStream.isUsed()) {
-                advanceToBoundary();            
+                advanceToBoundary();
                 state = EntityState.T_END_MULTIPART;
                 break;
             } else {
                 createMimePartStream();
                 state = EntityState.T_PREAMBLE;
-                
+
                 if (!currentMimePartStream.isEmptyStream()) break;
                 // continue to next state
             }
         case T_PREAMBLE:
-        	// removed specific code. Fallback to T_IN_BODYPART that
-        	// better handle missing parts.
-        	// Removed the T_IN_BODYPART state (always use T_PREAMBLE)
+            // removed specific code. Fallback to T_IN_BODYPART that
+            // better handle missing parts.
+            // Removed the T_IN_BODYPART state (always use T_PREAMBLE)
             advanceToBoundary();
             if (currentMimePartStream.eof() && !currentMimePartStream.isLastPart())
{
                 monitor(Event.MIME_BODY_PREMATURE_END);
@@ -198,7 +209,7 @@ class MimeEntity extends AbstractEntity 
 
     private void createMimePartStream() throws MimeException, IOException {
         String boundary = body.getBoundary();
-    	// TODO move the following lines inside the MimeBoundaryInputStream constructor
+        // TODO move the following lines inside the MimeBoundaryInputStream constructor
         int bufferSize = 2 * boundary.length();
         if (bufferSize < 4096) {
             bufferSize = 4096;
@@ -214,14 +225,14 @@ class MimeEntity extends AbstractEntity 
                 currentMimePartStream,
                 config.getMaxLineLen());
     }
-    
+
     private void clearMimePartStream() {
         currentMimePartStream = null;
         dataStream = new LineReaderInputStreamAdaptor(
                 inbuffer,
-                config.getMaxLineLen()); 
+                config.getMaxLineLen());
     }
-    
+
     private void advanceToBoundary() throws IOException {
         if (!dataStream.eof()) {
             if (tmpbuf == null) {
@@ -232,7 +243,7 @@ class MimeEntity extends AbstractEntity 
             }
         }
     }
-    
+
     private EntityStateMachine nextMessage() {
         // optimize nesting of streams returning the "lower" stream instead of
         // always return dataStream (that would add a LineReaderInputStreamAdaptor in the
chain)
@@ -244,27 +255,27 @@ class MimeEntity extends AbstractEntity 
     private InputStream decodedStream(InputStream instream) {
         String transferEncoding = body.getTransferEncoding();
         if (MimeUtil.isBase64Encoding(transferEncoding)) {
-            instream = new Base64InputStream(instream, monitor);                    
+            instream = new Base64InputStream(instream, monitor);
         } else if (MimeUtil.isQuotedPrintableEncoded(transferEncoding)) {
             instream = new QuotedPrintableInputStream(instream, monitor);
         }
         return instream;
     }
-    
+
     private EntityStateMachine nextMimeEntity() {
-    	return nextMimeEntity(EntityState.T_START_BODYPART, EntityState.T_END_BODYPART, currentMimePartStream);
+        return nextMimeEntity(EntityState.T_START_BODYPART, EntityState.T_END_BODYPART, currentMimePartStream);
     }
-    
+
     private EntityStateMachine nextMimeEntity(EntityState startState, EntityState endState,
InputStream instream) {
         if (recursionMode == RecursionMode.M_RAW) {
             RawEntity message = new RawEntity(instream);
             return message;
         } else {
             MimeEntity mimeentity = new MimeEntity(
-                    lineSource, 
+                    lineSource,
                     instream,
                     config,
-                    startState, 
+                    startState,
                     endState,
                     monitor,
                     fieldBuilder,
@@ -273,7 +284,7 @@ class MimeEntity extends AbstractEntity 
             return mimeentity;
         }
     }
-    
+
     private InputStream getLimitedContentStream() {
         long maxContentLimit = config.getMaxContentLen();
         if (maxContentLimit >= 0) {
@@ -282,7 +293,7 @@ class MimeEntity extends AbstractEntity 
             return dataStream;
         }
     }
-    
+
     /**
      * @see org.apache.james.mime4j.stream.EntityStateMachine#getContentStream()
      */

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/MimeEntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/MimeEntityTest.java?rev=1086697&r1=1086696&r2=1086697&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/MimeEntityTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/MimeEntityTest.java
Tue Mar 29 19:33:58 2011
@@ -21,6 +21,7 @@ package org.apache.james.mime4j.stream;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.Locale;
 
 import junit.framework.TestCase;
 
@@ -35,7 +36,7 @@ import org.apache.james.mime4j.io.MaxLin
 public class MimeEntityTest extends TestCase {
 
     public void testSimpleEntity() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -45,14 +46,14 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream, new DefaultBodyDescriptor());
-        
-        
+
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -88,7 +89,7 @@ public class MimeEntityTest extends Test
             fail("IllegalStateException should have been thrown");
         } catch (IllegalStateException expected) {
         }
-        
+
         entity.advance();
         assertEquals(EntityState.T_BODY, entity.getState());
         assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
@@ -109,7 +110,7 @@ public class MimeEntityTest extends Test
     }
 
     public void testObsoleteSyntaxEntity() throws Exception {
-        String message = 
+        String message =
             "To 	: Road Runner <runner@example.org>\r\n" +
             "From	: Wile E. Cayote <wile@example.org>\r\n" +
             "Date  	:Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -121,14 +122,14 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream, new DefaultBodyDescriptor());
-        
-        
+
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -164,7 +165,7 @@ public class MimeEntityTest extends Test
             fail("IllegalStateException should have been thrown");
         } catch (IllegalStateException expected) {
         }
-        
+
         entity.advance();
         assertEquals(EntityState.T_BODY, entity.getState());
         assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
@@ -185,7 +186,7 @@ public class MimeEntityTest extends Test
     }
 
     public void testMultipartEntity() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -205,13 +206,13 @@ public class MimeEntityTest extends Test
             "Goodbye!";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
24); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
24);
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream, new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -242,10 +243,10 @@ public class MimeEntityTest extends Test
         entity.advance();
         assertEquals(EntityState.T_PREAMBLE, entity.getState());
         assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
-        
+
         EntityStateMachine p1 = entity.advance();
         assertNotNull(p1);
-        
+
         assertEquals(EntityState.T_START_BODYPART, p1.getState());
         p1.advance();
         assertEquals(EntityState.T_START_HEADER, p1.getState());
@@ -265,7 +266,7 @@ public class MimeEntityTest extends Test
 
         EntityStateMachine p2 = entity.advance();
         assertNotNull(p2);
-        
+
         assertEquals(EntityState.T_START_BODYPART, p2.getState());
         p2.advance();
         assertEquals(EntityState.T_START_HEADER, p2.getState());
@@ -293,9 +294,9 @@ public class MimeEntityTest extends Test
         entity.advance();
         assertEquals(EntityState.T_END_OF_STREAM, entity.getState());
     }
-    
+
     public void testRawEntity() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -315,15 +316,15 @@ public class MimeEntityTest extends Test
             "Goodbye!";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
24); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
24);
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream, new DefaultBodyDescriptor());
-        
+
         entity.setRecursionMode(RecursionMode.M_RAW);
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -351,14 +352,14 @@ public class MimeEntityTest extends Test
         assertEquals(EntityState.T_END_HEADER, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_MULTIPART, entity.getState());
-        
+
         entity.advance();
         assertEquals(EntityState.T_PREAMBLE, entity.getState());
         assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
-        
+
         EntityStateMachine p1 = entity.advance();
         assertNotNull(p1);
-        
+
         assertEquals(EntityState.T_RAW_ENTITY, p1.getState());
         assertNull(p1.getBodyDescriptor());
         assertNull(p1.getField());
@@ -371,7 +372,7 @@ public class MimeEntityTest extends Test
 
         EntityStateMachine p2 = entity.advance();
         assertNotNull(p2);
-        
+
         assertEquals(EntityState.T_RAW_ENTITY, p2.getState());
         assertNull(p2.getBodyDescriptor());
         assertNull(p2.getField());
@@ -397,7 +398,7 @@ public class MimeEntityTest extends Test
         MimeEntityConfig config = new MimeEntityConfig();
         config.setMaxLineLen(50);
 
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -408,15 +409,15 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen()); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen());
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream,
                 config,
                 new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance(); // advances to T_START_HEADER
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -433,9 +434,9 @@ public class MimeEntityTest extends Test
             assertTrue(expected.getCause() instanceof MaxLineLimitException);
         }
     }
-    
+
     public void testMaxHeaderLimitCheckFoldedLines() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -457,9 +458,9 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
         MimeEntityConfig config = new MimeEntityConfig();
         config.setMaxLineLen(100);
         config.setMaxHeaderLen(200);
@@ -468,7 +469,7 @@ public class MimeEntityTest extends Test
                 rawstream,
                 config,
                 new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -492,7 +493,7 @@ public class MimeEntityTest extends Test
         config.setMaxLineLen(50);
         config.setMaxHeaderLen(130);
 
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -506,15 +507,15 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen()); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12, config.getMaxLineLen());
+
         MimeEntity entity = new MimeEntity(
                 lineInput,
                 rawstream,
                 config,
                 new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -525,9 +526,9 @@ public class MimeEntityTest extends Test
         entity.advance();
         assertEquals(EntityState.T_END_HEADER, entity.getState());
     }
-    
+
     public void testMaxHeaderCount() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -553,9 +554,9 @@ public class MimeEntityTest extends Test
             "a very important message";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
         MimeEntityConfig config = new MimeEntityConfig();
         config.setMaxHeaderCount(20);
         MimeEntity entity = new MimeEntity(
@@ -563,11 +564,11 @@ public class MimeEntityTest extends Test
                 rawstream,
                 config,
                 new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
-        
+
         for (int i = 0; i < 20; i++) {
             entity.advance();
             assertEquals(EntityState.T_FIELD, entity.getState());
@@ -580,7 +581,7 @@ public class MimeEntityTest extends Test
     }
 
     public void testMaxContentLimitCheck() throws Exception {
-        String message = 
+        String message =
             "To: Road Runner <runner@example.org>\r\n" +
             "From: Wile E. Cayote <wile@example.org>\r\n" +
             "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
@@ -599,9 +600,9 @@ public class MimeEntityTest extends Test
             "DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS DoS\r\n";
         byte[] raw = message.getBytes("US-ASCII");
         ByteArrayInputStream instream = new ByteArrayInputStream(raw);
-        LineNumberInputStream lineInput = new LineNumberInputStream(instream); 
-        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12); 
-        
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
         MimeEntityConfig config = new MimeEntityConfig();
         config.setMaxContentLen(100);
         MimeEntity entity = new MimeEntity(
@@ -609,7 +610,7 @@ public class MimeEntityTest extends Test
                 rawstream,
                 config,
                 new DefaultBodyDescriptor());
-        
+
         assertEquals(EntityState.T_START_MESSAGE, entity.getState());
         entity.advance();
         assertEquals(EntityState.T_START_HEADER, entity.getState());
@@ -633,5 +634,59 @@ public class MimeEntityTest extends Test
         } catch (IOException expected) {
         }
     }
-    
+
+    public void testSkipFields() throws Exception {
+        String message =
+            "To: Road Runner <runner@example.org>\r\n" +
+            "From: Wile E. Cayote <wile@example.org>\r\n" +
+            "Date: Tue, 12 Feb 2008 17:34:09 +0000 (GMT)\r\n" +
+            "Subject: Mail\r\n" +
+            "Content-Type: text/plain\r\n" +
+            "\r\n" +
+            "a very important message";
+        byte[] raw = message.getBytes("US-ASCII");
+        ByteArrayInputStream instream = new ByteArrayInputStream(raw);
+        LineNumberInputStream lineInput = new LineNumberInputStream(instream);
+        BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(lineInput,
12);
+
+        DefaultFieldBuilder fieldBuilder = new DefaultFieldBuilder(-1) {
+
+            @Override
+            public RawField build() throws MimeException {
+                RawField raw = super.build();
+                String name = raw.getName().toLowerCase(Locale.US);
+                if (name.equals("content-type") || name.equals("subject")) {
+                    return raw;
+                } else {
+                    return null;
+                }
+            }
+
+        };
+
+        MimeEntity entity = new MimeEntity(
+                lineInput,
+                rawstream, fieldBuilder, new DefaultBodyDescriptor());
+
+
+        assertEquals(EntityState.T_START_MESSAGE, entity.getState());
+        entity.advance();
+        assertEquals(EntityState.T_START_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityState.T_FIELD, entity.getState());
+        assertEquals("Subject", entity.getField().getName());
+        assertEquals("Mail", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityState.T_FIELD, entity.getState());
+        assertEquals("Content-Type", entity.getField().getName());
+        assertEquals("text/plain", entity.getField().getBody());
+        entity.advance();
+        assertEquals(EntityState.T_END_HEADER, entity.getState());
+        entity.advance();
+        assertEquals(EntityState.T_BODY, entity.getState());
+        assertEquals("a very important message", IOUtils.toString(entity.getContentStream()));
+        entity.advance();
+        assertEquals(EntityState.T_END_MESSAGE, entity.getState());
+    }
+
 }



Mime
View raw message