Skip to content

Package: message_rfc822

message_rfc822

nameinstructionbranchcomplexitylinemethod
getContent(DataSource)
M: 36 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getDataFlavors()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
message_rfc822()
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%
static {...}
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
writeTo(Object, String, OutputStream)
M: 56 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 13 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.handlers;
18:
19: import jakarta.activation.ActivationDataFlavor;
20: import jakarta.activation.DataSource;
21: import jakarta.mail.Message;
22: import jakarta.mail.MessageAware;
23: import jakarta.mail.MessageContext;
24: import jakarta.mail.MessagingException;
25: import jakarta.mail.Session;
26: import jakarta.mail.internet.MimeMessage;
27:
28: import java.io.IOException;
29: import java.io.OutputStream;
30: import java.util.Properties;
31:
32:
33: /**
34: * @author Christopher Cotton
35: */
36:
37:
38: public class message_rfc822 extends handler_base {
39:
40: private static ActivationDataFlavor[] ourDataFlavor = {
41: new ActivationDataFlavor(Message.class, "message/rfc822", "Message")
42: };
43:
44: /**
45: * Creates a default {@code message_rfc822}.
46: */
47: public message_rfc822() {
48: }
49:
50: @Override
51: protected ActivationDataFlavor[] getDataFlavors() {
52: return ourDataFlavor;
53: }
54:
55: /**
56: * Return the content.
57: */
58: @Override
59: public Object getContent(DataSource ds) throws IOException {
60: // create a new MimeMessage
61: try {
62: Session session;
63:• if (ds instanceof MessageAware) {
64: MessageContext mc = ((MessageAware) ds).getMessageContext();
65: session = mc.getSession();
66: } else {
67: // Hopefully a rare case. Also hopefully the application
68: // has created a default Session that can just be returned
69: // here. If not, the one we create here is better than
70: // nothing, but overall not a really good answer.
71: session = Session.getDefaultInstance(new Properties(), null);
72: }
73: return new MimeMessage(session, ds.getInputStream());
74: } catch (MessagingException me) {
75: IOException ioex =
76: new IOException("Exception creating MimeMessage in " +
77: "message/rfc822 DataContentHandler");
78: ioex.initCause(me);
79: throw ioex;
80: }
81: }
82:
83: /**
84: * Write the object as a byte stream.
85: */
86: @Override
87: public void writeTo(Object obj, String mimeType, OutputStream os)
88: throws IOException {
89:• if (!(obj instanceof Message))
90: throw new IOException("\"" + getDataFlavors()[0].getMimeType() +
91: "\" DataContentHandler requires Message object, " +
92: "was given object of type " + obj.getClass().toString() +
93: "; obj.cl " + obj.getClass().getClassLoader() +
94: ", Message.cl " + Message.class.getClassLoader());
95:
96: // if the object is a message, we know how to write that out
97: Message m = (Message) obj;
98: try {
99: m.writeTo(os);
100: } catch (MessagingException me) {
101: IOException ioex = new IOException("Exception writing message");
102: ioex.initCause(me);
103: throw ioex;
104: }
105: }
106: }