1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
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
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
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
310
311 ByteArrayOutputStream baos = new ByteArrayOutputStream();
312
313
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
332
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 }