1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-27 14:32:06 +01:00

Create JingleTransportManager framework

This commit is contained in:
vanitasvitae 2017-06-08 01:59:38 +02:00
parent 05b0e3650e
commit 1775c691af
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
19 changed files with 315 additions and 48 deletions

View file

@ -37,7 +37,7 @@ import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingJingleFileTransferCallback;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChildElement;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferContentDescription;

View file

@ -22,30 +22,27 @@ import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.JingleContentTransportManager;
import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager;
import org.jivesoftware.smackx.jingle.JingleInputStream;
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider;
/**
* Manager for Jingle In-Band-Bytestreams.
*/
public final class JingleInBandBytestreamTransportManager extends Manager implements JingleContentTransportManager {
public final class JingleInBandBytestreamTransportManager extends AbstractJingleContentTransportManager<JingleInBandBytestreamTransport> {
private static final Logger LOGGER = Logger.getLogger(JingleInBandBytestreamTransportManager.class.getName());
public static final String NAMESPACE_V1 = "urn:xmpp:jingle:transports:ibb:1";
@ -54,8 +51,11 @@ public final class JingleInBandBytestreamTransportManager extends Manager implem
private JingleInBandBytestreamTransportManager(XMPPConnection connection) {
super(connection);
JingleContentProviderManager.addJingleContentTransportProvider(NAMESPACE_V1, new JingleInBandByteStreamTransportProvider());
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE_V1);
}
@Override
protected JingleContentTransportProvider<JingleInBandBytestreamTransport> createJingleContentTransportProvider() {
return new JingleInBandByteStreamTransportProvider();
}
public static JingleInBandBytestreamTransportManager getInstanceFor(XMPPConnection connection) {
@ -67,6 +67,11 @@ public final class JingleInBandBytestreamTransportManager extends Manager implem
return manager;
}
@Override
public String getNamespace() {
return NAMESPACE_V1;
}
@Override
public void acceptInputStream(final Jingle jingle, final JingleTransportInputStreamCallback callback) {
final int blockSize = ((JingleInBandBytestreamTransport)
@ -117,12 +122,4 @@ public final class JingleInBandBytestreamTransportManager extends Manager implem
return ibs.getOutputStream();
}
/**
* Generate a random session id.
* @return
*/
public static String generateSessionId() {
return StringUtils.randomString(24);
}
}

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smackx.jingle_ibb.element;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.jingle.JingleTransportManager;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle_ibb.JingleInBandBytestreamTransportManager;
@ -37,7 +38,7 @@ public class JingleInBandBytestreamTransport extends JingleContentTransport {
}
public JingleInBandBytestreamTransport(short blockSize) {
this(blockSize, JingleInBandBytestreamTransportManager.generateSessionId());
this(blockSize, JingleTransportManager.generateSessionId());
}
public JingleInBandBytestreamTransport(short blockSize, String sid) {

View file

@ -1,14 +1,57 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b;
import org.jivesoftware.smack.Manager;
import java.io.OutputStream;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager;
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransport;
import org.jivesoftware.smackx.jingle_s5b.provider.JingleSocks5BytestreamTransportProvider;
/**
* Created by vanitas on 07.06.17.
* Manager for JingleSocks5BytestreamTransports.
*/
public class JingleSocks5BytestreamTransportManager extends Manager {
public final class JingleSocks5BytestreamTransportManager extends AbstractJingleContentTransportManager<JingleSocks5BytestreamTransport> {
private JingleSocks5BytestreamTransportManager(XMPPConnection connection) {
super(connection);
}
@Override
protected JingleContentTransportProvider<JingleSocks5BytestreamTransport> createJingleContentTransportProvider() {
return new JingleSocks5BytestreamTransportProvider();
}
@Override
public String getNamespace() {
return JingleSocks5BytestreamTransport.NAMESPACE_V1;
}
@Override
public void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback) {
}
@Override
public OutputStream createOutputStream(Jingle jingle) {
return null;
}
}

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b.elements;
import java.util.ArrayList;

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b.elements;
import java.util.logging.Logger;
@ -12,7 +28,7 @@ import org.jxmpp.stringprep.XmppStringprepException;
/**
* TransportCandidate for Jingle Socks5Bytestream transports.
*/
public class JingleSocks5BytestreamTransportCandidate extends JingleContentTransportCandidate {
public final class JingleSocks5BytestreamTransportCandidate extends JingleContentTransportCandidate {
private static final Logger LOGGER = Logger.getLogger(JingleSocks5BytestreamTransportCandidate.class.getName());
@ -111,7 +127,7 @@ public class JingleSocks5BytestreamTransportCandidate extends JingleContentTrans
return new Builder();
}
public static class Builder {
public static final class Builder {
private String cid;
private String host;
private Jid jid;

View file

@ -1,8 +0,0 @@
package org.jivesoftware.smackx.jingle_s5b.elements;
import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
/**
* Created by vanitas on 07.06.17.
*/

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b.elements;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -33,7 +49,7 @@ public abstract class JingleSocks5BytestreamTransportInfo extends JingleContentT
return PEI;
}
public static class CandidateActivated extends JingleSocks5BytestreamTransportInfo {
public static final class CandidateActivated extends JingleSocks5BytestreamTransportInfo {
public static final String ELEMENT = "candidate-activated";
public static final String ATTR_CID = "cid";
@ -73,7 +89,7 @@ public abstract class JingleSocks5BytestreamTransportInfo extends JingleContentT
}
}
public static class CandidateUsed extends JingleSocks5BytestreamTransportInfo {
public static final class CandidateUsed extends JingleSocks5BytestreamTransportInfo {
public static final String ELEMENT = "candidate-used";
public static final String ATTR_CID = "cid";
@ -113,7 +129,7 @@ public abstract class JingleSocks5BytestreamTransportInfo extends JingleContentT
}
}
public static class CandidateError extends JingleSocks5BytestreamTransportInfo {
public static final class CandidateError extends JingleSocks5BytestreamTransportInfo {
public static final String ELEMENT = "candidate-error";
private CandidateError() {
@ -144,7 +160,7 @@ public abstract class JingleSocks5BytestreamTransportInfo extends JingleContentT
}
}
public static class ProxyError extends JingleSocks5BytestreamTransportInfo {
public static final class ProxyError extends JingleSocks5BytestreamTransportInfo {
public static final String ELEMENT = "proxy-error";
private ProxyError() {

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b.provider;
import static org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.Mode.tcp;

View file

@ -23,6 +23,7 @@ import static junit.framework.TestCase.assertTrue;
import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.jingle.JingleTransportManager;
import org.jivesoftware.smackx.jingle_ibb.element.JingleInBandBytestreamTransport;
import org.jivesoftware.smackx.jingle_ibb.provider.JingleInBandByteStreamTransportProvider;
import org.junit.Test;
@ -34,7 +35,7 @@ public class JingleInBandByteStreamTransportTest extends SmackTestSuite {
@Test
public void parserTest() throws Exception {
String sid = JingleInBandBytestreamTransportManager.generateSessionId();
String sid = JingleTransportManager.generateSessionId();
short size = 8192;
String xml = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='8192' sid='" + sid + "'/>";

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle_s5b;
import static junit.framework.TestCase.assertNull;

View file

@ -0,0 +1,47 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle;
import java.io.OutputStream;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
/**
* Interface with methods that JingleContentTransportManagers must implement.
*/
public abstract class AbstractJingleContentTransportManager<D extends JingleContentTransport> extends Manager {
public AbstractJingleContentTransportManager(XMPPConnection connection) {
super(connection);
JingleTransportManager.getInstanceFor(connection).registerJingleContentTransportManager(this);
JingleContentProviderManager.addJingleContentTransportProvider(getNamespace(), createJingleContentTransportProvider());
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(getNamespace());
}
protected abstract JingleContentTransportProvider<D> createJingleContentTransportProvider();
public abstract String getNamespace();
public abstract void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback);
public abstract OutputStream createOutputStream(Jingle jingle);
}

View file

@ -14,11 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingle.provider;
package org.jivesoftware.smackx.jingle;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smackx.jingle.provider.JingleContentDescriptionProvider;
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
public class JingleContentProviderManager {
private static final Map<String, JingleContentDescriptionProvider<?>> jingleContentDescriptionProviders = new ConcurrentHashMap<>();

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.jingle;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
@ -56,7 +55,6 @@ public final class JingleManager extends Manager {
private final Map<String, JingleHandler> descriptionHandlers = new ConcurrentHashMap<>();
private final Map<FullJidAndSessionId, JingleSession> jingleSessions = new ConcurrentHashMap<>();
private final Map<String, JingleContentTransportManager> transportManagers = new HashMap<>();
private JingleManager(final XMPPConnection connection) {
super(connection);

View file

@ -0,0 +1,68 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle;
import java.util.HashMap;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.jingle.exception.UnsupportedJingleTransportException;
/**
* Manager for JingleContentTransportManagers.
*/
public final class JingleTransportManager extends Manager {
public static final WeakHashMap<XMPPConnection, JingleTransportManager> INSTANCES = new WeakHashMap<>();
private final HashMap<String, AbstractJingleContentTransportManager<?>> contentTransportManagers = new HashMap<>();
private JingleTransportManager(XMPPConnection connection) {
super(connection);
}
public static JingleTransportManager getInstanceFor(XMPPConnection connection) {
JingleTransportManager manager = INSTANCES.get(connection);
if (manager == null) {
manager = new JingleTransportManager(connection);
INSTANCES.put(connection, manager);
}
return manager;
}
public AbstractJingleContentTransportManager<?> getJingleContentTransportManager(String namespace) throws UnsupportedJingleTransportException {
AbstractJingleContentTransportManager<?> manager = contentTransportManagers.get(namespace);
if (manager == null) {
throw new UnsupportedJingleTransportException("Cannot find registered JingleContentTransportManager for " + namespace);
}
return manager;
}
public void registerJingleContentTransportManager(AbstractJingleContentTransportManager<?> manager) {
contentTransportManagers.put(manager.getNamespace(), manager);
}
public void unregisterJingleContentTransportManager(AbstractJingleContentTransportManager<?> manager) {
contentTransportManagers.remove(manager.getNamespace());
}
public static String generateSessionId() {
return StringUtils.randomString(24);
}
}

View file

@ -1,3 +1,19 @@
/**
*
* Copyright 2017 Paul Schaub
*
* 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.
*/
package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement;

View file

@ -14,18 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingle;
import java.io.OutputStream;
import org.jivesoftware.smackx.jingle.element.Jingle;
package org.jivesoftware.smackx.jingle.exception;
/**
* Interface with methods that JingleContentTransportManagers must implement.
* Exception gets thrown when we miss a JingleContentTransportManager for a certain transport namespace.
*/
public interface JingleContentTransportManager {
public class UnsupportedJingleTransportException extends Exception {
void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback);
private static final long serialVersionUID = 1L;
OutputStream createOutputStream(Jingle jingle);
public UnsupportedJingleTransportException(String message) {
super(message);
}
}

View file

@ -0,0 +1,22 @@
/**
*
* Copyright 2017 Florian Schmaus
*
* 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.
*/
/**
* Stanzas and Extension Elements for <a href="https://xmpp.org/extensions/xep-0166.html">XEP-0166: Jingle</a>.
* Exceptions.
*/
package org.jivesoftware.smackx.jingle.exception;

View file

@ -20,6 +20,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.ParserUtils;
import org.jivesoftware.smackx.jingle.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.jivesoftware.smackx.jingle.element.JingleContent;