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.SmackException;
023import org.jivesoftware.smack.XMPPConnection;
024import org.jivesoftware.smack.XMPPException;
025import org.jivesoftware.smack.XMPPException.XMPPErrorException;
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.packet.Stanza;
028import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
029import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
030import org.jivesoftware.smackx.si.packet.StreamInitiation;
031import org.jxmpp.jid.Jid;
032
033
034/**
035 * The fault tolerant negotiator takes two stream negotiators, the primary and the secondary
036 * negotiator. If the primary negotiator fails during the stream negotiaton process, the second
037 * negotiator is used.
038 */
039public class FaultTolerantNegotiator extends StreamNegotiator {
040
041    private final StreamNegotiator primaryNegotiator;
042    private final StreamNegotiator secondaryNegotiator;
043    private final XMPPConnection connection;
044
045    public FaultTolerantNegotiator(XMPPConnection connection, StreamNegotiator primary,
046            StreamNegotiator secondary) {
047        this.primaryNegotiator = primary;
048        this.secondaryNegotiator = secondary;
049        this.connection = connection;
050    }
051
052    @Override
053    public void newStreamInitiation(Jid from, String streamID) {
054        primaryNegotiator.newStreamInitiation(from, streamID);
055        secondaryNegotiator.newStreamInitiation(from, streamID);
056    }
057
058    @Override
059    InputStream negotiateIncomingStream(Stanza streamInitiation) {
060        throw new UnsupportedOperationException("Negotiation only handled by create incoming " +
061                "stream method.");
062    }
063
064    @Override
065    public InputStream createIncomingStream(final StreamInitiation initiation) throws SmackException, XMPPErrorException, InterruptedException {
066        // This could be either an xep47 ibb 'open' iq or an xep65 streamhost iq
067        IQ initationSet = initiateIncomingStream(connection, initiation);
068
069        StreamNegotiator streamNegotiator = determineNegotiator(initationSet);
070
071        return streamNegotiator.negotiateIncomingStream(initationSet);
072    }
073
074    private StreamNegotiator determineNegotiator(Stanza streamInitiation) {
075        if (streamInitiation instanceof Bytestream) {
076            return primaryNegotiator;
077        } else if (streamInitiation instanceof Open){
078            return secondaryNegotiator;
079        } else {
080            throw new IllegalStateException("Unknown stream initation type");
081        }
082    }
083
084    @Override
085    public OutputStream createOutgoingStream(String streamID, Jid initiator, Jid target)
086                    throws SmackException, XMPPException, InterruptedException {
087        OutputStream stream;
088        try {
089            stream = primaryNegotiator.createOutgoingStream(streamID, initiator, target);
090        }
091        catch (Exception ex) {
092            stream = secondaryNegotiator.createOutgoingStream(streamID, initiator, target);
093        }
094
095        return stream;
096    }
097
098    @Override
099    public String[] getNamespaces() {
100        String[] primary = primaryNegotiator.getNamespaces();
101        String[] secondary = secondaryNegotiator.getNamespaces();
102
103        String[] namespaces = new String[primary.length + secondary.length];
104        System.arraycopy(primary, 0, namespaces, 0, primary.length);
105        System.arraycopy(secondary, 0, namespaces, primary.length, secondary.length);
106
107        return namespaces;
108    }
109
110}