View Javadoc

1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.mime.encoder;
36  
37  import java.io.IOException;
38  import java.io.OutputStream;
39  
40  import org.marre.mime.MimeBodyPart;
41  import org.marre.mime.MimeContentType;
42  import org.marre.mime.MimeHeader;
43  import org.marre.mime.MimeHeaderParam;
44  import org.marre.mime.MimeMultipart;
45  import org.marre.util.StringUtil;
46  
47  /***
48   * Converts mime documents to text.
49   * 
50   * TODO: Content-Transfer-Encoding. <br>
51   * TODO: Special handling of some headers like Content-Id.
52   * 
53   * @author Markus Eriksson
54   * @version $Id: TextMimeEncoder.java,v 1.1 2004/11/02 17:59:54 c95men Exp $
55   */
56  public class TextMimeEncoder implements MimeEncoder
57  {
58      /*** The length of the random boundary string. */
59      private static final int DEFAULT_BOUNDARY_STRING_LENGTH = 35;
60  
61      /***
62       * Creates a TextMimeEncoder.
63       */
64      public TextMimeEncoder()
65      {
66          super();
67      }
68  
69      /***
70       * Writes the content-type of the message to the given stream.
71       * 
72       * @param theOs
73       *            The stream to write to
74       * @param theMsg
75       *            The message to get the content-type from
76       * @throws IOException
77       *             Thrown if we fail to write the content-type to the stream
78       */
79      public void writeContentType(OutputStream theOs, MimeBodyPart theMsg) throws IOException
80      {
81          MimeContentType ct = theMsg.getContentType();
82  
83          if (theMsg instanceof MimeMultipart)
84          {
85              String boundary = StringUtil.randString(DEFAULT_BOUNDARY_STRING_LENGTH);
86              ct.setParam("boundary", boundary);
87          }
88  
89          writeHeader(theOs, ct);
90      }
91  
92      /***
93       * Writes the headers of the message to the given stream.
94       * 
95       * @param theOs
96       *            The stream to write to
97       * @param theMsg
98       *            The message to get the headers from
99       * @throws IOException
100      *             Thrown if we fail to write the headers to the stream
101      */
102     public void writeHeaders(OutputStream theOs, MimeBodyPart theMsg) throws IOException
103     {
104         for (int i = 0; i < theMsg.getHeaderCount(); i++)
105         {
106             MimeHeader header = theMsg.getHeader(i);
107             writeHeader(theOs, header);
108         }
109         theOs.write("\r\n".getBytes());
110     }
111 
112     /***
113      * Writes the body of the message to the given stream.
114      * 
115      * @param theOs
116      *            The stream to write to
117      * @param theMsg
118      *            The message to get the data from
119      * @throws IOException
120      *             Thrown if we fail to write the body to the stream
121      */
122     public void writeBody(OutputStream theOs, MimeBodyPart theMsg) throws IOException
123     {
124         if (theMsg instanceof MimeMultipart)
125         {
126             String ct = theMsg.getContentType().getValue();
127             if (ct.startsWith("application/vnd.wap.multipart."))
128             {
129                 // WSP encoded multipart
130                 // TODO: Write wsp encoded multipart
131             }
132             else
133             {
134                 writeMultipart(theOs, (MimeMultipart) theMsg);
135             }
136         }
137         else
138         {
139             theOs.write(theMsg.getBody());
140             theOs.write("\r\n".getBytes());
141         }
142     }
143 
144     /***
145      * Write one header to the stream.
146      * 
147      * @param theOs
148      *            The stream to write to
149      * @param header
150      *            The header to write.
151      * @throws IOException
152      *             Thrown if we fail to write the header to the stream
153      */
154     protected void writeHeader(OutputStream theOs, MimeHeader header) throws IOException
155     {
156         StringBuffer strBuff = new StringBuffer();
157 
158         String name = header.getName();
159         String value = header.getValue();
160 
161         strBuff.append(name + ": " + value);
162 
163         for (int i = 0; i < header.getParamCount(); i++)
164         {
165             MimeHeaderParam headerParam = header.getParam(i);
166             // + "; charset=adsfasdf; param=value"
167             strBuff.append("; " + headerParam.getName() + "=" + headerParam.getValue());
168         }
169 
170         // <CR><LF>
171         strBuff.append("\r\n");
172 
173         theOs.write(strBuff.toString().getBytes());
174     }
175 
176     /***
177      * Writes a multipart entry to the stream.
178      * 
179      * @param theOs
180      *            The stream to write to
181      * @param theMultipart
182      *            The header to write.
183      * @throws IOException
184      *             Thrown if we fail to write an entry to the stream
185      */
186     private void writeMultipart(OutputStream theOs, MimeMultipart theMultipart) throws IOException
187     {
188         MimeContentType ct = theMultipart.getContentType();
189         MimeHeaderParam boundaryParam = ct.getParam("boundary");
190         String boundary = "--" + boundaryParam.getValue();
191 
192         for (int i = 0; i < theMultipart.getBodyPartCount(); i++)
193         {
194             MimeBodyPart part = (MimeBodyPart) theMultipart.getBodyPart(i);
195 
196             // Write boundary string
197             theOs.write(boundary.getBytes());
198             theOs.write("\r\n".getBytes());
199 
200             // Generate headers + content-type
201             writeContentType(theOs, part);
202             writeHeaders(theOs, part);
203 
204             // Write data
205             writeBody(theOs, part);
206         }
207         // Write end of boundary
208         theOs.write(boundary.getBytes());
209         theOs.write("--\r\n".getBytes());
210     }
211 }