Skip to content

Commit 89e9e51

Browse files
meteatamellesv
authored andcommitted
Added samples showcasing XMPP (#182)
* Added samples showcasing XMPP Added a number of Servlet based examples for XMPP * Minor fixes to my previous commit
1 parent cf6a5c2 commit 89e9e51

File tree

9 files changed

+431
-0
lines changed

9 files changed

+431
-0
lines changed

appengine/xmpp/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google App Engine Standard Environment XMPP Java API Overview
2+
3+
This sample demonstrates how to use XMPP Java API on Google App Engine.
4+
5+
See the [Google App Engine standard environment documentation][ae-docs] for more
6+
detailed instructions.
7+
8+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
9+
10+
## Setup
11+
1. Update the `<application>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
12+
with your project name.
13+
1. Update the `<version>` tag in `src/main/webapp/WEB-INF/appengine-web.xml`
14+
with your version name.
15+
16+
## Running locally
17+
$ mvn appengine:devserver
18+
19+
## Deploying
20+
$ mvn appengine:update

appengine/xmpp/pom.xml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!--
2+
Copyright 2015 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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.appengine</groupId>
21+
<artifactId>appengine-xmpp</artifactId>
22+
<parent>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>doc-samples</artifactId>
25+
<version>1.0.0</version>
26+
<relativePath>../..</relativePath>
27+
</parent>
28+
<dependencies>
29+
<dependency>
30+
<groupId>javax.servlet</groupId>
31+
<artifactId>servlet-api</artifactId>
32+
<type>jar</type>
33+
<scope>provided</scope>
34+
</dependency>
35+
36+
<!-- [START dependencies] -->
37+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
38+
<dependency>
39+
<groupId>com.google.appengine</groupId>
40+
<artifactId>appengine-api-1.0-sdk</artifactId>
41+
</dependency>
42+
<!-- [END dependencies] -->
43+
44+
<dependency>
45+
<groupId>com.google.guava</groupId>
46+
<artifactId>guava</artifactId>
47+
<version>19.0</version>
48+
</dependency>
49+
50+
</dependencies>
51+
<build>
52+
<!-- for hot reload of the web application -->
53+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<version>3.3</version>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<source>1.7</source>
61+
<target>1.7</target>
62+
</configuration>
63+
</plugin>
64+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
65+
<plugin>
66+
<groupId>com.google.appengine</groupId>
67+
<artifactId>appengine-maven-plugin</artifactId>
68+
<version>${appengine.sdk.version}</version>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.xmpp;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.util.logging.Logger;
22+
import javax.servlet.ServletInputStream;
23+
import javax.servlet.http.*;
24+
import com.google.common.io.ByteStreams;
25+
26+
// [START example]
27+
@SuppressWarnings("serial")
28+
public class ErrorServlet extends HttpServlet {
29+
30+
private static final Logger log = Logger.getLogger(ErrorServlet.class.getName());
31+
32+
@Override
33+
public void doPost(HttpServletRequest req, HttpServletResponse res)
34+
throws IOException {
35+
36+
// Parse the POST data, which is sent as a MIME stream containing the stanza.
37+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
38+
ServletInputStream inputStream = req.getInputStream();
39+
ByteStreams.copy(inputStream, baos);
40+
41+
// Log the error
42+
log.warning("Error stanza received: " + baos.toString());
43+
}
44+
}
45+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.xmpp;
18+
19+
import java.io.IOException;
20+
import java.util.logging.Logger;
21+
import javax.servlet.http.*;
22+
import com.google.appengine.api.xmpp.JID;
23+
import com.google.appengine.api.xmpp.Message;
24+
import com.google.appengine.api.xmpp.XMPPService;
25+
import com.google.appengine.api.xmpp.XMPPServiceFactory;
26+
27+
// [START example]
28+
@SuppressWarnings("serial")
29+
public class MessageReceiverServlet extends HttpServlet {
30+
31+
private static final Logger log = Logger.getLogger(MessageReceiverServlet.class.getName());
32+
33+
@Override
34+
public void doPost(HttpServletRequest req, HttpServletResponse res)
35+
throws IOException {
36+
37+
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
38+
Message message = xmpp.parseMessage(req);
39+
40+
JID fromJid = message.getFromJid();
41+
String body = message.getBody();
42+
43+
log.info("Received a message with id: " + fromJid + " and body: " + body);
44+
// ...
45+
}
46+
}
47+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.xmpp;
18+
19+
import java.io.IOException;
20+
import java.util.logging.Logger;
21+
import javax.servlet.http.*;
22+
import com.google.appengine.api.xmpp.JID;
23+
import com.google.appengine.api.xmpp.Message;
24+
import com.google.appengine.api.xmpp.MessageBuilder;
25+
import com.google.appengine.api.xmpp.SendResponse;
26+
import com.google.appengine.api.xmpp.XMPPService;
27+
import com.google.appengine.api.xmpp.XMPPServiceFactory;
28+
29+
// [START example]
30+
@SuppressWarnings("serial")
31+
public class MessageSenderServlet extends HttpServlet {
32+
33+
private static final Logger log = Logger.getLogger(MessageSenderServlet.class.getName());
34+
35+
@Override
36+
public void doGet(HttpServletRequest req, HttpServletResponse res)
37+
throws IOException {
38+
39+
JID jid = new JID("example@gmail.com");
40+
String msgBody = "Someone has sent you a gift on Example.com. To view: http://example.com/gifts/";
41+
Message msg = new MessageBuilder()
42+
.withRecipientJids(jid)
43+
.withBody(msgBody)
44+
.build();
45+
46+
boolean messageSent = false;
47+
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
48+
SendResponse status = xmpp.sendMessage(msg);
49+
messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
50+
51+
log.info("Message sent? " + messageSent);
52+
53+
if (!messageSent) {
54+
// Send an email message instead...
55+
}
56+
}
57+
}
58+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.xmpp;
18+
19+
import java.io.IOException;
20+
import java.util.logging.Logger;
21+
import javax.servlet.http.*;
22+
import com.google.appengine.api.xmpp.Presence;
23+
import com.google.appengine.api.xmpp.PresenceType;
24+
import com.google.appengine.api.xmpp.XMPPService;
25+
import com.google.appengine.api.xmpp.XMPPServiceFactory;
26+
27+
// [START example]
28+
@SuppressWarnings("serial")
29+
public class PresenceServlet extends HttpServlet {
30+
31+
private static final Logger log = Logger.getLogger(PresenceServlet.class.getName());
32+
33+
@Override
34+
public void doPost(HttpServletRequest req, HttpServletResponse res)
35+
throws IOException {
36+
37+
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
38+
Presence presence = xmpp.parsePresence(req);
39+
40+
// Split the XMPP address (e.g., user@gmail.com)
41+
// from the resource (e.g., gmail.CD6EBC4A)
42+
String from = presence.getFromJid().getId().split("/")[0];
43+
44+
log.info("Received presence from: " + from);
45+
46+
// Mirror the contact's presence back to them
47+
xmpp.sendPresence(presence.getFromJid(), PresenceType.AVAILABLE, presence.getPresenceShow(), presence.getStatus());
48+
}
49+
}
50+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.xmpp;
18+
19+
import java.io.IOException;
20+
import java.util.logging.Logger;
21+
import javax.servlet.http.*;
22+
import com.google.appengine.api.xmpp.Subscription;
23+
import com.google.appengine.api.xmpp.XMPPService;
24+
import com.google.appengine.api.xmpp.XMPPServiceFactory;
25+
26+
// [START example]
27+
@SuppressWarnings("serial")
28+
public class SubscriptionServlet extends HttpServlet {
29+
30+
private static final Logger log = Logger.getLogger(SubscriptionServlet.class.getName());
31+
32+
@Override
33+
public void doPost(HttpServletRequest req, HttpServletResponse res)
34+
throws IOException {
35+
36+
XMPPService xmppService = XMPPServiceFactory.getXMPPService();
37+
Subscription sub = xmppService.parseSubscription(req);
38+
39+
// Split the bare XMPP address (e.g., user@gmail.com)
40+
// from the resource (e.g., gmail.CD6EBC4A)
41+
String from = sub.getFromJid().getId().split("/")[0];
42+
43+
log.info("Received subscription event from: " + from);
44+
}
45+
}
46+
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-ID</version>
19+
<threadsafe>true</threadsafe>
20+
21+
<inbound-services>
22+
<service>xmpp_message</service>
23+
<service>xmpp_presence</service>
24+
<service>xmpp_subscribe</service>
25+
<service>xmpp_error</service>
26+
</inbound-services>
27+
28+
</appengine-web-app>

0 commit comments

Comments
 (0)