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