james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1126712 [2/2] - in /james/mime4j/trunk: core/src/main/java/org/apache/james/mime4j/util/ dom/src/main/java/org/apache/james/mime4j/dom/field/ dom/src/main/java/org/apache/james/mime4j/field/ dom/src/main/java/org/apache/james/mime4j/messag...
Date Mon, 23 May 2011 20:20:45 GMT
Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MinimalBodyDescriptor.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MinimalBodyDescriptor.java?rev=1126712&r1=1126711&r2=1126712&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MinimalBodyDescriptor.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MinimalBodyDescriptor.java
Mon May 23 20:20:44 2011
@@ -20,13 +20,15 @@
 package org.apache.james.mime4j.message;
 
 import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 
 import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.codec.DecodeMonitor;
+import org.apache.james.mime4j.dom.field.ContentLengthField;
 import org.apache.james.mime4j.dom.field.ContentTransferEncodingField;
 import org.apache.james.mime4j.dom.field.ContentTypeField;
+import org.apache.james.mime4j.dom.field.FieldName;
+import org.apache.james.mime4j.field.ContentLengthFieldImpl;
 import org.apache.james.mime4j.field.ContentTransferEncodingFieldImpl;
 import org.apache.james.mime4j.field.ContentTypeFieldImpl;
 import org.apache.james.mime4j.stream.BodyDescriptor;
@@ -63,11 +65,11 @@ public class MinimalBodyDescriptor imple
     private String mimeType = DEFAULT_MIME_TYPE;
     private String boundary = null;
     private String charset = US_ASCII;
-    private String transferEncoding = "7bit";
     private Map<String, String> parameters = new HashMap<String, String>();
-    private boolean contentTypeSet;
-    private boolean contentTransferEncSet;
-    private long contentLength = -1;
+
+    private ContentTypeField contentTypeField;
+    private ContentLengthField contentLengthField;
+    private ContentTransferEncodingField contentTransferEncodingField;
     
     /**
      * Creates a new root <code>BodyDescriptor</code> instance.
@@ -110,74 +112,55 @@ public class MinimalBodyDescriptor imple
      * @param field the MIME field.
      */
     public void addField(Field field) throws MimeException {
-        String name = field.getName().toLowerCase(Locale.US);
-        
-        if (name.equals("content-transfer-encoding") && !contentTransferEncSet) {
+        String name = field.getName();
+        if (name.equalsIgnoreCase(FieldName.CONTENT_TRANSFER_ENCODING)&& contentTransferEncodingField
== null) {
             parseContentTransferEncoding(field);
-        } else if (name.equals("content-length") && contentLength == -1) {
+        } else if (name.equalsIgnoreCase(FieldName.CONTENT_LENGTH) && contentLengthField
== null) {
             parseContentLength(field);
-        } else if (name.equals("content-type") && !contentTypeSet) {
+        } else if (name.equalsIgnoreCase(FieldName.CONTENT_TYPE) && contentTypeField
== null) {
             parseContentType(field);
         }
     }
 
     private void parseContentTransferEncoding(Field field) throws MimeException {
-        contentTransferEncSet = true;
-        ContentTransferEncodingField f;
         if (field instanceof ContentTransferEncodingField) {
-            f = (ContentTransferEncodingField) field;
+            contentTransferEncodingField = (ContentTransferEncodingField) field;
         } else {
-            f = ContentTransferEncodingFieldImpl.PARSER.parse(
+            contentTransferEncodingField = ContentTransferEncodingFieldImpl.PARSER.parse(
                     field.getName(), field.getBody(), field.getRaw(), monitor);
         }
-        transferEncoding = f.getEncoding();
     }
 
     private void parseContentLength(Field field) throws MimeException {
-        String value = field.getBody();
-        if (value != null) {
-            try {
-                long v = Long.parseLong(value);
-                if (v < 0) {
-                    if (monitor.warn("Negative content length: " + value, 
-                            "ignoring Content-Length header")) {
-                        throw new MimeException("Negative Content-Length header: " + value);
-                    }
-                } else {
-                    contentLength = v;
-                }
-            } catch (NumberFormatException e) {
-                if (monitor.warn("Invalid content length: " + value, 
-                        "ignoring Content-Length header")) {
-                    throw new MimeException("Invalid Content-Length header: " + value);
-                }
-            }
+        if (field instanceof ContentLengthField) {
+            contentLengthField = (ContentLengthField) field;
+        } else {
+            contentLengthField = ContentLengthFieldImpl.PARSER.parse(
+                    field.getName(), field.getBody(), field.getRaw(), monitor);
         }
     }
 
     private void parseContentType(Field field) throws MimeException {
-        contentTypeSet = true;
-        ContentTypeField f;
         if (field instanceof ContentTypeField) {
-            f = (ContentTypeField) field;
+            contentTypeField = (ContentTypeField) field;
         } else {
-            f = ContentTypeFieldImpl.PARSER.parse(
+            contentTypeField = ContentTypeFieldImpl.PARSER.parse(
                     field.getName(), field.getBody(), field.getRaw(), monitor);
         }
-        String mimetype = f.getMimeType();
+        String mimetype = contentTypeField.getMimeType();
         if (mimetype != null) {
             mimeType = mimetype;
-            mediaType = f.getMediaType();
-            subType = f.getSubType();
+            mediaType = contentTypeField.getMediaType();
+            subType = contentTypeField.getSubType();
         }
         if (MimeUtil.isMultipart(mimeType)) {
-            boundary = f.getBoundary();
+            boundary = contentTypeField.getBoundary();
         }
-        charset = f.getCharset();
+        charset = contentTypeField.getCharset();
         if (charset == null && MEDIA_TYPE_TEXT.equalsIgnoreCase(mediaType)) {
             charset = US_ASCII;
         }
-        parameters.putAll(f.getParameters());
+        parameters.putAll(contentTypeField.getParameters());
         parameters.remove("charset");
         parameters.remove("boundary");
     }
@@ -224,7 +207,8 @@ public class MinimalBodyDescriptor imple
      * @return transferEncoding
      */
     public String getTransferEncoding() {
-        return transferEncoding;
+        return contentTransferEncodingField != null ? contentTransferEncodingField.getEncoding()
: 
+            MimeUtil.ENC_7BIT;
     }
     
     @Override
@@ -233,7 +217,7 @@ public class MinimalBodyDescriptor imple
     }
 
     public long getContentLength() {
-        return contentLength;
+        return contentLengthField != null ? contentLengthField.getContentLength() : -1;
     }
 
     public String getMediaType() {

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/MaximalBodyDescriptorTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/MaximalBodyDescriptorTest.java?rev=1126712&r1=1126711&r2=1126712&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/MaximalBodyDescriptorTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/MaximalBodyDescriptorTest.java
Mon May 23 20:20:44 2011
@@ -20,9 +20,10 @@
 package org.apache.james.mime4j.message;
 
 import java.io.ByteArrayInputStream;
+import java.text.SimpleDateFormat;
+import java.util.TimeZone;
 
 import org.apache.james.mime4j.ExampleMail;
-import org.apache.james.mime4j.dom.datetime.DateTime;
 import org.apache.james.mime4j.field.DefaultFieldParser;
 import org.apache.james.mime4j.message.MaximalBodyDescriptor;
 import org.apache.james.mime4j.message.MaximalBodyDescriptorFactory;
@@ -55,21 +56,18 @@ public class MaximalBodyDescriptorTest e
         MaximalBodyDescriptor descriptor = describe(ExampleMail.RFC822_SIMPLE_BYTES);
         assertEquals(1, descriptor.getMimeMajorVersion());
         assertEquals(0, descriptor.getMimeMinorVersion());
-        assertNull(descriptor.getMimeVersionParseException());
     }
     
     public void testMimeVersion() throws Exception {
         MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES);
         assertEquals(2, descriptor.getMimeMajorVersion());
         assertEquals(4, descriptor.getMimeMinorVersion());
-        assertNull(descriptor.getMimeVersionParseException());
     }
     
     public void testContentId() throws Exception {
         MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_8859_BYTES);
         assertEquals(1, descriptor.getMimeMajorVersion());
         assertEquals(0, descriptor.getMimeMinorVersion());
-        assertNull(descriptor.getMimeVersionParseException());
         assertEquals(ExampleMail.CONTENT_ID, descriptor.getContentId());
     }
 
@@ -77,7 +75,6 @@ public class MaximalBodyDescriptorTest e
         MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_8859_BYTES);
         assertEquals(1, descriptor.getMimeMajorVersion());
         assertEquals(0, descriptor.getMimeMinorVersion());
