2017-06-08 01:59:38 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2017-06-09 21:35:49 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
2017-06-08 01:59:38 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.Manager;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2017-06-09 21:35:49 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
2017-06-08 01:59:38 +02:00
|
|
|
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<>();
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
private final HashMap<String, AbstractJingleTransportManager<?>> contentTransportManagers = new HashMap<>();
|
2017-06-08 01:59:38 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
public AbstractJingleTransportManager<?> getJingleContentTransportManager(String namespace) throws UnsupportedJingleTransportException {
|
|
|
|
AbstractJingleTransportManager<?> manager = contentTransportManagers.get(namespace);
|
2017-06-08 01:59:38 +02:00
|
|
|
if (manager == null) {
|
|
|
|
throw new UnsupportedJingleTransportException("Cannot find registered JingleContentTransportManager for " + namespace);
|
|
|
|
}
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
public AbstractJingleTransportManager<?> getJingleContentTransportManager(Jingle jingle) throws UnsupportedJingleTransportException {
|
2017-06-09 21:35:49 +02:00
|
|
|
return getJingleContentTransportManager(jingle.getContents().get(0).getJingleTransports().get(0).getNamespace());
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
public void registerJingleContentTransportManager(AbstractJingleTransportManager<?> manager) {
|
2017-06-08 01:59:38 +02:00
|
|
|
contentTransportManagers.put(manager.getNamespace(), manager);
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
public void unregisterJingleContentTransportManager(AbstractJingleTransportManager<?> manager) {
|
2017-06-08 01:59:38 +02:00
|
|
|
contentTransportManagers.remove(manager.getNamespace());
|
|
|
|
}
|
|
|
|
|
2017-06-09 23:16:15 +02:00
|
|
|
public Collection<AbstractJingleTransportManager<?>> getAvailableJingleBytestreamManagers() {
|
2017-06-09 21:35:49 +02:00
|
|
|
return Collections.unmodifiableCollection(contentTransportManagers.values());
|
|
|
|
}
|
|
|
|
|
2017-06-08 17:32:37 +02:00
|
|
|
public static String generateRandomId() {
|
2017-06-08 01:59:38 +02:00
|
|
|
return StringUtils.randomString(24);
|
|
|
|
}
|
|
|
|
}
|