• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one   *
3  * or more contributor license agreements.  See the NOTICE file *
4  * distributed with this work for additional information        *
5  * regarding copyright ownership.  The ASF licenses this file   *
6  * to you under the Apache License, Version 2.0 (the            *
7  * "License"); you may not use this file except in compliance   *
8  * with the License.  You may obtain a copy of the License at   *
9  *                                                              *
10  *   http://www.apache.org/licenses/LICENSE-2.0                 *
11  *                                                              *
12  * Unless required by applicable law or agreed to in writing,   *
13  * software distributed under the License is distributed on an  *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15  * KIND, either express or implied.  See the License for the    *
16  * specific language governing permissions and limitations      *
17  * under the License.                                           *
18  ****************************************************************/
19 
20 package org.apache.james.mime4j.message;
21 
22 import java.io.BufferedWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31 import java.util.List;
32 
33 import org.apache.james.mime4j.AbstractContentHandler;
34 import org.apache.james.mime4j.MimeStreamParser;
35 import org.apache.james.mime4j.field.ContentTypeField;
36 import org.apache.james.mime4j.field.Field;
37 import org.apache.james.mime4j.util.CharsetUtil;
38 
39 
40 /**
41  * The header of an entity (see RFC 2045).
42  *
43  *
44  * @version $Id: Header.java,v 1.3 2004/10/04 15:36:44 ntherning Exp $
45  */
46 public class Header {
47     private List fields = new LinkedList();
48     private HashMap fieldMap = new HashMap();
49 
50     /**
51      * Creates a new empty <code>Header</code>.
52      */
Header()53     public Header() {
54     }
55 
56     /**
57      * Creates a new <code>Header</code> from the specified stream.
58      *
59      * @param is the stream to read the header from.
60      */
Header(InputStream is)61     public Header(InputStream is) throws IOException {
62         final MimeStreamParser parser = new MimeStreamParser();
63         parser.setContentHandler(new AbstractContentHandler() {
64             public void endHeader() {
65                 parser.stop();
66             }
67             public void field(String fieldData) {
68                 addField(Field.parse(fieldData));
69             }
70         });
71         parser.parse(is);
72     }
73 
74     /**
75      * Adds a field to the end of the list of fields.
76      *
77      * @param field the field to add.
78      */
addField(Field field)79     public void addField(Field field) {
80         List values = (List) fieldMap.get(field.getName().toLowerCase());
81         if (values == null) {
82             values = new LinkedList();
83             fieldMap.put(field.getName().toLowerCase(), values);
84         }
85         values.add(field);
86         fields.add(field);
87     }
88 
89     /**
90      * Gets the fields of this header. The returned list will not be
91      * modifiable.
92      *
93      * @return the list of <code>Field</code> objects.
94      */
getFields()95     public List getFields() {
96         return Collections.unmodifiableList(fields);
97     }
98 
99     /**
100      * Gets a <code>Field</code> given a field name. If there are multiple
101      * such fields defined in this header the first one will be returned.
102      *
103      * @param name the field name (e.g. From, Subject).
104      * @return the field or <code>null</code> if none found.
105      */
getField(String name)106     public Field getField(String name) {
107         List l = (List) fieldMap.get(name.toLowerCase());
108         if (l != null && !l.isEmpty()) {
109             return (Field) l.get(0);
110         }
111         return null;
112     }
113 
114     /**
115      * Gets all <code>Field</code>s having the specified field name.
116      *
117      * @param name the field name (e.g. From, Subject).
118      * @return the list of fields.
119      */
getFields(String name)120     public List getFields(String name) {
121         List l = (List) fieldMap.get(name.toLowerCase());
122         return Collections.unmodifiableList(l);
123     }
124 
125     /**
126      * Return Header Object as String representation. Each headerline is
127      * seperated by "\r\n"
128      *
129      * @return headers
130      */
toString()131     public String toString() {
132         StringBuffer str = new StringBuffer();
133         for (Iterator it = fields.iterator(); it.hasNext();) {
134             str.append(it.next().toString());
135             str.append("\r\n");
136         }
137         return str.toString();
138     }
139 
140 
141     /**
142      * Write the Header to the given OutputStream
143      *
144      * @param out the OutputStream to write to
145      * @throws IOException
146      */
writeTo(OutputStream out)147     public void writeTo(OutputStream out) throws IOException {
148         String charString = ((ContentTypeField) getField(Field.CONTENT_TYPE)).getCharset();
149 
150         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, CharsetUtil.getCharset(charString)),8192);
151         writer.write(toString()+ "\r\n");
152         writer.flush();
153     }
154 
155 }
156