Gateway interaction is described by XEP-0100. The query xmlns (namespace) used for this is same as IN-Band registration, that is “jabber:iq:register”. Smack API provides an easy way handle gateway interaction with the server. It has predefined classes for the required namespace and operations which one can easily use and do the thing. In this article I will explain how one can register, edit, unregister and retrieve gateway information using Smack library. I have listed both XML and code using Smack API for the purpose. I have used Openfire server with Kraken plugin in it for gateway related operations described in this article.

1. Gateway service discovery

First you have to send an IQ-get with xmlns of the service discovery info to the Gateway. The XML would look like this:

By this you can identify which namespace features are supported by the gateway. To register and/or unregister the gateway “jabber:iq:register” support is required. This response can differ at your side based on your server and Gateway support. Now the code using Smack API for this operation is as follows:

By this you will know that which fields are required for gateway registration. Here in this example 2 fields are require, one is username and another is password.
Code to do this in smack is:
1) Send GET request to gateway

4. Retrieve registered gateway information:

To retrieve registered gateway information one has to send an IQ GET packet with “jabber:iq:register” query to the gateway id (i.e. gtalk.myHost). This is similar what we did to get gateway information. In short when you can query gateway for information it will return the information with currently registered information, if any and if there there are no registered information then response is just about the required field names to register.

5. Edit registered information:

To edit registered information one has to first fetch information as shown in above section and then follow the same code for registering gateway. In simple words do point 3 & 4 form this article in reverse order.

6. Unregister gateway

To unregister a gateway first query for the registered gateway to see if any gateway information is registered or not. If it is registered then to unregister you just have to add a “remove” attribute in the IQ set as follows:

This is very similar to the register gateway xml so difference in code is very small. Instead of adding username and password as attributes in Map you just add a “remove” attribute and everything else is same.