You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ol...@apache.org on 2008/09/22 19:22:22 UTC

svn commit: r697902 - in /james/mime4j/trunk/src/main/java/org/apache/james/mime4j: field/Field.java message/Header.java message/MessageBuilder.java

Author: olegk
Date: Mon Sep 22 10:22:22 2008
New Revision: 697902

URL: http://svn.apache.org/viewvc?rev=697902&view=rev
Log:
MIME4J-73: MessageBuilder declares to throw MimeException in every method it implements from ContentHandler
Contributed by Markus Wiederkehr <markus.wiederkehr at gmail.com>

Modified:
    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/MessageBuilder.java

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=697902&r1=697901&r2=697902&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 Mon Sep 22 10:22:22 2008
@@ -1,194 +1,194 @@
-/****************************************************************
- * 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.field;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.james.mime4j.MimeException;
-
-/**
- * The base class of all field classes.
- *
- * 
- * @version $Id: Field.java,v 1.6 2004/10/25 07:26:46 ntherning Exp $
- */
-public abstract class Field {
-    public static final String SENDER = "Sender";
-    public static final String FROM = "From";
-    public static final String TO = "To";
-    public static final String CC = "Cc";
-    public static final String BCC = "Bcc";
-    public static final String REPLY_TO = "Reply-To";
-    public static final String RESENT_SENDER = "Resent-Sender";
-    public static final String RESENT_FROM = "Resent-From";
-    public static final String RESENT_TO = "Resent-To";
-    public static final String RESENT_CC = "Resent-Cc";
-    public static final String RESENT_BCC = "Resent-Bcc";
-
-    public static final String DATE = "Date";
-    public static final String RESENT_DATE = "Resent-Date";
-
-    public static final String SUBJECT = "Subject";
-    public static final String CONTENT_TYPE = "Content-Type";
-    public static final String CONTENT_TRANSFER_ENCODING = 
-                                        "Content-Transfer-Encoding";
-    
-    private static final String FIELD_NAME_PATTERN = 
-        "^([\\x21-\\x39\\x3b-\\x7e]+)[ \t]*:";
-    private static final Pattern fieldNamePattern = 
-        Pattern.compile(FIELD_NAME_PATTERN);
-        
-    private static final DefaultFieldParser parser = new DefaultFieldParser();
-    
-    private final String name;
-    private final String body;
-    private final String raw;
-    
-    protected Field(final String name, final String body, final String raw) {
-        this.name = name;
-        this.body = body;
-        this.raw = raw;
-    }
-    
-    /**
-     * Parses the given string and returns an instance of the 
-     * <code>Field</code> class. The type of the class returned depends on
-     * the field name:
-     * <table>
-     *      <tr>
-     *          <td><em>Field name</em></td><td><em>Class returned</em></td>
-     *          <td>Content-Type</td><td>org.apache.james.mime4j.field.ContentTypeField</td>
-     *          <td>other</td><td>org.apache.james.mime4j.field.UnstructuredField</td>
-     *      </tr>
-     * </table>
-     * 
-     * @param raw the string to parse.
-     * @return a <code>Field</code> instance.
-     * @throws IllegalArgumentException on parse errors.
-     */
-    public static Field parse(final String raw) throws MimeException {
-        
-        /*
-         * Unfold the field.
-         */
-        final String unfolded = raw.replaceAll("\r|\n", "");
-        
-        /*
-         * Split into name and value.
-         */
-        final Matcher fieldMatcher = fieldNamePattern.matcher(unfolded);
-        if (!fieldMatcher.find()) {
-            throw new MimeException("Invalid field in string");
-        }
-        final String name = fieldMatcher.group(1);
-        
-        String body = unfolded.substring(fieldMatcher.end());
-        if (body.length() > 0 && body.charAt(0) == ' ') {
-            body = body.substring(1);
-        }
-        
-        return parser.parse(name, body, raw);
-    }
-    
-    /**
-     * Gets the default parser used to parse fields.
-     * @return the default field parser
-     */
-    public static DefaultFieldParser getParser() {
-        return parser;
-    }
-    
-    /**
-     * Gets the name of the field (<code>Subject</code>, 
-     * <code>From</code>, etc).
-     * 
-     * @return the field name.
-     */
-    public String getName() {
-        return name;
-    }
-    
-    /**
-     * Gets the original raw field string.
-     * 
-     * @return the original raw field string.
-     */
-    public String getRaw() {
-        return raw;
-    }
-    
-    /**
-     * Gets the unfolded, unparsed and possibly encoded (see RFC 2047) field 
-     * body string.
-     * 
-     * @return the unfolded unparsed field body string.
-     */
-    public String getBody() {
-        return body;
-    }
-    
-    /**
-     * Determines if this is a <code>Content-Type</code> field.
-     * 
-     * @return <code>true</code> if this is a <code>Content-Type</code> field,
-     *         <code>false</code> otherwise.
-     */
-    public boolean isContentType() {
-        return CONTENT_TYPE.equalsIgnoreCase(name);
-    }
-    
-    /**
-     * Determines if this is a <code>Subject</code> field.
-     * 
-     * @return <code>true</code> if this is a <code>Subject</code> field,
-     *         <code>false</code> otherwise.
-     */
-    public boolean isSubject() {
-        return SUBJECT.equalsIgnoreCase(name);
-    }
-    
-    /**
-     * Determines if this is a <code>From</code> field.
-     * 
-     * @return <code>true</code> if this is a <code>From</code> field,
-     *         <code>false</code> otherwise.
-     */
-    public boolean isFrom() {
-        return FROM.equalsIgnoreCase(name);
-    }
-    
-    /**
-     * Determines if this is a <code>To</code> field.
-     * 
-     * @return <code>true</code> if this is a <code>To</code> field,
-     *         <code>false</code> otherwise.
-     */
-    public boolean isTo() {
-        return TO.equalsIgnoreCase(name);
-    }
-    
-    /**
-     * @see #getRaw()
-     */
-    public String toString() {
-        return raw;
-    }
-}
+/****************************************************************
+ * 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.field;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.james.mime4j.MimeException;
+
+/**
+ * The base class of all field classes.
+ *
+ * 
+ * @version $Id: Field.java,v 1.6 2004/10/25 07:26:46 ntherning Exp $
+ */
+public abstract class Field {
+    public static final String SENDER = "Sender";
+    public static final String FROM = "From";
+    public static final String TO = "To";
+    public static final String CC = "Cc";
+    public static final String BCC = "Bcc";
+    public static final String REPLY_TO = "Reply-To";
+    public static final String RESENT_SENDER = "Resent-Sender";
+    public static final String RESENT_FROM = "Resent-From";
+    public static final String RESENT_TO = "Resent-To";
+    public static final String RESENT_CC = "Resent-Cc";
+    public static final String RESENT_BCC = "Resent-Bcc";
+
+    public static final String DATE = "Date";
+    public static final String RESENT_DATE = "Resent-Date";
+
+    public static final String SUBJECT = "Subject";
+    public static final String CONTENT_TYPE = "Content-Type";
+    public static final String CONTENT_TRANSFER_ENCODING = 
+                                        "Content-Transfer-Encoding";
+    
+    private static final String FIELD_NAME_PATTERN = 
+        "^([\\x21-\\x39\\x3b-\\x7e]+)[ \t]*:";
+    private static final Pattern fieldNamePattern = 
+        Pattern.compile(FIELD_NAME_PATTERN);
+        
+    private static final DefaultFieldParser parser = new DefaultFieldParser();
+    
+    private final String name;
+    private final String body;
+    private final String raw;
+    
+    protected Field(final String name, final String body, final String raw) {
+        this.name = name;
+        this.body = body;
+        this.raw = raw;
+    }
+    
+    /**
+     * Parses the given string and returns an instance of the 
+     * <code>Field</code> class. The type of the class returned depends on
+     * the field name:
+     * <table>
+     *      <tr>
+     *          <td><em>Field name</em></td><td><em>Class returned</em></td>
+     *          <td>Content-Type</td><td>org.apache.james.mime4j.field.ContentTypeField</td>
+     *          <td>other</td><td>org.apache.james.mime4j.field.UnstructuredField</td>
+     *      </tr>
+     * </table>
+     * 
+     * @param raw the string to parse.
+     * @return a <code>Field</code> instance.
+     * @throws MimeException on parse errors.
+     */
+    public static Field parse(final String raw) throws MimeException {
+        
+        /*
+         * Unfold the field.
+         */
+        final String unfolded = raw.replaceAll("\r|\n", "");
+        
+        /*
+         * Split into name and value.
+         */
+        final Matcher fieldMatcher = fieldNamePattern.matcher(unfolded);
+        if (!fieldMatcher.find()) {
+            throw new MimeException("Invalid field in string");
+        }
+        final String name = fieldMatcher.group(1);
+        
+        String body = unfolded.substring(fieldMatcher.end());
+        if (body.length() > 0 && body.charAt(0) == ' ') {
+            body = body.substring(1);
+        }
+        
+        return parser.parse(name, body, raw);
+    }
+    
+    /**
+     * Gets the default parser used to parse fields.
+     * @return the default field parser
+     */
+    public static DefaultFieldParser getParser() {
+        return parser;
+    }
+    
+    /**
+     * Gets the name of the field (<code>Subject</code>, 
+     * <code>From</code>, etc).
+     * 
+     * @return the field name.
+     */
+    public String getName() {
+        return name;
+    }
+    
+    /**
+     * Gets the original raw field string.
+     * 
+     * @return the original raw field string.
+     */
+    public String getRaw() {
+        return raw;
+    }
+    
+    /**
+     * Gets the unfolded, unparsed and possibly encoded (see RFC 2047) field 
+     * body string.
+     * 
+     * @return the unfolded unparsed field body string.
+     */
+    public String getBody() {
+        return body;
+    }
+    
+    /**
+     * Determines if this is a <code>Content-Type</code> field.
+     * 
+     * @return <code>true</code> if this is a <code>Content-Type</code> field,
+     *         <code>false</code> otherwise.
+     */
+    public boolean isContentType() {
+        return CONTENT_TYPE.equalsIgnoreCase(name);
+    }
+    
+    /**
+     * Determines if this is a <code>Subject</code> field.
+     * 
+     * @return <code>true</code> if this is a <code>Subject</code> field,
+     *         <code>false</code> otherwise.
+     */
+    public boolean isSubject() {
+        return SUBJECT.equalsIgnoreCase(name);
+    }
+    
+    /**
+     * Determines if this is a <code>From</code> field.
+     * 
+     * @return <code>true</code> if this is a <code>From</code> field,
+     *         <code>false</code> otherwise.
+     */
+    public boolean isFrom() {
+        return FROM.equalsIgnoreCase(name);
+    }
+    
+    /**
+     * Determines if this is a <code>To</code> field.
+     * 
+     * @return <code>true</code> if this is a <code>To</code> field,
+     *         <code>false</code> otherwise.
+     */
+    public boolean isTo() {
+        return TO.equalsIgnoreCase(name);
+    }
+    
+    /**
+     * @see #getRaw()
+     */
+    public String toString() {
+        return raw;
+    }
+}

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=697902&r1=697901&r2=697902&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 Mon Sep 22 10:22:22 2008
@@ -1,206 +1,206 @@
-/****************************************************************
- * 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.BufferedWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.nio.charset.Charset;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.apache.james.mime4j.MimeException;
-import org.apache.james.mime4j.field.ContentTypeField;
-import org.apache.james.mime4j.field.Field;
-import org.apache.james.mime4j.parser.AbstractContentHandler;
-import org.apache.james.mime4j.parser.MimeStreamParser;
-import org.apache.james.mime4j.util.CharArrayBuffer;
-import org.apache.james.mime4j.util.CharsetUtil;
-import org.apache.james.mime4j.util.MessageUtils;
-
-
-/**
- * The header of an entity (see RFC 2045).
- *
- * 
- * @version $Id: Header.java,v 1.3 2004/10/04 15:36:44 ntherning Exp $
- */
-public class Header {
-    private List fields = new LinkedList();
-    private HashMap fieldMap = new HashMap();
-    
-    /**
-     * Creates a new empty <code>Header</code>.
-     */
-    public Header() {
-    }
-
-    /**
-     * Creates a new <code>Header</code> from the specified stream.
-     * 
-     * @param is the stream to read the header from.
-     */
-    public Header(InputStream is) throws IOException {
-        final MimeStreamParser parser = new MimeStreamParser();
-        parser.setContentHandler(new AbstractContentHandler() {
-            public void endHeader() {
-                parser.stop();
-            }
-            public void field(String fieldData) throws MimeException {
-                addField(Field.parse(fieldData));
-            }
-        });
-        parser.parse(is);
-    }
-
-    /**
-     * Adds a field to the end of the list of fields.
-     * 
-     * @param field the field to add.
-     */
-    public void addField(Field field) {
-        List values = (List) fieldMap.get(field.getName().toLowerCase());
-        if (values == null) {
-            values = new LinkedList();
-            fieldMap.put(field.getName().toLowerCase(), values);
-        }
-        values.add(field);
-        fields.add(field);
-    }
-    
-    /**
-     * Gets the fields of this header. The returned list will not be
-     * modifiable.
-     * 
-     * @return the list of <code>Field</code> objects.
-     */
-    public List getFields() {
-        return Collections.unmodifiableList(fields);
-    }
-
-    /**
-     * Gets a <code>Field</code> given a field name. If there are multiple
-     * such fields defined in this header the first one will be returned.
-     * 
-     * @param name the field name (e.g. From, Subject).
-     * @return the field or <code>null</code> if none found.
-     */
-    public Field getField(String name) {
-        List l = (List) fieldMap.get(name.toLowerCase());
-        if (l != null && !l.isEmpty()) {
-            return (Field) l.get(0);
-        }
-        return null;
-    }
-    
-    /**
-     * Gets all <code>Field</code>s having the specified field name. 
-     * 
-     * @param name the field name (e.g. From, Subject).
-     * @return the list of fields.
-     */
-    public List getFields(final String name) {
-        final String lowerCaseName = name.toLowerCase();
-        final List l = (List) fieldMap.get(lowerCaseName);
-        final List results;
-        if (l == null || l.isEmpty()) {
-            results = Collections.EMPTY_LIST;
-        } else {
-            results = Collections.unmodifiableList(l);
-        }
-        return results;
-    }
-    
-    /**
-     * Return Header Object as String representation. Each headerline is
-     * seperated by "\r\n"
-     * 
-     * @return headers
-     */
-    public String toString() {
-        CharArrayBuffer str = new CharArrayBuffer(128);
-        for (Iterator it = fields.iterator(); it.hasNext();) {
-            str.append(it.next().toString());
-            str.append("\r\n");
-        }
-        return str.toString();
-    }
-    
-    
-    /**
-     * Write the Header to the given OutputStream. 
-     * <p>
-     * Compatibility mode:
-     * <ul>
-     *  <li>
-     *   {@link MessageUtils#LENIENT}: use charset of the Content-Type header
-     *  </li>
-     *  <li>
-     *   {@link MessageUtils#STRICT_ERROR}: use US-ASCII and throw {@link MimeException} 
-     *    if a non ASCII character is encountered
-     *   </li>
-     *  <li>
-     *   {@link MessageUtils#STRICT_ERROR}: ignore non ASCII characters if encountered
-     *   </li>
-     * </ul>
-     * @param out the OutputStream to write to
-     * @param mode compatibility mode:
-     *   {@link MessageUtils#LENIENT}, {@link MessageUtils#STRICT_ERROR}, {@link MessageUtils#STRICT_IGNORE}  
-     * 
-     * @throws IOException if case of an I/O error
-     * @throws MimeException if case of a MIME protocol violation
-     */
-    public void writeTo(final OutputStream out, int mode) throws IOException, MimeException {
-        Charset charset = null;
-        if (mode == MessageUtils.LENIENT) {
-            final ContentTypeField contentTypeField = ((ContentTypeField) getField(Field.CONTENT_TYPE));
-            if (contentTypeField == null) {
-                charset = MessageUtils.DEFAULT_CHARSET;
-            } else {
-                final String contentTypeFieldCharset = contentTypeField.getCharset();
-                if (contentTypeField != null && contentTypeFieldCharset != null) {
-                    charset = CharsetUtil.getCharset(contentTypeFieldCharset);
-                } else {
-                    charset = MessageUtils.ISO_8859_1;
-                }
-            }
-        } else {
-            charset = MessageUtils.DEFAULT_CHARSET;
-        }
-        BufferedWriter writer = new BufferedWriter(
-                new OutputStreamWriter(out, charset), 8192);
-        for (Iterator it = fields.iterator(); it.hasNext();) {
-            Field field = (Field) it.next();
-            String fs = field.toString();
-            if (mode == MessageUtils.STRICT_ERROR && !MessageUtils.isASCII(fs)) {
-                throw new MimeException("Header '" + fs + "' violates RFC 822");
-            }
-            writer.write(fs);
-            writer.write(MessageUtils.CRLF);
-        }
-        writer.write(MessageUtils.CRLF);
-        writer.flush();
-    }
-}
+/****************************************************************
+ * 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.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.field.ContentTypeField;
+import org.apache.james.mime4j.field.Field;
+import org.apache.james.mime4j.parser.AbstractContentHandler;
+import org.apache.james.mime4j.parser.MimeStreamParser;
+import org.apache.james.mime4j.util.CharArrayBuffer;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.apache.james.mime4j.util.MessageUtils;
+
+
+/**
+ * The header of an entity (see RFC 2045).
+ *
+ * 
+ * @version $Id: Header.java,v 1.3 2004/10/04 15:36:44 ntherning Exp $
+ */
+public class Header {
+    private List fields = new LinkedList();
+    private HashMap fieldMap = new HashMap();
+    
+    /**
+     * Creates a new empty <code>Header</code>.
+     */
+    public Header() {
+    }
+
+    /**
+     * Creates a new <code>Header</code> from the specified stream.
+     * 
+     * @param is the stream to read the header from.
+     */
+    public Header(InputStream is) throws MimeException, IOException {
+        final MimeStreamParser parser = new MimeStreamParser();
+        parser.setContentHandler(new AbstractContentHandler() {
+            public void endHeader() {
+                parser.stop();
+            }
+            public void field(String fieldData) throws MimeException {
+                addField(Field.parse(fieldData));
+            }
+        });
+        parser.parse(is);
+    }
+
+    /**
+     * Adds a field to the end of the list of fields.
+     * 
+     * @param field the field to add.
+     */
+    public void addField(Field field) {
+        List values = (List) fieldMap.get(field.getName().toLowerCase());
+        if (values == null) {
+            values = new LinkedList();
+            fieldMap.put(field.getName().toLowerCase(), values);
+        }
+        values.add(field);
+        fields.add(field);
+    }
+    
+    /**
+     * Gets the fields of this header. The returned list will not be
+     * modifiable.
+     * 
+     * @return the list of <code>Field</code> objects.
+     */
+    public List getFields() {
+        return Collections.unmodifiableList(fields);
+    }
+
+    /**
+     * Gets a <code>Field</code> given a field name. If there are multiple
+     * such fields defined in this header the first one will be returned.
+     * 
+     * @param name the field name (e.g. From, Subject).
+     * @return the field or <code>null</code> if none found.
+     */
+    public Field getField(String name) {
+        List l = (List) fieldMap.get(name.toLowerCase());
+        if (l != null && !l.isEmpty()) {
+            return (Field) l.get(0);
+        }
+        return null;
+    }
+    
+    /**
+     * Gets all <code>Field</code>s having the specified field name. 
+     * 
+     * @param name the field name (e.g. From, Subject).
+     * @return the list of fields.
+     */
+    public List getFields(final String name) {
+        final String lowerCaseName = name.toLowerCase();
+        final List l = (List) fieldMap.get(lowerCaseName);
+        final List results;
+        if (l == null || l.isEmpty()) {
+            results = Collections.EMPTY_LIST;
+        } else {
+            results = Collections.unmodifiableList(l);
+        }
+        return results;
+    }
+    
+    /**
+     * Return Header Object as String representation. Each headerline is
+     * seperated by "\r\n"
+     * 
+     * @return headers
+     */
+    public String toString() {
+        CharArrayBuffer str = new CharArrayBuffer(128);
+        for (Iterator it = fields.iterator(); it.hasNext();) {
+            str.append(it.next().toString());
+            str.append("\r\n");
+        }
+        return str.toString();
+    }
+    
+    
+    /**
+     * Write the Header to the given OutputStream. 
+     * <p>
+     * Compatibility mode:
+     * <ul>
+     *  <li>
+     *   {@link MessageUtils#LENIENT}: use charset of the Content-Type header
+     *  </li>
+     *  <li>
+     *   {@link MessageUtils#STRICT_ERROR}: use US-ASCII and throw {@link MimeException} 
+     *    if a non ASCII character is encountered
+     *   </li>
+     *  <li>
+     *   {@link MessageUtils#STRICT_ERROR}: ignore non ASCII characters if encountered
+     *   </li>
+     * </ul>
+     * @param out the OutputStream to write to
+     * @param mode compatibility mode:
+     *   {@link MessageUtils#LENIENT}, {@link MessageUtils#STRICT_ERROR}, {@link MessageUtils#STRICT_IGNORE}  
+     * 
+     * @throws IOException if case of an I/O error
+     * @throws MimeException if case of a MIME protocol violation
+     */
+    public void writeTo(final OutputStream out, int mode) throws IOException, MimeException {
+        Charset charset = null;
+        if (mode == MessageUtils.LENIENT) {
+            final ContentTypeField contentTypeField = ((ContentTypeField) getField(Field.CONTENT_TYPE));
+            if (contentTypeField == null) {
+                charset = MessageUtils.DEFAULT_CHARSET;
+            } else {
+                final String contentTypeFieldCharset = contentTypeField.getCharset();
+                if (contentTypeField != null && contentTypeFieldCharset != null) {
+                    charset = CharsetUtil.getCharset(contentTypeFieldCharset);
+                } else {
+                    charset = MessageUtils.ISO_8859_1;
+                }
+            }
+        } else {
+            charset = MessageUtils.DEFAULT_CHARSET;
+        }
+        BufferedWriter writer = new BufferedWriter(
+                new OutputStreamWriter(out, charset), 8192);
+        for (Iterator it = fields.iterator(); it.hasNext();) {
+            Field field = (Field) it.next();
+            String fs = field.toString();
+            if (mode == MessageUtils.STRICT_ERROR && !MessageUtils.isASCII(fs)) {
+                throw new MimeException("Header '" + fs + "' violates RFC 822");
+            }
+            writer.write(fs);
+            writer.write(MessageUtils.CRLF);
+        }
+        writer.write(MessageUtils.CRLF);
+        writer.flush();
+    }
+}

