001/** 002 * 003 * Copyright 2017 Paul Schaub 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.jingle; 018 019import java.util.HashMap; 020import java.util.Iterator; 021import java.util.Set; 022import java.util.WeakHashMap; 023 024import org.jivesoftware.smack.Manager; 025import org.jivesoftware.smack.XMPPConnection; 026 027import org.jivesoftware.smackx.jingle.element.Jingle; 028import org.jivesoftware.smackx.jingle.element.JingleContent; 029import org.jivesoftware.smackx.jingle.element.JingleContentTransport; 030import org.jivesoftware.smackx.jingle.transports.JingleTransportManager; 031import org.jivesoftware.smackx.jingle.transports.jingle_ibb.element.JingleIBBTransport; 032import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransport; 033 034/** 035 * Manager where TransportMethods are registered. 036 */ 037public final class JingleTransportMethodManager extends Manager { 038 039 private static final WeakHashMap<XMPPConnection, JingleTransportMethodManager> INSTANCES = new WeakHashMap<>(); 040 041 private final HashMap<String, JingleTransportManager<?>> transportManagers = new HashMap<>(); 042 043 private static final String[] transportPreference = new String[] { 044 JingleS5BTransport.NAMESPACE_V1, 045 JingleIBBTransport.NAMESPACE_V1 046 }; 047 048 private JingleTransportMethodManager(XMPPConnection connection) { 049 super(connection); 050 } 051 052 public static synchronized JingleTransportMethodManager getInstanceFor(XMPPConnection connection) { 053 JingleTransportMethodManager manager = INSTANCES.get(connection); 054 if (manager == null) { 055 manager = new JingleTransportMethodManager(connection); 056 INSTANCES.put(connection, manager); 057 } 058 return manager; 059 } 060 061 public void registerTransportManager(JingleTransportManager<?> manager) { 062 transportManagers.put(manager.getNamespace(), manager); 063 } 064 065 public static JingleTransportManager<?> getTransportManager(XMPPConnection connection, String namespace) { 066 return getInstanceFor(connection).getTransportManager(namespace); 067 } 068 069 public JingleTransportManager<?> getTransportManager(String namespace) { 070 return transportManagers.get(namespace); 071 } 072 073 public static JingleTransportManager<?> getTransportManager(XMPPConnection connection, Jingle request) { 074 return getInstanceFor(connection).getTransportManager(request); 075 } 076 public JingleTransportManager<?> getTransportManager(Jingle request) { 077 078 JingleContent content = request.getContents().get(0); 079 if (content == null) { 080 return null; 081 } 082 083 JingleContentTransport transport = content.getTransport(); 084 if (transport == null) { 085 return null; 086 } 087 088 return getTransportManager(transport.getNamespace()); 089 } 090 091 public static JingleTransportManager<?> getBestAvailableTransportManager(XMPPConnection connection) { 092 return getInstanceFor(connection).getBestAvailableTransportManager(); 093 } 094 095 public JingleTransportManager<?> getBestAvailableTransportManager() { 096 JingleTransportManager<?> tm; 097 for (String ns : transportPreference) { 098 tm = getTransportManager(ns); 099 if (tm != null) { 100 return tm; 101 } 102 } 103 104 Iterator<String> it = transportManagers.keySet().iterator(); 105 if (it.hasNext()) { 106 return getTransportManager(it.next()); 107 } 108 109 return null; 110 } 111 112 public JingleTransportManager<?> getBestAvailableTransportManager(Set<String> except) { 113 JingleTransportManager<?> tm; 114 for (String ns : transportPreference) { 115 tm = getTransportManager(ns); 116 if (tm != null) { 117 if (except.contains(tm.getNamespace())) { 118 continue; 119 } 120 return tm; 121 } 122 } 123 124 for (String ns : transportManagers.keySet()) { 125 if (except.contains(ns)) { 126 continue; 127 } 128 return getTransportManager(ns); 129 } 130 131 return null; 132 } 133}