-        assertNull(descriptor.getMimeVersionParseException());
         assertEquals(ExampleMail.CONTENT_DESCRIPTION, descriptor.getContentDescription());
     }
     
@@ -85,7 +82,6 @@ public class MaximalBodyDescriptorTest e
         MaximalBodyDescriptor descriptor = describe(ExampleMail.ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES);
         assertEquals(4, descriptor.getMimeMajorVersion());
         assertEquals(1, descriptor.getMimeMinorVersion());
-        assertNull(descriptor.getMimeVersionParseException());
     }
     
     public void testContentDispositionType() throws Exception {
@@ -116,9 +112,13 @@ public class MaximalBodyDescriptorTest e
         assertNotNull(descriptor.getContentDispositionParameters());
         assertEquals(5, descriptor.getContentDispositionParameters().size());
         assertEquals("blob.png", descriptor.getContentDispositionFilename());
-        assertEquals(new DateTime("2008", 6, 21, 15, 32, 18, 0), descriptor.getContentDispositionModificationDate());
-        assertEquals(new DateTime("2008", 6, 20, 10, 15, 9, 0), descriptor.getContentDispositionCreationDate());
-        assertEquals(new DateTime("2008", 6, 22, 12, 8, 56, 0), descriptor.getContentDispositionReadDate());
+        
+        SimpleDateFormat dateparser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        dateparser.setTimeZone(TimeZone.getTimeZone("GMT"));
+        
+        assertEquals(dateparser.parse("2008-06-21 15:32:18"), descriptor.getContentDispositionModificationDate());
+        assertEquals(dateparser.parse("2008-06-20 10:15:09"), descriptor.getContentDispositionCreationDate());
+        assertEquals(dateparser.parse("2008-06-22 12:08:56"), descriptor.getContentDispositionReadDate());
         assertEquals(10234, descriptor.getContentDispositionSize());
     }
     



Mime
View raw message