2 package jp.gr.java_conf.u6k.money_notebook.web.servlet.mail;
\r
4 import java.io.IOException;
\r
5 import java.io.UnsupportedEncodingException;
\r
6 import java.util.Date;
\r
7 import java.util.Properties;
\r
8 import java.util.TimeZone;
\r
9 import java.util.logging.Logger;
\r
11 import javax.jdo.PersistenceManager;
\r
12 import javax.jdo.Transaction;
\r
13 import javax.mail.Message;
\r
14 import javax.mail.MessagingException;
\r
15 import javax.mail.Session;
\r
16 import javax.mail.Transport;
\r
17 import javax.mail.internet.InternetAddress;
\r
18 import javax.mail.internet.MimeMessage;
\r
19 import javax.servlet.ServletException;
\r
20 import javax.servlet.http.HttpServlet;
\r
21 import javax.servlet.http.HttpServletRequest;
\r
22 import javax.servlet.http.HttpServletResponse;
\r
24 import jp.gr.java_conf.u6k.money_notebook.dao.AccountDao;
\r
25 import jp.gr.java_conf.u6k.money_notebook.dao.AccountEntity;
\r
26 import jp.gr.java_conf.u6k.money_notebook.dao.PMF;
\r
27 import jp.gr.java_conf.u6k.money_notebook.dao.TempAccountEntity;
\r
28 import jp.gr.java_conf.u6k.money_notebook.dao.UserEntity;
\r
29 import jp.gr.java_conf.u6k.money_notebook.util.DateUtil;
\r
31 @SuppressWarnings("serial")
\r
32 public class RegistMailServlet extends HttpServlet {
\r
34 private static final Logger LOG = Logger.getLogger(RegistMailServlet.class.getName());
\r
37 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
\r
38 RegistMailServlet.LOG.config("会員登録メール受信処理開始");
\r
40 TimeZone tz = DateUtil.getUserTimeZone();
\r
41 Date currentDatetime = DateUtil.getNow(tz);
\r
43 // メールを解析して、メール送信元を取得する。
\r
44 String email = this.getFromAddressFromRequestMail(req);
\r
48 PersistenceManager pm = PMF.get().getPersistenceManager();
\r
50 Transaction tx = pm.currentTransaction();
\r
54 // 既にメールアドレスが登録されている場合、会員登録はしないで処理を終了する。
\r
55 boolean exist = this.existAccount(pm, email);
\r
58 // 既に会員登録されているので、ユーザを作成しないでメールを返信する。
\r
59 this.sendReturnAlreadyExistMail(email);
\r
61 // ユーザを作成して、会員仮登録する。
\r
62 user = this.createUserAndTempAccount(email, currentDatetime);
\r
63 pm.makePersistent(user);
\r
65 // 仮登録したことをユーザにメール送信する。
\r
66 this.sendReturnMail(user);
\r
71 if (tx.isActive()) {
\r
79 RegistMailServlet.LOG.config("会員登録メール受信処理終了");
\r
80 } catch (MessagingException e) {
\r
81 throw new ServletException(e);
\r
85 private String getFromAddressFromRequestMail(HttpServletRequest req) throws MessagingException, IOException, ServletException {
\r
86 RegistMailServlet.LOG.config("メール送信元取得処理開始");
\r
88 Properties props = System.getProperties();
\r
89 Session mailSession = Session.getDefaultInstance(props, null);
\r
90 MimeMessage msg = new MimeMessage(mailSession, req.getInputStream());
\r
92 if (msg.getFrom().length != 1) {
\r
93 throw new ServletException("メール送信元が1つではありません。");
\r
95 if (!(msg.getFrom()[0] instanceof InternetAddress)) {
\r
96 throw new ServletException("メール送信元がInternetAddressではありません。");
\r
99 String email = ((InternetAddress) msg.getFrom()[0]).getAddress();
\r
101 RegistMailServlet.LOG.config("メール送信元: " + email);
\r
102 RegistMailServlet.LOG.config("メール送信元取得処理終了");
\r
107 private boolean existAccount(PersistenceManager pm, String email) {
\r
108 RegistMailServlet.LOG.config("既存会員判定処理開始");
\r
110 AccountDao accountDao = new AccountDao(pm);
\r
111 AccountEntity account = accountDao.findByEmail(email);
\r
113 boolean result = (account != null);
\r
115 RegistMailServlet.LOG.config("既存会員判定処理終了");
\r
120 private UserEntity createUserAndTempAccount(String email, Date currentDatetime) {
\r
121 RegistMailServlet.LOG.config("ユーザ、及び一時会員登録処理開始");
\r
123 TempAccountEntity tempAccount = new TempAccountEntity();
\r
124 tempAccount.setEmail(email);
\r
125 tempAccount.setUpdateDatetime(currentDatetime);
\r
127 UserEntity user = new UserEntity();
\r
128 user.setUpdateDatetime(currentDatetime);
\r
129 user.setTempAccount(tempAccount);
\r
131 RegistMailServlet.LOG.config("ユーザ、及び一時会員登録処理終了");
\r
136 private void sendReturnMail(UserEntity user) throws MessagingException, UnsupportedEncodingException {
\r
137 RegistMailServlet.LOG.config("一時会員登録返信メール送信処理開始");
\r
139 String content = "以下のURLにアクセスして、会員登録を続行してください。\r\n";
\r
140 content += "http://money-notebook.appspot.com" + this.getServletContext().getContextPath() + "/m/regist/" + user.getKey().getId() + "/";
\r
142 InternetAddress fromAddress = new InternetAddress("u6k.yu1@gmail.com");
\r
143 InternetAddress toAddress = new InternetAddress(user.getTempAccount().getEmail());
\r
145 Properties props = System.getProperties();
\r
146 Session mailSession = Session.getDefaultInstance(props, null);
\r
148 MimeMessage mail = new MimeMessage(mailSession);
\r
149 mail.setHeader("Content-Type", "text/plain; charset=ISO-2022-JP");
\r
150 mail.setHeader("Content-Transfer-Encoding", "7bit");
\r
151 mail.setFrom(fromAddress);
\r
152 mail.addRecipient(Message.RecipientType.TO, toAddress);
\r
153 mail.setSubject("[小遣い帳(仮)]会員登録受付のお知らせ", "ISO-2022-JP");
\r
154 mail.setText(content, "ISO-2022-JP");
\r
156 Transport.send(mail);
\r
158 RegistMailServlet.LOG.config("一時会員登録返信メール送信処理終了");
\r
161 private void sendReturnAlreadyExistMail(String email) throws MessagingException {
\r
162 RegistMailServlet.LOG.config("既に会員登録されています返信メール送信処理開始");
\r
164 String content = email + "様はすでに会員登録されています。\r\n";
\r
165 content += "ログインして小遣い帳(仮)を利用できます。\r\n";
\r
166 content += "http://money-notebook.appspot.com" + this.getServletContext().getContextPath() + "/m/login\r\n";
\r
168 content += "パスワードを忘れてしまった場合、一時的なパスワードを発行することができます。\r\n";
\r
169 content += "http://money-notebook.appspot.com" + this.getServletContext().getContextPath() + "/m/onetime-password\r\n";
\r
171 content += "そのほかのお問い合わせは、ヘルプをご覧下さい。\r\n";
\r
172 content += "http://money-notebook.appspot.com" + this.getServletContext().getContextPath() + "m/help\r\n";
\r
174 InternetAddress fromAddress = new InternetAddress("u6k.yu1@gmail.com");
\r
175 InternetAddress toAddress = new InternetAddress(email);
\r
177 Properties props = System.getProperties();
\r
178 Session mailSession = Session.getDefaultInstance(props, null);
\r
180 MimeMessage mail = new MimeMessage(mailSession);
\r
181 mail.setHeader("Content-Type", "text/plain; charset=ISO-2022-JP");
\r
182 mail.setHeader("Content-Transfer-Encoding", "7bit");
\r
183 mail.setFrom(fromAddress);
\r
184 mail.addRecipient(Message.RecipientType.TO, toAddress);
\r
185 mail.setSubject("[小遣い帳(仮)]既に会員登録されています", "ISO-2022-JP");
\r
186 mail.setText(content, "ISO-2022-JP");
\r
188 Transport.send(mail);
\r
190 RegistMailServlet.LOG.config("既に会員登録されています返信メール送信処理終了");
\r