1 17 package org.apache.servicemix.components.email; 18 19 import java.text.DateFormat ; 20 import java.text.ParseException ; 21 import java.util.Collection ; 22 import java.util.Date ; 23 import java.util.Iterator ; 24 25 import javax.jbi.messaging.MessageExchange; 26 import javax.jbi.messaging.MessagingException; 27 import javax.jbi.messaging.NormalizedMessage; 28 import javax.mail.Address ; 29 import javax.mail.internet.AddressException ; 30 import javax.mail.internet.InternetAddress ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.TransformerException ; 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 43 public abstract class MailMarshalerSupport extends MarshalerSupport { 44 private DateFormat 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 getDateFormat() { 130 return dateFormat; 131 } 132 133 public void setDateFormat(DateFormat 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 protected Date getSentDate(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException { 147 return asDate(sentDate.evaluate(exchange, normalizedMessage)); 148 } 149 150 protected String getSubject(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException { 151 return asString(subject.evaluate(exchange, normalizedMessage)); 152 } 153 154 protected String getText(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, TransformerException { 155 String text = asString(this.text.evaluate(exchange, normalizedMessage)); 156 if (text == null) { 157 Source content = normalizedMessage.getContent(); 159 if (content != null) { 160 text = getTransformer().toString(content); 161 } 162 } 163 return text; 164 } 165 166 protected String getHtml(MessageExchange exchange, NormalizedMessage normalizedMessage) throws MessagingException, TransformerException { 167 return (this.html != null) ? asString(this.html.evaluate(exchange, normalizedMessage)) : null; 168 } 169 170 protected Address asAddress(Object value) throws AddressException { 171 if (value instanceof Address ) { 172 return (Address ) value; 173 } 174 if (value instanceof String ) { 175 return new InternetAddress ((String ) value); 176 } 177 if (value != null) { 178 throw new IllegalArgumentException ("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 asDate(Object value) { 184 if (value instanceof Date ) { 185 return (Date ) value; 186 } 187 if (value instanceof String ) { 188 String text = (String ) value; 189 try { 190 return dateFormat.parse(text); 191 } 192 catch (ParseException e) { 193 throw new IllegalArgumentException ("Invalid date format for: " + text + ". Reason: " + e); 194 } 195 } 196 if (value != null) { 197 throw new IllegalArgumentException ("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 [] asAddressArray(Object value) throws AddressException { 204 if (value instanceof Address []) { 205 return (Address []) value; 206 } 207 if (value instanceof Collection ) { 208 Collection collection = (Collection ) value; 209 Address [] answer = new Address [collection.size()]; 210 int i = 0; 211 for (Iterator iter = collection.iterator(); iter.hasNext();) { 212 answer[i++] = asAddress(iter.next()); 213 } 214 return answer; 215 } 216 if (value != null) { 217 throw new IllegalArgumentException ("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 asStringOrStringArray(Object value) { 223 if (value instanceof String ) { 224 return value; 225 } 226 if (value instanceof String []) { 227 return value; 228 } 229 if (value instanceof Collection ) { 230 Collection collection = (Collection ) value; 231 String [] answer = new String [collection.size()]; 232 int i = 0; 233 for (Iterator iter = collection.iterator(); iter.hasNext();) { 234 answer[i++] = asString(iter.next()); 235 } 236 return answer; 237 } 238 if (value != null) { 239 throw new IllegalArgumentException ("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 |