GAEでデータストア登録 -サークルIT化のお手伝い
勝手に思ってるサークルの問題点に勝手に取り組むシリーズ改め、「サークルIT化のお手伝い」で引き続きやっていこうと思います。今回は、GAEでデータストア登録です。
おさらい -こんなことがしたいんです!
新入生からのメールに対して、自動でGmailにアドレスを登録するところまで持ってきたい。
今回は、メール受信から次の3つを取得してデータストアに登録します。
- メールアドレス
- 件名
- 本文
件名に自分の名前を入れてもらえれば、メールアドレスと件名を取得して、アドレス帳ができる。本文に記入するのは、予備ってことで。
今回も Google App Engine for Javaでのメール受信コード -きしだのはてな のソースを参考にしています。
今回新たに、
を付け加えました。データストアに登録するのって難しいです>_< いまいちよくわかってません。
そんで、デプロイ。GAE宛にメールを送ってみると・・・・
ソース
PMF.java
package javamail.util;import javax.jdo.JDOHelper; import javax.jdo.PersistenceManagerFactory;
//永続化マネージャを管理 public final class PMF { private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional"); private PMF() {} public static PersistenceManagerFactory get(){ return pmfInstance; } }
ReceivedMail.java
package javamail;import javax.jdo.annotations.IdGeneratorStrategy; import javax.jdo.annotations.IdentityType; import javax.jdo.annotations.PersistenceCapable; import javax.jdo.annotations.Persistent; import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(identityType=IdentityType.APPLICATION) public class ReceivedMail { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Long id;
<span class="synPreProc">@Persistent</span> <span class="synType">private</span> String from; <span class="synPreProc">@Persistent</span> <span class="synType">private</span> String subject; <span class="synPreProc">@Persistent</span> <span class="synType">private</span> String text; <span class="synType">public</span> String getFrom() { <span class="synStatement">return</span> from; } <span class="synType">public</span> String getSubject() { <span class="synStatement">return</span> subject; } <span class="synType">public</span> String getText() { <span class="synStatement">return</span> text; } <span class="synType">public</span> <span class="synType">void</span> setFrom(String from) { <span class="synType">this</span>.from = from; } <span class="synType">public</span> <span class="synType">void</span> setSubject(String subject) { <span class="synType">this</span>.subject = subject; } <span class="synType">public</span> <span class="synType">void</span> setText(String text) { <span class="synType">this</span>.text = text; } <span class="synType">public</span> <span class="synType">void</span> setId(Long id) { <span class="synType">this</span>.id = id; } <span class="synType">public</span> Long getId() { <span class="synStatement">return</span> id; }
}
JavaMailServlet.java
package javamail;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Properties;
import javamail.util.PMF;
import javax.jdo.PersistenceManager; import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial") public class JavaMailServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { PersistenceManager pm = PMF.get().getPersistenceManager();
Properties props = <span class="synStatement">new</span> Properties(); Session session = Session.getDefaultInstance(props, <span class="synConstant">null</span>); <span class="synStatement">try</span> { MimeMessage message = <span class="synStatement">new</span> MimeMessage(session, req.getInputStream()); ReceivedMail mail = <span class="synStatement">new</span> ReceivedMail(); mail.setSubject(message.getSubject()); <span class="synComment">//ローカルサーバーでは文字化けするので次のようなコードが必要。本番サーバーでは不要</span> <span class="synComment">//mail.setSubject(new String(message.getSubject().getBytes("8859_1"), "UTF-8"));</span> mail.setFrom(parseFrom(message.getFrom()[<span class="synConstant">0</span>].toString())); String contentType = message.getContentType(); InputStream is = <span class="synConstant">null</span>; String mess = <span class="synConstant">""</span>; <span class="synStatement">if</span>(message.isMimeType(<span class="synConstant">"text/plain"</span>)){ <span class="synComment">//ふつうのメールの処理</span> mess = (String)message.getContent(); }<span class="synStatement">else</span>{ <span class="synComment">//HTMLメールや添付メールの処理</span> Multipart content = (Multipart)message.getContent(); <span class="synStatement">for</span>(<span class="synType">int</span> i = <span class="synConstant">0</span>; i < content.getCount(); ++i){ BodyPart bp = content.getBodyPart(i); <span class="synStatement">if</span>(!bp.isMimeType(<span class="synConstant">"text/plain"</span>)) <span class="synStatement">continue</span>; is = bp.getInputStream(); contentType = bp.getContentType(); <span class="synStatement">break</span>; } } <span class="synStatement">if</span>(is != <span class="synConstant">null</span>){ <span class="synComment">//contentTypeからエンコーディングを取得</span> String encoding = <span class="synConstant">null</span>; String[] elms = contentType.split(<span class="synConstant">";"</span>); <span class="synStatement">for</span>(String elm : elms){ <span class="synStatement">if</span>(elm.trim().startsWith(<span class="synConstant">"charset="</span>)){ encoding = elm.trim().substring(<span class="synConstant">"charset="</span>.length()); } } Reader r = <span class="synConstant">null</span>; <span class="synStatement">if</span>(encoding != <span class="synConstant">null</span>){ <span class="synComment">//エンコーディングが入っている</span> <span class="synStatement">if</span>(encoding.startsWith(<span class="synConstant">"</span><span class="synSpecial">\"</span><span class="synConstant">"</span>)) encoding = encoding.substring(<span class="synConstant">1</span>); <span class="synStatement">if</span>(encoding.endsWith(<span class="synConstant">"</span><span class="synSpecial">\"</span><span class="synConstant">"</span>)) encoding = encoding.substring(<span class="synConstant">0</span>, encoding.length() - <span class="synConstant">1</span>); r = <span class="synStatement">new</span> InputStreamReader(is, encoding); }<span class="synStatement">else</span>{ <span class="synComment">//エンコーディングが入っていない</span> r = <span class="synStatement">new</span> InputStreamReader(is); } BufferedReader buf = <span class="synStatement">new</span> BufferedReader(r); <span class="synStatement">for</span>(String line; (line = buf.readLine()) != <span class="synConstant">null</span>;){ mess += line + <span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>; } } <span class="synComment">//パース処理</span> ArrayList<String> sets = <span class="synStatement">new</span> ArrayList<String>(); <span class="synStatement">for</span> (String str : mess.split(<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>)) { sets.add(str); } String changedmess = <span class="synConstant">""</span>; <span class="synStatement">for</span> (String set : sets) { changedmess += set; } mail.setText(changedmess); pm.makePersistent(mail); } <span class="synStatement">catch</span> (MessagingException ex) { ex.printStackTrace(); } <span class="synStatement">finally</span> { pm.close(); } } <span class="synComment">//アドレス部分だけ抜き出す</span> <span class="synType">private</span> String parseFrom(String from){ <span class="synStatement">return</span> from.substring(from.indexOf(<span class="synConstant">"<"</span>) + <span class="synConstant">1</span>, from.lastIndexOf(<span class="synConstant">">"</span>)); }
}
Related contents
TECH
2010.09.18
TECH
2010.07.13
TECH
2010.05.29
TECH
2011.04.18
TECH
2009.05.19
TECHBOOK
2009.05.14
TECH
2008.05.31