]> git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/mbox/SunV3BodyPart.java
Make logging synchronous during native image build
[gpl/argeo-slc.git] / ext / javax.mail.mbox / src / com / sun / mail / mbox / SunV3BodyPart.java
1 /*
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package com.sun.mail.mbox;
18
19 import javax.mail.*;
20 import javax.mail.internet.*;
21 import javax.activation.*;
22 import java.io.*;
23
24 /**
25 * This class represents a SunV3 BodyPart.
26 *
27 * @author Bill Shannon
28 * @see javax.mail.Part
29 * @see javax.mail.internet.MimePart
30 * @see javax.mail.internet.MimeBodyPart
31 */
32
33 public class SunV3BodyPart extends MimeBodyPart {
34 /**
35 * Constructs a SunV3BodyPart using the given header and
36 * content bytes. <p>
37 *
38 * Used by providers.
39 *
40 * @param headers The header of this part
41 * @param content bytes representing the body of this part.
42 */
43 public SunV3BodyPart(InternetHeaders headers, byte[] content)
44 throws MessagingException {
45 super(headers, content);
46 }
47
48 /**
49 * Return the size of the content of this BodyPart in bytes.
50 * Return -1 if the size cannot be determined. <p>
51 *
52 * Note that this number may not be an exact measure of the
53 * content size and may or may not account for any transfer
54 * encoding of the content. <p>
55 *
56 * @return size in bytes
57 */
58 public int getSize() throws MessagingException {
59 String s = getHeader("X-Sun-Content-Length", null);
60 try {
61 return Integer.parseInt(s);
62 } catch (NumberFormatException ex) {
63 return -1;
64 }
65 }
66
67 /**
68 * Return the number of lines for the content of this Part.
69 * Return -1 if this number cannot be determined. <p>
70 *
71 * Note that this number may not be an exact measure of the
72 * content length and may or may not account for any transfer
73 * encoding of the content.
74 */
75 public int getLineCount() throws MessagingException {
76 String s = getHeader("X-Sun-Content-Lines", null);
77 try {
78 return Integer.parseInt(s);
79 } catch (NumberFormatException ex) {
80 return -1;
81 }
82 }
83
84 /*
85 * This is just enough to get us going.
86 *
87 * For more complete transformation from V3 to MIME, refer to
88 * sun_att.c from the Sun IMAP server code.
89 */
90 static class MimeV3Map {
91 String mime;
92 String v3;
93
94 MimeV3Map(String mime, String v3) {
95 this.mime = mime;
96 this.v3 = v3;
97 }
98
99 private static MimeV3Map[] mimeV3Table = new MimeV3Map[] {
100 new MimeV3Map("text/plain", "text"),
101 new MimeV3Map("text/plain", "default"),
102 new MimeV3Map("multipart/x-sun-attachment", "X-sun-attachment"),
103 new MimeV3Map("application/postscript", "postscript-file"),
104 new MimeV3Map("image/gif", "gif-file")
105 // audio-file
106 // cshell-script
107 };
108
109 // V3 Content-Type to MIME Content-Type
110 static String toMime(String s) {
111 for (int i = 0; i < mimeV3Table.length; i++) {
112 if (mimeV3Table[i].v3.equalsIgnoreCase(s))
113 return mimeV3Table[i].mime;
114 }
115 return "application/x-" + s;
116 }
117
118 // MIME Content-Type to V3 Content-Type
119 static String toV3(String s) {
120 for (int i = 0; i < mimeV3Table.length; i++) {
121 if (mimeV3Table[i].mime.equalsIgnoreCase(s))
122 return mimeV3Table[i].v3;
123 }
124 return s;
125 }
126 }
127
128 /**
129 * Returns the value of the RFC822 "Content-Type" header field.
130 * This represents the content-type of the content of this
131 * BodyPart. This value must not be null. If this field is
132 * unavailable, "text/plain" should be returned. <p>
133 *
134 * This implementation uses <code>getHeader(name)</code>
135 * to obtain the requisite header field.
136 *
137 * @return Content-Type of this BodyPart
138 */
139 public String getContentType() throws MessagingException {
140 String ct = getHeader("Content-Type", null);
141 if (ct == null)
142 ct = getHeader("X-Sun-Data-Type", null);
143 if (ct == null)
144 ct = "text/plain";
145 else if (ct.indexOf('/') < 0)
146 ct = MimeV3Map.toMime(ct);
147 return ct;
148 }
149
150 /**
151 * Returns the value of the "Content-Transfer-Encoding" header
152 * field. Returns <code>null</code> if the header is unavailable
153 * or its value is absent. <p>
154 *
155 * This implementation uses <code>getHeader(name)</code>
156 * to obtain the requisite header field.
157 *
158 * @see #headers
159 */
160 public String getEncoding() throws MessagingException {
161 String enc = super.getEncoding();
162 if (enc == null)
163 enc = getHeader("X-Sun-Encoding-Info", null);
164 return enc;
165 }
166
167 /**
168 * Returns the "Content-Description" header field of this BodyPart.
169 * This typically associates some descriptive information with
170 * this part. Returns null if this field is unavailable or its
171 * value is absent. <p>
172 *
173 * If the Content-Description field is encoded as per RFC 2047,
174 * it is decoded and converted into Unicode. If the decoding or
175 * conversion fails, the raw data is returned as-is <p>
176 *
177 * This implementation uses <code>getHeader(name)</code>
178 * to obtain the requisite header field.
179 *
180 * @return content-description
181 */
182 public String getDescription() throws MessagingException {
183 String desc = super.getDescription();
184 if (desc == null)
185 desc = getHeader("X-Sun-Data-Description", null);
186 return desc;
187 }
188
189 /**
190 * Set the "Content-Description" header field for this BodyPart.
191 * If the description parameter is <code>null</code>, then any
192 * existing "Content-Description" fields are removed. <p>
193 *
194 * If the description contains non US-ASCII characters, it will
195 * be encoded using the platform's default charset. If the
196 * description contains only US-ASCII characters, no encoding
197 * is done and it is used as-is.
198 *
199 * @param description content-description
200 * @exception IllegalWriteException if the underlying
201 * implementation does not support modification
202 * @exception IllegalStateException if this BodyPart is
203 * obtained from a READ_ONLY folder.
204 */
205 public void setDescription(String description) throws MessagingException {
206 throw new MethodNotSupportedException("SunV3BodyPart not writable");
207 }
208
209 /**
210 * Set the "Content-Description" header field for this BodyPart.
211 * If the description parameter is <code>null</code>, then any
212 * existing "Content-Description" fields are removed. <p>
213 *
214 * If the description contains non US-ASCII characters, it will
215 * be encoded using the specified charset. If the description
216 * contains only US-ASCII characters, no encoding is done and
217 * it is used as-is
218 *
219 * @param description Description
220 * @param charset Charset for encoding
221 * @exception IllegalWriteException if the underlying
222 * implementation does not support modification
223 * @exception IllegalStateException if this BodyPart is
224 * obtained from a READ_ONLY folder.
225 */
226 public void setDescription(String description, String charset)
227 throws MessagingException {
228 throw new MethodNotSupportedException("SunV3BodyPart not writable");
229 }
230
231 /**
232 * Get the filename associated with this BodyPart. <p>
233 *
234 * Returns the value of the "filename" parameter from the
235 * "Content-Disposition" header field of this BodyPart. If its
236 * not available, returns the value of the "name" parameter from
237 * the "Content-Type" header field of this BodyPart.
238 * Returns <code>null</code> if both are absent.
239 *
240 * @return filename
241 */
242 public String getFileName() throws MessagingException {
243 String name = super.getFileName();
244 if (name == null)
245 name = getHeader("X-Sun-Data-Name", null);
246 return name;
247 }
248
249 /**
250 * Set the filename associated with this BodyPart, if possible. <p>
251 *
252 * Sets the "filename" parameter of the "Content-Disposition"
253 * header field of this BodyPart.
254 *
255 * @exception IllegalWriteException if the underlying
256 * implementation does not support modification
257 * @exception IllegalStateException if this BodyPart is
258 * obtained from a READ_ONLY folder.
259 */
260 public void setFileName(String filename) throws MessagingException {
261 throw new MethodNotSupportedException("SunV3BodyPart not writable");
262 }
263
264 /**
265 * This method provides the mechanism to set this BodyPart's content.
266 * The given DataHandler object should wrap the actual content.
267 *
268 * @param dh The DataHandler for the content
269 * @exception IllegalWriteException if the underlying
270 * implementation does not support modification
271 * @exception IllegalStateException if this BodyPart is
272 * obtained from a READ_ONLY folder.
273 */
274 public void setDataHandler(DataHandler dh)
275 throws MessagingException {
276 throw new MethodNotSupportedException("SunV3BodyPart not writable");
277 }
278
279 /**
280 * Output the BodyPart as a RFC822 format stream.
281 *
282 * @exception MessagingException
283 * @exception IOException if an error occurs writing to the
284 * stream or if an error is generated
285 * by the javax.activation layer.
286 * @see javax.activation.DataHandler#writeTo()
287 */
288 public void writeTo(OutputStream os)
289 throws IOException, MessagingException {
290 throw new MethodNotSupportedException("SunV3BodyPart writeTo");
291 }
292
293 /**
294 * This is the method that has the 'smarts' to query the 'content'
295 * and update the appropriate headers. Typical headers that get
296 * set here are: Content-Type, Content-Encoding, boundary (for
297 * multipart). Now, the tricky part here is when to actually
298 * activate this method:
299 *
300 * - A Message being crafted by a mail-application will certainly
301 * need to activate this method at some point to fill up its internal
302 * headers. Typically this is triggered off by our writeTo() method.
303 *
304 * - A message read-in from a MessageStore will have obtained
305 * all its headers from the store, and so does'nt need this.
306 * However, if this message is editable and if any edits have
307 * been made to either the content or message-structure, we might
308 * need to resync our headers. Typically this is triggered off by
309 * the Message.saveChanges() methods.
310 */
311 protected void updateHeaders() throws MessagingException {
312 throw new MethodNotSupportedException("SunV3BodyPart updateHeaders");
313 }
314 }