james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwiederk...@apache.org
Subject svn commit: r733322 - in /james/mime4j/trunk: examples/src/java/org/apache/james/mime4j/samples/transform/ src/main/java/org/apache/james/mime4j/field/ src/main/java/org/apache/james/mime4j/message/ src/test/java/org/apache/james/mime4j/field/
Date Sat, 10 Jan 2009 19:45:49 GMT
Author: mwiederkehr
Date: Sat Jan 10 11:45:48 2009
New Revision: 733322

URL: http://svn.apache.org/viewvc?rev=733322&view=rev
Log:
Field.parse(String) throws MimeException again; also added method Field.parse(String, String).
Fixes MIME4J-90.

Modified:
    james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java
    james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/SimpleContentHandler.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java

Modified: james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java?rev=733322&r1=733321&r2=733322&view=diff
==============================================================================
--- james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
(original)
+++ james/mime4j/trunk/examples/src/java/org/apache/james/mime4j/samples/transform/TransformMessage.java
Sat Jan 10 11:45:48 2009
@@ -111,12 +111,12 @@
 
         // Create a unique message ID for the new message.
         Header header = message.getHeader();
-        Field messageId = Field.parse("Message-ID: "
-                + MimeUtil.createUniqueMessageId(HOSTNAME));
+        Field messageId = Field.parse("Message-ID", MimeUtil
+                .createUniqueMessageId(HOSTNAME));
         header.setField(messageId);
 
         // Set a new subject; note the usage of setField instead of addField
-        header.setField(Field.parse("Subject: Transformed message"));
+        header.setField(Field.parse("Subject", "Transformed message"));
 
         return message;
     }
@@ -127,10 +127,10 @@
      */
     private static Message createTemplate() throws IOException {
         Header header = new Header();
-        header.addField(Field
-                .parse("Content-Type: multipart/mixed;\r\n\tboundary=\""
+        header.addField(Field.parse("Content-Type",
+                "multipart/mixed;\r\n\tboundary=\""
                         + MimeUtil.createUniqueBoundary() + "\""));
-        header.addField(Field.parse("Subject: Template message"));
+        header.addField(Field.parse("Subject", "Template message"));
 
         Multipart multipart = new Multipart("mixed");
 
@@ -155,7 +155,7 @@
      */
     private static BodyPart createTextPart(String text) {
         Header header = new Header();
-        header.addField(Field.parse("Content-Type: text/plain"));
+        header.addField(Field.parse("Content-Type", "text/plain"));
 
         Body body = new BodyFactory().textBody(text);
 
@@ -172,8 +172,8 @@
     private static BodyPart createRandomBinaryPart(int numberOfBytes)
             throws IOException {
         Header header = new Header();
-        header.addField(Field.parse("Content-Type: application/octet-stream"));
-        header.addField(Field.parse("Content-Transfer-Encoding: base64"));
+        header.addField(Field.parse("Content-Type", "application/octet-stream"));
+        header.addField(Field.parse("Content-Transfer-Encoding", "base64"));
 
         byte[] data = new byte[numberOfBytes];
         new Random().nextBytes(data);

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java?rev=733322&r1=733321&r2=733322&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Field.java Sat Jan 10 11:45:48
2009
@@ -22,6 +22,8 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.james.mime4j.MimeException;
+
 /**
  * The base class of all field classes.
  *
@@ -80,9 +82,9 @@
      * 
      * @param raw the string to parse.
      * @return a <code>Field</code> instance.
-     * @throws IllegalArgumentException on parse errors.
+     * @throws MimeException if the raw string cannot be split into field name and body.
      */
-    public static Field parse(final String raw) {
+    public static Field parse(final String raw) throws MimeException {
         
         /*
          * Unfold the field.
@@ -94,11 +96,7 @@
          */
         final Matcher fieldMatcher = fieldNamePattern.matcher(unfolded);
         if (!fieldMatcher.find()) {
-            // We don't have to throw a MimeException because this error can
-            // never happen when Field.parse() is called by a ContentHandler.
-            // org.apache.james.mime4j.parser.AbstractEntity drops header
-            // lines that do not start with a valid field name.
-            throw new IllegalArgumentException("Invalid field in string");
+            throw new MimeException("Invalid field in string");
         }
         final String name = fieldMatcher.group(1);
         
@@ -109,7 +107,39 @@
         
         return parser.parse(name, body, raw);
     }
-    
+
+    /**
+     * Parses the given field name and field body strings and returns an
+     * instance of the <code>Field</code> class. The type of the class
+     * returned depends on the field name (see {@link #parse(String)}.
+     * <p>
+     * This method is convenient for creating or manipulating messages because
+     * contrary to {@link #parse(String)} it does not throw a
+     * {@link MimeException}.
+     * <p>
+     * Note that this method does not fold the header field; the specified field
+     * body should already have been folded into multiple lines prior to calling
+     * this method if folding is desired.
+     * 
+     * @param name
+     *            the field name.
+     * @param body
+     *            the field body (a.k.a value).
+     * @return a <code>Field</code> instance.
+     */
+    public static Field parse(String name, String body) {
+        if (body.length() > 0 && body.charAt(0) == ' ') {
+            body = body.substring(1);
+        }
+
+        String raw = name + ": " + body;
+
+        // Unfold body
+        body = body.replaceAll("\r|\n", "");
+
+        return parser.parse(name, body, raw);
+    }
+
     /**
      * Gets the default parser used to parse fields.
      * @return the default field parser

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java?rev=733322&r1=733321&r2=733322&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/Header.java Sat Jan 10
11:45:48 2009
@@ -92,7 +92,7 @@
                 parser.stop();
             }
             @Override
-            public void field(String fieldData) {
+            public void field(String fieldData) throws MimeException {
                 addField(Field.parse(fieldData));
             }
         });

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/SimpleContentHandler.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/SimpleContentHandler.java?rev=733322&r1=733321&r2=733322&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/SimpleContentHandler.java
(original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/SimpleContentHandler.java
Sat Jan 10 11:45:48 2009
@@ -19,6 +19,7 @@
 
 package org.apache.james.mime4j.message;
 
+import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.decoder.Base64InputStream;
 import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
 import org.apache.james.mime4j.descriptor.BodyDescriptor;
@@ -74,7 +75,7 @@
      * @see org.apache.james.mime4j.parser.AbstractContentHandler#field(java.lang.String)
      */
     @Override
-    public final void field(String fieldData) {
+    public final void field(String fieldData) throws MimeException {
         currHeader.addField(Field.parse(fieldData));
     }
 

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java?rev=733322&r1=733321&r2=733322&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java Sat Jan
10 11:45:48 2009
@@ -19,6 +19,7 @@
 
 package org.apache.james.mime4j.field;
 
+import org.apache.james.mime4j.MimeException;
 import org.apache.james.mime4j.field.ContentTransferEncodingField;
 import org.apache.james.mime4j.field.ContentTypeField;
 import org.apache.james.mime4j.field.Field;
@@ -45,9 +46,8 @@
         
         try {
             f = Field.parse("Yada yada yada");
-            fail("IllegalArgumentException not thrown when using an invalid "
-                    + "field");
-        } catch (IllegalArgumentException e) {
+            fail("MimeException not thrown when using an invalid field");
+        } catch (MimeException e) {
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message