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.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
130
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
167 strBuff.append("; " + headerParam.getName() + "=" + headerParam.getValue());
168 }
169
170
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
197 theOs.write(boundary.getBytes());
198 theOs.write("\r\n".getBytes());
199
200
201 writeContentType(theOs, part);
202 writeHeaders(theOs, part);
203
204
205 writeBody(theOs, part);
206 }
207
208 theOs.write(boundary.getBytes());
209 theOs.write("--\r\n".getBytes());
210 }
211 }