Send session-accept/decline

This commit is contained in:
vanitasvitae 2017-06-19 15:26:10 +02:00
parent e0a54c19d6
commit 73f9af474e
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
5 changed files with 137 additions and 20 deletions

View File

@ -17,13 +17,22 @@
package org.jivesoftware.smackx.jingle_filetransfer;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
import org.jivesoftware.smackx.jingle.Role;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback;
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileRequestCallback;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jxmpp.jid.FullJid;
@ -31,7 +40,9 @@ import org.jxmpp.jid.FullJid;
/**
* Offer.
*/
public class JingleFileOffer extends JingleFileTransferSession {
public class JingleFileOffer extends JingleFileTransferSession implements IncomingFileOfferCallback, IncomingFileRequestCallback {
private static final Logger LOGGER = Logger.getLogger(JingleFileOffer.class.getName());
public JingleFileOffer(XMPPConnection connection, FullJid initiator, FullJid responder, Role role, String sid) {
super(connection, initiator, responder, role, sid, Type.offer);
@ -49,27 +60,69 @@ public class JingleFileOffer extends JingleFileTransferSession {
@Override
public IQ handleSessionInitiate(Jingle initiate) {
setState(State.pending);
if (role == Role.initiator) {
//TODO: Illegal stanza. Figure out, if we handle it correct.
return jutil.createErrorTieBreak(initiate);
}
if (getState() != State.fresh) {
return jutil.createErrorOutOfOrder(initiate);
}
IncomingFileOfferCallback callback = new IncomingFileOfferCallback() {
@Override
public void accept(JingleFileTransfer file, File target) {
}
@Override
public void decline() {
}
};
JingleFileTransferManager.getInstanceFor(connection).notifyIncomingFileOffer(initiate, callback);
JingleFileTransferManager.getInstanceFor(connection).notifyIncomingFileOffer(initiate, this);
return jutil.createAck(initiate);
}
@Override
public void acceptIncomingFileOffer(Jingle request, File target) {
FullJid recipient = request.getInitiator();
String sid = request.getSid();
JingleContent content = request.getContents().get(0);
//Get TransportManager
JingleTransportManager<?> transportManager = JingleTransportMethodManager.getInstanceFor(connection)
.getTransportManager(request);
if (transportManager == null) {
//Unsupported transport
LOGGER.log(Level.WARNING, "Unsupported Transport method.");
setState(State.terminated);
try {
jutil.sendSessionTerminateUnsupportedTransports(recipient, sid);
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
LOGGER.log(Level.SEVERE, "Could not send session-terminate: " + e, e);
}
return;
}
JingleContentTransport transport = transportManager.createTransport(request);
try {
jutil.sendSessionAccept(recipient, sid, content.getCreator(), content.getName(), content.getSenders(),
content.getDescription(), transport);
setState(State.active);
} catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send session-accept: " + e, e);
}
}
@Override
public void declineIncomingFileOffer(Jingle request) {
setState(State.terminated);
try {
jutil.sendSessionTerminateDecline(request.getInitiator(), request.getSid());
} catch (SmackException.NotConnectedException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send session-terminate: " + e, e);
}
}
@Override
public void acceptIncomingFileRequest(JingleFileTransfer file, File source) {
}
@Override
public void declineIncomingFileRequest() {
}
}

View File

@ -18,14 +18,14 @@ package org.jivesoftware.smackx.jingle_filetransfer.callback;
import java.io.File;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jivesoftware.smackx.jingle.element.Jingle;
/**
* Callback used to accept/decline file offers.
*/
public interface IncomingFileOfferCallback {
void accept(JingleFileTransfer file, File target);
void acceptIncomingFileOffer(Jingle request, File target);
void decline();
void declineIncomingFileOffer(Jingle request);
}

View File

@ -25,7 +25,7 @@ import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
*/
public interface IncomingFileRequestCallback {
void accept(JingleFileTransfer file, File source);
void acceptIncomingFileRequest(JingleFileTransfer file, File source);
void decline();
void declineIncomingFileRequest();
}

View File

@ -0,0 +1,47 @@
package org.jivesoftware.smackx.jingle;
import java.util.HashMap;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.transports.JingleTransportManager;
/**
* Created by vanitas on 19.06.17.
*/
public final class JingleTransportMethodManager extends Manager {
private static final WeakHashMap<XMPPConnection, JingleTransportMethodManager> INSTANCES = new WeakHashMap<>();
private final HashMap<String, JingleTransportManager<?>> transportManagers = new HashMap<>();
private JingleTransportMethodManager(XMPPConnection connection) {
super(connection);
}
public static JingleTransportMethodManager getInstanceFor(XMPPConnection connection) {
JingleTransportMethodManager manager = INSTANCES.get(connection);
if (manager == null) {
manager = new JingleTransportMethodManager(connection);
INSTANCES.put(connection, manager);
}
return manager;
}
public void registerTransportManager(JingleTransportManager<?> manager) {
transportManagers.put(manager.getNamespace(), manager);
}
public JingleTransportManager<?> getTransportManager(String namespace) {
return transportManagers.get(namespace);
}
public JingleTransportManager<?> getTransportManager(Jingle request) {
JingleContent content = request.getContents().get(0);
JingleContentTransport transport = content.getJingleTransports().get(0);
return getTransportManager(transport.getNamespace());
}
}

View File

@ -0,0 +1,17 @@
package org.jivesoftware.smackx.jingle.transports;
import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
/**
* Created by vanitas on 19.06.17.
*/
public abstract class JingleTransportManager<D extends JingleContentTransport> {
public abstract String getNamespace();
public abstract D createTransport();
public abstract D createTransport(Jingle request);
}