Skip to content

Package: SunV3BodyPart

SunV3BodyPart

nameinstructionbranchcomplexitylinemethod
SunV3BodyPart(InternetHeaders, byte[])
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getContentType()
M: 26 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getDescription()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getEncoding()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getFileName()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getLineCount()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getSize()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setDataHandler(DataHandler)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setDescription(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setDescription(String, String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setFileName(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
updateHeaders()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
writeTo(OutputStream)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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