Skip to content

Commit 5754319

Browse files
committed
chore: upgrade mail example to java21
1 parent 73333b1 commit 5754319

File tree

10 files changed

+623
-0
lines changed

10 files changed

+623
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# JavaMail API Email Sample for Google App Engine Standard Environment
2+
3+
This sample demonstrates how to use [JavaMail][javamail-api] on [Google App Engine
4+
standard environment][ae-docs].
5+
6+
See the [sample application documentaion][sample-docs] for more detailed
7+
instructions.
8+
9+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
10+
[javamail-api]: http://javamail.java.net/
11+
[sample-docs]: https://cloud.google.com/appengine/docs/java/mail/
12+
13+
## Setup
14+
15+
gcloud init
16+
17+
## Running locally
18+
$ mvn appengine:run
19+
20+
## Deploying
21+
$ mvn clean package appengine:deploy

appengine-java21/ee8/mail/pom.xml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!--
2+
Copyright 2016 Google Inc.
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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-mail-j21</artifactId>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.2.0</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>21</maven.compiler.target>
36+
<maven.compiler.source>21</maven.compiler.source>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>javax.servlet</groupId>
42+
<artifactId>javax.servlet-api</artifactId>
43+
<version>3.1.0</version>
44+
<type>jar</type>
45+
<scope>provided</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>com.google.appengine</groupId>
50+
<artifactId>appengine-api-1.0-sdk</artifactId>
51+
<version>2.0.23</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>javax.mail</groupId>
55+
<artifactId>mail</artifactId>
56+
<version>1.4.7</version>
57+
</dependency>
58+
</dependencies>
59+
<build>
60+
<!-- for hot reload of the web application -->
61+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-war-plugin</artifactId>
66+
<version>3.4.0</version>
67+
</plugin>
68+
<plugin>
69+
<groupId>com.google.cloud.tools</groupId>
70+
<artifactId>appengine-maven-plugin</artifactId>
71+
<version>2.5.0</version>
72+
<configuration>
73+
<projectId>GCLOUD_CONFIG</projectId>
74+
<version>GCLOUD_CONFIG</version>
75+
<deploy.promote>true</deploy.promote>
76+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2016 Google LLC
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+
// [START gae_java21_mail_bounce_handler]
20+
21+
import com.google.appengine.api.mail.BounceNotification;
22+
import com.google.appengine.api.mail.BounceNotificationParser;
23+
import java.io.IOException;
24+
import java.util.logging.Logger;
25+
import javax.mail.MessagingException;
26+
import javax.servlet.http.HttpServlet;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpServletResponse;
29+
30+
public class BounceHandlerServlet extends HttpServlet {
31+
32+
private static final Logger log = Logger.getLogger(BounceHandlerServlet.class.getName());
33+
34+
@Override
35+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
36+
try {
37+
BounceNotification bounce = BounceNotificationParser.parse(req);
38+
log.warning("Bounced email notification.");
39+
// The following data is available in a BounceNotification object
40+
// bounce.getOriginal().getFrom()
41+
// bounce.getOriginal().getTo()
42+
// bounce.getOriginal().getSubject()
43+
// bounce.getOriginal().getText()
44+
// bounce.getNotification().getFrom()
45+
// bounce.getNotification().getTo()
46+
// bounce.getNotification().getSubject()
47+
// bounce.getNotification().getText()
48+
// ...
49+
} catch (MessagingException e) {
50+
// ...
51+
}
52+
}
53+
}
54+
// [END gae_java21_mail_bounce_handler]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 Google LLC
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 java.util.logging.Logger;
20+
import java.util.regex.Matcher;
21+
import javax.mail.internet.MimeMessage;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.http.HttpServletRequest;
24+
import javax.servlet.http.HttpServletResponse;
25+
26+
// [START gae_java21_mail_discussion_email]
27+
public class HandleDiscussionEmail extends MailHandlerBase {
28+
29+
private static final Logger log = Logger.getLogger(HandleDiscussionEmail.class.getName());
30+
31+
public HandleDiscussionEmail() {
32+
super("discuss-(.*)@(.*)");
33+
}
34+
35+
@Override
36+
protected boolean processMessage(HttpServletRequest req, HttpServletResponse res)
37+
throws ServletException {
38+
log.info("Received e-mail sent to discuss list.");
39+
MimeMessage msg = getMessageFromRequest(req);
40+
Matcher match = getMatcherFromRequest(req);
41+
// ...
42+
return true;
43+
}
44+
}
45+
// [END gae_java21_mail_discussion_email]
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2016 Google LLC
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 java.io.IOException;
20+
import java.util.Properties;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
23+
import javax.mail.MessagingException;
24+
import javax.mail.Session;
25+
import javax.mail.internet.MimeMessage;
26+
import javax.servlet.Filter;
27+
import javax.servlet.FilterChain;
28+
import javax.servlet.FilterConfig;
29+
import javax.servlet.ServletException;
30+
import javax.servlet.ServletRequest;
31+
import javax.servlet.ServletResponse;
32+
import javax.servlet.http.HttpServletRequest;
33+
import javax.servlet.http.HttpServletResponse;
34+
35+
/** Base class for handling the filtering of incoming emails in App Engine. */
36+
// [START gae_java21_mail_handler_base]
37+
public abstract class MailHandlerBase implements Filter {
38+
39+
private Pattern pattern = null;
40+
41+
protected MailHandlerBase(String pattern) {
42+
if (pattern == null || pattern.trim().length() == 0) {
43+
throw new IllegalArgumentException("Expected non-empty regular expression");
44+
}
45+
this.pattern = Pattern.compile("/_ah/mail/" + pattern);
46+
}
47+
48+
@Override
49+
public void init(FilterConfig config) throws ServletException {}
50+
51+
@Override
52+
public void destroy() {}
53+
54+
/**
55+
* Process the message. A message will only be passed to this method if the servletPath of the
56+
* message (typically the recipient for appengine) satisfies the pattern passed to the
57+
* constructor. If the implementation returns false, control is passed to the next filter in the
58+
* chain. If the implementation returns true, the filter chain is terminated.
59+
*
60+
* <p>The Matcher for the pattern can be retrieved via getMatcherFromRequest (e.g. if groups are
61+
* used in the pattern).
62+
*/
63+
protected abstract boolean processMessage(HttpServletRequest req, HttpServletResponse res)
64+
throws ServletException;
65+
66+
@Override
67+
public void doFilter(ServletRequest sreq, ServletResponse sres, FilterChain chain)
68+
throws IOException, ServletException {
69+
70+
HttpServletRequest req = (HttpServletRequest) sreq;
71+
HttpServletResponse res = (HttpServletResponse) sres;
72+
73+
MimeMessage message = getMessageFromRequest(req);
74+
Matcher m = applyPattern(req);
75+
76+
if (m != null && processMessage(req, res)) {
77+
return;
78+
}
79+
80+
chain.doFilter(req, res); // Try the next one
81+
}
82+
83+
private Matcher applyPattern(HttpServletRequest req) {
84+
Matcher m = pattern.matcher(req.getServletPath());
85+
if (!m.matches()) {
86+
m = null;
87+
}
88+
89+
req.setAttribute("matcher", m);
90+
return m;
91+
}
92+
93+
protected Matcher getMatcherFromRequest(ServletRequest req) {
94+
return (Matcher) req.getAttribute("matcher");
95+
}
96+
97+
protected MimeMessage getMessageFromRequest(ServletRequest req) throws ServletException {
98+
MimeMessage message = (MimeMessage) req.getAttribute("mimeMessage");
99+
if (message == null) {
100+
try {
101+
Properties props = new Properties();
102+
Session session = Session.getDefaultInstance(props, null);
103+
message = new MimeMessage(session, req.getInputStream());
104+
req.setAttribute("mimeMessage", message);
105+
106+
} catch (MessagingException e) {
107+
throw new ServletException("Error processing inbound message", e);
108+
} catch (IOException e) {
109+
throw new ServletException("Error processing inbound message", e);
110+
}
111+
}
112+
return message;
113+
}
114+
}
115+
// [END gae_java21_mail_handler_base]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Google LLC
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+
// [START gae_java21_mail_handler_servlet]
20+
21+
import java.io.IOException;
22+
import java.util.Properties;
23+
import java.util.logging.Logger;
24+
import javax.mail.MessagingException;
25+
import javax.mail.Session;
26+
import javax.mail.internet.MimeMessage;
27+
import javax.servlet.http.HttpServlet;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
public class MailHandlerServlet extends HttpServlet {
32+
33+
private static final Logger log = Logger.getLogger(MailHandlerServlet.class.getName());
34+
35+
@Override
36+
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
37+
Properties props = new Properties();
38+
Session session = Session.getDefaultInstance(props, null);
39+
try {
40+
MimeMessage message = new MimeMessage(session, req.getInputStream());
41+
log.info("Received mail message.");
42+
} catch (MessagingException e) {
43+
// ...
44+
}
45+
// ...
46+
}
47+
}
48+
// [END gae_java21_mail_handler_servlet]

0 commit comments

Comments
 (0)