2017-07-13 21:45:04 +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.jet;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
import java.io.File;
|
2017-07-13 21:36:37 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.WeakHashMap;
|
2017-07-31 12:08:18 +02:00
|
|
|
import java.util.logging.Logger;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.Manager;
|
2017-08-07 14:35:00 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2017-07-13 21:36:37 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2017-07-31 12:08:18 +02:00
|
|
|
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
2017-08-04 15:08:16 +02:00
|
|
|
import org.jivesoftware.smackx.ciphers.Aes256GcmNoPadding;
|
2017-07-30 23:32:00 +02:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2017-08-04 23:05:59 +02:00
|
|
|
import org.jivesoftware.smackx.jet.component.JetSecurity;
|
2017-07-31 12:08:18 +02:00
|
|
|
import org.jivesoftware.smackx.jet.provider.JetSecurityProvider;
|
2017-07-30 22:31:29 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleDescriptionManager;
|
2017-07-30 15:40:04 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportManager;
|
2017-08-04 23:05:59 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.component.JingleContent;
|
|
|
|
import org.jivesoftware.smackx.jingle.component.JingleSession;
|
2017-07-21 23:05:46 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
|
2017-07-30 15:40:04 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.util.Role;
|
2017-08-14 22:15:23 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager;
|
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleOutgoingFileOffer;
|
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
|
2017-07-14 21:19:03 +02:00
|
|
|
|
|
|
|
import org.jxmpp.jid.FullJid;
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-13 21:58:49 +02:00
|
|
|
* Manager for Jingle Encrypted Transfers (XEP-XXXX).
|
2017-07-13 21:36:37 +02:00
|
|
|
*/
|
2017-07-30 22:31:29 +02:00
|
|
|
public final class JetManager extends Manager implements JingleDescriptionManager {
|
2017-07-14 21:19:03 +02:00
|
|
|
|
2017-07-31 12:08:18 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(JetManager.class.getName());
|
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
private static final WeakHashMap<XMPPConnection, JetManager> INSTANCES = new WeakHashMap<>();
|
2017-07-31 12:08:18 +02:00
|
|
|
private static final HashMap<String, JingleEncryptionMethod> encryptionMethods = new HashMap<>();
|
|
|
|
private static final HashMap<String, ExtensionElementProvider<?>> encryptionMethodProviders = new HashMap<>();
|
2017-07-13 21:36:37 +02:00
|
|
|
|
2017-07-30 15:40:04 +02:00
|
|
|
private final JingleManager jingleManager;
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
static {
|
|
|
|
JingleManager.addJingleSecurityAdapter(new JetSecurityAdapter());
|
2017-07-31 12:08:18 +02:00
|
|
|
JingleManager.addJingleSecurityProvider(new JetSecurityProvider());
|
2017-07-30 22:31:29 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
private JetManager(XMPPConnection connection) {
|
2017-07-13 21:36:37 +02:00
|
|
|
super(connection);
|
2017-07-30 15:40:04 +02:00
|
|
|
this.jingleManager = JingleManager.getInstanceFor(connection);
|
2017-07-30 23:32:00 +02:00
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(getNamespace());
|
|
|
|
jingleManager.addJingleDescriptionManager(this);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:58:49 +02:00
|
|
|
public static JetManager getInstanceFor(XMPPConnection connection) {
|
|
|
|
JetManager manager = INSTANCES.get(connection);
|
2017-07-13 21:36:37 +02:00
|
|
|
|
|
|
|
if (manager == null) {
|
2017-07-13 21:58:49 +02:00
|
|
|
manager = new JetManager(connection);
|
2017-07-13 21:36:37 +02:00
|
|
|
INSTANCES.put(connection, manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
public OutgoingFileOfferController sendEncryptedFile(FullJid recipient, File file, JingleEncryptionMethod method) throws Exception {
|
2017-07-30 15:40:04 +02:00
|
|
|
if (file == null || !file.exists()) {
|
|
|
|
throw new IllegalArgumentException("File MUST NOT be null and MUST exist.");
|
2017-07-14 21:19:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 14:35:00 +02:00
|
|
|
ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection());
|
|
|
|
if (!disco.supportsFeature(recipient, getNamespace()) || !disco.supportsFeature(recipient, method.getNamespace())) {
|
|
|
|
throw new SmackException.FeatureNotSupportedException(getNamespace(), recipient);
|
|
|
|
}
|
|
|
|
|
2017-07-30 15:40:04 +02:00
|
|
|
JingleSession session = jingleManager.createSession(Role.initiator, recipient);
|
2017-07-17 20:18:08 +02:00
|
|
|
|
2017-07-30 15:40:04 +02:00
|
|
|
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
|
|
|
|
session.addContent(content);
|
2017-07-14 21:19:03 +02:00
|
|
|
|
2017-07-27 17:35:16 +02:00
|
|
|
JingleOutgoingFileOffer offer = new JingleOutgoingFileOffer(file);
|
2017-07-30 15:40:04 +02:00
|
|
|
content.setDescription(offer);
|
2017-07-14 21:19:03 +02:00
|
|
|
|
2017-08-07 14:35:00 +02:00
|
|
|
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager(recipient);
|
2017-08-04 22:48:33 +02:00
|
|
|
content.setTransport(transportManager.createTransportForInitiator(content));
|
2017-07-17 20:18:08 +02:00
|
|
|
|
2017-08-04 15:08:16 +02:00
|
|
|
JetSecurity security = new JetSecurity(method, recipient, content.getName(), Aes256GcmNoPadding.NAMESPACE);
|
2017-07-30 15:40:04 +02:00
|
|
|
content.setSecurity(security);
|
2017-08-07 23:55:32 +02:00
|
|
|
session.sendInitiate(connection());
|
2017-07-17 20:18:08 +02:00
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
return offer;
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
public void registerEncryptionMethod(JingleEncryptionMethod method) {
|
|
|
|
encryptionMethods.put(method.getNamespace(), method);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public void unregisterEncryptionMethod(String namespace) {
|
|
|
|
encryptionMethods.remove(namespace);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 21:19:03 +02:00
|
|
|
public JingleEncryptionMethod getEncryptionMethod(String namespace) {
|
|
|
|
return encryptionMethods.get(namespace);
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|
2017-07-14 21:19:03 +02:00
|
|
|
|
2017-07-31 12:08:18 +02:00
|
|
|
public static void registerEncryptionMethodProvider(String namespace, ExtensionElementProvider<?> provider) {
|
|
|
|
encryptionMethodProviders.put(namespace, provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeEncryptionMethodProvider(String namespace) {
|
|
|
|
encryptionMethodProviders.remove(namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ExtensionElementProvider<?> getEncryptionMethodProvider(String namespace) {
|
|
|
|
return encryptionMethodProviders.get(namespace);
|
|
|
|
}
|
|
|
|
|
2017-07-30 22:31:29 +02:00
|
|
|
@Override
|
|
|
|
public String getNamespace() {
|
|
|
|
return JetSecurity.NAMESPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void notifySessionInitiate(JingleSession session) {
|
|
|
|
JingleFileTransferManager.getInstanceFor(connection()).notifySessionInitiate(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void notifyContentAdd(JingleSession session, JingleContent content) {
|
|
|
|
JingleFileTransferManager.getInstanceFor(connection()).notifyContentAdd(session, content);
|
|
|
|
}
|
2017-07-13 21:36:37 +02:00
|
|
|
}
|