Sending messages through REST

You can also send messages, or really any XMPP stanza to users and components through REST API. Sending XMPP messages or stanzas using HTTP is realized as a groovy script bundled in the installation package from v7.0.2. If you want to be sure your current install supports this feature, check for the presence of Stream.groovy file in the scripts/rest/stream/ directory.

As in other examples, be sure that you have the following line in your init.properties:

http/rest/api-keys[s]=test_key

You may also opt to have open_access set to disable API key parameter.

Usage

Using the HTTP POST method, XMPP stanzas can be sent using the built in HTTP API. In a local installation, the request can be sent to http://localhost:8080/rest/stream/?api-key=API_KEY with a seralized XMPP stanza as content, where API_KEY is the API key for HTTP API which is set in etc/init.properties as rest/api-keys[s]. In the case we laid out, it would be test_key. Because XMPP uses XML for formatting, all content in these requests MUST be encoded in UTF-8 and Content-type must be set to application/xml. Lets take a look at some examples.

In all examples the data is sent as an HTTP POST request to /rest/stream/?api-key=test-key.

Send XMPP stanza with from set to HTTP API component to bare JID
<message xmlns="jabber:client" type="chat" to="test@example.com/resource-1">
    <body>Example message 1</body>
</message>

Once this message is sent, the Groovy script adds the remaining information automatically, and the following is what is receieved by test@example.com/resource-1.

<message xmlns="jabber:client" type="chat" from:"http@example.com" to="test@example.com/resource-1">
    <body>Example message 1</body>
</message>

As you can see, the HTTP component is automatically populated as the sender.

Send XMPP stanza with from set to HTTP API component to full JID
<message xmlns="jabber:client" type="chat" to="test@example.com">
    <body>Example message 1</body>
</message>

The syntax and formatting is the same, with the recepiant messaging being exactly the same.

<message xmlns="jabber:client" type="chat" from:"http@example.com" to="test@example.com/resource-1">
    <body>Example message 1</body>
</message>
Send XMPP stanza with from set to specified JID

You may specify any JID that is registered in the server to send the stanza, ones that use a name that is not registered will return an error.

<message xmlns="jabber:client" type="chat" from="sender@example.com" to="test@example.com/resource-1">
    <body>Example message 1</body>
</message>

Ends with the result being somewhat customized.

<message xmlns="jabber:client" type="chat" from:"sender@example.com" to="test@example.com/resource-1">
    <body>Example message 1</body>
</message>
Avatar retrieval requests

There are different formats for avatar retrieval depending on how they are stored, see below for the resources for each type of avatar.

  • /rest/avatar/user@domain - which returns first avatar found (PEP, VCard4 or VCardTemp in this order)
  • /rest/avatar/user@domain/avatar - which returns PEP avatar
  • /rest/avatar/user@domain/vcard4 - which returns avatar from VCard4
  • /rest/avatar/user@domain/vcard-temp - which returns avatar from VCardTemp