From 5f7a0220430a285b9537eb4596f792a1eb62c977 Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Thu, 3 Mar 2016 14:19:09 -0800 Subject: [PATCH 1/3] Add appengine mailgun sample Add gitignore for appengine dir --- appengine/.gitignore | 33 +++++++ appengine/mailgun/pom.xml | 76 +++++++++++++++ .../appengine/mailgun/MailgunServlet.java | 94 +++++++++++++++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 10 ++ .../mailgun/src/main/webapp/WEB-INF/web.xml | 14 +++ appengine/mailgun/src/main/webapp/index.html | 15 +++ 6 files changed, 242 insertions(+) create mode 100644 appengine/.gitignore create mode 100644 appengine/mailgun/pom.xml create mode 100644 appengine/mailgun/src/main/java/com/example/appengine/mailgun/MailgunServlet.java create mode 100644 appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml create mode 100644 appengine/mailgun/src/main/webapp/WEB-INF/web.xml create mode 100644 appengine/mailgun/src/main/webapp/index.html diff --git a/appengine/.gitignore b/appengine/.gitignore new file mode 100644 index 00000000000..d30ea344569 --- /dev/null +++ b/appengine/.gitignore @@ -0,0 +1,33 @@ +# Google App Engine generated folder +appengine-generated/ + +# Java +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties + +service-account.json + +#eclipse +.classpath +.settings +.project diff --git a/appengine/mailgun/pom.xml b/appengine/mailgun/pom.xml new file mode 100644 index 00000000000..77dc011713a --- /dev/null +++ b/appengine/mailgun/pom.xml @@ -0,0 +1,76 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-mailgun + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + javax.servlet + javax.servlet-api + 3.1.0 + jar + provided + + + + com.sun.jersey + jersey-core + 1.19 + + + com.sun.jersey + jersey-client + 1.19 + + + com.sun.jersey.contribs + jersey-multipart + 1.19 + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + com.google.appengine + appengine-maven-plugin + 1.9.31 + + + + diff --git a/appengine/mailgun/src/main/java/com/example/appengine/mailgun/MailgunServlet.java b/appengine/mailgun/src/main/java/com/example/appengine/mailgun/MailgunServlet.java new file mode 100644 index 00000000000..6d03cc29449 --- /dev/null +++ b/appengine/mailgun/src/main/java/com/example/appengine/mailgun/MailgunServlet.java @@ -0,0 +1,94 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.mailgun; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.util.MultivaluedMapImpl; +import com.sun.jersey.multipart.FormDataMultiPart; +import com.sun.jersey.multipart.file.FileDataBodyPart; + +import java.io.File; +import java.io.IOException; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; + +// [START example] +@SuppressWarnings("serial") +@WebServlet(name = "mailgun", value = "/send/email") +public class MailgunServlet extends HttpServlet { + + private static final String MAILGUN_DOMAIN_NAME = System.getenv("MAILGUN_DOMAIN_NAME"); + private static final String MAILGUN_API_KEY = System.getenv("MAILGUN_API_KEY"); + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + String type = req.getParameter("submit"); + String recipient = req.getParameter("to"); + ClientResponse clientResponse; + if (type.equals("Send simple email")) { + clientResponse = sendSimpleMessage(recipient); + } else { + clientResponse = sendComplexMessage(recipient); + } + if (clientResponse.getStatus() == 200) { + resp.getWriter().print("Email sent."); + } + } + + // [START simple] + private ClientResponse sendSimpleMessage(String recipient) { + Client client = Client.create(); + client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY)); + WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + + "/messages"); + MultivaluedMapImpl formData = new MultivaluedMapImpl(); + formData.add("from", "Mailgun User "); + formData.add("to", recipient); + formData.add("subject", "Simple Mailgun Example"); + formData.add("text", "Plaintext content"); + return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, + formData); + } + // [END simple] + + // [START complex] + private ClientResponse sendComplexMessage(String recipient) { + Client client = Client.create(); + client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY)); + WebResource webResource = client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + + "/messages"); + FormDataMultiPart formData = new FormDataMultiPart(); + formData.field("from", "Mailgun User "); + formData.field("to", recipient); + formData.field("subject", "Complex Mailgun Example"); + formData.field("html", "HTML content"); + ClassLoader classLoader = getClass().getClassLoader(); + File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile()); + formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE)); + return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE) + .post(ClientResponse.class, formData); + } + // [END complex] +} +// [END example] diff --git a/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..6be85da73a7 --- /dev/null +++ b/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,10 @@ + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + + + + + diff --git a/appengine/mailgun/src/main/webapp/WEB-INF/web.xml b/appengine/mailgun/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..40ef318e353 --- /dev/null +++ b/appengine/mailgun/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + + mailgun + com.example.appengine.mailgun.MailgunServlet + + + mailgun + /send/email + + \ No newline at end of file diff --git a/appengine/mailgun/src/main/webapp/index.html b/appengine/mailgun/src/main/webapp/index.html new file mode 100644 index 00000000000..3b26f817c78 --- /dev/null +++ b/appengine/mailgun/src/main/webapp/index.html @@ -0,0 +1,15 @@ + + + + Mailgun on Google App Engine Managed VMs + + + +
+ + + +
+ + + From 4c831a14b94278002a7e63c1b9db58cd434a494b Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Thu, 3 Mar 2016 14:30:19 -0800 Subject: [PATCH 2/3] Update parent pom with appengine mailgun --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 30a23aa7e92..df3183dcfab 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ appengine/appidentity appengine/helloworld + appengine/mailgun bigquery datastore logging From 4cc54e2eb5c8b045c9fe111f678cd2d8b42c84a6 Mon Sep 17 00:00:00 2001 From: Shun Fan Date: Thu, 10 Mar 2016 11:01:11 -0800 Subject: [PATCH 3/3] Add licenses and extra lines to mailgun --- appengine/.gitignore | 11 +++++++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 15 +++++++++++++++ .../mailgun/src/main/webapp/WEB-INF/web.xml | 17 ++++++++++++++++- appengine/mailgun/src/main/webapp/index.html | 12 ++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/appengine/.gitignore b/appengine/.gitignore index d30ea344569..453750bc157 100644 --- a/appengine/.gitignore +++ b/appengine/.gitignore @@ -1,3 +1,14 @@ +# Copyright 2015 Google Inc. All Rights Reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # Google App Engine generated folder appengine-generated/ diff --git a/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml index 6be85da73a7..f720d147b74 100644 --- a/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/mailgun/src/main/webapp/WEB-INF/appengine-web.xml @@ -1,4 +1,18 @@ + + + YOUR-PROJECT-ID YOUR-VERSION-ID @@ -8,3 +22,4 @@ + diff --git a/appengine/mailgun/src/main/webapp/WEB-INF/web.xml b/appengine/mailgun/src/main/webapp/WEB-INF/web.xml index 40ef318e353..26e63250cbe 100644 --- a/appengine/mailgun/src/main/webapp/WEB-INF/web.xml +++ b/appengine/mailgun/src/main/webapp/WEB-INF/web.xml @@ -1,4 +1,18 @@ + + + mailgun /send/email - \ No newline at end of file + + diff --git a/appengine/mailgun/src/main/webapp/index.html b/appengine/mailgun/src/main/webapp/index.html index 3b26f817c78..6049c6945e7 100644 --- a/appengine/mailgun/src/main/webapp/index.html +++ b/appengine/mailgun/src/main/webapp/index.html @@ -1,4 +1,16 @@ + Mailgun on Google App Engine Managed VMs