1 21 22 27 28 package com.sun.mail.dsn; 29 30 import java.io.*; 31 import java.util.*; 32 33 import javax.activation.*; 34 import javax.mail.*; 35 import javax.mail.internet.*; 36 37 import com.sun.mail.util.LineOutputStream; 39 43 public class DeliveryStatus { 44 45 private static boolean debug = false; 46 47 static { 48 try { 49 String s = System.getProperty("mail.dsn.debug"); 50 debug = s != null && !s.equalsIgnoreCase("false"); 52 } catch (SecurityException sex) { 53 } 55 } 56 57 60 protected InternetHeaders messageDSN; 61 62 65 protected InternetHeaders[] recipientDSN; 66 67 70 public DeliveryStatus() throws MessagingException { 71 messageDSN = new InternetHeaders(); 72 recipientDSN = new InternetHeaders[0]; 73 } 74 75 79 public DeliveryStatus(InputStream is) 80 throws MessagingException, IOException { 81 messageDSN = new InternetHeaders(is); 82 if (debug) 83 System.out.println("DSN: got messageDSN"); 84 Vector v = new Vector(); 85 try { 86 while (is.available() > 0) { 87 InternetHeaders h = new InternetHeaders(is); 88 if (debug) 89 System.out.println("DSN: got recipientDSN"); 90 v.addElement(h); 91 } 92 } catch (EOFException ex) { 93 if (debug) 94 System.out.println("DSN: got EOFException"); 95 } 96 if (debug) 97 System.out.println("DSN: recipientDSN size " + v.size()); 98 recipientDSN = new InternetHeaders[v.size()]; 99 v.copyInto(recipientDSN); 100 } 101 102 116 public InternetHeaders getMessageDSN() { 118 return messageDSN; 119 } 120 121 124 public void setMessageDSN(InternetHeaders messageDSN) { 125 this.messageDSN = messageDSN; 126 } 127 128 132 public int getRecipientDSNCount() { 133 return recipientDSN.length; 134 } 135 136 140 public InternetHeaders getRecipientDSN(int n) { 141 return recipientDSN[n]; 142 } 143 144 148 public void addRecipientDSN(InternetHeaders h) { 149 InternetHeaders[] rh = new InternetHeaders[recipientDSN.length + 1]; 150 System.arraycopy(recipientDSN, 0, rh, 0, recipientDSN.length); 151 recipientDSN = rh; 152 recipientDSN[recipientDSN.length - 1] = h; 153 } 154 155 public void writeTo(OutputStream os) 156 throws IOException, MessagingException { 157 LineOutputStream los = null; 159 if (os instanceof LineOutputStream) { 160 los = (LineOutputStream) os; 161 } else { 162 los = new LineOutputStream(os); 163 } 164 165 writeInternetHeaders(messageDSN, los); 166 los.writeln(); 167 for (int i = 0; i < recipientDSN.length; i++) { 168 writeInternetHeaders(recipientDSN[i], los); 169 los.writeln(); 170 } 171 } 172 173 private static void writeInternetHeaders(InternetHeaders h, 174 LineOutputStream los) throws IOException { 175 Enumeration e = h.getAllHeaderLines(); 176 try { 177 while (e.hasMoreElements()) 178 los.writeln((String )e.nextElement()); 179 } catch (MessagingException mex) { 180 Exception ex = mex.getNextException(); 181 if (ex instanceof IOException) 182 throw (IOException)ex; 183 else 184 throw new IOException("Exception writing headers: " + mex); 185 } 186 } 187 188 public String toString() { 189 return "DeliveryStatus: Reporting-MTA=" + 190 messageDSN.getHeader("Reporting-MTA", null) + ", #Recipients=" + 191 recipientDSN.length; 192 } 193 } 194 | Popular Tags |