📜 ⬆️ ⬇️

GAE XMPP (Java API) - Zhabber in your application

While Google has this section only in English, I share my acquaintance with this service.

image

XMPP allows GAE applications to send and receive gabber messages.
XMPP is an open, instant messaging protocol based on XML, also known as Jabber. That it is already used in Google Talk.
')
APIs hide the whole mechanism of working with the protocol. The developer receives very high-level methods that simplify development, but at the same time, they limit the possibilities very much.
If you look at javadk on the XMPP API , then you can see that only the most necessary minimum is implemented.

We send the message

All work with the protocol is done through an instance of the com.google.appengine.api.xmpp.XMPPService object, which contains all the necessary methods. Example of sending a message:

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; // ... JID jid = new JID("example@gmail.com"); String msgBody = "Hello World from GAE"; Message msg = new MessageBuilder() .withRecipientJids(jid) .withBody(msgBody) .build(); boolean messageSent = false; XMPPService xmpp = XMPPServiceFactory.getXMPPService(); if (xmpp.getPresence(jid).isAvailable()) { SendResponse status = xmpp.sendMessage(msg); messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS); } if (!messageSent) { // Send an email message instead... } 


The application has the following addresses: JIDs : app-id@appspot.com (by default) and anything@app-id.appspotchat.com . If the application version is not default, then it is used (forcibly!) - anything@version.latest.app-id.appspotchat.com . While the service does not allow to use in JID the domain name used for the application.

The destination can be any valid JID (not necessarily a Google Talk user).

To exchange messages, the application and the recipient must be logged in with each other - to exchange invites, as in any messenger. You can send an invitation to the application, then the service will automatically accept it and send a response invitation. Or, send an invitation from the application:

 xmpp.sendInvitation(jid); 


Unfortunately, there is no way to check whether the JID is authorized. You can only find out the status, and then with great restrictions:

 xmpp.getPresence(fromJid).isAvailable(); 


The result will be positive only if the interlocutor is on-line, uses Google Talk and is authorized. Additional statuses cannot be determined.

There is no possibility to work with the list of contacts. And there is no possibility to log in with the usual jabber client under the application account, for manually editing the list of contacts.

Accept message

To receive messages, you must "enable" the service by adding the following lines to the application's configuration file appengine-web.xml :

 <inbound-services> <service>xmpp_message</service> </inbound-services> 


Now, when receiving a message to one of the addresses that belong to the application, GAE performs a POST request to the URL / _ah / xmpp / message / chat / . The request contains the message itself, the sender and recipient JIDs (which address was sent to) and stanza - the full format of the XMPP message in XML.

GAE provides access to the URL / _ah / xmpp / message / chat / only to users with the administrator role, so it is not available to the outside world.

It remains to write the servlet itself:

 import java.io.IOException; 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; @SuppressWarnings("serial") public class XMPPReceiverServlet extends HttpServlet { 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(); // ... } } 


As you can see, the API already has a ready-made parser for processing such requests.

It remains only to map the servlet to this URL ( web.xml ):

 <web-app><servlet> <servlet-name>xmppreceiver</servlet-name> <servlet-class>XMPPReceiverServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xmppreceiver</servlet-name> <url-pattern>/_ah/xmpp/message/chat/</url-pattern> </servlet-mapping><web-app> 


XMPP service has its quotas for the number of sent messages, the volume of sent messages, as well as the number of API calls to the service itself. Incoming messages are also charged as an incoming HTTP request. Quotas

Finally

In fact, the XMPP service can be used to send various admin notifications, especially conveniently to mobile devices. There is no protection against malicious consumption of quotas, if desired, the attacker can exhaust all quotas.

Who cares, I offer a small example of the service: http://samples-gae.appspot.com/samples/xmpp.html

Documentation section (in English): XMPP Java API Overview

Thank you so much garbuz for an invite!

Posted on 02-14-2010:

A new release of GAE SDK 1.4.2 has been released, with the second version of XMPP, a lot of new.

Source: https://habr.com/ru/post/107972/


All Articles