KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > DefaultXmlRpcTransportFactory


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc;
19
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.apache.xmlrpc.util.HttpUtil;
27
28 /**
29  * Default XML-RPC transport factory, produces HTTP, HTTPS with SSL or TLS based on URI protocol.
30  *
31  * @author <a HREF="mailto:lmeader@ghsinc.com">Larry Meader</a>
32  * @author <a HREF="mailto:cjackson@ghsinc.com">Chris Jackson</a>
33  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
34  * @version $Id: DefaultXmlRpcTransportFactory.java,v 1.6 2005/04/22 10:25:57 hgomez Exp $
35  * @since 1.2
36  */

37 public class DefaultXmlRpcTransportFactory implements XmlRpcTransportFactory
38 {
39     // Default properties for new http transports
40
protected URL JavaDoc url;
41     protected String JavaDoc auth;
42
43     protected static XmlRpcTransportFactory httpsTransportFactory;
44
45     public static final String JavaDoc DEFAULT_HTTPS_PROVIDER = "comnetsun";
46
47     private static Hashtable JavaDoc transports = new Hashtable JavaDoc (1);
48
49     static
50     {
51         // A mapping of short identifiers to the fully qualified class names of
52
// common transport factories. If more mappings are added here,
53
// increase the size of the transports Hashtable used to store them.
54
transports.put("comnetsun", "org.apache.xmlrpc.secure.sunssl.SunSSLTransportFactory");
55     }
56
57     public static void setHTTPSTransport(String JavaDoc transport, Properties JavaDoc properties)
58         throws XmlRpcClientException
59     {
60         httpsTransportFactory = createTransportFactory(transport, properties);
61     }
62
63     public static XmlRpcTransportFactory createTransportFactory(String JavaDoc transport, Properties JavaDoc properties)
64         throws XmlRpcClientException
65     {
66         String JavaDoc transportFactoryClassName = null;
67         Class JavaDoc transportFactoryClass;
68         Constructor JavaDoc transportFactoryConstructor;
69         Object JavaDoc transportFactoryInstance;
70
71         try
72         {
73             transportFactoryClassName = (String JavaDoc) transports.get(transport);
74             if (transportFactoryClassName == null)
75             {
76                 // Identifier lookup failed, assuming we were provided
77
// with the fully qualified class name.
78
transportFactoryClassName = transport;
79             }
80             transportFactoryClass = Class.forName(transportFactoryClassName);
81
82             transportFactoryConstructor = transportFactoryClass.getConstructor(
83                 XmlRpcTransportFactory.CONSTRUCTOR_SIGNATURE);
84             transportFactoryInstance = transportFactoryConstructor.newInstance(
85                 new Object JavaDoc [] { 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 JavaDoc cnfe)
98         {
99             throw new XmlRpcClientException("Transport Factory not found: " +
100                 transportFactoryClassName, cnfe);
101         }
102         catch (NoSuchMethodException JavaDoc nsme)
103         {
104             throw new XmlRpcClientException("Transport Factory constructor not found: " +
105                 transportFactoryClassName +
106                 XmlRpcTransportFactory.CONSTRUCTOR_SIGNATURE_STRING, nsme);
107         }
108         catch (IllegalAccessException JavaDoc iae)
109         {
110             throw new XmlRpcClientException("Unable to access Transport Factory constructor: " +
111                 transportFactoryClassName, iae);
112         }
113         catch (InstantiationException JavaDoc ie)
114         {
115             throw new XmlRpcClientException("Unable to instantiate Transport Factory: " +
116                 transportFactoryClassName, ie);
117         }
118         catch (InvocationTargetException JavaDoc ite)
119         {
120             throw new XmlRpcClientException("Error calling Transport Factory constructor: ",
121                 ite.getTargetException());
122         }
123     }
124   
125     public DefaultXmlRpcTransportFactory(URL JavaDoc url)
126     {
127         this.url = url;
128     }
129     
130     /**
131      * Contructor taking a Base64 encoded Basic Authentication string.
132      *
133      * @deprecated use setBasicAuthentication method instead
134      */

135     public DefaultXmlRpcTransportFactory(URL JavaDoc url, String JavaDoc 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 JavaDoc properties = new Properties JavaDoc();
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     /**
163      * Sets Authentication for this client. This will be sent as Basic
164      * Authentication header to the server as described in
165      * <a HREF="http://www.ietf.org/rfc/rfc2617.txt">
166      * http://www.ietf.org/rfc/rfc2617.txt</a>.
167      */

168     public void setBasicAuthentication(String JavaDoc user, String JavaDoc password)
169     {
170         setProperty(TRANSPORT_AUTH, HttpUtil.encodeBasicAuthentication(user, password));
171     }
172
173     public void setProperty(String JavaDoc propertyName, Object JavaDoc value)
174     {
175         if (httpsTransportFactory != null)
176         {
177             httpsTransportFactory.setProperty(propertyName, value);
178         }
179         if (TRANSPORT_AUTH.equals(propertyName))
180         {
181             auth = (String JavaDoc) value;
182         }
183         else if (TRANSPORT_URL.equals(propertyName))
184         {
185             url = (URL JavaDoc) value;
186         }
187     }
188 }
189
Popular Tags