1 21 22 27 28 package com.sun.mail.dsn; 29 30 import java.io.*; 31 import java.util.Vector ; 32 33 import javax.activation.*; 34 import javax.mail.*; 35 import javax.mail.internet.*; 36 37 60 public class MultipartReport extends MimeMultipart { 61 protected boolean constructed; 63 66 public MultipartReport() throws MessagingException { 67 super("report"); 68 MimeBodyPart mbp = new MimeBodyPart(); 70 setBodyPart(mbp, 0); 71 mbp = new MimeBodyPart(); 72 setBodyPart(mbp, 1); 73 constructed = true; 74 } 75 76 80 public MultipartReport(String text, DeliveryStatus status) 81 throws MessagingException { 82 super("report"); 83 ContentType ct = new ContentType(contentType); 84 ct.setParameter("report-type", "delivery-status"); 85 contentType = ct.toString(); 86 MimeBodyPart mbp = new MimeBodyPart(); 87 mbp.setText(text); 88 setBodyPart(mbp, 0); 89 mbp = new MimeBodyPart(); 90 mbp.setContent(status, "message/delivery-status"); 91 setBodyPart(mbp, 1); 92 constructed = true; 93 } 94 95 99 public MultipartReport(String text, DeliveryStatus status, 100 MimeMessage msg) throws MessagingException { 101 this(text, status); 102 if (msg != null) { 103 MimeBodyPart mbp = new MimeBodyPart(); 104 mbp.setContent(msg, "message/rfc822"); 105 setBodyPart(mbp, 2); 106 } 107 } 108 109 114 public MultipartReport(String text, DeliveryStatus status, 115 InternetHeaders hdr) throws MessagingException { 116 this(text, status); 117 if (hdr != null) { 118 MimeBodyPart mbp = new MimeBodyPart(); 119 mbp.setContent(new MessageHeaders(hdr), "text/rfc822-headers"); 120 setBodyPart(mbp, 2); 121 } 122 } 123 124 130 public MultipartReport(DataSource ds) throws MessagingException { 131 super(ds); 132 parse(); 133 constructed = true; 134 142 } 143 144 153 public synchronized String getText() throws MessagingException { 154 try { 155 BodyPart bp = getBodyPart(0); 156 if (bp.isMimeType("text/plain")) 157 return (String )bp.getContent(); 158 if (bp.isMimeType("multipart/alternative")) { 159 Multipart mp = (Multipart)bp.getContent(); 160 for (int i = 0; i < mp.getCount(); i++) { 161 bp = mp.getBodyPart(i); 162 if (bp.isMimeType("text/plain")) 163 return (String )bp.getContent(); 164 } 165 } 166 } catch (IOException ex) { 167 throw new MessagingException("Exception getting text content", ex); 168 } 169 return null; 170 } 171 172 176 public synchronized void setText(String text) throws MessagingException { 177 MimeBodyPart mbp = new MimeBodyPart(); 178 mbp.setText(text); 179 setBodyPart(mbp, 0); 180 } 181 182 186 public synchronized MimeBodyPart getTextBodyPart() 187 throws MessagingException { 188 return (MimeBodyPart)getBodyPart(0); 189 } 190 191 198 public synchronized void setTextBodyPart(MimeBodyPart mbp) 199 throws MessagingException { 200 setBodyPart(mbp, 0); 201 } 202 203 206 public synchronized DeliveryStatus getDeliveryStatus() 207 throws MessagingException { 208 if (getCount() < 2) 209 return null; 210 BodyPart bp = getBodyPart(1); 211 if (!bp.isMimeType("message/delivery-status")) 212 return null; 213 try { 214 return (DeliveryStatus)bp.getContent(); 215 } catch (IOException ex) { 216 throw new MessagingException("IOException getting DeliveryStatus", 217 ex); 218 } 219 } 220 221 224 public synchronized void setDeliveryStatus(DeliveryStatus status) 225 throws MessagingException { 226 MimeBodyPart mbp = new MimeBodyPart(); 227 mbp.setContent(status, "message/delivery-status"); 228 setBodyPart(mbp, 2); 229 ContentType ct = new ContentType(contentType); 230 ct.setParameter("report-type", "delivery-status"); 231 contentType = ct.toString(); 232 } 233 234 240 public synchronized MimeMessage getReturnedMessage() 241 throws MessagingException { 242 if (getCount() < 3) 243 return null; 244 BodyPart bp = getBodyPart(2); 245 if (!bp.isMimeType("message/rfc822") && 246 !bp.isMimeType("text/rfc822-headers")) 247 return null; 248 try { 249 return (MimeMessage)bp.getContent(); 250 } catch (IOException ex) { 251 throw new MessagingException("IOException getting ReturnedMessage", 252 ex); 253 } 254 } 255 256 261 public synchronized void setReturnedMessage(MimeMessage msg) 262 throws MessagingException { 263 if (msg == null) { 264 BodyPart part = (BodyPart)parts.elementAt(2); 265 super.removeBodyPart(2); 266 return; 267 } 268 MimeBodyPart mbp = new MimeBodyPart(); 269 if (msg instanceof MessageHeaders) 270 mbp.setContent(msg, "text/rfc822-headers"); 271 else 272 mbp.setContent(msg, "message/rfc822"); 273 setBodyPart(mbp, 2); 274 } 275 276 private synchronized void setBodyPart(BodyPart part, int index) 277 throws MessagingException { 278 if (parts == null) parts = new Vector (); 280 281 super.removeBodyPart(index); 282 super.addBodyPart(part, index); 283 } 284 285 286 288 294 public synchronized void setSubType(String subtype) 295 throws MessagingException { 296 throw new MessagingException("Can't change subtype of MultipartReport"); 297 } 298 299 306 public boolean removeBodyPart(BodyPart part) throws MessagingException { 307 throw new MessagingException( 308 "Can't remove body parts from multipart/report"); 309 } 310 311 318 public void removeBodyPart(int index) throws MessagingException { 319 throw new MessagingException( 320 "Can't remove body parts from multipart/report"); 321 } 322 323 330 public synchronized void addBodyPart(BodyPart part) 331 throws MessagingException { 332 if (!constructed) 334 super.addBodyPart(part); 335 else 336 throw new MessagingException( 337 "Can't add body parts to multipart/report 1"); 338 } 339 340 348 public synchronized void addBodyPart(BodyPart part, int index) 349 throws MessagingException { 350 throw new MessagingException( 351 "Can't add body parts to multipart/report 2"); 352 } 353 } 354 | Popular Tags |