KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > email > SimpleMailMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.email;
18
19 import java.util.Date JavaDoc;
20
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.messaging.MessagingException;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.mail.internet.AddressException JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26
27 import org.springframework.mail.SimpleMailMessage;
28
29 /**
30  * The default marshaler from the {@link NormalizedMessage} to a {@link SimpleMailMessage} using
31  * expressions for each field required on the email.
32  *
33  * @version $Revision: 429277 $
34  */

35 public class SimpleMailMarshaler extends MailMarshalerSupport{
36
37     /**
38      * Populates the mime email message with values extracted from the message exchange using expressions.
39      *
40      * @param mailMessage the mime email
41      * @param exchange the JBI message exchange
42      * @param normalizedMessage the normalized message from JBI
43      * @throws javax.mail.MessagingException if the message could not be constructed or there was an error creating an address
44      */

45     public void prepareMessage(SimpleMailMessage mailMessage, MessageExchange exchange, NormalizedMessage normalizedMessage) throws javax.mail.MessagingException JavaDoc {
46         try {
47             Object JavaDoc to = getTo(exchange, normalizedMessage);
48             if (to != null) {
49                 if (to instanceof String JavaDoc) {
50                     mailMessage.setTo((String JavaDoc) to);
51                 }
52                 else {
53                     mailMessage.setTo((String JavaDoc[]) to);
54                 }
55             }
56             Object JavaDoc cc = getCc(exchange, normalizedMessage);
57             if (cc != null) {
58                 if (cc instanceof String JavaDoc) {
59                     mailMessage.setCc((String JavaDoc) cc);
60                 }
61                 else {
62                     mailMessage.setCc((String JavaDoc[]) cc);
63                 }
64             }
65             Object JavaDoc bcc = getBcc(exchange, normalizedMessage);
66             if (bcc != null) {
67                 if (bcc instanceof String JavaDoc) {
68                     mailMessage.setBcc((String JavaDoc) bcc);
69                 }
70                 else {
71                     mailMessage.setBcc((String JavaDoc[]) bcc);
72                 }
73             }
74             String JavaDoc from = getFrom(exchange, normalizedMessage);
75             if (from != null) {
76                 mailMessage.setFrom(from);
77             }
78             String JavaDoc replyTo = getReplyTo(exchange, normalizedMessage);
79             if (replyTo != null) {
80                 mailMessage.setReplyTo(replyTo);
81             }
82
83             String JavaDoc text = getText(exchange, normalizedMessage);
84             if (text != null) {
85                 mailMessage.setText(text);
86             }
87             String JavaDoc subject = getSubject(exchange, normalizedMessage);
88             if (subject != null) {
89                 mailMessage.setSubject(subject);
90             }
91             Date JavaDoc sentDate = getSentDate(exchange, normalizedMessage);
92             if (sentDate != null) {
93                 mailMessage.setSentDate(sentDate);
94             }
95         }
96         catch (MessagingException e) {
97             throw new javax.mail.MessagingException JavaDoc(e.getMessage(), e);
98         }
99         catch (TransformerException JavaDoc e) {
100             throw new javax.mail.MessagingException JavaDoc(e.getMessage(), e);
101         }
102     }
103
104     // Implementation methods
105
//-------------------------------------------------------------------------
106
protected String JavaDoc getFrom(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException JavaDoc {
107         return asString(getFrom().evaluate(exchange, normalizedMessage));
108     }
109
110     protected String JavaDoc getReplyTo(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException {
111         return asString(getReplyTo().evaluate(exchange, normalizedMessage));
112     }
113
114     protected Object JavaDoc getTo(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException JavaDoc {
115         return asStringOrStringArray(getTo().evaluate(exchange, normalizedMessage));
116     }
117
118     protected Object JavaDoc getCc(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException JavaDoc {
119         return asStringOrStringArray(getCc().evaluate(exchange, normalizedMessage));
120     }
121
122     protected Object JavaDoc getBcc(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, AddressException JavaDoc {
123         return asStringOrStringArray(getBcc().evaluate(exchange, normalizedMessage));
124     }
125
126
127 }
128
Popular Tags