2017-06-18 16:47:49 +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.jingle_filetransfer;
|
|
|
|
|
2017-06-19 14:44:35 +02:00
|
|
|
import java.io.File;
|
2017-06-19 15:26:10 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2017-06-19 14:44:35 +02:00
|
|
|
|
2017-06-19 15:26:10 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2017-06-19 14:44:35 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2017-06-19 15:26:10 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2017-06-19 14:44:35 +02:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2017-06-19 15:26:10 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
|
2017-06-18 16:47:49 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.Role;
|
2017-06-19 14:44:35 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
2017-06-19 15:26:10 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContent;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
|
2017-06-19 14:44:35 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback;
|
2017-06-19 19:22:59 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
|
2017-06-18 16:47:49 +02:00
|
|
|
|
|
|
|
import org.jxmpp.jid.FullJid;
|
|
|
|
|
|
|
|
/**
|
2017-06-19 17:55:04 +02:00
|
|
|
* We are the responder and we are the recipient.
|
2017-06-18 16:47:49 +02:00
|
|
|
*/
|
2017-06-19 17:55:04 +02:00
|
|
|
public class IncomingJingleFileOffer extends JingleFileTransferSession implements IncomingFileOfferCallback {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(IncomingJingleFileOffer.class.getName());
|
2017-06-21 14:11:42 +02:00
|
|
|
private Jingle pendingSessionInitiate = null;
|
|
|
|
|
|
|
|
public enum State {
|
|
|
|
fresh,
|
|
|
|
pending,
|
|
|
|
sent_transport_replace,
|
|
|
|
active,
|
|
|
|
terminated,
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
private State state;
|
2017-06-19 15:26:10 +02:00
|
|
|
|
2017-06-19 17:55:04 +02:00
|
|
|
public IncomingJingleFileOffer(XMPPConnection connection, FullJid initiator, String sid) {
|
|
|
|
super(connection, initiator, connection.getUser().asFullJidOrThrow(), Role.responder, sid, Type.offer);
|
2017-06-19 14:44:35 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 17:55:04 +02:00
|
|
|
public IncomingJingleFileOffer(XMPPConnection connection, Jingle request) {
|
|
|
|
this(connection, request.getInitiator(), request.getSid());
|
2017-06-19 14:44:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-06-21 14:11:42 +02:00
|
|
|
public IQ handleSessionInitiate(Jingle initiate) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
|
|
|
|
JingleTransportMethodManager tm = JingleTransportMethodManager.getInstanceFor(connection);
|
2017-06-19 14:44:35 +02:00
|
|
|
|
2017-06-21 14:11:42 +02:00
|
|
|
if (state != State.fresh) {
|
2017-06-19 17:55:04 +02:00
|
|
|
//Out of order (initiate after accept)
|
2017-06-19 14:44:35 +02:00
|
|
|
return jutil.createErrorOutOfOrder(initiate);
|
|
|
|
}
|
|
|
|
|
2017-06-19 19:22:59 +02:00
|
|
|
JingleContent content = initiate.getContents().get(0);
|
|
|
|
this.creator = content.getCreator();
|
|
|
|
this.file = (JingleFileTransfer) content.getDescription();
|
|
|
|
this.name = content.getName();
|
2017-06-21 14:11:42 +02:00
|
|
|
this.transport = content.getJingleTransport();
|
|
|
|
this.transportManager = tm.getTransportManager(initiate);
|
|
|
|
|
|
|
|
if (transportManager == null) {
|
|
|
|
//Fallback
|
|
|
|
pendingSessionInitiate = initiate;
|
|
|
|
transportManager = tm.getBestAvailableTransportManager();
|
|
|
|
|
|
|
|
if (transportManager == null) {
|
|
|
|
jutil.sendSessionTerminateUnsupportedTransports(initiate.getInitiator(), initiate.getSid());
|
|
|
|
state = State.terminated;
|
|
|
|
return jutil.createAck(initiate);
|
|
|
|
}
|
|
|
|
|
2017-06-21 15:42:00 +02:00
|
|
|
transport = transportManager.createTransport(getInitiator());
|
2017-06-21 14:11:42 +02:00
|
|
|
jutil.sendTransportReplace(initiate.getFrom().asFullJidOrThrow(), initiate.getInitiator(),
|
|
|
|
initiate.getSid(), creator, name, transport);
|
|
|
|
state = State.sent_transport_replace;
|
|
|
|
return jutil.createAck(initiate);
|
|
|
|
}
|
2017-06-19 19:22:59 +02:00
|
|
|
|
2017-06-19 15:26:10 +02:00
|
|
|
JingleFileTransferManager.getInstanceFor(connection).notifyIncomingFileOffer(initiate, this);
|
2017-06-21 14:11:42 +02:00
|
|
|
state = State.pending;
|
2017-06-19 15:26:10 +02:00
|
|
|
return jutil.createAck(initiate);
|
|
|
|
}
|
2017-06-19 14:44:35 +02:00
|
|
|
|
2017-06-19 15:26:10 +02:00
|
|
|
@Override
|
2017-06-21 14:11:42 +02:00
|
|
|
public IQ handleTransportAccept(Jingle transportAccept) {
|
|
|
|
|
|
|
|
if (state != State.sent_transport_replace) {
|
|
|
|
return jutil.createErrorOutOfOrder(transportAccept);
|
|
|
|
}
|
2017-06-19 14:44:35 +02:00
|
|
|
|
2017-06-21 14:11:42 +02:00
|
|
|
JingleFileTransferManager.getInstanceFor(connection).notifyIncomingFileOffer(pendingSessionInitiate, this);
|
|
|
|
transport = transportAccept.getContents().get(0).getJingleTransport();
|
|
|
|
state = State.pending;
|
|
|
|
return jutil.createAck(transportAccept);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void acceptIncomingFileOffer(Jingle request, File target) {
|
2017-06-19 14:44:35 +02:00
|
|
|
|
2017-06-19 15:26:10 +02:00
|
|
|
if (transportManager == null) {
|
|
|
|
//Unsupported transport
|
|
|
|
LOGGER.log(Level.WARNING, "Unsupported Transport method.");
|
|
|
|
try {
|
2017-06-21 14:11:42 +02:00
|
|
|
jutil.sendSessionTerminateUnsupportedTransports(request.getFrom().asFullJidOrThrow(), sid);
|
2017-06-19 15:26:10 +02:00
|
|
|
} catch (InterruptedException | SmackException.NoResponseException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Could not send session-terminate: " + e, e);
|
2017-06-19 14:44:35 +02:00
|
|
|
}
|
2017-06-19 15:26:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JingleContentTransport transport = transportManager.createTransport(request);
|
|
|
|
try {
|
2017-06-21 14:11:42 +02:00
|
|
|
jutil.sendSessionAccept(getInitiator(), sid, creator, name, JingleContent.Senders.initiator, file, transport);
|
|
|
|
state = State.active;
|
2017-06-19 15:26:10 +02:00
|
|
|
} 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) {
|
2017-06-21 14:11:42 +02:00
|
|
|
state = State.terminated;
|
2017-06-19 15:26:10 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 16:47:49 +02:00
|
|
|
}
|