Coverage report

  %line %branch
com.ozacc.mail.fetch.impl.FetchMailProImpl
14% 
77% 

 1  
 package com.ozacc.mail.fetch.impl;
 2  
 
 3  
 import java.util.Properties;
 4  
 
 5  
 import javax.mail.AuthenticationFailedException;
 6  
 import javax.mail.Flags;
 7  
 import javax.mail.Folder;
 8  
 import javax.mail.Message;
 9  
 import javax.mail.MessagingException;
 10  
 import javax.mail.NoSuchProviderException;
 11  
 import javax.mail.Session;
 12  
 import javax.mail.Store;
 13  
 import javax.mail.internet.MimeMessage;
 14  
 
 15  
 import org.apache.commons.logging.Log;
 16  
 import org.apache.commons.logging.LogFactory;
 17  
 
 18  
 import com.ozacc.mail.MailAuthenticationException;
 19  
 import com.ozacc.mail.MailException;
 20  
 import com.ozacc.mail.NotConnectedException;
 21  
 import com.ozacc.mail.fetch.FetchMailPro;
 22  
 import com.ozacc.mail.fetch.MailFetchException;
 23  
 import com.ozacc.mail.fetch.ReceivedMail;
 24  
 
 25  
 /**
 26  
  * <code>FetchMail</code>インターフェースの実装クラス。
 27  
  * <p>
 28  
  * このクラスのインスタンスは、インスタンス変数を用いて状態を保�?す�?ため、
 29  
  * ステート�?スではありません。ステートフ�?です。
 30  
  * 
 31  
  * @since 1.2
 32  
  * @author Tomohiro Otsuka
 33  
  * @version $Id: FetchMailProImpl.java,v 1.1.2.10 2005/01/29 23:13:17 otsuka Exp $
 34  
  */
 35  1
 public class FetchMailProImpl implements FetchMailPro {
 36  
 
 37  1
 	private static Log log = LogFactory.getLog(FetchMailProImpl.class);
 38  
 
 39  
 	/** デフォ�?トのSMTPサーバ。「localhost」 */
 40  
 	public static final String DEFAULT_HOST = "localhost";
 41  
 
 42  
 	/** デフォ�?トのプ��組コ�?。「pop3」 */
 43  
 	public static final String DEFAULT_PROTOCOL = "pop3";
 44  
 
 45  
 	/**
 46  
 	 * デフォ�?トのポート。「-1」<br>
 47  
 	 * -1はプ��組コ�?に�?じた適切なポートを設定す�?特別な値。
 48  
 	 */
 49  
 	public static final int DEFAULT_PORT = -1;
 50  
 
 51  
 	private static final String INBOX_NAME = "INBOX";
 52  
 
 53  1
 	private String host = DEFAULT_HOST;
 54  
 
 55  1
 	private String protocol = DEFAULT_PROTOCOL;
 56  
 
 57  1
 	private int port = DEFAULT_PORT;
 58  
 
 59  
 	private String username;
 60  
 
 61  
 	private String password;
 62  
 
 63  
 	private boolean javaMailLogEnabled;
 64  
 
 65  
 	private Store store;
 66  
 
 67  
 	private Folder currentFolder;
 68  
 
 69  
 	/**
 70  
 	 * コンストラクタ。
 71  
 	 */
 72  1
 	public FetchMailProImpl() {
 73  1
 		System.setProperty("mail.mime.multipart.ignoremissingendboundary", "true");
 74  1
 	}
 75  
 
 76  
 	/**
 77  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#connect()
 78  
 	 */
 79  
 	public synchronized void connect() throws MailException {
 80  0
 		log.debug(protocol.toUpperCase() + "サーバ[" + host + "]に接続します。");
 81  
 
 82  0
 		Session session = Session.getInstance(createProperties(), null);
 83  0
 		if (javaMailLogEnabled) {
 84  0
 			session.setDebug(true);
 85  
 		}
 86  
 
 87  
 		try {
 88  0
 			store = session.getStore(protocol);
 89  0
 		} catch (NoSuchProviderException e) {
 90  0
 			log.error("指定さ�?たプ��組コ�?[" + protocol + "]はサポートさ�?ていません。", e);
 91  0
 			throw new MailException("指定さ�?たプ��組コ�?[" + protocol + "]はサポートさ�?ていません。", e);
 92  
 		}
 93  
 
 94  
 		try {
 95  0
 			store.connect(host, port, username, password);
 96  0
 		} catch (AuthenticationFailedException e) {
 97  0
 			log.error(protocol.toUpperCase() + "サーバ[" + host + "]への接続認証に失敗しました。", e);
 98  0
 			throw new MailAuthenticationException(protocol.toUpperCase() + "サーバ[" + host
 99  0
 					+ "]への接続認証に失敗しました。", e);
 100  0
 		} catch (MessagingException e) {
 101  0
 			log.error(protocol.toUpperCase() + "サーバ[" + host + "]への接続に失敗しました。", e);
 102  0
 			throw new MailException(protocol.toUpperCase() + "サーバ[" + host + "]への接続に失敗しました。", e);
 103  
 		}
 104  
 
 105  0
 		log.info(protocol.toUpperCase() + "サーバ[" + host + "]に接続しました。");
 106  
 
 107  0
 		changeFolder(INBOX_NAME);
 108  0
 	}
 109  
 
 110  
 	/**
 111  
 	 * Sessionに渡すPropertiesインスタンスを返します。
 112  
 	 * APOP認証を行う�?合に、"mail.pop3.apop.enable"をセットします。
 113  
 	 * 
 114  
 	 * @return Sessionに渡すPropertiesインスタンス
 115  
 	 */
 116  
 	private Properties createProperties() {
 117  0
 		Properties prop = new Properties();
 118  0
 		if ("apop".equalsIgnoreCase(protocol)) {
 119  0
 			prop.put("mail.pop3.apop.enable", "true");
 120  
 		}
 121  0
 		return prop;
 122  
 	}
 123  
 
 124  
 	/**
 125  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#disconnect()
 126  
 	 */
 127  
 	public synchronized void disconnect() throws MailException {
 128  
 		try {
 129  0
 			closeCurrentFolderIfOpen();
 130  0
 		} finally {
 131  0
 			if (isConnected()) {
 132  0
 				log.debug(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断します。");
 133  
 				try {
 134  0
 					store.close();
 135  0
 					store = null;
 136  0
 				} catch (MessagingException e) {
 137  0
 					throw new MailException("サーバ[" + host + "]との接続切断に失敗しました。", e);
 138  
 				}
 139  
 			}
 140  0
 		}
 141  0
 		log.info(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断しました。");
 142  0
 	}
 143  
 
 144  
 	/**
 145  
 	 * 現在のメッセージフォ�?ダをク��充ズします。
 146  
 	 * 
 147  
 	 * @throws MailException メッセージフォ�?ダのク��充ズに失敗した�?�?
 148  
 	 */
 149  
 	private void closeCurrentFolderIfOpen() throws MailException {
 150  0
 		if (currentFolder != null && currentFolder.isOpen()) {
 151  0
 			log.debug("メッセージフォ�?ダ[" + currentFolder.getName() + "]をク��充ズします。");
 152  
 			try {
 153  0
 				currentFolder.close(true);
 154  0
 			} catch (MessagingException e) {
 155  0
 				log.error("メッセージフォ�?ダ[" + currentFolder.getName() + "]のク��充ズに失敗しました。", e);
 156  0
 				throw new MailException("メッセージフォ�?ダ[" + currentFolder.getName() + "]のク��充ズに失敗しました。",
 157  0
 						e);
 158  
 			}
 159  0
 			log.debug("メッセージフォ�?ダ[" + currentFolder.getName() + "]をク��充ズしました。");
 160  0
 			currentFolder = null;
 161  
 		}
 162  0
 	}
 163  
 
 164  
 	/**
 165  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#changeFolder(java.lang.String)
 166  
 	 */
 167  
 	public synchronized void changeFolder(String folderName) throws MailException {
 168  0
 		closeCurrentFolderIfOpen();
 169  0
 		log.debug("メッセージフォ�?ダ[" + folderName + "]をオープンします。");
 170  
 		try {
 171  0
 			currentFolder = store.getFolder(folderName);
 172  0
 			currentFolder.open(Folder.READ_WRITE);
 173  0
 		} catch (MessagingException e) {
 174  0
 			log.error("メッセージフォ�?ダ[" + folderName + "]のオープンに失敗しました。", e);
 175  0
 			throw new MailException("メッセージフォ�?ダ[" + folderName + "]のオープンに失敗しました。", e);
 176  
 		}
 177  0
 		log.debug("メッセージフォ�?ダ[" + folderName + "]をオープンしました。");
 178  0
 	}
 179  
 
 180  
 	/**
 181  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMailCount()
 182  
 	 */
 183  
 	public int getMailCount() throws MailException {
 184  0
 		checkIfCurrentFolderIsOpen();
 185  
 		try {
 186  0
 			return currentFolder.getMessageCount();
 187  0
 		} catch (MessagingException e) {
 188  0
 			throw new MailFetchException("メー�?数の取得に失敗しました。", e);
 189  
 		}
 190  
 	}
 191  
 
 192  
 	/**
 193  
 	 * メー�?サーバに接続さ�?ていて、フォ�?ダが操笹税き�?状態かどうか調べます。
 194  
 	 * フォ�?ダが操笹税き�?状態にない�?合、NotConnectedExceptionをス��充します。
 195  
 	 * 
 196  
 	 * @throws NotConnectedException
 197  
 	 */
 198  
 	private void checkIfCurrentFolderIsOpen() throws NotConnectedException {
 199  0
 		if (currentFolder == null || !currentFolder.isOpen()) {
 200  0
 			throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続さ�?ていません。");
 201  
 		}
 202  0
 	}
 203  
 
 204  
 	/**
 205  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMail(int)
 206  
 	 */
 207  
 	public ReceivedMail getMail(int num) throws MailException {
 208  0
 		MimeMessage mimeMessage = getMessage(num);
 209  0
 		MailConverter converter = new MailConverter(mimeMessage);
 210  0
 		return converter.convertIntoMails()[0];
 211  
 	}
 212  
 
 213  
 	public ReceivedMail[] getMails(boolean delete) throws MailException {
 214  0
 		MimeMessage[] mimeMessages = getMessages(delete);
 215  0
 		MailConverter converter = new MailConverter(mimeMessages);
 216  0
 		return converter.convertIntoMails();
 217  
 	}
 218  
 
 219  
 	/**
 220  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMessage(int)
 221  
 	 */
 222  
 	public MimeMessage getMessage(int num) throws MailException {
 223  0
 		checkIfCurrentFolderIsOpen();
 224  
 		try {
 225  0
 			return (MimeMessage)currentFolder.getMessage(num);
 226  0
 		} catch (MessagingException e) {
 227  0
 			log.error("メッセージの取得に失敗しました。", e);
 228  0
 			throw new MailFetchException("メッセージの取得に失敗しました。", e);
 229  
 		}
 230  
 	}
 231  
 
 232  
 	public MimeMessage[] getMessages(boolean delete) throws MailException {
 233  0
 		checkIfCurrentFolderIsOpen();
 234  
 		try {
 235  0
 			Message[] messages = currentFolder.getMessages();
 236  0
 			if (log.isInfoEnabled()) {
 237  0
 				if (messages.length > 0) {
 238  0
 					log.info(messages.length + "通のメー�?を受信します。");
 239  
 				} else {
 240  0
 					log.info("受信す�?メー�?はありません。");
 241  
 				}
 242  
 			}
 243  
 			// SEENフラグを立て�?
 244  0
 			currentFolder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
 245  
 			// DELETEDフラグを立て�?
 246  0
 			if (delete) {
 247  0
 				currentFolder.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
 248  
 			}
 249  0
 			MimeMessage[] mimeMessages = new MimeMessage[messages.length];
 250  0
 			for (int i = 0; i < messages.length; i++) {
 251  0
 				mimeMessages[i] = (MimeMessage)messages[i];
 252  
 			}
 253  0
 			return mimeMessages;
 254  0
 		} catch (MessagingException e) {
 255  0
 			log.error("メッセージの取得に失敗しました。", e);
 256  0
 			throw new MailFetchException("メッセージの取得に失敗しました。", e);
 257  
 		}
 258  
 	}
 259  
 
 260  
 	/**
 261  
 	 * @see com.ozacc.mail.fetch.FetchMailPro#isConnected()
 262  
 	 */
 263  
 	public boolean isConnected() {
 264  0
 		return store != null && store.isConnected();
 265  
 	}
 266  
 
 267  
 	/**
 268  
 	 *  メー�?サーバのホスト名、またはIPアド�?スを返します。
 269  
 	 * 
 270  
 	 * @return  メー�?サーバのホスト名、またはIPアド�?ス
 271  
 	 */
 272  
 	public String getHost() {
 273  0
 		return host;
 274  
 	}
 275  
 
 276  
 	/**
 277  
 	 * メー�?サーバのホスト名、またはIPアド�?スをセットします。
 278  
 	 * デフォ�?トは localhost です。
 279  
 	 * 
 280  
 	 * @param host メー�?サーバのホスト名、またはIPアド�?ス
 281  
 	 */
 282  
 	public void setHost(String host) {
 283  1
 		this.host = host;
 284  1
 	}
 285  
 
 286  
 	/**
 287  
 	 * メー�?サーバの認証パス�?ードを返します。
 288  
 	 * 
 289  
 	 * @return メー�?サーバの認証パス�?ード
 290  
 	 */
 291  
 	public String getPassword() {
 292  0
 		return password;
 293  
 	}
 294  
 
 295  
 	/**
 296  
 	 * メー�?サーバの認証パス�?ード名をセットします。
 297  
 	 * 
 298  
 	 * @param password メー�?サーバの認証パス�?ード
 299  
 	 */
 300  
 	public void setPassword(String password) {
 301  1
 		this.password = password;
 302  1
 	}
 303  
 
 304  
 	/**
 305  
 	 * メー�?受信に使用す�?プ��組コ���?をセットします。
 306  
 	 * 
 307  
 	 * @return プ��組コ�?
 308  
 	 */
 309  
 	public String getProtocol() {
 310  0
 		return protocol;
 311  
 	}
 312  
 
 313  
 	/**
 314  
 	 * メー�?受信に使用す�?プ��組コ���?をセットします。
 315  
 	 * 現在サポートさ�?てい�?プ��組コ�?は、「pop3」と「imap」の二つです。
 316  
 	 * デフォ�?トは「pop3」です。
 317  
 	 * <p>
 318  
 	 * POP3サーバへの認証をAPOPで行いたい�?合は、プ��組コ�?名ではありませんが、
 319  
 	 * 「apop」を指定してください。APOP認証を使用す�?には、JavaMail 1.3.2以降が必要です。
 320  
 	 * 
 321  
 	 * @param protocol プ��組コ�?
 322  
 	 */
 323  
 	public void setProtocol(String protocol) {
 324  1
 		this.protocol = protocol;
 325  1
 	}
 326  
 
 327  
 	/**
 328  
 	 * @return 認証ユーザ名
 329  
 	 */
 330  
 	public String getUsername() {
 331  0
 		return username;
 332  
 	}
 333  
 
 334  
 	/**
 335  
 	 * メー�?サーバの認証ユーザ名をセットします。
 336  
 	 * 
 337  
 	 * @param username 認証ユーザ名
 338  
 	 */
 339  
 	public void setUsername(String username) {
 340  1
 		this.username = username;
 341  1
 	}
 342  
 
 343  
 	/**
 344  
 	 * @return ポート番�?
 345  
 	 */
 346  
 	public int getPort() {
 347  0
 		return port;
 348  
 	}
 349  
 
 350  
 	/**
 351  
 	 * メー�?受信に使用す�?ポート番号をセットします。
 352  
 	 * プ��組コ�?に�?じたポート番号が自動的に使用さ�?ますので、通�?ここでポート番号をセットす�?必要はありません。
 353  
 	 * 
 354  
 	 * @param port ポート番�?
 355  
 	 */
 356  
 	public void setPort(int port) {
 357  0
 		this.port = port;
 358  0
 	}
 359  
 
 360  
 	/**
 361  
 	 * JavaMailのデバッグが有効かどうか判定します。
 362  
 	 * 
 363  
 	 * @return JavaMailのデバッグが有効な�?�? ture
 364  
 	 */
 365  
 	public boolean isJavaMailLogEnabled() {
 366  0
 		return javaMailLogEnabled;
 367  
 	}
 368  
 
 369  
 	/**
 370  
 	 * JavaMailのデバッグを有効にす�?かどうか指定します。
 371  
 	 * 有効にす�?と、<code>System.out</code>のデバッグメッセージが出力さ�?ます。<br>
 372  
 	 * デフォ�?トは無効になっています。
 373  
 	 * 
 374  
 	 * @see javax.mail.session#setDebug(boolean)
 375  
 	 * @param javaMailLogEnabled The javaMailLogEnabled to set.
 376  
 	 */
 377  
 	public void setJavaMailLogEnabled(boolean javaMailLogEnabled) {
 378  0
 		this.javaMailLogEnabled = javaMailLogEnabled;
 379  0
 	}
 380  
 }

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