1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-23 21:17:58 +01:00
Smack/documentation/extensions/hoxt.md

115 lines
3.5 KiB
Markdown
Raw Normal View History

2014-08-16 00:09:55 +02:00
HTTP over XMPP transport
========================
[Back](index.md)
2018-04-03 14:51:18 +02:00
Allows the transport of HTTP communication over XMPP peer-to-peer networks.
2014-08-16 00:09:55 +02:00
* Discover HOXT support
* IQ exchange
Discover HOXT support
---------------------
**Description**
Before using this extension you must ensure that your counterpart supports it
also.
**Usage**
Once you have your _**ServiceDiscoveryManager**_ you will be able to discover
information associated with an XMPP entity. To discover the information of a
given XMPP entity send **discoverInfo(entityID)** to your
_**ServiceDiscoveryManager**_ where entityID is the ID of the entity. The
2018-04-03 14:51:18 +02:00
message **discoverInfo(entityID)** will answer with an instance of
2014-08-16 00:09:55 +02:00
_**DiscoverInfo**_ that contains the discovered information.
**Examples**
In this example we can see how to check if the counterpart supports HOXT:
```
// Obtain the ServiceDiscoveryManager associated with my XMPPConnection
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection);
2018-04-03 14:51:18 +02:00
// Get the information of a given XMPP entity, where entityID is a Jid
DiscoverInfo discoInfo = discoManager.discoverInfo(entityID);
2014-08-16 00:09:55 +02:00
// Check if room is HOXT is supported
2018-04-03 14:51:18 +02:00
boolean isSupported = discoInfo.containsFeature("urn:xmpp:http");
2014-08-16 00:09:55 +02:00
```
IQ exchange
-----------
**Description**
You can use IQ's to perform HTTP requests and responses. This is applicable to
2018-04-03 14:51:18 +02:00
relatively short requests and responses (due to the limitation of XMPP message
2014-08-16 00:09:55 +02:00
size).
**Usage**
2018-04-03 14:51:18 +02:00
First you need to register a _**StanzaListener**_ to be able to handle
2014-08-16 00:09:55 +02:00
intended IQs.
For the HTTP client you:
* You create and send _**HttpOverXmppReq**_ request.
2018-04-03 14:51:18 +02:00
* Then you handle the _**HttpOverXmppResp**_ response in your _**StanzaListener**_.
2014-08-16 00:09:55 +02:00
For the HTTP server you:
2018-04-03 14:51:18 +02:00
* You handle the _**HttpOverXmppReq**_ requests in your _**StanzaListener**_.
2014-08-16 00:09:55 +02:00
* And create and send _**HttpOverXmppResp**_ responses.
**Examples**
2018-04-03 14:51:18 +02:00
In this example we are an HTTP client, so we send a request (POST) and handle the
2014-08-16 00:09:55 +02:00
response:
```
// create a request body
String urlEncodedMessage = "I_love_you";
// prepare headers
2018-04-03 14:51:18 +02:00
List<Header> headers = new ArrayList<>();
headers.add(new Header("Host", "juliet.capulet.com"));
headers.add(new Header("Content-Type", "application/x-www-form-urlencoded"));
headers.add(new Header("Content-Length", Integer.toString(urlEncodedMessage.length())));
2014-08-16 00:09:55 +02:00
// provide body or request (not mandatory, - empty body is used for GET)
AbstractHttpOverXmpp.Text child = new AbstractHttpOverXmpp.Text(urlEncodedMessage);
AbstractHttpOverXmpp.Data data = new AbstractHttpOverXmpp.Data(child);
2018-04-03 14:51:18 +02:00
// create request
HttpOverXmppReq req = HttpOverXmppReq.buider()
.setMethod(HttpMethod.POST)
.setResource("/mailbox")
.setHeaders(headers)
.setVersion("1.1")
.setData(data)
.build();
// add to, where jid is the Jid of the individual the packet is sent to
req.setTo(jid);
2014-08-16 00:09:55 +02:00
// send it
2018-04-03 14:51:18 +02:00
connection.sendIqWithResponseCallback(req, new StanzaListener() {
public void processStanza(Stanza iq) {
HttpOverXmppResp resp = (HttpOverXmppResp) iq;
// check HTTP response code
if (resp.getStatusCode() == 200) {
// get content of the response
NamedElement child = resp.getData().getChild();
// check which type of content of the response arrived
if (child instanceof AbstractHttpOverXmpp.Xml) {
2018-04-03 14:51:18 +02:00
// print the message and anxiously read if from the console ;)
System.out.println(((AbstractHttpOverXmpp.Xml) child).getText());
} else {
2018-04-03 14:51:18 +02:00
// process other AbstractHttpOverXmpp data child subtypes
2014-08-16 00:09:55 +02:00
}
}
}
});
2014-08-16 00:09:55 +02:00
```