use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class FileTransferNegotiator method negotiateOutgoingTransfer.
/**
* Send a request to another user to send them a file. The other user has
* the option of, accepting, rejecting, or not responding to a received file
* transfer request.
* <p>
* If they accept, the stanza will contain the other user's chosen stream
* type to send the file across. The two choices this implementation
* provides to the other user for file transfer are <a
* href="http://www.xmpp.org/extensions/jep-0065.html">SOCKS5 Bytestreams</a>,
* which is the preferred method of transfer, and <a
* href="http://www.xmpp.org/extensions/jep-0047.html">In-Band Bytestreams</a>,
* which is the fallback mechanism.
* </p>
* <p>
* The other user may choose to decline the file request if they do not
* desire the file, their client does not support XEP-0096, or if there are
* no acceptable means to transfer the file.
* </p>
* Finally, if the other user does not respond this method will return null
* after the specified timeout.
*
* @param userID The userID of the user to whom the file will be sent.
* @param streamID The unique identifier for this file transfer.
* @param fileName The name of this file. Preferably it should include an
* extension as it is used to determine what type of file it is.
* @param size The size, in bytes, of the file.
* @param desc A description of the file.
* @param responseTimeout The amount of time, in milliseconds, to wait for the remote
* user to respond. If they do not respond in time, this
* @return Returns the stream negotiator selected by the peer.
* @throws XMPPErrorException Thrown if there is an error negotiating the file transfer.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws NoResponseException if there was no response from the remote entity.
* @throws NoAcceptableTransferMechanisms if no acceptable transfer mechanisms are available
* @throws InterruptedException if the calling thread was interrupted.
*/
public StreamNegotiator negotiateOutgoingTransfer(final Jid userID, final String streamID, final String fileName, final long size, final String desc, int responseTimeout) throws XMPPErrorException, NotConnectedException, NoResponseException, NoAcceptableTransferMechanisms, InterruptedException {
StreamInitiation si = new StreamInitiation();
si.setSessionID(streamID);
si.setMimeType(URLConnection.guessContentTypeFromName(fileName));
StreamInitiation.File siFile = new StreamInitiation.File(fileName, size);
siFile.setDesc(desc);
si.setFile(siFile);
si.setFeatureNegotiationForm(createDefaultInitiationForm());
si.setFrom(connection().getUser());
si.setTo(userID);
si.setType(IQ.Type.set);
Stanza siResponse = connection().createStanzaCollectorAndSend(si).nextResultOrThrow(responseTimeout);
if (siResponse instanceof IQ) {
IQ iqResponse = (IQ) siResponse;
if (iqResponse.getType().equals(IQ.Type.result)) {
StreamInitiation response = (StreamInitiation) siResponse;
return getOutgoingNegotiator(getStreamMethodField(response.getFeatureNegotiationForm()));
} else {
throw new XMPPErrorException(iqResponse, iqResponse.getError());
}
} else {
return null;
}
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ServiceDiscoveryManager method discoverItems.
/**
* Returns the discovered items of a given XMPP entity addressed by its JID and
* note attribute. Use this message only when trying to query information which is not
* directly addressable.
*
* @param entityID the address of the XMPP entity.
* @param node the optional attribute that supplements the 'jid' attribute.
* @return the discovered items.
* @throws XMPPErrorException if the operation failed for some reason.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws InterruptedException if the calling thread was interrupted.
*/
public DiscoverItems discoverItems(Jid entityID, String node) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
// Discover the entity's items
DiscoverItems disco = new DiscoverItems();
disco.setType(IQ.Type.get);
disco.setTo(entityID);
disco.setNode(node);
Stanza result = connection().sendIqRequestAndWaitForResponse(disco);
return (DiscoverItems) result;
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class Socks5TransferNegotiator method createIncomingStream.
@Override
public InputStream createIncomingStream(StreamInitiation initiation) throws XMPPErrorException, InterruptedException, SmackException {
/*
* SOCKS5 initiation listener must ignore next SOCKS5 Bytestream request with given session
* ID
*/
this.manager.ignoreBytestreamRequestOnce(initiation.getSessionID());
Stanza streamInitiation = initiateIncomingStream(connection(), initiation);
return negotiateIncomingStream(streamInitiation);
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ForwardedProvider method parse.
@Override
public Forwarded<?> parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
DelayInformation di = null;
Stanza packet = null;
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
switch(name) {
case DelayInformation.ELEMENT:
if (DelayInformation.NAMESPACE.equals(namespace)) {
di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth(), null);
} else {
LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + DelayInformation.NAMESPACE + "'");
}
break;
case Message.ELEMENT:
packet = PacketParserUtils.parseMessage(parser);
break;
default:
LOGGER.warning("Unsupported forwarded packet type: " + name);
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
if (packet == null) {
// TODO: Should be SmackParseException.
throw new IOException("forwarded extension must contain a packet");
}
return new Forwarded<>(packet, di);
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class Socks5ByteStreamRequestTest method shouldAcceptSocks5BytestreamRequestAndReceiveData.
/**
* Accepting the SOCKS5 Bytestream request should be successfully.
*
* @throws Exception should not happen
*/
@Test
public void shouldAcceptSocks5BytestreamRequestAndReceiveData() throws Exception {
final Protocol protocol = new Protocol();
final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, targetJID);
// start a local SOCKS5 proxy
try (Socks5TestProxy socks5Proxy = new Socks5TestProxy()) {
// build SOCKS5 Bytestream initialization request
Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, socks5Proxy.getPort());
// create test data for stream
byte[] data = new byte[] { 1, 2, 3 };
// get SOCKS5 Bytestream manager for connection
Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
// build SOCKS5 Bytestream request with the bytestream initialization
Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
// accept the stream (this is the call that is tested here)
InputStream inputStream = byteStreamRequest.accept().getInputStream();
// create digest to get the socket opened by target
String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
// test stream by sending some data
OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
outputStream.write(data);
// verify that data is transferred correctly
byte[] result = new byte[3];
inputStream.read(result);
assertArrayEquals(data, result);
// verify targets response
assertEquals(1, protocol.getRequests().size());
Stanza targetResponse = protocol.getRequests().remove(0);
assertEquals(Bytestream.class, targetResponse.getClass());
assertEquals(initiatorJID, targetResponse.getTo());
assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
}
}
Aggregations