|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.mail; |
| 18 | + |
| 19 | +import javax.mail.internet.MimeMessage; |
| 20 | +import javax.mail.MessagingException; |
| 21 | +import javax.mail.Session; |
| 22 | +import javax.servlet.Filter; |
| 23 | +import javax.servlet.FilterChain; |
| 24 | +import javax.servlet.FilterConfig; |
| 25 | +import javax.servlet.http.HttpServletRequest; |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | +import javax.servlet.ServletException; |
| 28 | +import javax.servlet.ServletRequest; |
| 29 | +import javax.servlet.ServletResponse; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Properties; |
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
| 34 | + |
| 35 | +/** |
| 36 | + * Base class for handling the filtering of incoming emails in App Engine. |
| 37 | + */ |
| 38 | +// [START example] |
| 39 | +public abstract class MailHandlerBase implements Filter { |
| 40 | + |
| 41 | + private Pattern pattern = null; |
| 42 | + |
| 43 | + protected MailHandlerBase(String pattern) { |
| 44 | + if (pattern == null || pattern.trim().length() == 0) |
| 45 | + { |
| 46 | + throw new IllegalArgumentException("Expected non-empty regular expression"); |
| 47 | + } |
| 48 | + this.pattern = Pattern.compile("/_ah/mail/"+pattern); |
| 49 | + } |
| 50 | + |
| 51 | + @Override public void init(FilterConfig config) throws ServletException { } |
| 52 | + |
| 53 | + @Override public void destroy() { } |
| 54 | + |
| 55 | + /** |
| 56 | + * Process the message. A message will only be passed to this method |
| 57 | + * if the servletPath of the message (typically the recipient for |
| 58 | + * appengine) satisfies the pattern passed to the constructor. If |
| 59 | + * the implementation returns false, control is passed |
| 60 | + * to the next filter in the chain. If the implementation returns |
| 61 | + * true, the filter chain is terminated. |
| 62 | + * |
| 63 | + * The Matcher for the pattern can be retrieved via |
| 64 | + * getMatcherFromRequest (e.g. if groups are used in the pattern). |
| 65 | + */ |
| 66 | + protected abstract boolean processMessage(HttpServletRequest req, HttpServletResponse res) throws ServletException; |
| 67 | + |
| 68 | + @Override |
| 69 | + public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain) |
| 70 | + throws IOException, ServletException { |
| 71 | + |
| 72 | + HttpServletRequest req = (HttpServletRequest) sreq; |
| 73 | + HttpServletResponse res = (HttpServletResponse) sres; |
| 74 | + |
| 75 | + MimeMessage message = getMessageFromRequest(req); |
| 76 | + Matcher m = applyPattern(req); |
| 77 | + |
| 78 | + if (m != null && processMessage(req, res)) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + chain.doFilter(req, res); // Try the next one |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + private Matcher applyPattern(HttpServletRequest req) { |
| 87 | + Matcher m = pattern.matcher(req.getServletPath()); |
| 88 | + if (!m.matches()) m = null; |
| 89 | + |
| 90 | + req.setAttribute("matcher", m); |
| 91 | + return m; |
| 92 | + } |
| 93 | + |
| 94 | + protected Matcher getMatcherFromRequest(ServletRequest req) { |
| 95 | + return (Matcher) req.getAttribute("matcher"); |
| 96 | + } |
| 97 | + |
| 98 | + protected MimeMessage getMessageFromRequest(ServletRequest req) throws ServletException { |
| 99 | + MimeMessage message = (MimeMessage) req.getAttribute("mimeMessage"); |
| 100 | + if (message == null) { |
| 101 | + try { |
| 102 | + Properties props = new Properties(); |
| 103 | + Session session = Session.getDefaultInstance(props, null); |
| 104 | + message = new MimeMessage(session, req.getInputStream()); |
| 105 | + req.setAttribute("mimeMessage", message); |
| 106 | + |
| 107 | + } catch (MessagingException e) { |
| 108 | + throw new ServletException("Error processing inbound message", e); |
| 109 | + } catch (IOException e) { |
| 110 | + throw new ServletException("Error processing inbound message", e); |
| 111 | + } |
| 112 | + } |
| 113 | + return message; |
| 114 | + } |
| 115 | +} |
| 116 | +// [END example] |
0 commit comments