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;
032import org.jivesoftware.smack.util.EventManger.Callback;
033
034import org.jivesoftware.smackx.si.packet.StreamInitiation;
035import org.jivesoftware.smackx.xdata.FormField;
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 namespaces            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[] namespaces) {
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 form = new DataForm(DataForm.Type.submit);
084        FormField field = new FormField(
085                FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
086        for (String namespace : namespaces) {
087            field.addValue(namespace);
088        }
089        form.addField(field);
090
091        response.setFeatureNegotiationForm(form);
092        return response;
093    }
094
095    protected final IQ initiateIncomingStream(final XMPPConnection connection, StreamInitiation initiation)
096                    throws NoResponseException, XMPPErrorException, NotConnectedException {
097        final StreamInitiation response = createInitiationAccept(initiation,
098                getNamespaces());
099
100        newStreamInitiation(initiation.getFrom(), initiation.getSessionID());
101
102        final String eventKey = initiation.getFrom().toString() + '\t' + initiation.getSessionID();
103        IQ streamMethodInitiation;
104        try {
105            streamMethodInitiation = initationSetEvents.performActionAndWaitForEvent(eventKey, connection.getReplyTimeout(), new Callback<NotConnectedException>() {
106                @Override
107                public void action() throws NotConnectedException {
108                    try {
109                        connection.sendStanza(response);
110                    }
111                    catch (InterruptedException e) {
112                        // Ignore
113                    }
114                }
115            });
116        }
117        catch (InterruptedException e) {
118            // TODO remove this try/catch once merged into 4.2's master branch
119            throw new IllegalStateException(e);
120        }
121
122        if (streamMethodInitiation == null) {
123            throw NoResponseException.newWith(connection, "stream initiation");
124        }
125        XMPPErrorException.ifHasErrorThenThrow(streamMethodInitiation);
126        return streamMethodInitiation;
127    }
128
129    /**
130     * Signal that a new stream initiation arrived. The negotiator may needs to prepare for it.
131     *
132     * @param from     The initiator of the file transfer.
133     * @param streamID The stream ID related to the transfer.
134     */
135    protected abstract void newStreamInitiation(Jid from, String streamID);
136
137
138    abstract InputStream negotiateIncomingStream(Stanza streamInitiation) throws XMPPErrorException,
139            InterruptedException, SmackException;
140
141    /**
142     * This method handles the file stream download negotiation process. The
143     * appropriate stream negotiator's initiate incoming stream is called after
144     * an appropriate file transfer method is selected. The manager will respond
145     * to the initiator with the selected means of transfer, then it will handle
146     * any negotiation specific to the particular transfer method. This method
147     * returns the InputStream, ready to transfer the file.
148     *
149     * @param initiation The initiation that triggered this download.
150     * @return After the negotiation process is complete, the InputStream to
151     *         write a file to is returned.
152     * @throws XMPPErrorException If an error occurs during this process an XMPPException is
153     *                       thrown.
154     * @throws InterruptedException If thread is interrupted.
155     * @throws SmackException
156     */
157    public abstract InputStream createIncomingStream(StreamInitiation initiation)
158            throws XMPPErrorException, InterruptedException, SmackException;
159
160    /**
161     * This method handles the file upload stream negotiation process. The
162     * particular stream negotiator is determined during the file transfer
163     * negotiation process. This method returns the OutputStream to transmit the
164     * file to the remote user.
165     *
166     * @param streamID  The streamID that uniquely identifies the file transfer.
167     * @param initiator The fully-qualified JID of the initiator of the file transfer.
168     * @param target    The fully-qualified JID of the target or receiver of the file
169     *                  transfer.
170     * @return The negotiated stream ready for data.
171     * @throws SmackException
172     * @throws XMPPException
173     * @throws InterruptedException
174     */
175    public abstract OutputStream createOutgoingStream(String streamID,
176            Jid initiator, Jid target) throws SmackException, XMPPException, InterruptedException;
177
178    /**
179     * Returns the XMPP namespace reserved for this particular type of file
180     * transfer.
181     *
182     * @return Returns the XMPP namespace reserved for this particular type of
183     *         file transfer.
184     */
185    public abstract String[] getNamespaces();
186
187    public static void signal(String eventKey, IQ eventValue) {
188        initationSetEvents.signalEvent(eventKey, eventValue);
189    }
190}