1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Add functionality to create TransportElements

This commit is contained in:
vanitasvitae 2017-06-08 17:32:37 +02:00
parent 6c0162c341
commit db294f3f1b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 65 additions and 8 deletions

View file

@ -27,24 +27,25 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.hash.HashManager; import org.jivesoftware.smackx.hash.HashManager;
import org.jivesoftware.smackx.hash.element.HashElement; import org.jivesoftware.smackx.hash.element.HashElement;
import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager;
import org.jivesoftware.smackx.jingle.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle.JingleHandler; import org.jivesoftware.smackx.jingle.JingleHandler;
import org.jivesoftware.smackx.jingle.JingleManager; import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent; import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement; import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
import org.jivesoftware.smackx.jingle.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback; import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferContentDescription; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferContentDescription;
import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingJingleFileTransferListener; import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingJingleFileTransferListener;
import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferContentDescriptionProvider; import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferContentDescriptionProvider;
import org.jivesoftware.smackx.jingle_ibb.JingleInBandBytestreamTransportManager; import org.jivesoftware.smackx.jingle_ibb.JingleInBandBytestreamTransportManager;
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
/** /**
@ -112,7 +113,9 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
* QnD method. * QnD method.
* @param file * @param file
*/ */
public void sendFile(File file, final FullJid recipient) throws IOException, SmackException.NotConnectedException, InterruptedException { public void sendFile(File file, final FullJid recipient) throws IOException, SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException, SmackException.NoResponseException {
AbstractJingleContentTransportManager<?> preferedTransportManager = JingleInBandBytestreamTransportManager.getInstanceFor(connection());
JingleFileTransferSession session = new JingleFileTransferSession(connection(), recipient, connection().getUser(), recipient); JingleFileTransferSession session = new JingleFileTransferSession(connection(), recipient, connection().getUser(), recipient);
JingleFileTransferChildElement.Builder b = JingleFileTransferChildElement.getBuilder(); JingleFileTransferChildElement.Builder b = JingleFileTransferChildElement.getBuilder();
b.setFile(file); b.setFile(file);
@ -132,7 +135,7 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
bb.setDescription(new JingleFileTransferContentDescription(payloads)) bb.setDescription(new JingleFileTransferContentDescription(payloads))
.setCreator(JingleContent.Creator.initiator) .setCreator(JingleContent.Creator.initiator)
.setName(StringUtils.randomString(24)) .setName(StringUtils.randomString(24))
.addTransport(new JingleInBandBytestreamTransport()); .addTransport(preferedTransportManager.createJingleContentTransport());
Jingle jingle = (Jingle) session.initiate(Collections.singletonList(bb.build())); Jingle jingle = (Jingle) session.initiate(Collections.singletonList(bb.build()));
jingle.setTo(recipient); jingle.setTo(recipient);

View file

@ -38,6 +38,7 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport; import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider; import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider;
import org.jxmpp.jid.Jid;
/** /**
* Manager for Jingle In-Band-Bytestreams. * Manager for Jingle In-Band-Bytestreams.
@ -122,4 +123,9 @@ public final class JingleInBandBytestreamTransportManager extends AbstractJingle
return ibs.getOutputStream(); return ibs.getOutputStream();
} }
@Override
public JingleInBandBytestreamTransport createJingleContentTransport(Jid otherUser) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
return new JingleInBandBytestreamTransport();
}
} }

View file

@ -38,7 +38,7 @@ public class JingleInBandBytestreamTransport extends JingleContentTransport {
} }
public JingleInBandBytestreamTransport(short blockSize) { public JingleInBandBytestreamTransport(short blockSize) {
this(blockSize, JingleTransportManager.generateSessionId()); this(blockSize, JingleTransportManager.generateRandomId());
} }
public JingleInBandBytestreamTransport(short blockSize, String sid) { public JingleInBandBytestreamTransport(short blockSize, String sid) {

View file

@ -17,18 +17,23 @@
package org.jivesoftware.smackx.jingle_s5b; package org.jivesoftware.smackx.jingle_s5b;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager; import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.hash.HashManager;
import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager; import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager;
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback; import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
import org.jivesoftware.smackx.jingle.JingleTransportManager;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransport; import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransport;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransportCandidate;
import org.jivesoftware.smackx.jingle_s5b.provider.JingleSocks5BytestreamTransportProvider; import org.jivesoftware.smackx.jingle_s5b.provider.JingleSocks5BytestreamTransportProvider;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
@ -71,4 +76,38 @@ public final class JingleSocks5BytestreamTransportManager extends AbstractJingle
public OutputStream createOutputStream(Jingle jingle) { public OutputStream createOutputStream(Jingle jingle) {
return null; return null;
} }
@Override
public JingleSocks5BytestreamTransport createJingleContentTransport(Jid otherUser) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
JingleSocks5BytestreamTransport.Builder builder = JingleSocks5BytestreamTransport.getBuilder();
List<Bytestream.StreamHost> localStreams = getLocalStreamHosts();
List<Bytestream.StreamHost> availableStreams = getAvailableStreamHosts();
for (Bytestream.StreamHost host : localStreams) {
JingleSocks5BytestreamTransportCandidate candidate = new JingleSocks5BytestreamTransportCandidate(host, 100);
builder.addTransportCandidate(candidate);
}
for (Bytestream.StreamHost host : availableStreams) {
JingleSocks5BytestreamTransportCandidate candidate = new JingleSocks5BytestreamTransportCandidate(host, 0);
builder.addTransportCandidate(candidate);
}
builder.setMode(Bytestream.Mode.tcp);
String sid = JingleTransportManager.generateRandomId();
builder.setStreamId(sid);
String digestString =
sid +
connection().getUser().asFullJidIfPossible().toString() +
otherUser.asFullJidIfPossible().toString();
try {
builder.setDestinationAddress(
new String(HashManager.sha_1(digestString.getBytes(StringUtils.UTF8)), StringUtils.UTF8));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return builder.build();
}
} }

View file

@ -58,7 +58,7 @@ public final class JingleSocks5BytestreamTransportCandidate extends JingleConten
} }
public JingleSocks5BytestreamTransportCandidate(Bytestream.StreamHost streamHost, int priority) { public JingleSocks5BytestreamTransportCandidate(Bytestream.StreamHost streamHost, int priority) {
this.cid = JingleTransportManager.generateSessionId(); this.cid = JingleTransportManager.generateRandomId();
this.host = streamHost.getAddress(); this.host = streamHost.getAddress();
this.jid = streamHost.getJID(); this.jid = streamHost.getJID();
this.port = streamHost.getPort(); this.port = streamHost.getPort();
@ -117,6 +117,10 @@ public final class JingleSocks5BytestreamTransportCandidate extends JingleConten
return type; return type;
} }
public Bytestream.StreamHost getStreamHost() {
return new Bytestream.StreamHost(jid, host, port);
}
@Override @Override
public CharSequence toXML() { public CharSequence toXML() {
XmlStringBuilder xml = new XmlStringBuilder(); XmlStringBuilder xml = new XmlStringBuilder();

View file

@ -35,7 +35,7 @@ public class JingleInBandByteStreamTransportTest extends SmackTestSuite {
@Test @Test
public void parserTest() throws Exception { public void parserTest() throws Exception {
String sid = JingleTransportManager.generateSessionId(); String sid = JingleTransportManager.generateRandomId();
short size = 8192; short size = 8192;
String xml = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='8192' sid='" + sid + "'/>"; String xml = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='8192' sid='" + sid + "'/>";

View file

@ -19,11 +19,14 @@ package org.jivesoftware.smackx.jingle;
import java.io.OutputStream; import java.io.OutputStream;
import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport; import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider; import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jxmpp.jid.Jid;
/** /**
* Interface with methods that JingleContentTransportManagers must implement. * Interface with methods that JingleContentTransportManagers must implement.
@ -44,4 +47,6 @@ public abstract class AbstractJingleContentTransportManager<D extends JingleCont
public abstract void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback); public abstract void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback);
public abstract OutputStream createOutputStream(Jingle jingle); public abstract OutputStream createOutputStream(Jingle jingle);
public abstract D createJingleContentTransport(Jid remote) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException;
} }

View file

@ -62,7 +62,7 @@ public final class JingleTransportManager extends Manager {
contentTransportManagers.remove(manager.getNamespace()); contentTransportManagers.remove(manager.getNamespace());
} }
public static String generateSessionId() { public static String generateRandomId() {
return StringUtils.randomString(24); return StringUtils.randomString(24);
} }
} }