1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Merge pull request #226 from fuentesj11/update-hoxt-doc

Update hoxt.md
This commit is contained in:
Florian Schmaus 2018-04-04 14:48:53 +02:00 committed by GitHub
commit bcf6d3ecef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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