]>
git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/mbox/SunV3BodyPart.java
2 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
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.
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.
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
17 package com
.sun
.mail
.mbox
;
20 import javax
.mail
.internet
.*;
21 import javax
.activation
.*;
25 * This class represents a SunV3 BodyPart.
27 * @author Bill Shannon
28 * @see javax.mail.Part
29 * @see javax.mail.internet.MimePart
30 * @see javax.mail.internet.MimeBodyPart
33 public class SunV3BodyPart
extends MimeBodyPart
{
35 * Constructs a SunV3BodyPart using the given header and
40 * @param headers The header of this part
41 * @param content bytes representing the body of this part.
43 public SunV3BodyPart(InternetHeaders headers
, byte[] content
)
44 throws MessagingException
{
45 super(headers
, content
);
49 * Return the size of the content of this BodyPart in bytes.
50 * Return -1 if the size cannot be determined. <p>
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>
56 * @return size in bytes
58 public int getSize() throws MessagingException
{
59 String s
= getHeader("X-Sun-Content-Length", null);
61 return Integer
.parseInt(s
);
62 } catch (NumberFormatException ex
) {
68 * Return the number of lines for the content of this Part.
69 * Return -1 if this number cannot be determined. <p>
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.
75 public int getLineCount() throws MessagingException
{
76 String s
= getHeader("X-Sun-Content-Lines", null);
78 return Integer
.parseInt(s
);
79 } catch (NumberFormatException ex
) {
85 * This is just enough to get us going.
87 * For more complete transformation from V3 to MIME, refer to
88 * sun_att.c from the Sun IMAP server code.
90 static class MimeV3Map
{
94 MimeV3Map(String mime
, String v3
) {
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")
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
;
115 return "application/x-" + s
;
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
;
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>
134 * This implementation uses <code>getHeader(name)</code>
135 * to obtain the requisite header field.
137 * @return Content-Type of this BodyPart
139 public String
getContentType() throws MessagingException
{
140 String ct
= getHeader("Content-Type", null);
142 ct
= getHeader("X-Sun-Data-Type", null);
145 else if (ct
.indexOf('/') < 0)
146 ct
= MimeV3Map
.toMime(ct
);
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>
155 * This implementation uses <code>getHeader(name)</code>
156 * to obtain the requisite header field.
160 public String
getEncoding() throws MessagingException
{
161 String enc
= super.getEncoding();
163 enc
= getHeader("X-Sun-Encoding-Info", null);
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>
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>
177 * This implementation uses <code>getHeader(name)</code>
178 * to obtain the requisite header field.
180 * @return content-description
182 public String
getDescription() throws MessagingException
{
183 String desc
= super.getDescription();
185 desc
= getHeader("X-Sun-Data-Description", null);
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>
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.
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.
205 public void setDescription(String description
) throws MessagingException
{
206 throw new MethodNotSupportedException("SunV3BodyPart not writable");
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>
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
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.
226 public void setDescription(String description
, String charset
)
227 throws MessagingException
{
228 throw new MethodNotSupportedException("SunV3BodyPart not writable");
232 * Get the filename associated with this BodyPart. <p>
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.
242 public String
getFileName() throws MessagingException
{
243 String name
= super.getFileName();
245 name
= getHeader("X-Sun-Data-Name", null);
250 * Set the filename associated with this BodyPart, if possible. <p>
252 * Sets the "filename" parameter of the "Content-Disposition"
253 * header field of this BodyPart.
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.
260 public void setFileName(String filename
) throws MessagingException
{
261 throw new MethodNotSupportedException("SunV3BodyPart not writable");
265 * This method provides the mechanism to set this BodyPart's content.
266 * The given DataHandler object should wrap the actual content.
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.
274 public void setDataHandler(DataHandler dh
)
275 throws MessagingException
{
276 throw new MethodNotSupportedException("SunV3BodyPart not writable");
280 * Output the BodyPart as a RFC822 format stream.
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()
288 public void writeTo(OutputStream os
)
289 throws IOException
, MessagingException
{
290 throw new MethodNotSupportedException("SunV3BodyPart writeTo");
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:
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.
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.
311 protected void updateHeaders() throws MessagingException
{
312 throw new MethodNotSupportedException("SunV3BodyPart updateHeaders");