001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.filetransfer;
018
019import java.io.InputStream;
020import java.io.OutputStream;
021
022import org.jivesoftware.smack.Manager;
023import org.jivesoftware.smack.SmackException;
024import org.jivesoftware.smack.SmackException.NoResponseException;
025import org.jivesoftware.smack.SmackException.NotConnectedException;
026import org.jivesoftware.smack.XMPPConnection;
027import org.jivesoftware.smack.XMPPException;
028import org.jivesoftware.smack.XMPPException.XMPPErrorException;
029import org.jivesoftware.smack.packet.IQ;
030import org.jivesoftware.smack.packet.Stanza;
031import org.jivesoftware.smack.util.EventManger;
032
033import org.jivesoftware.smackx.si.packet.StreamInitiation;
034import org.jivesoftware.smackx.xdata.FormField;
035import org.jivesoftware.smackx.xdata.ListSingleFormField;
036import org.jivesoftware.smackx.xdata.packet.DataForm;
037
038import org.jxmpp.jid.Jid;
039
040/**
041 * After the file transfer negotiation process is completed according to
042 * XEP-0096, the negotiation process is passed off to a particular stream
043 * negotiator. The stream negotiator will then negotiate the chosen stream and
044 * return the stream to transfer the file.
045 *
046 * @author Alexander Wenckus
047 */
048public abstract class StreamNegotiator extends Manager {
049
050    protected StreamNegotiator(XMPPConnection connection) {
051        super(connection);
052    }
053
054    /**
055     * A event manager for stream initiation requests send to us.
056     * <p>
057     * Those are typical XEP-45 Open or XEP-65 Bytestream IQ requests. The even key is in the format
058     * "initiationFrom + '\t' + streamId"
059     * </p>
060     */
061    // TODO This field currently being static is considered a quick hack. Ideally this should take
062    // the local connection into account, for example by changing the key to
063    // "localJid + '\t' + initiationFrom + '\t' + streamId" or making the field non-static (but then
064    // you need to provide access to the InitiationListeners, which could get tricky)
065    protected static final EventManger<String, IQ, SmackException.NotConnectedException> initationSetEvents = new EventManger<>();
066
067    /**
068     * Creates the initiation acceptance stanza to forward to the stream
069     * initiator.
070     *
071     * @param streamInitiationOffer The offer from the stream initiator to connect for a stream.
072     * @param namespace            The namespace that relates to the accepted means of transfer.
073     * @return The response to be forwarded to the initiator.
074     */
075    protected static StreamInitiation createInitiationAccept(
076            StreamInitiation streamInitiationOffer, String namespace) {
077        StreamInitiation response = new StreamInitiation();
078        response.setTo(streamInitiationOffer.getFrom());
079        response.setFrom(streamInitiationOffer.getTo());
080        response.setType(IQ.Type.result);
081        response.setStanzaId(streamInitiationOffer.getStanzaId());
082
083        DataForm.Builder form = DataForm.builder();
084        ListSingleFormField.Builder field = FormField.listSingleBuilder(
085                FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
086        field.setValue(namespace);
087        form.addField(field.build());
088
089        response.setFeatureNegotiationForm(form.build());
090        return response;
091    }
092
093    protected final IQ initiateIncomingStream(final XMPPConnection connection, StreamInitiation initiation)
094                    throws NoResponseException, XMPPErrorException, NotConnectedException {
095        final StreamInitiation response = createInitiationAccept(initiation,
096                getNamespace());
097
098        newStreamInitiation(initiation.getFrom(), initiation.getSessionID());
099
100        final String eventKey = initiation.getFrom().toString() + '\t' + initiation.getSessionID();
101        IQ streamMethodInitiation;
102        try {
103            streamMethodInitiation = initationSetEvents.performActionAndWaitForEvent(eventKey, connection.getReplyTimeout(), new EventManger.Callback<NotConnectedException>() {
104                @Override
105                public void action() throws NotConnectedException {
106                    try {
107                        connection.sendStanza(response);
108                    }
109                    catch (InterruptedException e) {
110                        // Ignore
111                    }
112                }
113            });
114        }
115        catch (InterruptedException e) {
116            // TODO remove this try/catch once merged into 4.2's master branch
117            throw new IllegalStateException(e);
118        }
119
120        if (streamMethodInitiation == null) {
121            throw NoResponseException.newWith(connection, "stream initiation");
122        }
123        XMPPErrorException.ifHasErrorThenThrow(streamMethodInitiation);
124        return streamMethodInitiation;
125    }
126
127    /**
128     * Signal that a new stream initiation arrived. The negotiator may needs to prepare for it.
129     *
130     * @param from     The initiator of the file transfer.
131     * @param streamID The stream ID related to the transfer.
132     */
133    protected abstract void newStreamInitiation(Jid from, String streamID);
134
135
136    abstract InputStream negotiateIncomingStream(Stanza streamInitiation) throws XMPPErrorException,
137            InterruptedException, SmackException;
138
139    /**
140     * This method handles the file stream download negotiation process. The
141     * appropriate stream negotiator's initiate incoming stream is called after
142     * an appropriate file transfer method is selected. The manager will respond
143     * to the initiator with the selected means of transfer, then it will handle
144     * any negotiation specific to the particular transfer method. This method
145     * returns the InputStream, ready to transfer the file.
146     *
147     * @param initiation The initiation that triggered this download.
148     * @return After the negotiation process is complete, the InputStream to
149     *         write a file to is returned.
150     * @throws XMPPErrorException If an error occurs during this process an XMPPException is
151     *                       thrown.
152     * @throws InterruptedException If thread is interrupted.
153     * @throws SmackException if Smack detected an exceptional situation.
154     */
155    public abstract InputStream createIncomingStream(StreamInitiation initiation)
156            throws XMPPErrorException, InterruptedException, SmackException;
157
158    /**
159     * This method handles the file upload stream negotiation process. The
160     * particular stream negotiator is determined during the file transfer
161     * negotiation process. This method returns the OutputStream to transmit the
162     * file to the remote user.
163     *
164     * @param streamID  The streamID that uniquely identifies the file transfer.
165     * @param initiator The fully-qualified JID of the initiator of the file transfer.
166     * @param target    The fully-qualified JID of the target or receiver of the file
167     *                  transfer.
168     * @return The negotiated stream ready for data.
169     * @throws SmackException if Smack detected an exceptional situation.
170     * @throws XMPPException if an XMPP protocol error was received.
171     * @throws InterruptedException if the calling thread was interrupted.
172     */
173    public abstract OutputStream createOutgoingStream(String streamID,
174            Jid initiator, Jid target) throws SmackException, XMPPException, InterruptedException;
175
176    /**
177     * Returns the XMPP namespace reserved for this particular type of file
178     * transfer.
179     *
180     * @return Returns the XMPP namespace reserved for this particular type of
181     *         file transfer.
182     */
183    public abstract String getNamespace();
184
185    public static void signal(String eventKey, IQ eventValue) {
186        initationSetEvents.signalEvent(eventKey, eventValue);
187    }
188}