2006-02-03 20:13:23 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision: $
|
|
|
|
* $Date: $
|
|
|
|
*
|
|
|
|
* Copyright 2003-2006 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.filetransfer;
|
|
|
|
|
2010-08-15 13:57:11 +02:00
|
|
|
import java.net.URLConnection;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.Connection;
|
2006-02-03 19:44:22 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
|
|
|
import org.jivesoftware.smack.PacketCollector;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smack.filter.PacketIDFilter;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
import org.jivesoftware.smackx.Form;
|
|
|
|
import org.jivesoftware.smackx.FormField;
|
|
|
|
import org.jivesoftware.smackx.ServiceDiscoveryManager;
|
2010-08-15 13:57:11 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
|
2006-02-03 19:44:22 +01:00
|
|
|
import org.jivesoftware.smackx.packet.DataForm;
|
|
|
|
import org.jivesoftware.smackx.packet.StreamInitiation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages the negotiation of file transfers according to JEP-0096. If a file is
|
|
|
|
* being sent the remote user chooses the type of stream under which the file
|
|
|
|
* will be sent.
|
|
|
|
*
|
|
|
|
* @author Alexander Wenckus
|
2010-08-15 13:57:11 +02:00
|
|
|
* @see <a href="http://www.jabber.org/jeps/jep-0096.html">JEP-0096: File Transfer</a>
|
2006-02-03 19:44:22 +01:00
|
|
|
*/
|
|
|
|
public class FileTransferNegotiator {
|
|
|
|
|
|
|
|
// Static
|
|
|
|
|
|
|
|
private static final String[] NAMESPACE = {
|
|
|
|
"http://jabber.org/protocol/si/profile/file-transfer",
|
2010-08-15 13:57:11 +02:00
|
|
|
"http://jabber.org/protocol/si"};
|
2006-02-03 19:44:22 +01:00
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private static final Map<Connection, FileTransferNegotiator> transferObject =
|
|
|
|
new ConcurrentHashMap<Connection, FileTransferNegotiator>();
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
private static final String STREAM_INIT_PREFIX = "jsi_";
|
|
|
|
|
|
|
|
protected static final String STREAM_DATA_FIELD_NAME = "stream-method";
|
|
|
|
|
|
|
|
private static final Random randomGenerator = new Random();
|
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
/**
|
|
|
|
* A static variable to use only offer IBB for file transfer. It is generally recommend to only
|
|
|
|
* set this variable to true for testing purposes as IBB is the backup file transfer method
|
|
|
|
* and shouldn't be used as the only transfer method in production systems.
|
|
|
|
*/
|
2006-02-08 01:31:17 +01:00
|
|
|
public static boolean IBB_ONLY = false;
|
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
/**
|
|
|
|
* Returns the file transfer negotiator related to a particular connection.
|
|
|
|
* When this class is requested on a particular connection the file transfer
|
|
|
|
* service is automatically enabled.
|
|
|
|
*
|
|
|
|
* @param connection The connection for which the transfer manager is desired
|
|
|
|
* @return The IMFileTransferManager
|
|
|
|
*/
|
|
|
|
public static FileTransferNegotiator getInstanceFor(
|
2010-02-09 12:55:56 +01:00
|
|
|
final Connection connection) {
|
2006-02-08 01:31:17 +01:00
|
|
|
if (connection == null) {
|
|
|
|
throw new IllegalArgumentException("Connection cannot be null");
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
if (!connection.isConnected()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transferObject.containsKey(connection)) {
|
2006-09-14 21:16:40 +02:00
|
|
|
return transferObject.get(connection);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
FileTransferNegotiator transfer = new FileTransferNegotiator(
|
|
|
|
connection);
|
|
|
|
setServiceEnabled(connection, true);
|
|
|
|
transferObject.put(connection, transfer);
|
|
|
|
return transfer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable the Jabber services related to file transfer on the particular
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* @param connection The connection on which to enable or disable the services.
|
|
|
|
* @param isEnabled True to enable, false to disable.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public static void setServiceEnabled(final Connection connection,
|
2006-02-03 19:44:22 +01:00
|
|
|
final boolean isEnabled) {
|
|
|
|
ServiceDiscoveryManager manager = ServiceDiscoveryManager
|
|
|
|
.getInstanceFor(connection);
|
2010-08-15 13:57:11 +02:00
|
|
|
|
|
|
|
List<String> namespaces = new ArrayList<String>();
|
|
|
|
namespaces.addAll(Arrays.asList(NAMESPACE));
|
|
|
|
namespaces.add(InBandBytestreamManager.NAMESPACE);
|
|
|
|
if (!IBB_ONLY) {
|
|
|
|
namespaces.add(Socks5BytestreamManager.NAMESPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String namespace : namespaces) {
|
2006-02-03 19:44:22 +01:00
|
|
|
if (isEnabled) {
|
2010-08-15 13:57:11 +02:00
|
|
|
if (!manager.includesFeature(namespace)) {
|
|
|
|
manager.addFeature(namespace);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
manager.removeFeature(namespace);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-15 13:57:11 +02:00
|
|
|
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see if all file transfer related services are enabled on the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* @param connection The connection to check
|
|
|
|
* @return True if all related services are enabled, false if they are not.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public static boolean isServiceEnabled(final Connection connection) {
|
2010-08-15 13:57:11 +02:00
|
|
|
ServiceDiscoveryManager manager = ServiceDiscoveryManager
|
|
|
|
.getInstanceFor(connection);
|
|
|
|
|
|
|
|
List<String> namespaces = new ArrayList<String>();
|
|
|
|
namespaces.addAll(Arrays.asList(NAMESPACE));
|
|
|
|
namespaces.add(InBandBytestreamManager.NAMESPACE);
|
|
|
|
if (!IBB_ONLY) {
|
|
|
|
namespaces.add(Socks5BytestreamManager.NAMESPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String namespace : namespaces) {
|
|
|
|
if (!manager.includesFeature(namespace)) {
|
2006-02-03 19:44:22 +01:00
|
|
|
return false;
|
2010-08-15 13:57:11 +02:00
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 13:57:11 +02:00
|
|
|
* A convenience method to create an IQ packet.
|
2006-02-03 19:44:22 +01:00
|
|
|
*
|
|
|
|
* @param ID The packet ID of the
|
|
|
|
* @param to To whom the packet is addressed.
|
|
|
|
* @param from From whom the packet is sent.
|
2010-08-15 13:57:11 +02:00
|
|
|
* @param type The IQ type of the packet.
|
2006-02-03 19:44:22 +01:00
|
|
|
* @return The created IQ packet.
|
|
|
|
*/
|
2006-03-20 18:59:01 +01:00
|
|
|
public static IQ createIQ(final String ID, final String to,
|
2006-02-03 19:44:22 +01:00
|
|
|
final String from, final IQ.Type type) {
|
|
|
|
IQ iqPacket = new IQ() {
|
|
|
|
public String getChildElementXML() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
iqPacket.setPacketID(ID);
|
|
|
|
iqPacket.setTo(to);
|
|
|
|
iqPacket.setFrom(from);
|
|
|
|
iqPacket.setType(type);
|
|
|
|
|
|
|
|
return iqPacket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a collection of the supported transfer protocols.
|
|
|
|
*
|
|
|
|
* @return Returns a collection of the supported transfer protocols.
|
|
|
|
*/
|
2009-06-04 13:58:25 +02:00
|
|
|
public static Collection<String> getSupportedProtocols() {
|
2010-08-15 13:57:11 +02:00
|
|
|
List<String> protocols = new ArrayList<String>();
|
|
|
|
protocols.add(InBandBytestreamManager.NAMESPACE);
|
|
|
|
if (!IBB_ONLY) {
|
|
|
|
protocols.add(Socks5BytestreamManager.NAMESPACE);
|
|
|
|
}
|
|
|
|
return Collections.unmodifiableList(protocols);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// non-static
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private final Connection connection;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
2010-08-15 13:57:11 +02:00
|
|
|
private final StreamNegotiator byteStreamTransferManager;
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
private final StreamNegotiator inbandTransferManager;
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private FileTransferNegotiator(final Connection connection) {
|
2006-02-03 19:44:22 +01:00
|
|
|
configureConnection(connection);
|
|
|
|
|
|
|
|
this.connection = connection;
|
2010-08-15 13:57:11 +02:00
|
|
|
byteStreamTransferManager = new Socks5TransferNegotiator(connection);
|
2006-02-03 19:44:22 +01:00
|
|
|
inbandTransferManager = new IBBTransferNegotiator(connection);
|
|
|
|
}
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private void configureConnection(final Connection connection) {
|
2006-02-03 19:44:22 +01:00
|
|
|
connection.addConnectionListener(new ConnectionListener() {
|
|
|
|
public void connectionClosed() {
|
|
|
|
cleanup(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
|
|
|
cleanup(connection);
|
|
|
|
}
|
2006-09-14 21:16:40 +02:00
|
|
|
|
|
|
|
public void reconnectionFailed(Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2006-11-15 23:29:25 +01:00
|
|
|
public void reconnectionSuccessful() {
|
2006-09-14 21:16:40 +02:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
// ignore
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private void cleanup(final Connection connection) {
|
2006-09-14 21:16:40 +02:00
|
|
|
if (transferObject.remove(connection) != null) {
|
|
|
|
inbandTransferManager.cleanup();
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects an appropriate stream negotiator after examining the incoming file transfer request.
|
|
|
|
*
|
|
|
|
* @param request The related file transfer request.
|
|
|
|
* @return The file transfer object that handles the transfer
|
|
|
|
* @throws XMPPException If there are either no stream methods contained in the packet, or
|
|
|
|
* there is not an appropriate stream method.
|
|
|
|
*/
|
|
|
|
public StreamNegotiator selectStreamNegotiator(
|
|
|
|
FileTransferRequest request) throws XMPPException {
|
|
|
|
StreamInitiation si = request.getStreamInitiation();
|
|
|
|
FormField streamMethodField = getStreamMethodField(si
|
|
|
|
.getFeatureNegotiationForm());
|
|
|
|
|
|
|
|
if (streamMethodField == null) {
|
2006-07-19 21:24:00 +02:00
|
|
|
String errorMessage = "No stream methods contained in packet.";
|
|
|
|
XMPPError error = new XMPPError(XMPPError.Condition.bad_request, errorMessage);
|
2006-02-03 19:44:22 +01:00
|
|
|
IQ iqPacket = createIQ(si.getPacketID(), si.getFrom(), si.getTo(),
|
|
|
|
IQ.Type.ERROR);
|
|
|
|
iqPacket.setError(error);
|
|
|
|
connection.sendPacket(iqPacket);
|
2006-07-19 21:24:00 +02:00
|
|
|
throw new XMPPException(errorMessage, error);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// select the appropriate protocol
|
|
|
|
|
|
|
|
StreamNegotiator selectedStreamNegotiator;
|
|
|
|
try {
|
2006-02-08 01:31:17 +01:00
|
|
|
selectedStreamNegotiator = getNegotiator(streamMethodField);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
|
|
|
IQ iqPacket = createIQ(si.getPacketID(), si.getFrom(), si.getTo(),
|
|
|
|
IQ.Type.ERROR);
|
|
|
|
iqPacket.setError(e.getXMPPError());
|
|
|
|
connection.sendPacket(iqPacket);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the appropriate negotiator
|
|
|
|
|
|
|
|
return selectedStreamNegotiator;
|
|
|
|
}
|
|
|
|
|
|
|
|
private FormField getStreamMethodField(DataForm form) {
|
|
|
|
FormField field = null;
|
2009-06-04 13:58:25 +02:00
|
|
|
for (Iterator<FormField> it = form.getFields(); it.hasNext();) {
|
|
|
|
field = it.next();
|
2006-02-03 19:44:22 +01:00
|
|
|
if (field.getVariable().equals(STREAM_DATA_FIELD_NAME)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
field = null;
|
|
|
|
}
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
private StreamNegotiator getNegotiator(final FormField field)
|
2006-02-03 19:44:22 +01:00
|
|
|
throws XMPPException {
|
2006-02-08 01:31:17 +01:00
|
|
|
String variable;
|
2006-02-03 19:44:22 +01:00
|
|
|
boolean isByteStream = false;
|
|
|
|
boolean isIBB = false;
|
2009-06-04 13:58:25 +02:00
|
|
|
for (Iterator<FormField.Option> it = field.getOptions(); it.hasNext();) {
|
|
|
|
variable = it.next().getValue();
|
2010-08-15 13:57:11 +02:00
|
|
|
if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) {
|
2006-02-03 19:44:22 +01:00
|
|
|
isByteStream = true;
|
|
|
|
}
|
2010-08-15 13:57:11 +02:00
|
|
|
else if (variable.equals(InBandBytestreamManager.NAMESPACE)) {
|
2006-02-03 19:44:22 +01:00
|
|
|
isIBB = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isByteStream && !isIBB) {
|
2006-07-19 21:24:00 +02:00
|
|
|
XMPPError error = new XMPPError(XMPPError.Condition.bad_request,
|
|
|
|
"No acceptable transfer mechanism");
|
|
|
|
throw new XMPPException(error.getMessage(), error);
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 16:11:35 +02:00
|
|
|
//if (isByteStream && isIBB && field.getType().equals(FormField.TYPE_LIST_MULTI)) {
|
|
|
|
if (isByteStream && isIBB) {
|
2007-03-21 05:09:52 +01:00
|
|
|
return new FaultTolerantNegotiator(connection,
|
2010-08-15 13:57:11 +02:00
|
|
|
byteStreamTransferManager,
|
2007-03-21 05:09:52 +01:00
|
|
|
inbandTransferManager);
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
|
|
|
else if (isByteStream) {
|
2010-08-15 13:57:11 +02:00
|
|
|
return byteStreamTransferManager;
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return inbandTransferManager;
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reject a stream initiation request from a remote user.
|
|
|
|
*
|
|
|
|
* @param si The Stream Initiation request to reject.
|
|
|
|
*/
|
|
|
|
public void rejectStream(final StreamInitiation si) {
|
2006-07-19 21:24:00 +02:00
|
|
|
XMPPError error = new XMPPError(XMPPError.Condition.forbidden, "Offer Declined");
|
2006-02-03 19:44:22 +01:00
|
|
|
IQ iqPacket = createIQ(si.getPacketID(), si.getFrom(), si.getTo(),
|
|
|
|
IQ.Type.ERROR);
|
|
|
|
iqPacket.setError(error);
|
|
|
|
connection.sendPacket(iqPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new, unique, stream ID to identify a file transfer.
|
|
|
|
*
|
|
|
|
* @return Returns a new, unique, stream ID to identify a file transfer.
|
|
|
|
*/
|
|
|
|
public String getNextStreamID() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buffer = new StringBuilder();
|
2006-02-03 19:44:22 +01:00
|
|
|
buffer.append(STREAM_INIT_PREFIX);
|
|
|
|
buffer.append(Math.abs(randomGenerator.nextLong()));
|
|
|
|
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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/>
|
2010-08-15 13:57:11 +02:00
|
|
|
* If they accept, the packet will contain the other user's chosen stream
|
2006-02-03 19:44:22 +01:00
|
|
|
* type to send the file across. The two choices this implementation
|
|
|
|
* provides to the other user for file transfer are <a
|
|
|
|
* href="http://www.jabber.org/jeps/jep-0065.html">SOCKS5 Bytestreams</a>,
|
2010-08-15 13:57:11 +02:00
|
|
|
* which is the preferred method of transfer, and <a
|
2006-02-03 19:44:22 +01:00
|
|
|
* href="http://www.jabber.org/jeps/jep-0047.html">In-Band Bytestreams</a>,
|
|
|
|
* which is the fallback mechanism.
|
|
|
|
* <p/>
|
|
|
|
* The other user may choose to decline the file request if they do not
|
|
|
|
* desire the file, their client does not support JEP-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 XMPPException Thrown if there is an error negotiating the file transfer.
|
|
|
|
*/
|
|
|
|
public StreamNegotiator negotiateOutgoingTransfer(final String userID,
|
|
|
|
final String streamID, final String fileName, final long size,
|
|
|
|
final String desc, int responseTimeout) throws XMPPException {
|
|
|
|
StreamInitiation si = new StreamInitiation();
|
|
|
|
si.setSesssionID(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);
|
|
|
|
|
|
|
|
PacketCollector collector = connection
|
|
|
|
.createPacketCollector(new PacketIDFilter(si.getPacketID()));
|
|
|
|
connection.sendPacket(si);
|
|
|
|
Packet siResponse = collector.nextResult(responseTimeout);
|
|
|
|
collector.cancel();
|
|
|
|
|
|
|
|
if (siResponse instanceof IQ) {
|
|
|
|
IQ iqResponse = (IQ) siResponse;
|
|
|
|
if (iqResponse.getType().equals(IQ.Type.RESULT)) {
|
|
|
|
StreamInitiation response = (StreamInitiation) siResponse;
|
2006-02-08 01:31:17 +01:00
|
|
|
return getOutgoingNegotiator(getStreamMethodField(response
|
|
|
|
.getFeatureNegotiationForm()));
|
2006-02-03 19:44:22 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (iqResponse.getType().equals(IQ.Type.ERROR)) {
|
|
|
|
throw new XMPPException(iqResponse.getError());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new XMPPException("File transfer response unreadable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-08 01:31:17 +01:00
|
|
|
private StreamNegotiator getOutgoingNegotiator(final FormField field)
|
|
|
|
throws XMPPException {
|
|
|
|
String variable;
|
|
|
|
boolean isByteStream = false;
|
|
|
|
boolean isIBB = false;
|
2006-07-18 07:14:33 +02:00
|
|
|
for (Iterator<String> it = field.getValues(); it.hasNext();) {
|
|
|
|
variable = it.next();
|
2010-08-15 13:57:11 +02:00
|
|
|
if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) {
|
2006-02-08 01:31:17 +01:00
|
|
|
isByteStream = true;
|
|
|
|
}
|
2010-08-15 13:57:11 +02:00
|
|
|
else if (variable.equals(InBandBytestreamManager.NAMESPACE)) {
|
2006-02-08 01:31:17 +01:00
|
|
|
isIBB = true;
|
|
|
|
}
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
2006-02-08 01:31:17 +01:00
|
|
|
|
|
|
|
if (!isByteStream && !isIBB) {
|
2006-07-19 21:24:00 +02:00
|
|
|
XMPPError error = new XMPPError(XMPPError.Condition.bad_request,
|
|
|
|
"No acceptable transfer mechanism");
|
|
|
|
throw new XMPPException(error.getMessage(), error);
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isByteStream && isIBB) {
|
2007-03-21 05:09:52 +01:00
|
|
|
return new FaultTolerantNegotiator(connection,
|
2010-08-15 13:57:11 +02:00
|
|
|
byteStreamTransferManager, inbandTransferManager);
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
|
|
|
else if (isByteStream) {
|
2010-08-15 13:57:11 +02:00
|
|
|
return byteStreamTransferManager;
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
else {
|
2006-02-08 01:31:17 +01:00
|
|
|
return inbandTransferManager;
|
2006-02-03 19:44:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DataForm createDefaultInitiationForm() {
|
|
|
|
DataForm form = new DataForm(Form.TYPE_FORM);
|
|
|
|
FormField field = new FormField(STREAM_DATA_FIELD_NAME);
|
2011-03-30 02:43:35 +02:00
|
|
|
field.setType(FormField.TYPE_LIST_SINGLE);
|
2006-02-08 01:31:17 +01:00
|
|
|
if (!IBB_ONLY) {
|
2010-08-15 13:57:11 +02:00
|
|
|
field.addOption(new FormField.Option(Socks5BytestreamManager.NAMESPACE));
|
2006-02-08 01:31:17 +01:00
|
|
|
}
|
2010-08-15 13:57:11 +02:00
|
|
|
field.addOption(new FormField.Option(InBandBytestreamManager.NAMESPACE));
|
2006-02-03 19:44:22 +01:00
|
|
|
form.addField(field);
|
|
|
|
return form;
|
|
|
|
}
|
|
|
|
}
|