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.wap;
36  
37  import java.io.ByteArrayOutputStream;
38  import java.io.IOException;
39  import java.io.OutputStream;
40  
41  import org.marre.mime.MimeBodyPart;
42  import org.marre.mime.MimeHeader;
43  import org.marre.mime.MimeMultipart;
44  import org.marre.mime.encoder.MimeEncoder;
45  import org.marre.mime.encoder.TextMimeEncoder;
46  
47  /***
48   * Converts mime documents to a wsp encoded stream.
49   * 
50   * @author Markus Eriksson
51   * @version $Id: WapMimeEncoder.java,v 1.1 2004/11/02 17:59:48 c95men Exp $
52   */
53  public class WapMimeEncoder implements MimeEncoder
54  {
55      private TextMimeEncoder myTextMimeEncoder = new TextMimeEncoder();
56      
57      private byte myWspEncodingVersion;
58  
59      public WapMimeEncoder()
60      {
61          this(WapConstants.WSP_ENCODING_VERSION_1_2);
62      }
63      
64      public WapMimeEncoder(byte wspEncodingVersion)
65      {
66          myWspEncodingVersion = wspEncodingVersion;
67      }
68  
69      /***
70       * Writes an WSP encoded content type header to the given stream.
71       * <p>
72       * NOTE! It only writes an WSP encoded content-type to the stream. It does
73       * not add the content type header id.
74       * 
75       * @param theOs
76       *            The stream to write to
77       * @param theMsg
78       *            The message to get the content-type from
79       * @throws IOException
80       *             Thrown if we fail to write the content-type to the stream
81       */
82      public void writeContentType(OutputStream theOs, MimeBodyPart theMsg) throws IOException
83      {
84          if (theMsg instanceof MimeMultipart)
85          {
86              String ct = theMsg.getContentType().getValue();
87  
88              // Convert multipart headers...
89              // TODO: Clone content type... We shouldn't change theMsg...
90              String newCt = WspUtil.convertMultipartContentType(ct);
91              theMsg.getContentType().setValue(newCt);
92          }
93  
94          WspUtil.writeContentType(myWspEncodingVersion, theOs, theMsg.getContentType());
95      }
96  
97      /***
98       * Writes the headers of the message to the given stream.
99       * 
100      * @param theOs
101      *            The stream to write to
102      * @param theMsg
103      *            The message to get the headers from
104      * @throws IOException
105      *             Thrown if we fail to write the headers to the stream
106      */
107     public void writeHeaders(OutputStream theOs, MimeBodyPart theMsg) throws IOException
108     {
109         for (int i = 0; i < theMsg.getHeaderCount(); i++)
110         {
111             MimeHeader header = theMsg.getHeader(i);
112             WspHeaderEncoder.writeHeader(myWspEncodingVersion, theOs, header);
113         }
114     }
115 
116     /***
117      * Writes the body of the message to the given stream.
118      * 
119      * @param theOs
120      *            The stream to write to
121      * @param theMsg
122      *            The message to get the data from
123      * @throws IOException
124      *             Thrown if we fail to write the body to the stream
125      */
126     public void writeBody(OutputStream theOs, MimeBodyPart theMsg) throws IOException
127     {
128         if (theMsg instanceof MimeMultipart)
129         {
130             String ct = theMsg.getContentType().getValue();
131 
132             // Convert multipart headers...
133             // TODO: Clone content type... We shouldn't change theMsg...
134             String newCt = WspUtil.convertMultipartContentType(ct);
135             theMsg.getContentType().setValue(newCt);
136 
137             if (newCt.startsWith("application/vnd.wap.multipart."))
138             {
139                 // WSP encoded multipart
140                 writeMultipart(theOs, (MimeMultipart) theMsg);
141             }
142             else
143             {
144                 // Not WSP encoded
145                 // TODO: Write textual "multipart/"
146             }
147         }
148         else
149         {
150             theOs.write(theMsg.getBody());
151         }
152     }
153 
154     // Section 8.5.2 in WAP-230-WSP-20010705
155     private void writeMultipart(OutputStream theOs, MimeMultipart theMultipart) throws IOException
156     {
157         // nEntries
158         WspUtil.writeUintvar(theOs, theMultipart.getBodyPartCount());
159 
160         for (int i = 0; i < theMultipart.getBodyPartCount(); i++)
161         {
162             MimeBodyPart part = (MimeBodyPart) theMultipart.getBodyPart(i);
163             ByteArrayOutputStream headers = new ByteArrayOutputStream();
164             ByteArrayOutputStream content = new ByteArrayOutputStream();
165 
166             // Generate content-type + headers
167             writeContentType(headers, part);
168             writeHeaders(headers, part);
169             // Done with the headers...
170             headers.close();
171 
172             // Generate content...
173             writeBody(content, part);
174             content.close();
175 
176             // Write data to theOs
177 
178             // Length of the content type and headers combined
179             WspUtil.writeUintvar(theOs, headers.size());
180             // Length of the data (content)
181             WspUtil.writeUintvar(theOs, content.size());
182             // Content type + headers
183             theOs.write(headers.toByteArray());
184             // Data
185             theOs.write(content.toByteArray());
186         }
187     }
188 }