diff --git a/appengine/xmpp/README.md b/appengine/xmpp/README.md new file mode 100644 index 00000000000..7ca936b5f0c --- /dev/null +++ b/appengine/xmpp/README.md @@ -0,0 +1,20 @@ +# Google App Engine Standard Environment XMPP Java API Overview + +This sample demonstrates how to use XMPP Java API on Google App Engine. + +See the [Google App Engine standard environment documentation][ae-docs] for more +detailed instructions. + +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +## Setup +1. Update the `` tag in `src/main/webapp/WEB-INF/appengine-web.xml` + with your project name. +1. Update the `` tag in `src/main/webapp/WEB-INF/appengine-web.xml` + with your version name. + +## Running locally + $ mvn appengine:devserver + +## Deploying + $ mvn appengine:update diff --git a/appengine/xmpp/pom.xml b/appengine/xmpp/pom.xml new file mode 100644 index 00000000000..c7c90b8fce8 --- /dev/null +++ b/appengine/xmpp/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-xmpp + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + javax.servlet + servlet-api + jar + provided + + + + + + com.google.appengine + appengine-api-1.0-sdk + + + + + com.google.guava + guava + 19.0 + + + + + + ${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 + ${appengine.sdk.version} + + + + diff --git a/appengine/xmpp/src/main/java/com/example/appengine/xmpp/ErrorServlet.java b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/ErrorServlet.java new file mode 100644 index 00000000000..49e96ffbf0c --- /dev/null +++ b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/ErrorServlet.java @@ -0,0 +1,45 @@ +/** + * Copyright 2016 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.xmpp; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.logging.Logger; +import javax.servlet.ServletInputStream; +import javax.servlet.http.*; +import com.google.common.io.ByteStreams; + +// [START example] +@SuppressWarnings("serial") +public class ErrorServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(ErrorServlet.class.getName()); + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + // Parse the POST data, which is sent as a MIME stream containing the stanza. + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ServletInputStream inputStream = req.getInputStream(); + ByteStreams.copy(inputStream, baos); + + // Log the error + log.warning("Error stanza received: " + baos.toString()); + } +} +// [END example] diff --git a/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageReceiverServlet.java b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageReceiverServlet.java new file mode 100644 index 00000000000..2115c5cca7b --- /dev/null +++ b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageReceiverServlet.java @@ -0,0 +1,47 @@ +/** + * Copyright 2016 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.xmpp; + +import java.io.IOException; +import java.util.logging.Logger; +import javax.servlet.http.*; +import com.google.appengine.api.xmpp.JID; +import com.google.appengine.api.xmpp.Message; +import com.google.appengine.api.xmpp.XMPPService; +import com.google.appengine.api.xmpp.XMPPServiceFactory; + +// [START example] +@SuppressWarnings("serial") +public class MessageReceiverServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(MessageReceiverServlet.class.getName()); + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + XMPPService xmpp = XMPPServiceFactory.getXMPPService(); + Message message = xmpp.parseMessage(req); + + JID fromJid = message.getFromJid(); + String body = message.getBody(); + + log.info("Received a message with id: " + fromJid + " and body: " + body); + // ... + } +} +// [END example] diff --git a/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageSenderServlet.java b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageSenderServlet.java new file mode 100644 index 00000000000..4264c5190ef --- /dev/null +++ b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageSenderServlet.java @@ -0,0 +1,58 @@ +/** + * Copyright 2016 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.xmpp; + +import java.io.IOException; +import java.util.logging.Logger; +import javax.servlet.http.*; +import com.google.appengine.api.xmpp.JID; +import com.google.appengine.api.xmpp.Message; +import com.google.appengine.api.xmpp.MessageBuilder; +import com.google.appengine.api.xmpp.SendResponse; +import com.google.appengine.api.xmpp.XMPPService; +import com.google.appengine.api.xmpp.XMPPServiceFactory; + +// [START example] +@SuppressWarnings("serial") +public class MessageSenderServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(MessageSenderServlet.class.getName()); + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + JID jid = new JID("example@gmail.com"); + String msgBody = "Someone has sent you a gift on Example.com. To view: http://example.com/gifts/"; + Message msg = new MessageBuilder() + .withRecipientJids(jid) + .withBody(msgBody) + .build(); + + boolean messageSent = false; + XMPPService xmpp = XMPPServiceFactory.getXMPPService(); + SendResponse status = xmpp.sendMessage(msg); + messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS); + + log.info("Message sent? " + messageSent); + + if (!messageSent) { + // Send an email message instead... + } + } +} +// [END example] diff --git a/appengine/xmpp/src/main/java/com/example/appengine/xmpp/PresenceServlet.java b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/PresenceServlet.java new file mode 100644 index 00000000000..f85486fa288 --- /dev/null +++ b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/PresenceServlet.java @@ -0,0 +1,50 @@ +/** + * Copyright 2016 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.xmpp; + +import java.io.IOException; +import java.util.logging.Logger; +import javax.servlet.http.*; +import com.google.appengine.api.xmpp.Presence; +import com.google.appengine.api.xmpp.PresenceType; +import com.google.appengine.api.xmpp.XMPPService; +import com.google.appengine.api.xmpp.XMPPServiceFactory; + +// [START example] +@SuppressWarnings("serial") +public class PresenceServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(PresenceServlet.class.getName()); + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + XMPPService xmpp = XMPPServiceFactory.getXMPPService(); + Presence presence = xmpp.parsePresence(req); + + // Split the XMPP address (e.g., user@gmail.com) + // from the resource (e.g., gmail.CD6EBC4A) + String from = presence.getFromJid().getId().split("/")[0]; + + log.info("Received presence from: " + from); + + // Mirror the contact's presence back to them + xmpp.sendPresence(presence.getFromJid(), PresenceType.AVAILABLE, presence.getPresenceShow(), presence.getStatus()); + } +} +// [END example] diff --git a/appengine/xmpp/src/main/java/com/example/appengine/xmpp/SubscriptionServlet.java b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/SubscriptionServlet.java new file mode 100644 index 00000000000..9f7f2832350 --- /dev/null +++ b/appengine/xmpp/src/main/java/com/example/appengine/xmpp/SubscriptionServlet.java @@ -0,0 +1,46 @@ +/** + * Copyright 2016 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.xmpp; + +import java.io.IOException; +import java.util.logging.Logger; +import javax.servlet.http.*; +import com.google.appengine.api.xmpp.Subscription; +import com.google.appengine.api.xmpp.XMPPService; +import com.google.appengine.api.xmpp.XMPPServiceFactory; + +// [START example] +@SuppressWarnings("serial") +public class SubscriptionServlet extends HttpServlet { + + private static final Logger log = Logger.getLogger(SubscriptionServlet.class.getName()); + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse res) + throws IOException { + + XMPPService xmppService = XMPPServiceFactory.getXMPPService(); + Subscription sub = xmppService.parseSubscription(req); + + // Split the bare XMPP address (e.g., user@gmail.com) + // from the resource (e.g., gmail.CD6EBC4A) + String from = sub.getFromJid().getId().split("/")[0]; + + log.info("Received subscription event from: " + from); + } +} +// [END example] diff --git a/appengine/xmpp/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/xmpp/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..032d1f1e269 --- /dev/null +++ b/appengine/xmpp/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,28 @@ + + + + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + + + xmpp_message + xmpp_presence + xmpp_subscribe + xmpp_error + + + diff --git a/appengine/xmpp/src/main/webapp/WEB-INF/web.xml b/appengine/xmpp/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..45daddaf86e --- /dev/null +++ b/appengine/xmpp/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,65 @@ + + + + + + + error + com.example.appengine.xmpp.ErrorServlet + + + error + /_ah/xmpp/error/ + + + messagereceiver + com.example.appengine.xmpp.MessageReceiverServlet + + + messagereceiver + /_ah/xmpp/message/chat/ + + + messagesender + com.example.appengine.xmpp.MessageSenderServlet + + + messagesender + /messagesender + + + presence + com.example.appengine.xmpp.PresenceServlet + + + presence + /_ah/xmpp/presence/available/ + /_ah/xmpp/presence/unavailable/ + /_ah/xmpp/presence/probe/ + + + subscription + com.example.appengine.xmpp.SubscriptionServlet + + + subscription + /_ah/xmpp/subscription/subscribe/ + /_ah/xmpp/subscription/subscribed/ + /_ah/xmpp/subscription/unsubscribe/ + /_ah/xmpp/subscription/unsubscribed/ + +