1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.james.util.RFC2822Headers; 21 import org.apache.mailet.GenericMailet; 22 import org.apache.mailet.Mail; 23 import org.apache.mailet.MailAddress; 24 import org.apache.mailet.MailetException; 25 26 import javax.mail.Address ; 27 import javax.mail.Message ; 28 import javax.mail.MessagingException ; 29 import javax.mail.Session ; 30 import javax.mail.internet.InternetAddress ; 31 import javax.mail.internet.MimeBodyPart ; 32 import javax.mail.internet.MimeMessage ; 33 import javax.mail.internet.MimeMultipart ; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.io.StringWriter ; 37 import java.util.Date ; 38 import java.util.HashSet ; 39 import java.util.Set ; 40 import java.util.Collection ; 41 import java.util.Iterator ; 42 43 81 public abstract class AbstractNotify extends AbstractRedirect { 82 83 84 85 86 87 90 protected boolean getPassThrough() throws MessagingException { 91 if(getInitParameter("passThrough") == null) { 92 return true; 93 } else { 94 return new Boolean (getInitParameter("passThrough")).booleanValue(); 95 } 96 } 97 98 101 protected int getInLineType() throws MessagingException { 102 if(getInitParameter("inline") == null) { 103 return NONE; 104 } else { 105 return getTypeCode(getInitParameter("inline")); 106 } 107 } 108 109 112 protected int getAttachmentType() throws MessagingException { 113 if(getInitParameter("attachment") == null) { 114 return MESSAGE; 115 } else { 116 return getTypeCode(getInitParameter("attachment")); 117 } 118 } 119 120 125 protected String getMessage() { 126 if(getInitParameter("notice") == null) { 127 if(getInitParameter("message") == null) { 128 return "We were unable to deliver the attached message because of an error in the mail server."; 129 } else { 130 return getInitParameter("message"); 131 } 132 } else { 133 return getInitParameter("notice"); 134 } 135 } 136 137 140 protected String getMessage(Mail originalMail) throws MessagingException { 141 MimeMessage message = originalMail.getMessage(); 142 StringWriter sout = new StringWriter (); 143 PrintWriter out = new PrintWriter (sout, true); 144 145 out.println(getMessage()); 148 if (originalMail.getErrorMessage() != null) { 150 out.println(); 151 out.println("Error message below:"); 152 out.println(originalMail.getErrorMessage()); 153 } 154 out.println(); 155 out.println("Message details:"); 156 157 if (message.getSubject() != null) { 158 out.println(" Subject: " + message.getSubject()); 159 } 160 if (message.getSentDate() != null) { 161 out.println(" Sent date: " + message.getSentDate()); 162 } 163 out.println(" MAIL FROM: " + originalMail.getSender()); 164 Iterator rcptTo = originalMail.getRecipients().iterator(); 165 out.println(" RCPT TO: " + rcptTo.next()); 166 while (rcptTo.hasNext()) { 167 out.println(" " + rcptTo.next()); 168 } 169 String [] addresses = null; 170 addresses = message.getHeader(RFC2822Headers.FROM); 171 if (addresses != null) { 172 out.print(" From: "); 173 for (int i = 0; i < addresses.length; i++) { 174 out.print(addresses[i] + " "); 175 } 176 out.println(); 177 } 178 addresses = message.getHeader(RFC2822Headers.TO); 179 if (addresses != null) { 180 out.print(" To: "); 181 for (int i = 0; i < addresses.length; i++) { 182 out.print(addresses[i] + " "); 183 } 184 out.println(); 185 } 186 addresses = message.getHeader(RFC2822Headers.CC); 187 if (addresses != null) { 188 out.print(" CC: "); 189 for (int i = 0; i < addresses.length; i++) { 190 out.print(addresses[i] + " "); 191 } 192 out.println(); 193 } 194 out.println(" Size (in bytes): " + message.getSize()); 195 if (message.getLineCount() >= 0) { 196 out.println(" Number of lines: " + message.getLineCount()); 197 } 198 199 return sout.toString(); 200 } 201 202 abstract protected Collection getRecipients() throws MessagingException ; 204 205 208 protected InternetAddress [] getTo() throws MessagingException { 209 return null; 210 } 211 212 215 protected MailAddress getReplyTo() throws MessagingException { 216 return SpecialAddress.NULL; 217 } 218 219 222 protected MailAddress getReversePath(Mail originalMail) throws MessagingException { 223 return getSender(originalMail); 224 } 225 226 237 protected MailAddress getSender() throws MessagingException { 238 String addressString = getInitParameter("sendingAddress"); 239 240 if (addressString == null) { 241 addressString = getInitParameter("sender"); 242 if (addressString == null) { 243 return getMailetContext().getPostmaster(); 244 } 245 } 246 247 MailAddress specialAddress = getSpecialAddress(addressString, 248 new String [] {"postmaster", "sender", "unaltered"}); 249 if (specialAddress != null) { 250 return specialAddress; 251 } 252 253 try { 254 return new MailAddress(addressString); 255 } catch(Exception e) { 256 throw new MessagingException ("Exception thrown in getSender() parsing: " + addressString, e); 257 } 258 } 259 260 263 protected String getSubject() throws MessagingException { 264 return null; 265 } 266 267 270 protected String getSubjectPrefix() { 271 if(getInitParameter("prefix") == null) { 272 return "Re:"; 273 } else { 274 return getInitParameter("prefix"); 275 } 276 } 277 278 282 protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail) throws MessagingException { 283 String subject = originalMail.getMessage().getSubject(); 284 if (subject == null) { 285 subject = ""; 286 } 287 if (subjectPrefix==null || subject.indexOf(subjectPrefix) == 0) { 288 newMail.getMessage().setSubject(subject); 289 } else { 290 newMail.getMessage().setSubject(subjectPrefix + subject); 291 } 292 } 293 294 297 protected boolean isReply() { 298 return true; 299 } 300 301 302 303 304 305 } 306 | Popular Tags |