Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jet/JetManager.java

188 lines
8.1 KiB
Java
Raw Normal View History

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-08-16 19:58:43 +02:00
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
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
2017-08-16 19:58:43 +02:00
import javax.crypto.NoSuchPaddingException;
2017-07-13 21:36:37 +02:00
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
2017-07-13 21:36:37 +02:00
import org.jivesoftware.smack.XMPPConnection;
2017-08-16 19:58:43 +02:00
import org.jivesoftware.smack.XMPPException;
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.JingleFile;
2017-08-14 22:15:23 +02:00
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-08-16 16:42:11 +02:00
private static final HashMap<String, JingleEnvelopeManager> envelopeManagers = new HashMap<>();
private static final HashMap<String, ExtensionElementProvider<?>> envelopeProviders = 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-08-16 16:42:11 +02:00
public OutgoingFileOfferController sendEncryptedFile(File file, FullJid recipient, JingleEnvelopeManager envelopeManager) throws Exception {
return sendEncryptedFile(file, JingleFile.fromFile(file, null, null, null), recipient, envelopeManager);
2017-08-15 17:43:06 +02:00
}
public OutgoingFileOfferController sendEncryptedFile(File file, JingleFile metadata, FullJid recipient, JingleEnvelopeManager envelopeManager) 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-16 19:58:43 +02:00
throwIfRecipientLacksSupport(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
JingleOutgoingFileOffer offer = new JingleOutgoingFileOffer(file, metadata);
2017-07-30 15:40:04 +02:00
content.setDescription(offer);
2017-07-14 21:19:03 +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-16 16:42:11 +02:00
JetSecurity security = new JetSecurity(envelopeManager, 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;
}
public OutgoingFileOfferController sendEncryptedStream(InputStream inputStream, JingleFile metadata, FullJid recipient, JingleEnvelopeManager envelopeManager)
2017-08-16 19:58:43 +02:00
throws XMPPException.XMPPErrorException, SmackException.FeatureNotSupportedException, SmackException.NotConnectedException,
InterruptedException, SmackException.NoResponseException, NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException,
JingleEnvelopeManager.JingleEncryptionException, NoSuchProviderException, InvalidAlgorithmParameterException {
throwIfRecipientLacksSupport(recipient);
JingleSession session = jingleManager.createSession(Role.initiator, recipient);
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
session.addContent(content);
JingleOutgoingFileOffer offer = new JingleOutgoingFileOffer(inputStream, metadata);
2017-08-16 19:58:43 +02:00
content.setDescription(offer);
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager(recipient);
content.setTransport(transportManager.createTransportForInitiator(content));
JetSecurity security = new JetSecurity(envelopeManager, recipient, content.getName(), Aes256GcmNoPadding.NAMESPACE);
content.setSecurity(security);
session.sendInitiate(connection());
return offer;
}
2017-08-16 16:42:11 +02:00
public void registerEnvelopeManager(JingleEnvelopeManager method) {
envelopeManagers.put(method.getJingleEnvelopeNamespace(), method);
2017-07-13 21:36:37 +02:00
}
2017-08-16 16:42:11 +02:00
public void unregisterEnvelopeManager(String namespace) {
envelopeManagers.remove(namespace);
2017-07-13 21:36:37 +02:00
}
2017-08-16 16:42:11 +02:00
public JingleEnvelopeManager getEnvelopeManager(String namespace) {
return envelopeManagers.get(namespace);
2017-07-13 21:36:37 +02:00
}
2017-07-14 21:19:03 +02:00
2017-08-16 16:42:11 +02:00
public static void registerEnvelopeProvider(String namespace, ExtensionElementProvider<?> provider) {
envelopeProviders.put(namespace, provider);
2017-07-31 12:08:18 +02:00
}
2017-08-16 16:42:11 +02:00
public static void unregisterEnvelopeProvider(String namespace) {
envelopeProviders.remove(namespace);
2017-07-31 12:08:18 +02:00
}
2017-08-16 16:42:11 +02:00
public static ExtensionElementProvider<?> getEnvelopeProvider(String namespace) {
return envelopeProviders.get(namespace);
2017-07-31 12:08:18 +02:00
}
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-08-16 19:58:43 +02:00
private void throwIfRecipientLacksSupport(FullJid recipient) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, SmackException.FeatureNotSupportedException {
if (!ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(recipient, getNamespace())) {
throw new SmackException.FeatureNotSupportedException(getNamespace(), recipient);
}
}
2017-07-13 21:36:37 +02:00
}