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