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.mms;
36  
37  import java.io.ByteArrayOutputStream;
38  import java.io.IOException;
39  import java.io.OutputStream;
40  import java.util.Date;
41  
42  import org.marre.mime.MimeHeader;
43  import org.marre.util.StringUtil;
44  import org.marre.wap.WspUtil;
45  
46  /***
47   * 
48   * @author Markus Eriksson
49   * @version $Id: MmsHeaderEncoder.java,v 1.1 2004/11/02 17:59:54 c95men Exp $
50   */
51  public final class MmsHeaderEncoder
52  {
53      private MmsHeaderEncoder()
54      {
55      }
56  
57      /*
58       * This isn't really suitable for MMS public static void
59       * writeHeader(OutputStream theOs, MimeHeader theHeader) throws IOException {
60       * String name = theHeader.getName(); int wellKnownHeaderId =
61       * StringUtil.findString(MmsConstants.HEADER_NAMES, name.toLowerCase());
62       * 
63       * switch (wellKnownHeaderId) { case MmsConstants.HEADER_ID_BCC:
64       * MmsHeaderEncoder.writeHeaderBcc(theOs, theHeader.getValue()); break; case
65       * MmsConstants.HEADER_ID_CC: MmsHeaderEncoder.writeHeaderCc(theOs,
66       * theHeader.getValue()); break; case
67       * MmsConstants.HEADER_ID_X_MMS_CONTENT_LOCATION:
68       * MmsHeaderEncoder.writeHeaderContentLocation(theOs, theHeader.getValue());
69       * break; case MmsConstants.HEADER_ID_CONTENT_TYPE:
70       * MmsHeaderEncoder.writeHeaderContentType(theOs, theHeader); break; case
71       * MmsConstants.HEADER_ID_DATE: break; case
72       * MmsConstants.HEADER_ID_X_MMS_DELIVERY_REPORT: break; case
73       * MmsConstants.HEADER_ID_X_MMS_DELIVERY_TIME: break; case
74       * MmsConstants.HEADER_ID_X_MMS_EXPIRY:
75       * //MmsHeaderEncoder.writeHeaderXMmsExpiry(theOs, theHeader.getValue());
76       * break; case MmsConstants.HEADER_ID_FROM:
77       * MmsHeaderEncoder.writeHeaderFrom(theOs, theHeader.getValue()); break;
78       * case MmsConstants.HEADER_ID_X_MMS_MESSAGE_CLASS:
79       * MmsHeaderEncoder.writeHeaderXMmsMessageClass(theOs,
80       * theHeader.getValue()); break; case MmsConstants.HEADER_ID_MESSAGE_ID:
81       * break; case MmsConstants.HEADER_ID_X_MMS_MESSAGE_TYPE:
82       * MmsHeaderEncoder.writeHeaderXMmsMessageType(theOs, theHeader.getValue());
83       * break; case MmsConstants.HEADER_ID_X_MMS_MMS_VERSION:
84       * MmsHeaderEncoder.writeHeaderXMmsMmsVersion(theOs, theHeader.getValue());
85       * break; case MmsConstants.HEADER_ID_X_MMS_MESSAGE_SIZE: break; case
86       * MmsConstants.HEADER_ID_X_MMS_PRIORITY:
87       * MmsHeaderEncoder.writeHeaderXMmsPriority(theOs, theHeader.getValue());
88       * break; case MmsConstants.HEADER_ID_X_MMS_READ_REPLY:
89       * MmsHeaderEncoder.writeHeaderXMmsReadReply(theOs, theHeader.getValue());
90       * break; case MmsConstants.HEADER_ID_X_MMS_REPORT_ALLOWED: break; case
91       * MmsConstants.HEADER_ID_X_MMS_RESPONSE_STATUS: break; case
92       * MmsConstants.HEADER_ID_X_MMS_RESPONSE_TEXT: break; case
93       * MmsConstants.HEADER_ID_X_MMS_SENDER_VISIBILITY:
94       * MmsHeaderEncoder.writeHeaderXMmsSenderVisibility(theOs,
95       * theHeader.getValue()); break; case MmsConstants.HEADER_ID_X_MMS_STATUS:
96       * MmsHeaderEncoder.writeHeaderXMmsStatus(theOs, theHeader.getValue());
97       * break; case MmsConstants.HEADER_ID_SUBJECT:
98       * MmsHeaderEncoder.writeHeaderSubject(theOs, theHeader.getValue()); break;
99       * case MmsConstants.HEADER_ID_TO: MmsHeaderEncoder.writeHeaderTo(theOs,
100      * theHeader.getValue()); break; case
101      * MmsConstants.HEADER_ID_X_MMS_TRANSACTION_ID:
102      * MmsHeaderEncoder.writeHeaderXMmsTransactionId(theOs,
103      * theHeader.getValue()); break;
104      * 
105      * default: // Application-header WspUtil.writeTokenText(theOs,
106      * theHeader.getName()); WspUtil.writeTextString(theOs,
107      * theHeader.getValue()); break; } }
108      */
109 
110     /***
111      * Writes a wsp encoded content-location header as specified in
112      * WAP-230-WSP-20010705-a.pdf.
113      * 
114      * @param theOs
115      * @param theContentLocation
116      * @throws IOException
117      */
118     public static void writeHeaderContentLocation(OutputStream theOs, String theContentLocation) throws IOException
119     {
120         // TODO: Verify
121         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_CONTENT_LOCATION);
122         WspUtil.writeTextString(theOs, theContentLocation);
123     }
124 
125     public static void writeHeaderContentType(byte wspEncodingVersion, OutputStream theOs, String theContentType) throws IOException
126     {
127         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_CONTENT_TYPE);
128         WspUtil.writeContentType(wspEncodingVersion, theOs, theContentType);
129     }
130 
131     public static void writeHeaderContentType(byte wspEncodingVersion, OutputStream theOs, MimeHeader theContentType) throws IOException
132     {
133         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_CONTENT_TYPE);
134         WspUtil.writeContentType(wspEncodingVersion, theOs, theContentType);
135     }
136 
137     public static void writeEncodedStringValue(OutputStream baos, String theStringValue) throws IOException
138     {
139         // TODO: Charset...
140         WspUtil.writeTextString(baos, theStringValue);
141     }
142 
143     public static void writeHeaderXMmsMessageType(OutputStream theOs, int theMessageTypeId) throws IOException
144     {
145         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_MESSAGE_TYPE);
146         WspUtil.writeShortInteger(theOs, theMessageTypeId);
147     }
148 
149     public static void writeHeaderXMmsMessageType(OutputStream theOs, String theMessageType) throws IOException
150     {
151         int messageTypeId = StringUtil.findString(MmsConstants.X_MMS_MESSAGE_TYPE_NAMES, theMessageType.toLowerCase());
152         if (messageTypeId != -1)
153         {
154             writeHeaderXMmsMessageType(theOs, messageTypeId);
155         }
156     }
157 
158     public static void writeHeaderXMmsTransactionId(OutputStream theOs, String theTransactionId) throws IOException
159     {
160         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_TRANSACTION_ID);
161         WspUtil.writeTextString(theOs, theTransactionId);
162     }
163 
164     public static void writeHeaderXMmsMmsVersion(OutputStream theOs, int theVersionId) throws IOException
165     {
166         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_MMS_VERSION);
167 
168         switch (theVersionId)
169         {
170         case MmsConstants.X_MMS_MMS_VERSION_ID_1_0:
171         default:
172             WspUtil.writeShortInteger(theOs, 0x10);
173             break;
174         }
175     }
176 
177     public static void writeHeaderXMmsMmsVersion(OutputStream theOs, String theVersion) throws IOException
178     {
179         int versionId = StringUtil.findString(MmsConstants.X_MMS_MMS_VERSION_NAMES, theVersion.toLowerCase());
180         if (versionId != -1)
181         {
182             writeHeaderXMmsMessageType(theOs, versionId);
183         }
184     }
185 
186     public static void writeHeaderDate(OutputStream theOs, Date date) throws IOException
187     {
188         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_DATE);
189         long time = date.getTime();
190         WspUtil.writeLongInteger(theOs, time);
191     }
192 
193     public static void writeHeaderFrom(OutputStream theOs, String theFrom) throws IOException
194     {
195         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_FROM);
196 
197         if (theFrom == null)
198         {
199             WspUtil.writeValueLength(theOs, 1);
200             WspUtil.writeShortInteger(theOs, MmsConstants.FROM_INSERT_ADDRESS);
201         }
202         else
203         {
204             ByteArrayOutputStream baos = new ByteArrayOutputStream();
205 
206             // Write data to baos
207             WspUtil.writeShortInteger(baos, MmsConstants.FROM_ADDRESS_PRESENT);
208             MmsHeaderEncoder.writeEncodedStringValue(baos, theFrom);
209             baos.close();
210 
211             WspUtil.writeValueLength(theOs, baos.size());
212             theOs.write(baos.toByteArray());
213         }
214     }
215 
216     public static void writeHeaderSubject(OutputStream theOs, String theSubject) throws IOException
217     {
218         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_SUBJECT);
219         MmsHeaderEncoder.writeEncodedStringValue(theOs, theSubject);
220     }
221 
222     public static void writeHeaderTo(OutputStream theOs, String theRecipient) throws IOException
223     {
224         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_TO);
225         MmsHeaderEncoder.writeEncodedStringValue(theOs, theRecipient);
226     }
227 
228     public static void writeHeaderCc(OutputStream theOs, String theRecipient) throws IOException
229     {
230         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_CC);
231         MmsHeaderEncoder.writeEncodedStringValue(theOs, theRecipient);
232     }
233 
234     public static void writeHeaderBcc(OutputStream theOs, String theRecipient) throws IOException
235     {
236         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_BCC);
237         MmsHeaderEncoder.writeEncodedStringValue(theOs, theRecipient);
238     }
239 
240     public static void writeHeaderXMmsReadReply(OutputStream theOs, int theReadReplyId) throws IOException
241     {
242         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_READ_REPLY);
243         WspUtil.writeShortInteger(theOs, theReadReplyId);
244     }
245 
246     public static void writeHeaderXMmsReadReply(OutputStream theOs, String theReadReply) throws IOException
247     {
248         int readReplyId = StringUtil.findString(MmsConstants.X_MMS_READ_REPLY_NAMES, theReadReply.toLowerCase());
249         if (readReplyId != -1)
250         {
251             writeHeaderXMmsReadReply(theOs, readReplyId);
252         }
253     }
254 
255     public static void writeHeaderXMmsPriority(OutputStream theOs, int thePriorityId) throws IOException
256     {
257         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_PRIORITY);
258         WspUtil.writeShortInteger(theOs, thePriorityId);
259     }
260 
261     public static void writeHeaderXMmsPriority(OutputStream theOs, String thePriority) throws IOException
262     {
263         int priorityId = StringUtil.findString(MmsConstants.X_MMS_PRIORITY_NAMES, thePriority.toLowerCase());
264         if (priorityId != -1)
265         {
266             writeHeaderXMmsPriority(theOs, priorityId);
267         }
268     }
269 
270     public static void writeHeaderXMmsStatus(OutputStream theOs, int theStatusId) throws IOException
271     {
272         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_STATUS);
273         WspUtil.writeShortInteger(theOs, theStatusId);
274     }
275 
276     public static void writeHeaderXMmsStatus(OutputStream theOs, String theStatus) throws IOException
277     {
278         int statusId = StringUtil.findString(MmsConstants.X_MMS_STATUS_NAMES, theStatus.toLowerCase());
279         if (statusId != -1)
280         {
281             writeHeaderXMmsStatus(theOs, statusId);
282         }
283     }
284 
285     public static void writeHeaderXMmsMessageClass(OutputStream theOs, int theMessageClassId) throws IOException
286     {
287         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_MESSAGE_CLASS);
288         WspUtil.writeShortInteger(theOs, theMessageClassId);
289     }
290 
291     public static void writeHeaderXMmsMessageClass(OutputStream theOs, String theMessageClass) throws IOException
292     {
293         int messageClassId = StringUtil.findString(MmsConstants.X_MMS_MESSAGE_CLASS_NAMES, theMessageClass
294                 .toLowerCase());
295         if (messageClassId != -1)
296         {
297             writeHeaderXMmsMessageClass(theOs, messageClassId);
298         }
299     }
300 
301     public static void writeHeaderXMmsMessageSize(OutputStream theOs, long messageSize) throws IOException
302     {
303         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_MESSAGE_SIZE);
304         WspUtil.writeLongInteger(theOs, messageSize);
305     }
306 
307     public static void writeHeaderXMmsExpiryAbsolute(OutputStream theOs, long theExpiry) throws IOException
308     {
309         // Expiry-value = Value-length (Absolute-token Date-value |
310         // Relative-token Delta-seconds-value)
311         ByteArrayOutputStream baos = new ByteArrayOutputStream();
312 
313         // (Absolute-token Date-value)
314         WspUtil.writeShortInteger(baos, MmsConstants.ABSOLUTE_TOKEN);
315         WspUtil.writeLongInteger(baos, theExpiry);
316 
317         baos.close();
318 
319         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_EXPIRY);
320         WspUtil.writeValueLength(theOs, baos.size());
321         theOs.write(baos.toByteArray());
322     }
323 
324     public static void writeHeaderXMmsExpiryAbsolute(OutputStream theOs, Date theExpiry) throws IOException
325     {
326         writeHeaderXMmsExpiryAbsolute(theOs, theExpiry.getTime());
327     }
328 
329     public static void writeHeaderXMmsExpiryRelative(OutputStream theOs, long theExpiry) throws IOException
330     {
331         // Expiry-value = Value-length (Absolute-token Date-value |
332         // Relative-token Delta-seconds-value)
333         ByteArrayOutputStream baos = new ByteArrayOutputStream();
334 
335         WspUtil.writeShortInteger(baos, MmsConstants.RELATIVE_TOKEN);
336         WspUtil.writeLongInteger(baos, theExpiry);
337 
338         baos.close();
339 
340         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_EXPIRY);
341         WspUtil.writeValueLength(theOs, baos.size());
342         theOs.write(baos.toByteArray());
343     }
344 
345     public static void writeHeaderXMmsSenderVisibility(OutputStream theOs, int theVisibilityId) throws IOException
346     {
347         WspUtil.writeShortInteger(theOs, MmsConstants.HEADER_ID_X_MMS_SENDER_VISIBILITY);
348         WspUtil.writeShortInteger(theOs, theVisibilityId);
349     }
350 
351     public static void writeHeaderXMmsSenderVisibility(OutputStream theOs, String theVisibility) throws IOException
352     {
353         int visibilityId = StringUtil.findString(MmsConstants.X_MMS_SENDER_VISIBILITY_NAMES, theVisibility
354                 .toLowerCase());
355 
356         if (visibilityId != -1)
357         {
358             writeHeaderXMmsSenderVisibility(theOs, visibilityId);
359         }
360     }
361 
362     public static void writeApplicationHeader(OutputStream theOs, String theName, String theValue) throws IOException
363     {
364         WspUtil.writeTokenText(theOs, theName);
365         WspUtil.writeTextString(theOs, theValue);
366     }
367 }