Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/JingleFileTransferManager.java

219 lines
9.4 KiB
Java
Raw Normal View History

2017-07-27 17:35:16 +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.
*/
2017-08-14 13:58:48 +02:00
package org.jivesoftware.smackx.jingle_filetransfer;
2017-07-22 01:01:50 +02:00
2017-07-27 00:12:42 +02:00
import java.io.File;
2017-08-16 18:20:56 +02:00
import java.io.FileNotFoundException;
2017-08-13 17:22:26 +02:00
import java.io.InputStream;
2017-07-27 00:12:42 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2017-07-22 01:01:50 +02:00
import java.util.WeakHashMap;
2017-07-31 12:08:18 +02:00
import java.util.logging.Level;
import java.util.logging.Logger;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smack.Manager;
2017-07-28 14:02:32 +02:00
import org.jivesoftware.smack.SmackException;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smack.XMPPConnection;
2017-07-28 14:02:32 +02:00
import org.jivesoftware.smack.XMPPException;
2017-07-22 01:01:50 +02:00
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
2017-08-14 22:15:23 +02:00
import org.jivesoftware.smackx.jingle.JingleDescriptionManager;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleTransportManager;
import org.jivesoftware.smackx.jingle.component.JingleContent;
import org.jivesoftware.smackx.jingle.component.JingleSession;
import org.jivesoftware.smackx.jingle.component.JingleTransport;
import org.jivesoftware.smackx.jingle.element.JingleContentElement;
import org.jivesoftware.smackx.jingle.util.Role;
2017-08-14 13:58:48 +02:00
import org.jivesoftware.smackx.jingle_filetransfer.adapter.JingleFileTransferAdapter;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleFileTransfer;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleFileTransferFile;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleIncomingFileOffer;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleIncomingFileRequest;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleOutgoingFileOffer;
import org.jivesoftware.smackx.jingle_filetransfer.component.JingleOutgoingFileRequest;
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileRequestController;
import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingFileOfferListener;
import org.jivesoftware.smackx.jingle_filetransfer.listener.IncomingFileRequestListener;
import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferProvider;
2017-07-27 00:12:42 +02:00
import org.jxmpp.jid.FullJid;
2017-07-22 01:01:50 +02:00
/**
* Created by vanitas on 22.07.17.
*/
public final class JingleFileTransferManager extends Manager implements JingleDescriptionManager {
2017-08-16 15:36:49 +02:00
2017-07-31 12:08:18 +02:00
private static final Logger LOGGER = Logger.getLogger(JingleFileTransferManager.class.getName());
2017-07-22 01:01:50 +02:00
private static final WeakHashMap<XMPPConnection, JingleFileTransferManager> INSTANCES = new WeakHashMap<>();
private final JingleManager jingleManager;
2017-07-27 15:18:18 +02:00
private final List<IncomingFileOfferListener> offerListeners =
Collections.synchronizedList(new ArrayList<IncomingFileOfferListener>());
2017-08-15 17:43:06 +02:00
2017-07-27 15:18:18 +02:00
private final List<IncomingFileRequestListener> requestListeners =
Collections.synchronizedList(new ArrayList<IncomingFileRequestListener>());
2017-07-27 00:12:42 +02:00
2017-07-28 14:02:32 +02:00
static {
JingleManager.addJingleDescriptionAdapter(new JingleFileTransferAdapter());
2017-07-31 12:08:18 +02:00
JingleManager.addJingleDescriptionProvider(new JingleFileTransferProvider());
2017-07-28 14:02:32 +02:00
}
2017-07-22 01:01:50 +02:00
private JingleFileTransferManager(XMPPConnection connection) {
super(connection);
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(getNamespace());
jingleManager = JingleManager.getInstanceFor(connection);
jingleManager.addJingleDescriptionManager(this);
}
public static JingleFileTransferManager getInstanceFor(XMPPConnection connection) {
JingleFileTransferManager manager = INSTANCES.get(connection);
if (manager == null) {
manager = new JingleFileTransferManager(connection);
INSTANCES.put(connection, manager);
}
return manager;
}
2017-07-28 14:02:32 +02:00
public OutgoingFileOfferController sendFile(File file, FullJid to)
throws SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException,
2017-08-16 18:20:56 +02:00
SmackException.NoResponseException, SmackException.FeatureNotSupportedException, FileNotFoundException {
2017-08-15 17:43:06 +02:00
return sendFile(file, null, to);
2017-08-13 14:38:53 +02:00
}
2017-07-28 14:02:32 +02:00
2017-08-13 14:38:53 +02:00
public OutgoingFileOfferController sendFile(File file, String alternativeFilename, FullJid to)
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
2017-08-16 18:20:56 +02:00
SmackException.NoResponseException, SmackException.FeatureNotSupportedException, FileNotFoundException {
2017-07-27 00:12:42 +02:00
if (file == null || !file.exists()) {
throw new IllegalArgumentException("File MUST NOT be null and MUST exist.");
}
if (!ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(to, getNamespace())) {
throw new SmackException.FeatureNotSupportedException(getNamespace(), to);
}
2017-07-27 00:12:42 +02:00
JingleSession session = jingleManager.createSession(Role.initiator, to);
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
session.addContent(content);
JingleOutgoingFileOffer offer = new JingleOutgoingFileOffer(file);
2017-08-13 14:38:53 +02:00
if (alternativeFilename != null) {
offer.getFile().setName(alternativeFilename);
}
2017-07-27 00:12:42 +02:00
content.setDescription(offer);
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager(to);
2017-08-07 23:55:32 +02:00
JingleTransport<?> transport = transportManager.createTransportForInitiator(content);
content.setTransport(transport);
2017-07-27 00:12:42 +02:00
2017-08-07 23:55:32 +02:00
session.sendInitiate(connection());
2017-07-28 14:02:32 +02:00
2017-07-27 15:18:18 +02:00
return offer;
}
2017-07-27 00:12:42 +02:00
2017-08-16 19:58:43 +02:00
public OutgoingFileOfferController sendStream(final InputStream stream, JingleFileTransferFile.StreamFile file, FullJid recipient) throws SmackException.FeatureNotSupportedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
2017-08-15 17:43:06 +02:00
if (!ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(recipient, getNamespace())) {
throw new SmackException.FeatureNotSupportedException(getNamespace(), recipient);
}
JingleSession session = jingleManager.createSession(Role.initiator, recipient);
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
session.addContent(content);
2017-08-16 18:20:56 +02:00
JingleOutgoingFileOffer outgoingFileOffer = new JingleOutgoingFileOffer(file, stream);
2017-08-15 17:43:06 +02:00
content.setDescription(outgoingFileOffer);
JingleTransportManager transportManager = jingleManager.getBestAvailableTransportManager(recipient);
JingleTransport<?> transport = transportManager.createTransportForInitiator(content);
content.setTransport(transport);
session.sendInitiate(connection());
return outgoingFileOffer;
2017-08-13 17:22:26 +02:00
}
2017-08-13 19:44:02 +02:00
public OutgoingFileRequestController requestFile(JingleFileTransferFile.RemoteFile file, FullJid from) {
2017-07-28 14:02:32 +02:00
JingleOutgoingFileRequest request = new JingleOutgoingFileRequest(file);
2017-07-27 00:12:42 +02:00
2017-07-27 15:18:18 +02:00
//TODO at some point.
return request;
}
public void addIncomingFileOfferListener(IncomingFileOfferListener listener) {
offerListeners.add(listener);
2017-07-27 00:12:42 +02:00
}
2017-07-27 15:18:18 +02:00
public void removeIncomingFileOfferListener(IncomingFileOfferListener listener) {
offerListeners.remove(listener);
2017-07-27 00:12:42 +02:00
}
2017-07-27 15:18:18 +02:00
public void notifyIncomingFileOfferListeners(JingleIncomingFileOffer offer) {
2017-07-31 12:08:18 +02:00
LOGGER.log(Level.INFO, "Incoming File transfer: [" + offer.getNamespace() + ", "
+ offer.getParent().getTransport().getNamespace() + ", "
2017-07-31 15:26:52 +02:00
+ (offer.getParent().getSecurity() != null ? offer.getParent().getSecurity().getNamespace() : "") + "]");
2017-07-27 15:18:18 +02:00
for (IncomingFileOfferListener l : offerListeners) {
2017-07-28 14:02:32 +02:00
l.onIncomingFileOffer(offer);
2017-07-27 15:18:18 +02:00
}
}
public void addIncomingFileRequestListener(IncomingFileRequestListener listener) {
requestListeners.add(listener);
2017-07-27 00:12:42 +02:00
}
2017-07-27 15:18:18 +02:00
public void removeIncomingFileRequestListener(IncomingFileRequestListener listener) {
requestListeners.remove(listener);
}
public void notifyIncomingFileRequestListeners(JingleIncomingFileRequest request) {
for (IncomingFileRequestListener l : requestListeners) {
l.onIncomingFileRequest(request);
2017-07-27 00:12:42 +02:00
}
}
2017-07-22 01:01:50 +02:00
@Override
public String getNamespace() {
2017-07-27 17:35:16 +02:00
return JingleFileTransfer.NAMESPACE;
2017-07-22 01:01:50 +02:00
}
2017-07-27 17:35:16 +02:00
private void notifyTransfer(JingleFileTransfer transfer) {
2017-07-27 15:58:11 +02:00
if (transfer.isOffer()) {
notifyIncomingFileOfferListeners((JingleIncomingFileOffer) transfer);
} else {
notifyIncomingFileRequestListeners((JingleIncomingFileRequest) transfer);
}
2017-07-22 01:01:50 +02:00
}
2017-07-27 16:43:09 +02:00
@Override
public void notifySessionInitiate(JingleSession session) {
JingleContent content = session.getSoleContentOrThrow();
2017-07-27 17:35:16 +02:00
notifyTransfer((JingleFileTransfer) content.getDescription());
2017-07-27 16:43:09 +02:00
}
@Override
2017-07-29 19:31:16 +02:00
public void notifyContentAdd(JingleSession session, JingleContent content) {
2017-07-27 17:35:16 +02:00
notifyTransfer((JingleFileTransfer) content.getDescription());
2017-07-27 16:43:09 +02:00
}
2017-07-22 01:01:50 +02:00
}