1 17 18 package org.apache.james.core; 19 20 import org.apache.avalon.framework.activity.Disposable; 21 22 import org.apache.james.util.RFC2822Headers; 23 import org.apache.mailet.Mail; 24 import org.apache.mailet.MailAddress; 25 26 import javax.mail.Address ; 27 import javax.mail.Message ; 28 import javax.mail.MessagingException ; 29 import javax.mail.internet.InternetAddress ; 30 import javax.mail.internet.MimeMessage ; 31 import javax.mail.internet.ParseException ; 32 import java.io.*; 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.Date ; 36 import java.util.Enumeration ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.HashMap ; 40 41 53 public class MailImpl implements Disposable, Mail { 54 58 public static final long serialVersionUID = -4289663364703986260L; 59 62 private String errorMessage; 63 66 private String state; 67 70 private MimeMessage message; 71 74 private MailAddress sender; 75 78 private Collection recipients; 79 82 private String name; 83 86 private String remoteHost = "localhost"; 87 90 private String remoteAddr = "127.0.0.1"; 91 94 private Date lastUpdated = new Date (); 95 98 private HashMap attributes; 99 102 public MailImpl() { 103 setState(Mail.DEFAULT); 104 attributes = new HashMap (); 105 } 106 114 public MailImpl(String name, MailAddress sender, Collection recipients) { 115 this(); 116 this.name = name; 117 this.sender = sender; 118 this.recipients = null; 119 120 if (recipients != null) { 122 Iterator theIterator = recipients.iterator(); 123 this.recipients = new ArrayList (); 124 while (theIterator.hasNext()) { 125 this.recipients.add(theIterator.next()); 126 } 127 } 128 } 129 130 139 public MailImpl(String name, MailAddress sender, Collection recipients, InputStream messageIn) 140 throws MessagingException { 141 this(name, sender, recipients); 142 MimeMessageSource source = new MimeMessageInputStreamSource(name, messageIn); 143 MimeMessageWrapper wrapper = new MimeMessageWrapper(source); 144 this.setMessage(wrapper); 145 } 146 147 156 public MailImpl(String name, MailAddress sender, Collection recipients, MimeMessage message) { 157 this(name, sender, recipients); 158 this.setMessage(message); 159 } 160 161 165 public MailImpl(MimeMessage message) throws MessagingException { 166 this(); 167 MailAddress sender = getReturnPath(message); 168 Collection recipients = null; 169 Address[] addresses = message.getRecipients(MimeMessage.RecipientType.TO); 170 if (addresses != null) { 171 recipients = new ArrayList (); 172 for (int i = 0; i < addresses.length; i++) { 173 try { 174 recipients.add(new MailAddress(new InternetAddress (addresses[i].toString(), false))); 175 } catch (ParseException pe) { 176 try { 179 recipients.add(new MailAddress("<" + new InternetAddress (addresses[i].toString()).toString() + ">")); 180 } catch (ParseException _) { 181 throw new MessagingException ("Could not parse address: " + addresses[i].toString() + " from " + message.getHeader(RFC2822Headers.TO, ", "), pe); 182 } 183 } 184 } 185 } 186 this.name = message.toString(); 187 this.sender = sender; 188 this.recipients = recipients; 189 this.setMessage(message); 190 } 191 196 private MailAddress getReturnPath(MimeMessage message) throws MessagingException { 197 MailAddress mailAddress = null; 198 String [] returnPathHeaders = message.getHeader(RFC2822Headers.RETURN_PATH); 199 String returnPathHeader = null; 200 if (returnPathHeaders != null) { 201 returnPathHeader = returnPathHeaders[0]; 202 if (returnPathHeader != null) { 203 returnPathHeader = returnPathHeader.trim(); 204 if (!returnPathHeader.equals("<>")) { 205 try { 206 mailAddress = new MailAddress(new InternetAddress (returnPathHeader, false)); 207 } catch (ParseException pe) { 208 throw new MessagingException ("Could not parse address: " + returnPathHeader + " from " + message.getHeader(RFC2822Headers.RETURN_PATH, ", "), pe); 209 } 210 } 211 } 212 } 213 return mailAddress; 214 } 215 220 public Mail duplicate() { 221 return duplicate(name); 222 } 223 231 public Mail duplicate(String newName) { 232 try { 233 MailImpl newMail = new MailImpl(newName, sender, recipients, getMessage()); 234 newMail.setRemoteHost(remoteHost); 235 newMail.setRemoteAddr(remoteAddr); 236 newMail.setLastUpdated(lastUpdated); 237 newMail.setAttributesRaw((HashMap ) attributes.clone()); 238 return newMail; 239 } catch (MessagingException me) { 240 } 242 return (Mail) null; 243 } 244 249 public String getErrorMessage() { 250 return errorMessage; 251 } 252 257 public MimeMessage getMessage() throws MessagingException { 258 return message; 259 } 260 265 public void setName(String name) { 266 this.name = name; 267 } 268 273 public String getName() { 274 return name; 275 } 276 281 public Collection getRecipients() { 282 return recipients; 283 } 284 289 public MailAddress getSender() { 290 return sender; 291 } 292 297 public String getState() { 298 return state; 299 } 300 305 public String getRemoteHost() { 306 return remoteHost; 307 } 308 313 public String getRemoteAddr() { 314 return remoteAddr; 315 } 316 321 public Date getLastUpdated() { 322 return lastUpdated; 323 } 324 336 public long getMessageSize() throws MessagingException { 337 if (message instanceof MimeMessageWrapper) { 340 MimeMessageWrapper wrapper = (MimeMessageWrapper) message; 341 return wrapper.getMessageSize(); 342 } 343 long size = message.getSize(); 347 Enumeration e = message.getAllHeaderLines(); 348 while (e.hasMoreElements()) { 349 size += ((String ) e.nextElement()).length(); 350 } 351 return size; 352 } 353 358 public void setErrorMessage(String msg) { 359 this.errorMessage = msg; 360 } 361 366 public void setMessage(MimeMessage message) { 367 this.message = message; 368 } 369 374 public void setRecipients(Collection recipients) { 375 this.recipients = recipients; 376 } 377 382 public void setSender(MailAddress sender) { 383 this.sender = sender; 384 } 385 390 public void setState(String state) { 391 this.state = state; 392 } 393 398 public void setRemoteHost(String remoteHost) { 399 this.remoteHost = remoteHost; 400 } 401 406 public void setRemoteAddr(String remoteAddr) { 407 this.remoteAddr = remoteAddr; 408 } 409 414 public void setLastUpdated(Date lastUpdated) { 415 if (lastUpdated != null) { 418 lastUpdated = new Date (lastUpdated.getTime()); 419 } 420 this.lastUpdated = lastUpdated; 421 } 422 430 public void writeMessageTo(OutputStream out) throws IOException, MessagingException { 431 if (message != null) { 432 message.writeTo(out); 433 } else { 434 throw new MessagingException ("No message set for this MailImpl."); 435 } 436 } 437 446 public Mail bounce(String bounceText) throws MessagingException { 447 MimeMessage original = getMessage(); 449 MimeMessage reply = (MimeMessage ) original.reply(false); 450 reply.setSubject("Re: " + original.getSubject()); 451 Collection recipients = new HashSet (); 452 recipients.add(getSender()); 453 InternetAddress addr[] = { new InternetAddress (getSender().toString())}; 454 reply.setRecipients(Message.RecipientType.TO, addr); 455 reply.setFrom(new InternetAddress (getRecipients().iterator().next().toString())); 456 reply.setText(bounceText); 457 reply.setHeader(RFC2822Headers.MESSAGE_ID, "replyTo-" + getName()); 458 return new MailImpl( 459 "replyTo-" + getName(), 460 new MailAddress(getRecipients().iterator().next().toString()), 461 recipients, 462 reply); 463 } 464 474 public void writeContentTo(OutputStream out, int lines) 475 throws IOException, MessagingException { 476 String line; 477 BufferedReader br; 478 if (message != null) { 479 br = new BufferedReader(new InputStreamReader(message.getInputStream())); 480 while (lines-- > 0) { 481 if ((line = br.readLine()) == null) { 482 break; 483 } 484 line += "\r\n"; 485 out.write(line.getBytes()); 486 } 487 } else { 488 throw new MessagingException ("No message set for this MailImpl."); 489 } 490 } 491 503 private void readObject(java.io.ObjectInputStream in) 504 throws IOException, ClassNotFoundException { 505 try { 506 Object obj = in.readObject(); 507 if (obj == null) { 508 sender = null; 509 } else if (obj instanceof String ) { 510 sender = new MailAddress((String ) obj); 511 } else if (obj instanceof MailAddress) { 512 sender = (MailAddress) obj; 513 } 514 } catch (ParseException pe) { 515 throw new IOException("Error parsing sender address: " + pe.getMessage()); 516 } 517 recipients = (Collection ) in.readObject(); 518 state = (String ) in.readObject(); 519 errorMessage = (String ) in.readObject(); 520 name = (String ) in.readObject(); 521 remoteHost = (String ) in.readObject(); 522 remoteAddr = (String ) in.readObject(); 523 setLastUpdated((Date ) in.readObject()); 524 try { 527 attributes = (HashMap ) in.readObject(); 528 } catch (OptionalDataException ode) { 529 if (ode.eof) { 530 attributes = new HashMap (); 531 } else { 532 throw ode; 533 } 534 } 535 } 536 543 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 544 lastUpdated = new Date (); 545 out.writeObject(sender); 546 out.writeObject(recipients); 547 out.writeObject(state); 548 out.writeObject(errorMessage); 549 out.writeObject(name); 550 out.writeObject(remoteHost); 551 out.writeObject(remoteAddr); 552 out.writeObject(lastUpdated); 553 out.writeObject(attributes); 554 } 555 556 559 public void dispose() { 560 try { 561 MimeMessage wrapper = getMessage(); 562 if (wrapper instanceof Disposable) { 563 ((Disposable)wrapper).dispose(); 564 } 565 } catch (MessagingException me) { 566 } 568 } 569 570 578 public HashMap getAttributesRaw () 579 { 580 return attributes; 581 } 582 583 591 public void setAttributesRaw (HashMap attr) 592 { 593 this.attributes = (attr == null) ? new HashMap () : attr; 594 } 595 596 600 public Serializable getAttribute(String key) { 601 return (Serializable)attributes.get(key); 602 } 603 607 public Serializable setAttribute(String key, Serializable object) { 608 return (Serializable)attributes.put(key, object); 609 } 610 614 public Serializable removeAttribute(String key) { 615 return (Serializable)attributes.remove(key); 616 } 617 621 public void removeAllAttributes() { 622 attributes.clear(); 623 } 624 628 public Iterator getAttributeNames() { 629 return attributes.keySet().iterator(); 630 } 631 635 public boolean hasAttributes() { 636 return !attributes.isEmpty(); 637 } 638 } 639 | Popular Tags |