Skip to content

Package: text_rfc822headers

text_rfc822headers

nameinstructionbranchcomplexitylinemethod
getCharset(String)
M: 19 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getContent(DataSource)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getStringContent(DataSource)
M: 88 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
getTransferData(ActivationDataFlavor, DataSource)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getTransferDataFlavors()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
text_rfc822headers()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 88 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 24 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.dsn;
18:
19: import java.io.*;
20: import jakarta.activation.*;
21: import jakarta.mail.*;
22: import jakarta.mail.internet.*;
23:
24: /**
25: * DataContentHandler for text/rfc822-headers MIME type.
26: * Applications should not use this class directly, it's used indirectly
27: * through the JavaBeans Activation Framework.
28: *
29: * @since        JavaMail 1.4
30: *
31: */
32: public class text_rfc822headers implements DataContentHandler {
33: private static final ActivationDataFlavor myDF = new ActivationDataFlavor(
34:         MessageHeaders.class,
35:         "text/rfc822-headers",
36:         "RFC822 headers");
37: private static final ActivationDataFlavor myDFs = new ActivationDataFlavor(
38:         java.lang.String.class,
39:         "text/rfc822-headers",
40:         "RFC822 headers");
41:
42: /**
43: * Creates a default {@code text_rfc822headers}.
44: */
45: public text_rfc822headers() {
46: }
47:
48: /**
49: * Return the ActivationDataFlavors for this <code>DataContentHandler</code>.
50: *
51: * @return The ActivationDataFlavors
52: */
53: public ActivationDataFlavor[] getTransferDataFlavors() {
54:         return new ActivationDataFlavor[] { myDF, myDFs };
55: }
56:
57: /**
58: * Return the Transfer Data of type ActivationDataFlavor from InputStream.
59: *
60: * @param df The ActivationDataFlavor
61: * @param ds The DataSource corresponding to the data
62: * @return String object
63: */
64: public Object getTransferData(ActivationDataFlavor df, DataSource ds)
65:                         throws IOException {
66:         // use myDF.equals to be sure to get ActivationDataFlavor.equals,
67:         // which properly ignores Content-Type parameters in comparison
68:•        if (myDF.equals(df))
69:          return getContent(ds);
70:•        else if (myDFs.equals(df))
71:          return getStringContent(ds);
72:         else
73:          return null;
74: }
75:
76: public Object getContent(DataSource ds) throws IOException {
77:         try {
78:          return new MessageHeaders(ds.getInputStream());
79:         } catch (MessagingException mex) {
80: //System.out.println("Exception creating MessageHeaders: " + mex);
81:          throw new IOException("Exception creating MessageHeaders: " + mex);
82:         }
83: }
84:
85: private Object getStringContent(DataSource ds) throws IOException {
86:         String enc = null;
87:         InputStreamReader is = null;
88:
89:         try {
90:          enc = getCharset(ds.getContentType());
91:          is = new InputStreamReader(ds.getInputStream(), enc);
92:         } catch (IllegalArgumentException iex) {
93:          /*
94:          * An unknown charset of the form ISO-XXX-XXX will cause
95:          * the JDK to throw an IllegalArgumentException. The
96:          * JDK will attempt to create a classname using this string,
97:          * but valid classnames must not contain the character '-',
98:          * and this results in an IllegalArgumentException, rather than
99:          * the expected UnsupportedEncodingException. Yikes.
100:          */
101:          throw new UnsupportedEncodingException(enc);
102:         }
103:
104:         try {
105:          int pos = 0;
106:          int count;
107:          char buf[] = new char[1024];
108:
109:•         while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
110:                 pos += count;
111:•                if (pos >= buf.length) {
112:                  int size = buf.length;
113:•                 if (size < 256*1024)
114:                         size += size;
115:                  else
116:                         size += 256*1024;
117:                  char tbuf[] = new char[size];
118:                  System.arraycopy(buf, 0, tbuf, 0, pos);
119:                  buf = tbuf;
120:                 }
121:          }
122:          return new String(buf, 0, pos);
123:         } finally {
124:          try {
125:                 is.close();
126:          } catch (IOException ex) {
127:                 // ignore it
128:          }
129:         }
130: }
131:
132: /**
133: * Write the object to the output stream, using the specified MIME type.
134: */
135: public void writeTo(Object obj, String type, OutputStream os)
136:                         throws IOException {
137:•        if (obj instanceof MessageHeaders) {
138:          MessageHeaders mh = (MessageHeaders)obj;
139:          try {
140:                 mh.writeTo(os);
141:          } catch (MessagingException mex) {
142:                 Exception ex = mex.getNextException();
143:•                if (ex instanceof IOException)
144:                  throw (IOException)ex;
145:                 else
146:                  throw new IOException("Exception writing headers: " + mex);
147:          }
148:          return;
149:         }
150:•        if (!(obj instanceof String))
151:          throw new IOException("\"" + myDFs.getMimeType() +
152:                 "\" DataContentHandler requires String object, " +
153:                 "was given object of type " + obj.getClass().toString());
154:
155:         String enc = null;
156:         OutputStreamWriter osw = null;
157:
158:         try {
159:          enc = getCharset(type);
160:          osw = new OutputStreamWriter(os, enc);
161:         } catch (IllegalArgumentException iex) {
162:          /*
163:          * An unknown charset of the form ISO-XXX-XXX will cause
164:          * the JDK to throw an IllegalArgumentException. The
165:          * JDK will attempt to create a classname using this string,
166:          * but valid classnames must not contain the character '-',
167:          * and this results in an IllegalArgumentException, rather than
168:          * the expected UnsupportedEncodingException. Yikes.
169:          */
170:          throw new UnsupportedEncodingException(enc);
171:         }
172:
173:         String s = (String)obj;
174:         osw.write(s, 0, s.length());
175:         osw.flush();
176: }
177:
178: private String getCharset(String type) {
179:         try {
180:          ContentType ct = new ContentType(type);
181:          String charset = ct.getParameter("charset");
182:•         if (charset == null)
183:                 // If the charset parameter is absent, use US-ASCII.
184:                 charset = "us-ascii";
185:          return MimeUtility.javaCharset(charset);
186:         } catch (Exception ex) {
187:          return null;
188:         }
189: }
190: }