1 19 20 21 22 package org.apache.james.test.mock.javaxmail; 23 24 import javax.mail.internet.MimeMessage ; 25 import javax.mail.internet.InternetHeaders ; 26 import javax.mail.internet.InternetAddress ; 27 import javax.mail.*; 28 import javax.mail.search.SearchTerm ; 29 import javax.activation.DataHandler ; 30 import java.util.*; 31 import java.io.ByteArrayInputStream ; 32 import java.io.InputStream ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.UnsupportedEncodingException ; 36 37 public class MockMimeMessage extends MimeMessage { 38 39 private final List m_fromAddresses = new ArrayList(); 40 private Address m_senderAddress; 41 private final List m_toRecepients = new ArrayList(); 42 private final List m_ccRecepients = new ArrayList(); 43 private final List m_bccRecepients = new ArrayList(); 44 private final List m_replyToAddresses = new ArrayList(); 45 private String m_subject; 46 private int m_iMessageNumber; 47 private boolean m_bIsExpunged; 48 private Object m_content; 49 private Date m_sentDate; 50 private String [] m_contentLanguage; 51 private String m_fileName; 52 private DataHandler m_dataHandler; 53 private HashMap m_contentHeaders = new HashMap(); 54 private Flags m_setFlags = new Flags(); 55 private boolean m_doMatch; 56 57 public MockMimeMessage() throws MessagingException { 58 super((Session)null); 59 } 60 61 public MockMimeMessage(int messageNumber) throws MessagingException { 62 super((Session)null); 63 m_iMessageNumber = messageNumber; 64 } 65 66 public MockMimeMessage(MimeMessage mimeMessage) throws MessagingException { 67 super(mimeMessage); } 69 70 public Address [] getFrom() throws MessagingException { 71 return (Address [])m_fromAddresses.toArray(); 72 } 73 74 public void setFrom(Address address) throws MessagingException { 75 m_fromAddresses.clear(); 76 m_fromAddresses.add(address); 77 } 78 79 public void setFrom() throws MessagingException { 80 m_fromAddresses.clear(); 81 m_fromAddresses.add(InternetAddress.getLocalAddress(null)); 82 } 83 84 public void addFrom(Address [] addresses) throws MessagingException { 85 m_fromAddresses.add(addresses); 86 } 87 88 public Address getSender() throws MessagingException { 89 return m_senderAddress; 90 } 91 92 public void setSender(Address address) throws MessagingException { 93 m_senderAddress = address; 94 } 95 96 public Address [] getRecipients(Message.RecipientType recipientType) throws MessagingException { 97 List recipientsList = getRecipientsList(recipientType); 98 List recipientAddresses = new ArrayList(); 99 for (Iterator iterator = recipientsList.iterator(); iterator.hasNext();) { 100 String recipient = (String ) iterator.next(); 101 recipientAddresses.add(new InternetAddress (recipient)); 102 } 103 return (Address []) (recipientAddresses.toArray(new Address []{})); 104 } 105 106 private List getRecipientsList(Message.RecipientType recipientType) { 107 if (Message.RecipientType.TO.equals(recipientType)) return m_toRecepients; 108 if (Message.RecipientType.CC.equals(recipientType)) return m_ccRecepients; 109 if (Message.RecipientType.BCC.equals(recipientType)) return m_bccRecepients; 110 return null; 111 } 112 113 public Address [] getAllRecipients() throws MessagingException { 114 List allRecipients = new ArrayList(); 115 allRecipients.addAll(m_toRecepients); 116 allRecipients.addAll(m_ccRecepients); 117 allRecipients.addAll(m_bccRecepients); 118 return (Address []) allRecipients.toArray(); 119 } 120 121 public void setRecipients(Message.RecipientType recipientType, Address [] addresses) throws MessagingException { 122 getRecipientsList(recipientType).addAll(Arrays.asList(addresses)); 123 } 124 125 public void setRecipients(Message.RecipientType recipientType, String recipient) throws MessagingException { 126 getRecipientsList(recipientType).add(recipient); 127 } 128 129 public void addRecipients(Message.RecipientType recipientType, Address [] addresses) throws MessagingException { 130 getRecipientsList(recipientType).addAll(Arrays.asList(addresses)); 131 } 132 133 public void addRecipients(Message.RecipientType recipientType, String recipient) throws MessagingException { 134 getRecipientsList(recipientType).add(recipient); 135 } 136 137 public Address [] getReplyTo() throws MessagingException { 138 return (Address []) m_replyToAddresses.toArray(); 139 } 140 141 public void setReplyTo(Address [] addresses) throws MessagingException { 142 m_replyToAddresses.addAll(Arrays.asList(addresses)); 143 } 144 145 public String getSubject() throws MessagingException { 146 return m_subject; 147 } 148 149 public void setSubject(String subject) throws MessagingException { 150 m_subject = subject; 151 } 152 153 public void setSubject(String subject, String charset) throws MessagingException { 154 if (subject == null) 155 { 156 m_subject = null; 157 return; 158 } 159 try { 160 m_subject = new String (subject.getBytes(charset)); 161 } catch (UnsupportedEncodingException e) { 162 throw new MessagingException("setting subject failed", e); 163 } 164 } 165 166 public Date getSentDate() throws MessagingException { 167 return m_sentDate; 168 } 169 170 public void setSentDate(Date date) throws MessagingException { 171 m_sentDate = date; 172 } 173 174 public Date getReceivedDate() throws MessagingException { 175 return null; } 177 178 public int getSize() throws MessagingException { 179 return -1; } 181 182 public int getLineCount() throws MessagingException { 183 return -1; } 185 186 public String getContentType() throws MessagingException { 187 return getHeader("Content-Type", null); 188 } 189 190 public boolean isMimeType(String mimeType) throws MessagingException { 191 return mimeType.startsWith(getContentType()); 192 } 193 194 public String getDisposition() throws MessagingException { 195 return getHeader("Content-Disposition", null); 196 } 197 198 public void setDisposition(String disposition) throws MessagingException { 199 setHeader("Content-Disposition", disposition); 200 } 201 202 public String getEncoding() throws MessagingException { 203 return getHeader("Content-Transfer-Encoding", null); 204 } 205 206 public String getContentID() throws MessagingException { 207 return getHeader("Content-ID", null); 208 } 209 210 public void setContentID(String contentID) throws MessagingException { 211 setHeader("Content-ID", contentID); 212 } 213 214 public String getContentMD5() throws MessagingException { 215 return getHeader("Content-MD5", null); 216 } 217 218 public void setContentMD5(String value) throws MessagingException { 219 setHeader("Content-MD5", value); 220 } 221 222 public String getDescription() throws MessagingException { 223 return getHeader("Content-Description", null); 224 } 225 226 public void setDescription(String description) throws MessagingException { 227 setHeader("Content-Description", description); 228 } 229 230 public void setDescription(String description, String charset) throws MessagingException { 231 try { 232 setDescription(new String (description.getBytes(charset))); 233 } catch (UnsupportedEncodingException e) { 234 throw new MessagingException("setting description failed", e); 235 } 236 } 237 238 public String [] getContentLanguage() throws MessagingException { 239 return m_contentLanguage; 240 } 241 242 public void setContentLanguage(String [] contentLanguage) throws MessagingException { 243 m_contentLanguage = contentLanguage; 244 } 245 246 public String getMessageID() throws MessagingException { 247 return "ID-" + m_iMessageNumber; } 249 250 public String getFileName() throws MessagingException { 251 return m_fileName; 252 } 253 254 public void setFileName(String fileName) throws MessagingException { 255 m_fileName = fileName; 256 } 257 258 public InputStream getInputStream() throws IOException , MessagingException { 259 return null; } 261 262 protected InputStream getContentStream() throws MessagingException { 263 return null; } 265 266 public InputStream getRawInputStream() throws MessagingException { 267 if (m_content instanceof String ) { 268 return new ByteArrayInputStream (m_content.toString().getBytes()); 269 } 270 throw new UnsupportedOperationException ("Unimplementated method"); 271 } 272 273 public synchronized DataHandler getDataHandler() throws MessagingException { 274 return m_dataHandler; 275 } 276 277 public synchronized void setDataHandler(DataHandler dataHandler) throws MessagingException { 278 m_dataHandler = dataHandler; 279 } 280 281 public Object getContent() throws IOException , MessagingException { 282 return m_content; 283 } 284 285 public void setContent(Object object, String mimeType) throws MessagingException { 286 m_content = object; } 288 289 public void setText(String string) throws MessagingException { 290 setContent(string, "text/plain"); 291 } 292 293 public void setText(String string, String charset) throws MessagingException { 294 try { 295 setContent(new String (string.getBytes(charset)) , "text/plain"); 296 } catch (UnsupportedEncodingException e) { 297 throw new MessagingException("setting text content failed", e); 298 } 299 } 300 301 public void setContent(Multipart multipart) throws MessagingException { 302 m_content = multipart; 303 } 304 305 public Message reply(boolean b) throws MessagingException { 306 return new MockMimeMessage(this); } 308 309 public void writeTo(OutputStream outputStream) throws IOException , MessagingException { 310 ; } 312 313 public void writeTo(OutputStream outputStream, String [] strings) throws IOException , MessagingException { 314 ; } 316 317 public String [] getHeader(String name) throws MessagingException { 318 String value = (String ) m_contentHeaders.get(name); 319 if (value == null) return null; 320 return new String [] {value}; 321 } 322 323 public String getHeader(String name, String delimiter) throws MessagingException { 324 String [] header = getHeader(name); 325 if (header == null || header.length == 0) return null; 326 return header[0]; 327 } 328 329 public void setHeader(String name, String value) throws MessagingException { 330 addHeader(name, value); 331 } 332 333 public void addHeader(String name, String value) throws MessagingException { 334 m_contentHeaders.put(name, value); 335 } 336 337 public void removeHeader(String name) throws MessagingException { 338 m_contentHeaders.remove(name); 339 } 340 341 public Enumeration getAllHeaders() throws MessagingException { 342 return Collections.enumeration(m_contentHeaders.values()); 343 } 344 345 public Enumeration getMatchingHeaders(String [] names) throws MessagingException { 346 ArrayList matchingHeaders = new ArrayList(); 347 for (int i = 0; i < names.length; i++) { 348 String name = names[i]; 349 String value = getHeader(name, null); 350 if (value == null) continue; 351 matchingHeaders.add(value); 352 } 353 return Collections.enumeration(matchingHeaders); 354 } 355 356 public Enumeration getNonMatchingHeaders(String [] names) throws MessagingException { 357 List existingHeaders = Arrays.asList(names); 358 359 ArrayList nonMatchingHeaders = new ArrayList(); 360 361 Iterator iterator = m_contentHeaders.keySet().iterator(); 362 while (iterator.hasNext()) { 363 String name = (String ) iterator.next(); 364 if (existingHeaders.contains(name)) continue; 365 String value = getHeader(name, null); 366 if (value == null) continue; 367 nonMatchingHeaders.add(value); 368 } 369 return Collections.enumeration(nonMatchingHeaders); 370 } 371 372 public void addHeaderLine(String headerLine) throws MessagingException { 373 int separatorIndex = headerLine.indexOf(":"); 374 if (separatorIndex < 0) throw new MessagingException("header line does not conform to standard"); 375 376 addHeader(headerLine.substring(0,separatorIndex), headerLine.substring(separatorIndex,headerLine.length())); 377 } 378 379 public Enumeration getAllHeaderLines() throws MessagingException { 380 return Collections.enumeration(getHeadersAsStrings(m_contentHeaders)); 381 } 382 383 private ArrayList getHeadersAsStrings(HashMap contentHeaders) { 384 ArrayList headerLines = new ArrayList(); 385 Iterator iterator = contentHeaders.keySet().iterator(); 386 while (iterator.hasNext()) { 387 String key = (String ) iterator.next(); 388 String value = (String ) contentHeaders.get(key); 389 headerLines.add(key + ":" + value); 390 } 391 return headerLines; 392 } 393 394 public Enumeration getMatchingHeaderLines(String [] names) throws MessagingException { 395 ArrayList matchingHeaders = new ArrayList(); 396 for (int i = 0; i < names.length; i++) { 397 String name = names[i]; 398 String value = getHeader(name, null); 399 if (value == null) continue; 400 matchingHeaders.add(name + ":" + value); 401 } 402 return Collections.enumeration(matchingHeaders); 403 } 404 405 public Enumeration getNonMatchingHeaderLines(String [] names) throws MessagingException { 406 List existingHeaders = names != null ? Arrays.asList(names) : null; 407 408 ArrayList nonMatchingHeaders = new ArrayList(); 409 410 Iterator iterator = m_contentHeaders.keySet().iterator(); 411 while (iterator.hasNext()) { 412 String name = (String ) iterator.next(); 413 if (existingHeaders!=null && existingHeaders.contains(name)) continue; 414 String value = getHeader(name, null); 415 if (value == null) continue; 416 nonMatchingHeaders.add(name + ":" + value); 417 } 418 return Collections.enumeration(nonMatchingHeaders); 419 } 420 421 public synchronized Flags getFlags() throws MessagingException { 422 return new Flags(m_setFlags); 423 } 424 425 public synchronized boolean isSet(Flags.Flag flag) throws MessagingException { 426 return m_setFlags.contains(flag); 427 } 428 429 public synchronized void setFlags(Flags flags, boolean set) throws MessagingException { 430 if (set) m_setFlags.add(flags); 431 else m_setFlags.remove(flags); 432 } 433 434 public void saveChanges() throws MessagingException { 435 ; } 437 438 protected void updateHeaders() throws MessagingException { 439 ; } 441 442 protected InternetHeaders createInternetHeaders(InputStream inputStream) throws MessagingException { 443 return new InternetHeaders (); 444 } 445 446 public void setRecipient(Message.RecipientType recipientType, Address address) throws MessagingException { 447 setRecipients(recipientType, new Address []{address}); 448 } 449 450 public void addRecipient(Message.RecipientType recipientType, Address address) throws MessagingException { 451 setRecipients(recipientType, new Address []{address}); 452 } 453 454 public void setFlag(Flags.Flag flag, boolean set) throws MessagingException { 455 if (set) m_setFlags.add(flag); 456 else m_setFlags.remove(flag); 457 } 458 459 public int getMessageNumber() { 460 return m_iMessageNumber; 461 } 462 463 protected void setMessageNumber(int i) { 464 m_iMessageNumber = i; 465 } 466 467 public Folder getFolder() { 468 return null; 469 } 470 471 public boolean isExpunged() { 472 return m_bIsExpunged; 473 } 474 475 protected void setExpunged(boolean b) { 476 m_bIsExpunged = b; 477 } 478 479 public void setShouldMatch(boolean doMatch) { 480 m_doMatch = doMatch; 481 } 482 483 public boolean match(SearchTerm searchTerm) throws MessagingException { 484 return m_doMatch; 485 } 486 } 487 | Popular Tags |