Coverage report

  %line %branch
com.ozacc.mail.impl.MimeMessageBuilder
59% 
64% 

 1  
 package com.ozacc.mail.impl;
 2  
 
 3  
 import java.io.UnsupportedEncodingException;
 4  
 import java.util.Iterator;
 5  
 import java.util.Map;
 6  
 
 7  
 import javax.activation.DataHandler;
 8  
 import javax.activation.DataSource;
 9  
 import javax.mail.MessagingException;
 10  
 import javax.mail.internet.InternetAddress;
 11  
 import javax.mail.internet.MimeBodyPart;
 12  
 import javax.mail.internet.MimeMessage;
 13  
 import javax.mail.internet.MimeMultipart;
 14  
 import javax.mail.internet.MimePart;
 15  
 import javax.mail.internet.MimeUtility;
 16  
 
 17  
 import com.ozacc.mail.Mail;
 18  
 
 19  
 /**
 20  
  * MimeMessage¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤?¥¯¥é¥¹¡£
 21  
  * 
 22  
  * @since 1.0
 23  
  * @author Tomohiro Otsuka
 24  
  * @version $Id: MimeMessageBuilder.java,v 1.11.2.1 2005/01/21 22:14:26 otsuka Exp $
 25  
  */
 26  
 public class MimeMessageBuilder {
 27  
 
 28  
 	private MimeMessage mimeMessage;
 29  
 
 30  16
 	private String charset = Mail.JIS_CHARSET;
 31  
 
 32  16
 	private boolean hasRecipient = false;
 33  
 
 34  
 	/**
 35  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 36  
 	 * ¥Ç¥Õ¥©¥?¥È¤Îʸ»ú¥³¡¼¥É ISO-2022-JP ¤¬¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤµ¤?¤Þ¤¹¡£
 37  
 	 * 
 38  
 	 * @param mimeMessage
 39  
 	 */
 40  16
 	public MimeMessageBuilder(MimeMessage mimeMessage) {
 41  16
 		this.mimeMessage = mimeMessage;
 42  16
 	}
 43  
 
 44  
 	/**
 45  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 46  
 	 * ËÜʸ¤ä·?̾¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£
 47  
 	 * 
 48  
 	 * @param mimeMessage
 49  
 	 * @param charset ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ë»ÈÍѤ¹¤?ʸ»ú¥³¡¼¥É
 50  
 	 */
 51  0
 	public MimeMessageBuilder(MimeMessage mimeMessage, String charset) {
 52  0
 		this.mimeMessage = mimeMessage;
 53  0
 		this.charset = charset;
 54  0
 	}
 55  
 
 56  
 	/**
 57  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¤Î°ú¿ô¤ÇÅϤµ¤?¤¿MimeMessage¤ò¤½¤Î¤Þ¤ÞÊÖ¤·¤Þ¤¹¡£
 58  
 	 * 
 59  
 	 * @return MimeMessage
 60  
 	 */
 61  
 	public MimeMessage getMimeMessage() {
 62  0
 		return this.mimeMessage;
 63  
 	}
 64  
 
 65  
 	/**
 66  
 	 * »ØÄꤵ¤?¤¿¥á¡¼¥?¤«¤éMimeMessage¤òÀ¸À®¤·¤Þ¤¹¡£
 67  
 	 * 
 68  
 	 * @param mail MimeMessage¤Î¥½¡¼¥¹¤È¤Ê¤?Mail
 69  
 	 * @throws MessagingException
 70  
 	 * @throws UnsupportedEncodingException
 71  
 	 */
 72  
 	public void buildMimeMessage(Mail mail) throws UnsupportedEncodingException, MessagingException {
 73  
 
 74  16
 		setTo(mail);
 75  
 
 76  16
 		setCc(mail);
 77  
 
 78  16
 		setBcc(mail);
 79  
 
 80  
 		// °¸Àè¤Î»ØÄ꤬¤Ê¤¤¾?¹ç¥¨¥é¡¼
 81  16
 		if (!hasRecipient) {
 82  1
 			throw new MessagingException("°¸Àè¤Î»ØÄ꤬¤¢¤ê¤Þ¤»¤ó¡£To¡¢Cc¡¢Bcc¤Î¤¤¤º¤?¤«°?¤Ä¤Ï»ØÄꤹ¤?ɬÍפ¬¤¢¤ê¤Þ¤¹¡£");
 83  
 		}
 84  
 
 85  15
 		setFrom(mail);
 86  
 
 87  15
 		setSubject(mail);
 88  
 
 89  15
 		setReplyTo(mail);
 90  
 
 91  15
 		setXHeaders(mail);
 92  
 
 93  15
 		setImportance(mail);
 94  
 
 95  15
 		if (mail.isMultipartMail()) {
 96  
 
 97  3
 			if (!mail.isFileAttached() && mail.isHtmlMail()) { // Plain text, HTML
 98  
 
 99  3
 				if (mail.getText() != null && mail.getText().length() > 0) { // Plain text, HTML
 100  
 
 101  3
 					MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative");
 102  3
 					setPlainText(mail, textAndHtmlMultipart);
 103  3
 					setHtmlText(mail, textAndHtmlMultipart);
 104  3
 					this.mimeMessage.setContent(textAndHtmlMultipart);
 105  
 
 106  
 				} else { // HTML Only ¥Þ¥?¥Á¥Ñ¡¼¥È¤Ï»ÈÍѤ·¤Ê¤¤
 107  
 
 108  0
 					setHtmlText(mail.getHtmlText(), this.mimeMessage);
 109  
 
 110  
 				}
 111  
 
 112  0
 			} else if (mail.isFileAttached() && mail.isHtmlMail()) { // Plain text, HMTL, File
 113  
 
 114  0
 				MimeMultipart textAndHtmlMultipart = new MimeMultipart("alternative");
 115  0
 				setPlainText(mail, textAndHtmlMultipart);
 116  0
 				setHtmlText(mail, textAndHtmlMultipart);
 117  
 
 118  0
 				MimeMultipart containingMultipart = new MimeMultipart();
 119  0
 				MimeBodyPart textBodyPart = createMimeBodyPart(containingMultipart);
 120  0
 				textBodyPart.setContent(textAndHtmlMultipart);
 121  0
 				setAttachmentFiles(mail, containingMultipart);
 122  
 
 123  0
 				this.mimeMessage.setContent(containingMultipart);
 124  
 
 125  0
 			} else if (mail.isFileAttached() && !mail.isHtmlMail()) { // Plain text, File
 126  
 
 127  0
 				MimeMultipart textAndFileMultipart = new MimeMultipart();
 128  0
 				setPlainText(mail, textAndFileMultipart);
 129  0
 				setAttachmentFiles(mail, textAndFileMultipart);
 130  0
 				this.mimeMessage.setContent(textAndFileMultipart);
 131  
 
 132  
 			} else { // Plain text only ¥Þ¥?¥Á¥Ñ¡¼¥È¤Ï»ÈÍѤ·¤Ê¤¤
 133  
 
 134  0
 				setText(mail.getText(), this.mimeMessage);
 135  
 
 136  
 			}
 137  
 
 138  
 		} else {
 139  
 
 140  12
 			setText(mail.getText(), this.mimeMessage);
 141  
 
 142  
 		}
 143  
 
 144  15
 	}
 145  
 
 146  
 	/**
 147  
 	 * 
 148  
 	 * @since 1.1
 149  
 	 * 
 150  
 	 * @param mail
 151  
 	 * @param mimeMultipart
 152  
 	 * @throws MessagingException
 153  
 	 * @throws UnsupportedEncodingException 
 154  
 	 */
 155  
 	private void setAttachmentFiles(Mail mail, MimeMultipart mimeMultipart)
 156  
 																			throws MessagingException,
 157  
 																			UnsupportedEncodingException {
 158  0
 		Mail.AttachmentFile[] files = mail.getAttachmentFiles();
 159  0
 		for (int i = 0; i < files.length; i++) {
 160  0
 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
 161  0
 			Mail.AttachmentFile attachmentFile = files[i];
 162  0
 			addAttachment(attachmentFile.getName(), attachmentFile.getDataSource(), bodyPart);
 163  
 		}
 164  0
 	}
 165  
 
 166  
 	/**
 167  
 	 * 
 168  
 	 * @since 1.1
 169  
 	 * 
 170  
 	 * @param mail
 171  
 	 * @param mimeMultipart
 172  
 	 * @throws MessagingException 
 173  
 	 */
 174  
 	private void setHtmlText(Mail mail, MimeMultipart mimeMultipart) throws MessagingException {
 175  3
 		if (mail.isHtmlMail()) {
 176  3
 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
 177  3
 			setHtmlText(mail.getHtmlText(), bodyPart);
 178  
 		}
 179  3
 	}
 180  
 
 181  
 	/**
 182  
 	 * 
 183  
 	 * @since 1.1
 184  
 	 * 
 185  
 	 * @param mail
 186  
 	 * @param mimeMultipart
 187  
 	 * @throws MessagingException 
 188  
 	 */
 189  
 	private void setPlainText(Mail mail, MimeMultipart mimeMultipart) throws MessagingException {
 190  3
 		if (mail.getText() != null && mail.getText().length() > 0) {
 191  3
 			MimeBodyPart bodyPart = createMimeBodyPart(mimeMultipart);
 192  3
 			setText(mail.getText(), bodyPart);
 193  
 		}
 194  3
 	}
 195  
 
 196  
 	/**
 197  
 	 * ¿·¤·¤¤MimeBodyPart¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¡¢»ØÄꤵ¤?¤¿MimeMultipart¤ËÅÐÏ¿¤·¤Þ¤¹¡£
 198  
 	 * 
 199  
 	 * ¤³¤Î¥á¥½¥Ã¥É¤Ï¥Þ¥?¥Á¥Ñ¡¼¥È¥á¡¼¥?À¸À®»?¤Ë¤Î¤ß¸Æ¤Ó½Ð¤¹¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£
 200  
 	 * ¥×¥?¡¼¥ó¥Æ¥­¥¹¥È¥á¡¼¥?À¸À®»?¤Ë¤Ï¡¢mimeMulipart¤¬null¤Ê¤Î¤Ç¡¢
 201  
 	 * NullPointerException¤¬¥¹¥ú½¼¤µ¤?¤Þ¤¹¡£
 202  
 	 * 
 203  
 	 * @since 1.1
 204  
 	 * 
 205  
 	 * @param mm
 206  
 	 * @return À¸À®¤µ¤?¤¿MimeBodyPart
 207  
 	 * @throws MessagingException 
 208  
 	 */
 209  
 	private MimeBodyPart createMimeBodyPart(MimeMultipart mm) throws MessagingException {
 210  6
 		MimeBodyPart bodyPart = new MimeBodyPart();
 211  6
 		mm.addBodyPart(bodyPart);
 212  6
 		return bodyPart;
 213  
 	}
 214  
 
 215  
 	/**
 216  
 	 * @since 1.1
 217  
 	 * 
 218  
 	 * @param htmlText
 219  
 	 * @param mimePart 
 220  
 	 * @throws MessagingException
 221  
 	 */
 222  
 	private void setHtmlText(final String htmlText, MimePart mimePart) throws MessagingException {
 223  3
 		if (charset != null) {
 224  3
 			mimePart.setContent(htmlText, "text/html; charset=" + charset);
 225  
 		} else {
 226  0
 			mimePart.setContent(htmlText, "text/html");
 227  
 		}
 228  3
 		mimePart.setHeader("Content-Transfer-Encoding", "7bit");
 229  3
 	}
 230  
 
 231  
 	/**
 232  
 	 * @param mail 
 233  
 	 * @throws MessagingException
 234  
 	 */
 235  
 	private void setXHeaders(Mail mail) throws MessagingException {
 236  15
 		Map headers = mail.getHeaders();
 237  15
 		if (headers == null) {
 238  15
 			return;
 239  
 		}
 240  
 
 241  0
 		Iterator itr = headers.keySet().iterator();
 242  0
 		while (itr.hasNext()) {
 243  0
 			String key = (String)itr.next();
 244  0
 			String value = (String)headers.get(key);
 245  0
 			mimeMessage.setHeader(key, value);
 246  
 		}
 247  0
 	}
 248  
 
 249  
 	/**
 250  
 	 * @param mail 
 251  
 	 * @throws MessagingException
 252  
 	 */
 253  
 	private void setImportance(Mail mail) throws MessagingException {
 254  15
 		if (mail.getImportance() != null) {
 255  1
 			mimeMessage.setHeader("Importance", mail.getImportance());
 256  
 
 257  1
 			int level = 3;
 258  1
 			if (Mail.Importance.HIGH.equals(mail.getImportance())) {
 259  1
 				level = 1;
 260  0
 			} else if (Mail.Importance.LOW.equals(mail.getImportance())) {
 261  0
 				level = 5;
 262  
 			}
 263  1
 			mimeMessage.setHeader("X-Priority", String.valueOf(level));
 264  
 		}
 265  15
 	}
 266  
 
 267  
 	/**
 268  
 	 * @param mail 
 269  
 	 * @throws MessagingException
 270  
 	 */
 271  
 	private void setReplyTo(Mail mail) throws MessagingException {
 272  15
 		if (mail.getReplyTo() != null) {
 273  0
 			mimeMessage.setReplyTo(new InternetAddress[] { mail.getReplyTo() });
 274  
 		}
 275  15
 	}
 276  
 
 277  
 	/**
 278  
 	 * @param mail 
 279  
 	 * @throws MessagingException
 280  
 	 */
 281  
 	private void setBcc(Mail mail) throws MessagingException {
 282  16
 		if (mail.getBcc().length > 0) {
 283  0
 			mimeMessage.setRecipients(MimeMessage.RecipientType.BCC, mail.getBcc());
 284  0
 			hasRecipient = true;
 285  
 		}
 286  16
 	}
 287  
 
 288  
 	/**
 289  
 	 * @param mail 
 290  
 	 * @throws MessagingException
 291  
 	 */
 292  
 	private void setCc(Mail mail) throws MessagingException {
 293  16
 		if (mail.getCc().length > 0) {
 294  0
 			mimeMessage.setRecipients(MimeMessage.RecipientType.CC, mail.getCc());
 295  0
 			hasRecipient = true;
 296  
 		}
 297  16
 	}
 298  
 
 299  
 	/**
 300  
 	 * @param mail 
 301  
 	 * @throws MessagingException
 302  
 	 */
 303  
 	private void setTo(Mail mail) throws MessagingException {
 304  16
 		if (mail.getTo().length > 0) {
 305  15
 			mimeMessage.setRecipients(MimeMessage.RecipientType.TO, mail.getTo());
 306  15
 			hasRecipient = true;
 307  
 		}
 308  16
 	}
 309  
 
 310  
 	/**
 311  
 	 * ËÜʸ¤ò¥»¥Ã¥È¡£
 312  
 	 * <p>
 313  
 	 * NOTE: ËÜʸ¤ÎºÇ¸å¤Ë²?¹Ô¤¬¤Ê¤¤¤ÈMozilla·Ï¤Î¥á¡¼¥é¡¼¤ÇºÇ½ª¹Ô¤ÎÆ?Ëܸ?¤¬Ê¸»ú²½¤±¤·¤Æ¤·¤Þ¤¦°Ù¡¢
 314  
 	 * message.setText¤Î°ú¿ô¤ÇºÇ¸å¤Ë\n¤òÄɲ䷤Ƥ¤¤?¡£
 315  
 	 * 
 316  
 	 * @since 1.1
 317  
 	 * 
 318  
 	 * @param text ËÜʸ
 319  
 	 * @param mimePart ËÜʸ¤ò¥»¥Ã¥È¤¹¤?MimePart 
 320  
 	 * @throws MessagingException
 321  
 	 */
 322  
 	private void setText(String text, MimePart mimePart) throws MessagingException {
 323  15
 		if (charset != null) {
 324  15
 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
 325  
 				// Cp932¥¯¥é¥¹¤ò»ÈÍѤ·¤Æ¡¢²ø¤·¤¤µ­¹æ¤ò¶¯À©Åª¤ËJISÊÑ´¹
 326  15
 				mimePart.setText(Cp932.toJIS(text) + "\n", charset);
 327  
 			} else {
 328  0
 				mimePart.setText(text + "\n", charset);
 329  
 			}
 330  
 		} else {
 331  0
 			mimePart.setText(text + "\n");
 332  
 		}
 333  15
 		mimePart.setHeader("Content-Transfer-Encoding", "7bit");
 334  15
 	}
 335  
 
 336  
 	/**
 337  
 	 * @param mail
 338  
 	 * @throws MessagingException
 339  
 	 * @throws UnsupportedEncodingException
 340  
 	 */
 341  
 	private void setSubject(Mail mail) throws UnsupportedEncodingException, MessagingException {
 342  15
 		if (charset != null) {
 343  15
 			if (charset.equalsIgnoreCase(Mail.JIS_CHARSET)) {
 344  15
 				String subject = Cp932.toJIS(mail.getSubject());
 345  15
 				mimeMessage.setSubject(MimeUtility.encodeText(subject, charset, "B"));
 346  
 			} else {
 347  0
 				mimeMessage.setSubject(mail.getSubject(), charset);
 348  
 			}
 349  
 		} else {
 350  0
 			mimeMessage.setSubject(mail.getSubject());
 351  
 		}
 352  15
 	}
 353  
 
 354  
 	/**
 355  
 	 * @param mail
 356  
 	 * @throws MessagingException 
 357  
 	 */
 358  
 	private void setFrom(Mail mail) throws MessagingException {
 359  15
 		mimeMessage.setFrom(mail.getFrom());
 360  15
 	}
 361  
 
 362  
 	/**
 363  
 	 * źÉÕ¥Õ¥¡¥¤¥?¥Ç¡¼¥¿¤ò»ØÄꤵ¤?¤¿MimeBodyPart¤Ë¥»¥Ã¥È¤·¤Þ¤¹¡£
 364  
 	 * 
 365  
 	 * @since 1.1
 366  
 	 * 
 367  
 	 * @param fileName
 368  
 	 * @param dataSource
 369  
 	 * @param mimeBodyPart ¥Õ¥¡¥¤¥?¥Ç¡¼¥¿¤ò¥»¥Ã¥È¤¹¤?MimeBodyPart
 370  
 	 * @throws UnsupportedEncodingException
 371  
 	 * @throws MessagingException
 372  
 	 */
 373  
 	private void addAttachment(String fileName, DataSource dataSource, MimeBodyPart mimeBodyPart)
 374  
 																									throws UnsupportedEncodingException,
 375  
 																									MessagingException {
 376  0
 		if (charset != null) {
 377  
 			// ¥Õ¥¡¥¤¥?̾¤Î¥¨¥ó¥³¡¼¥É
 378  0
 			mimeBodyPart.setFileName(MimeUtility.encodeText(fileName, charset, "B"));
 379  
 		} else {
 380  0
 			mimeBodyPart.setFileName(fileName);
 381  
 		}
 382  
 
 383  0
 		mimeBodyPart.setDataHandler(new DataHandler(dataSource));
 384  0
 	}
 385  
 
 386  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.