GAE XMPP (Java API) - Jabber in his application

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

image

The XMPP service allows GAE applications to send and receive gill messages.
XMPP is an XML-based, open-source instant messaging protocol, also known as Jabber. That it is already used in Google Talk.

APIs hide the entire mechanism of working with the protocol. The developer gets very high-level methods, which simplify the development, but at the same time greatly limit the possibilities.
If you look at the javadok using the XMPP API , you can see that only the most necessary minimum is implemented.

Send message

All work with the protocol is carried out 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 addresses are available for the application - JIDs : app-id@appspot.com (by default) and anything@app-id.appspotchat.com . If the application version is not defaulted, then it is used (forced!) - anything@version.latest.app-id.appspotchat.com . So far, the service does not allow its domain name used for the application in the JID.

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

To exchange messages, the application and the recipient must be authorized with each other - 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 if 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 person you are talking to online uses Google Talk and is authorized. It is not possible to define additional statuses.

There is no way to work with the list of contacts. And there is no way to log in as a regular jabber client under the application account for manually editing the contact list.

Accept message

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

xmpp_message


Now, when a message is received at 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 JIDs of the sender and receiver (to which address it was sent) 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 ):

xmppreceiverXMPPReceiverServletxmppreceiver/_ah/xmpp/message/chat/


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

Finally

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

To whom it is interesting, I offer a small example of the service: http://samples-gae.appspot.com/samples/xmpp.html

Section in the documentation (in English): XMPP Java API Overview Many

thanks garbuz for the invite!

Added 02/14/2010:

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

Also popular now: