Add SendFileHandler

This commit is contained in:
vanitasvitae 2017-07-06 00:10:04 +02:00
parent a3bb7e3f59
commit aae2c7304d
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
4 changed files with 128 additions and 2 deletions

View File

@ -41,6 +41,7 @@ import org.jivesoftware.smackx.jingle.provider.JingleContentProviderManager;
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
import org.jivesoftware.smackx.jingle_filetransfer.handler.SendFileHandler;
import org.jivesoftware.smackx.jingle_filetransfer.listener.JingleFileTransferOfferListener;
import org.jivesoftware.smackx.jingle_filetransfer.provider.JingleFileTransferProvider;
@ -75,12 +76,14 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
return manager;
}
public void sendFile(FullJid recipient, File file)
public SendFileHandler sendFile(FullJid recipient, File file)
throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException {
OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient);
JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(recipient, offer.getSessionId(), offer);
SendFileHandler handler = new SendFileHandler(offer);
offer.send(file);
return handler;
}
public SmackFuture<?> asyncSendFile(FullJid recipient, File file) {

View File

@ -0,0 +1,99 @@
/**
*
* 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_filetransfer.handler;
import java.util.HashSet;
import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferSession;
/**
* Handler that provides some control over the JingleFileOffer session.
*/
public class SendFileHandler {
private final JingleFileTransferSession session;
private final HashSet<FinishedListener> finishedListeners = new HashSet<>();
private final HashSet<AcceptedListener> acceptedListeners = new HashSet<>();
public SendFileHandler(JingleFileTransferSession session) {
this.session = session;
}
/**
* Cancels the current file transfer.
*/
public void cancel() {
}
/**
* Returns true, if the file transfer is finished.
* @return true if transfer finished.
*/
public boolean isFinished() {
return false;
}
/**
* Add a new FinishedListener.
* @param listener listener
*/
public void addFinishedListener(FinishedListener listener) {
finishedListeners.add(listener);
}
/**
* Add a new AcceptedListener.
* @param listener listener
*/
public void addAcceptedListener(AcceptedListener listener) {
acceptedListeners.add(listener);
}
/**
* Notify all registered FinishedListeners that the file transfer has finished.
*/
void notifyFinishedListeners() {
for (FinishedListener f : finishedListeners) {
f.onFinished();
}
}
/**
* Notify all registered AcceptedListeners that the file transfer session has been accepted by the remote user.
*/
void notifyAcceptedListeners() {
for (AcceptedListener a : acceptedListeners) {
a.onAccepted();
}
}
/**
* A FinishedListener will be notified by the SendFileHandler when the corresponding file transfer is finished.
*/
public interface FinishedListener {
void onFinished();
}
/**
* An AcceptedListener will be notified by the SendFileHandler when the corresponding pending session has been
* accepted by the remote user.
*/
public interface AcceptedListener {
void onAccepted();
}
}

View File

@ -0,0 +1,22 @@
/**
*
* 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.
*/
/**
* Smack's API for <a href="https://xmpp.org/extensions/xep-0234.html">XEP-0234: Jingle File Transfer</a>.
* Handlers.
*/
package org.jivesoftware.smackx.jingle_filetransfer.handler;

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
@ -171,7 +172,8 @@ public abstract class JingleSession implements JingleSessionHandler {
return IQ.createResultIQ(jingle);
}
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
return null; //TODO:
LOGGER.log(Level.SEVERE, "Caught an Exception: ", e);
return null; //TODO: Handle better?
}
}