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;
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
89
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
133
134 String newCt = WspUtil.convertMultipartContentType(ct);
135 theMsg.getContentType().setValue(newCt);
136
137 if (newCt.startsWith("application/vnd.wap.multipart."))
138 {
139
140 writeMultipart(theOs, (MimeMultipart) theMsg);
141 }
142 else
143 {
144
145
146 }
147 }
148 else
149 {
150 theOs.write(theMsg.getBody());
151 }
152 }
153
154
155 private void writeMultipart(OutputStream theOs, MimeMultipart theMultipart) throws IOException
156 {
157
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
167 writeContentType(headers, part);
168 writeHeaders(headers, part);
169
170 headers.close();
171
172
173 writeBody(content, part);
174 content.close();
175
176
177
178
179 WspUtil.writeUintvar(theOs, headers.size());
180
181 WspUtil.writeUintvar(theOs, content.size());
182
183 theOs.write(headers.toByteArray());
184
185 theOs.write(content.toByteArray());
186 }
187 }
188 }