1 16 17 18 package org.apache.xmlrpc; 19 20 import java.lang.reflect.Constructor ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.net.URL ; 23 import java.util.Hashtable ; 24 import java.util.Properties ; 25 26 import org.apache.xmlrpc.util.HttpUtil; 27 28 37 public class DefaultXmlRpcTransportFactory implements XmlRpcTransportFactory 38 { 39 protected URL url; 41 protected String auth; 42 43 protected static XmlRpcTransportFactory httpsTransportFactory; 44 45 public static final String DEFAULT_HTTPS_PROVIDER = "comnetsun"; 46 47 private static Hashtable transports = new Hashtable (1); 48 49 static 50 { 51 transports.put("comnetsun", "org.apache.xmlrpc.secure.sunssl.SunSSLTransportFactory"); 55 } 56 57 public static void setHTTPSTransport(String transport, Properties properties) 58 throws XmlRpcClientException 59 { 60 httpsTransportFactory = createTransportFactory(transport, properties); 61 } 62 63 public static XmlRpcTransportFactory createTransportFactory(String transport, Properties properties) 64 throws XmlRpcClientException 65 { 66 String transportFactoryClassName = null; 67 Class transportFactoryClass; 68 Constructor transportFactoryConstructor; 69 Object transportFactoryInstance; 70 71 try 72 { 73 transportFactoryClassName = (String ) transports.get(transport); 74 if (transportFactoryClassName == null) 75 { 76 transportFactoryClassName = transport; 79 } 80 transportFactoryClass = Class.forName(transportFactoryClassName); 81 82 transportFactoryConstructor = transportFactoryClass.getConstructor( 83 XmlRpcTransportFactory.CONSTRUCTOR_SIGNATURE); 84 transportFactoryInstance = transportFactoryConstructor.newInstance( 85 new Object [] { properties }); 86 if (transportFactoryInstance instanceof XmlRpcTransportFactory) 87 { 88 return (XmlRpcTransportFactory) transportFactoryInstance; 89 } 90 else 91 { 92 throw new XmlRpcClientException("Class '" + 93 transportFactoryClass.getName() + "' does not implement '" + 94 XmlRpcTransportFactory.class.getName() + "'", null); 95 } 96 } 97 catch (ClassNotFoundException cnfe) 98 { 99 throw new XmlRpcClientException("Transport Factory not found: " + 100 transportFactoryClassName, cnfe); 101 } 102 catch (NoSuchMethodException nsme) 103 { 104 throw new XmlRpcClientException("Transport Factory constructor not found: " + 105 transportFactoryClassName + 106 XmlRpcTransportFactory.CONSTRUCTOR_SIGNATURE_STRING, nsme); 107 } 108 catch (IllegalAccessException iae) 109 { 110 throw new XmlRpcClientException("Unable to access Transport Factory constructor: " + 111 transportFactoryClassName, iae); 112 } 113 catch (InstantiationException ie) 114 { 115 throw new XmlRpcClientException("Unable to instantiate Transport Factory: " + 116 transportFactoryClassName, ie); 117 } 118 catch (InvocationTargetException ite) 119 { 120 throw new XmlRpcClientException("Error calling Transport Factory constructor: ", 121 ite.getTargetException()); 122 } 123 } 124 125 public DefaultXmlRpcTransportFactory(URL url) 126 { 127 this.url = url; 128 } 129 130 135 public DefaultXmlRpcTransportFactory(URL url, String auth) 136 { 137 this(url); 138 this.auth = auth; 139 } 140 141 public XmlRpcTransport createTransport() 142 throws XmlRpcClientException 143 { 144 if ("https".equals(url.getProtocol())) 145 { 146 if (httpsTransportFactory == null) 147 { 148 Properties properties = new Properties (); 149 150 properties.put(XmlRpcTransportFactory.TRANSPORT_URL, url); 151 properties.put(XmlRpcTransportFactory.TRANSPORT_AUTH, auth); 152 153 setHTTPSTransport(DEFAULT_HTTPS_PROVIDER, properties); 154 } 155 156 return httpsTransportFactory.createTransport(); 157 } 158 159 return new DefaultXmlRpcTransport(url); 160 } 161 162 168 public void setBasicAuthentication(String user, String password) 169 { 170 setProperty(TRANSPORT_AUTH, HttpUtil.encodeBasicAuthentication(user, password)); 171 } 172 173 public void setProperty(String propertyName, Object value) 174 { 175 if (httpsTransportFactory != null) 176 { 177 httpsTransportFactory.setProperty(propertyName, value); 178 } 179 if (TRANSPORT_AUTH.equals(propertyName)) 180 { 181 auth = (String ) value; 182 } 183 else if (TRANSPORT_URL.equals(propertyName)) 184 { 185 url = (URL ) value; 186 } 187 } 188 } 189 | Popular Tags |