-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added samples showcasing XMPP (#182)
* Added samples showcasing XMPP Added a number of Servlet based examples for XMPP * Minor fixes to my previous commit
- Loading branch information
1 parent
cf6a5c2
commit 89e9e51
Showing
9 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your project name. | ||
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml` | ||
with your version name. | ||
|
||
## Running locally | ||
$ mvn appengine:devserver | ||
|
||
## Deploying | ||
$ mvn appengine:update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<!-- | ||
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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-xmpp</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- [START dependencies] --> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
</dependency> | ||
<!-- [END dependencies] --> | ||
|
||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<version>3.3</version> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
45 changes: 45 additions & 0 deletions
45
appengine/xmpp/src/main/java/com/example/appengine/xmpp/ErrorServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
47 changes: 47 additions & 0 deletions
47
appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageReceiverServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
58 changes: 58 additions & 0 deletions
58
appengine/xmpp/src/main/java/com/example/appengine/xmpp/MessageSenderServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("[email protected]"); | ||
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] |
50 changes: 50 additions & 0 deletions
50
appengine/xmpp/src/main/java/com/example/appengine/xmpp/PresenceServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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., [email protected]) | ||
// 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] |
46 changes: 46 additions & 0 deletions
46
appengine/xmpp/src/main/java/com/example/appengine/xmpp/SubscriptionServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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., [email protected]) | ||
// from the resource (e.g., gmail.CD6EBC4A) | ||
String from = sub.getFromJid().getId().split("/")[0]; | ||
|
||
log.info("Received subscription event from: " + from); | ||
} | ||
} | ||
// [END example] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- [START_EXCLUDE] --> | ||
<!-- | ||
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. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-ID</version> | ||
<threadsafe>true</threadsafe> | ||
|
||
<inbound-services> | ||
<service>xmpp_message</service> | ||
<service>xmpp_presence</service> | ||
<service>xmpp_subscribe</service> | ||
<service>xmpp_error</service> | ||
</inbound-services> | ||
|
||
</appengine-web-app> |
Oops, something went wrong.