KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DateFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import javax.jbi.messaging.MessageExchange;
26 import javax.jbi.messaging.MessagingException;
27 import javax.jbi.messaging.NormalizedMessage;
28 import javax.mail.Address JavaDoc;
29 import javax.mail.internet.AddressException JavaDoc;
30 import javax.mail.internet.InternetAddress JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33
34 import org.apache.servicemix.components.util.MarshalerSupport;
35 import org.apache.servicemix.expression.Expression;
36 import org.apache.servicemix.expression.PropertyExpression;
37
38 /**
39  * A useful base class for mail marshalers.
40  *
41  * @version $Revision: 426415 $
42  */

43 public abstract class MailMarshalerSupport extends MarshalerSupport {
44     private DateFormat JavaDoc dateFormat = DateFormat.getInstance();
45
46     private Expression to = new PropertyExpression("org.apache.servicemix.email.to");
47     private Expression cc = new PropertyExpression("org.apache.servicemix.email.cc");
48     private Expression bcc = new PropertyExpression("org.apache.servicemix.email.bcc");
49     private Expression from = new PropertyExpression("org.apache.servicemix.email.from", "noone@servicemix.org");
50     private Expression text = new PropertyExpression("org.apache.servicemix.email.text");
51     private Expression html = new PropertyExpression("org.apache.servicemix.email.html");
52     private Expression subject = new PropertyExpression("org.apache.servicemix.email.subject", "Message from ServiceMix");
53     private Expression replyTo = new PropertyExpression("org.apache.servicemix.email.replyTo");
54     private Expression sentDate = new PropertyExpression("org.apache.servicemix.email.sentDate");
55     private Expression attachments = new PropertyExpression("org.apache.servicemix.email.attachments");
56
57     public Expression getTo() {
58         return to;
59     }
60
61     public void setTo(Expression to) {
62         this.to = to;
63     }
64
65     public Expression getCc() {
66         return cc;
67     }
68
69     public void setCc(Expression cc) {
70         this.cc = cc;
71     }
72
73     public Expression getBcc() {
74         return bcc;
75     }
76
77     public void setBcc(Expression bcc) {
78         this.bcc = bcc;
79     }
80
81     public Expression getFrom() {
82         return from;
83     }
84
85     public void setFrom(Expression from) {
86         this.from = from;
87     }
88
89     public Expression getText() {
90         return text;
91     }
92
93     public void setText(Expression text) {
94         this.text = text;
95     }
96
97     public Expression getHtml() {
98         return html;
99     }
100
101     public void setHtml(Expression html) {
102         this.html = html;
103     }
104
105     public Expression getSubject() {
106         return subject;
107     }
108
109     public void setSubject(Expression subject) {
110         this.subject = subject;
111     }
112
113     public Expression getReplyTo() {
114         return replyTo;
115     }
116
117     public void setReplyTo(Expression replyTo) {
118         this.replyTo = replyTo;
119     }
120
121     public Expression getSentDate() {
122         return sentDate;
123     }
124
125     public void setSentDate(Expression sentDate) {
126         this.sentDate = sentDate;
127     }
128
129     public DateFormat JavaDoc getDateFormat() {
130         return dateFormat;
131     }
132
133     public void setDateFormat(DateFormat JavaDoc dateFormat) {
134         this.dateFormat = dateFormat;
135     }
136
137     public Expression getAttachments() {
138         return attachments;
139     }
140
141     public void setAttachments(Expression attachments) {
142         this.attachments = attachments;
143     }
144     // Implementation methods
145
//-------------------------------------------------------------------------
146
protected Date JavaDoc getSentDate(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException {
147         return asDate(sentDate.evaluate(exchange, normalizedMessage));
148     }
149
150     protected String JavaDoc getSubject(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException {
151         return asString(subject.evaluate(exchange, normalizedMessage));
152     }
153
154     protected String JavaDoc getText(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, TransformerException JavaDoc {
155         String JavaDoc text = asString(this.text.evaluate(exchange, normalizedMessage));
156         if (text == null) {
157             // lets just turn the XML body to text
158
Source JavaDoc content = normalizedMessage.getContent();
159             if (content != null) {
160                 text = getTransformer().toString(content);
161             }
162         }
163         return text;
164     }
165     
166     protected String JavaDoc getHtml(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, TransformerException JavaDoc {
167         return (this.html != null) ? asString(this.html.evaluate(exchange, normalizedMessage)) : null;
168     }
169
170     protected Address JavaDoc asAddress(Object JavaDoc value) throws AddressException JavaDoc {
171         if (value instanceof Address JavaDoc) {
172             return (Address JavaDoc) value;
173         }
174         if (value instanceof String JavaDoc) {
175             return new InternetAddress JavaDoc((String JavaDoc) value);
176         }
177         if (value != null) {
178             throw new IllegalArgumentException JavaDoc("Expression does not evaluate to an Address. Is of type: " + value.getClass().getName() + " with value: " + value);
179         }
180         return null;
181     }
182
183     protected Date JavaDoc asDate(Object JavaDoc value) {
184         if (value instanceof Date JavaDoc) {
185             return (Date JavaDoc) value;
186         }
187         if (value instanceof String JavaDoc) {
188             String JavaDoc text = (String JavaDoc) value;
189             try {
190                 return dateFormat.parse(text);
191             }
192             catch (ParseException JavaDoc e) {
193                 throw new IllegalArgumentException JavaDoc("Invalid date format for: " + text + ". Reason: " + e);
194             }
195         }
196         if (value != null) {
197             throw new IllegalArgumentException JavaDoc("Expression does not evaluate to a Date. Is of type: " + value.getClass().getName() + " with value: " + value);
198
199         }
200         return null;
201     }
202
203     protected Address JavaDoc[] asAddressArray(Object JavaDoc value) throws AddressException JavaDoc {
204         if (value instanceof Address JavaDoc[]) {
205             return (Address JavaDoc[]) value;
206         }
207         if (value instanceof Collection JavaDoc) {
208             Collection JavaDoc collection = (Collection JavaDoc) value;
209             Address JavaDoc[] answer = new Address JavaDoc[collection.size()];
210             int i = 0;
211             for (Iterator JavaDoc iter = collection.iterator(); iter.hasNext();) {
212                 answer[i++] = asAddress(iter.next());
213             }
214             return answer;
215         }
216         if (value != null) {
217             throw new IllegalArgumentException JavaDoc("Expression does not evaluate to an Address[]. Is of type: " + value.getClass().getName() + " with value: " + value);
218         }
219         return null;
220     }
221
222     protected Object JavaDoc asStringOrStringArray(Object JavaDoc value) {
223         if (value instanceof String JavaDoc) {
224             return value;
225         }
226         if (value instanceof String JavaDoc[]) {
227             return value;
228         }
229         if (value instanceof Collection JavaDoc) {
230             Collection JavaDoc collection = (Collection JavaDoc) value;
231             String JavaDoc[] answer = new String JavaDoc[collection.size()];
232             int i = 0;
233             for (Iterator JavaDoc iter = collection.iterator(); iter.hasNext();) {
234                 answer[i++] = asString(iter.next());
235             }
236             return answer;
237         }
238         if (value != null) {
239             throw new IllegalArgumentException JavaDoc("Expression does not evaluate to a String[]. Is of type: " + value.getClass().getName() + " with value: " + value);
240         }
241         return null;
242     }
243 }
244
Popular Tags