Modified: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java?rev=697902&r1=697901&r2=697902&view=diff
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java (original)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/message/MessageBuilder.java Mon Sep 22 10:22:22 2008
@@ -52,7 +52,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#startMessage()
      */
-    public void startMessage() {
+    public void startMessage() throws MimeException {
         if (stack.isEmpty()) {
             stack.push(this.entity);
         } else {
@@ -66,7 +66,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#endMessage()
      */
-    public void endMessage() {
+    public void endMessage() throws MimeException {
         expect(Message.class);
         stack.pop();
     }
@@ -74,7 +74,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#startHeader()
      */
-    public void startHeader() {
+    public void startHeader() throws MimeException {
         stack.push(new Header());
     }
     
@@ -89,7 +89,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#endHeader()
      */
-    public void endHeader() {
+    public void endHeader() throws MimeException {
         expect(Header.class);
         Header h = (Header) stack.pop();
         expect(Entity.class);
@@ -99,7 +99,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#startMultipart(org.apache.james.mime4j.descriptor.BodyDescriptor)
      */
-    public void startMultipart(final BodyDescriptor bd) {
+    public void startMultipart(final BodyDescriptor bd) throws MimeException {
         expect(Entity.class);
         
         final Entity e = (Entity) stack.peek();
@@ -112,7 +112,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#body(org.apache.james.mime4j.descriptor.BodyDescriptor, java.io.InputStream)
      */
-    public void body(BodyDescriptor bd, final InputStream is) throws IOException {
+    public void body(BodyDescriptor bd, final InputStream is) throws MimeException, IOException {
         expect(Entity.class);
         
         final String enc = bd.getTransferEncoding();
@@ -141,14 +141,14 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#endMultipart()
      */
-    public void endMultipart() {
+    public void endMultipart() throws MimeException {
         stack.pop();
     }
     
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#startBodyPart()
      */
-    public void startBodyPart() {
+    public void startBodyPart() throws MimeException {
         expect(Multipart.class);
         
         BodyPart bodyPart = new BodyPart();
@@ -159,7 +159,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#endBodyPart()
      */
-    public void endBodyPart() {
+    public void endBodyPart() throws MimeException {
         expect(BodyPart.class);
         stack.pop();
     }
@@ -167,7 +167,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#epilogue(java.io.InputStream)
      */
-    public void epilogue(InputStream is) throws IOException {
+    public void epilogue(InputStream is) throws MimeException, IOException {
         expect(Multipart.class);
         CharArrayBuffer sb = new CharArrayBuffer(128);
         int b;
@@ -180,7 +180,7 @@
     /**
      * @see org.apache.james.mime4j.parser.ContentHandler#preamble(java.io.InputStream)
      */
-    public void preamble(InputStream is) throws IOException {
+    public void preamble(InputStream is) throws MimeException, IOException {
         expect(Multipart.class);
         CharArrayBuffer sb = new CharArrayBuffer(128);
         int b;
@@ -194,7 +194,7 @@
      * Unsupported.
      * @see org.apache.james.mime4j.parser.ContentHandler#raw(java.io.InputStream)
      */
-    public void raw(InputStream is) throws IOException {
+    public void raw(InputStream is) throws MimeException, IOException {
         throw new UnsupportedOperationException("Not supported");
     }
 



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