KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.zip.GZIPInputStream JavaDoc;
26 import java.util.zip.GZIPOutputStream JavaDoc;
27
28 import org.apache.commons.httpclient.Credentials;
29 import org.apache.commons.httpclient.Header;
30 import org.apache.commons.httpclient.HostConfiguration;
31 import org.apache.commons.httpclient.HttpClient;
32 import org.apache.commons.httpclient.URI;
33 import org.apache.commons.httpclient.UsernamePasswordCredentials;
34 import org.apache.commons.httpclient.methods.PostMethod;
35
36 /**
37  * Implementor of the XmlRpcTransport interface using the Apache
38  * Commons HttpClient library v2.0 available at
39  * http://jakarta.apache.org/commons/httpclient/
40  *
41  * Note: <b>Currently this transport is not thread safe</b>
42  *
43  * @author <a HREF="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
44  * @version $Id: CommonsXmlRpcTransport.java,v 1.7 2005/04/28 09:05:06 dlr Exp $
45  * @since 2.0
46  */

47 public class CommonsXmlRpcTransport implements XmlRpcTransport
48 {
49     
50     protected PostMethod method;
51
52     /** Creates a new instance of CommonsXmlRpcTransport */
53     public CommonsXmlRpcTransport(URL JavaDoc url, HttpClient client)
54     {
55         this.url = url;
56         if (client == null)
57         {
58             HttpClient newClient = new HttpClient();
59             this.client = newClient;
60         }
61         else
62         {
63             this.client = client;
64         }
65     }
66     
67     public CommonsXmlRpcTransport(URL JavaDoc url)
68     {
69         this(url, null);
70     }
71     
72     private URL JavaDoc url;
73     private HttpClient client;
74     private final Header userAgentHeader = new Header("User-Agent", XmlRpc.version);
75     private boolean http11 = false; // defaults to HTTP 1.0
76
private boolean gzip = false;
77     private boolean rgzip = false;
78     private Credentials creds;
79     
80     public InputStream JavaDoc sendXmlRpc(byte[] request) throws IOException JavaDoc, XmlRpcClientException
81     {
82         method = new PostMethod(url.toString());
83         method.setHttp11(http11);
84         method.setRequestHeader(new Header("Content-Type", "text/xml"));
85         
86         if (rgzip)
87             method.setRequestHeader(new Header("Content-Encoding", "gzip"));
88         
89         if (gzip)
90             method.setRequestHeader(new Header("Accept-Encoding", "gzip"));
91                 
92         method.setRequestHeader(userAgentHeader);
93
94         if (rgzip)
95         {
96             ByteArrayOutputStream JavaDoc lBo = new ByteArrayOutputStream JavaDoc();
97             GZIPOutputStream JavaDoc lGzo = new GZIPOutputStream JavaDoc(lBo);
98             lGzo.write(request);
99             lGzo.finish();
100             lGzo.close();
101             byte[] lArray = lBo.toByteArray();
102             method.setRequestBody(new ByteArrayInputStream JavaDoc(lArray));
103             method.setRequestContentLength(-1);
104         }
105         else
106             method.setRequestBody(new ByteArrayInputStream JavaDoc(request));
107         
108         URI hostURI = new URI(url.toString());
109         HostConfiguration hostConfig = new HostConfiguration();
110         hostConfig.setHost(hostURI);
111         client.executeMethod(hostConfig, method);
112
113         boolean lgzipo = false;
114         
115         Header lHeader = method.getResponseHeader( "Content-Encoding" );
116         if ( lHeader != null ) {
117             String JavaDoc lValue = lHeader.getValue();
118             if ( lValue != null )
119                 lgzipo = (lValue.indexOf( "gzip" ) >= 0);
120         }
121
122         if (lgzipo)
123             return( new GZIPInputStream JavaDoc( method.getResponseBodyAsStream() ) );
124         else
125             return method.getResponseBodyAsStream();
126     }
127     
128     /**
129      * Make use of HTTP 1.1
130      *
131      * @param http11 HTTP 1.1 will be used if http11 is true
132      */

133     public void setHttp11(boolean http11)
134     {
135         this.http11 = http11;
136     }
137     
138     /**
139      * Transport make use of the 'Accept-Encoding: gzip', so compliant HTTP servers
140      * could return HTTP reply compressed with gzip
141      *
142      * @param gzip Gzip compression will be used if gzip is true
143      */

144     public void setGzip(boolean gzip) {
145         this.gzip = gzip;
146     }
147     
148     /**
149      * Transport make use of the 'Content-Encoding: gzip' and send HTTP request
150      * compressed with gzip : works only with some compliant HTTP servers like Apache 2.x
151      * using SetInputFilter DEFLATE.
152      *
153      * @param gzip Compress request with gzip if gzip is true
154      */

155     public void setRGzip(boolean gzip) {
156         this.rgzip = gzip;
157     }
158     
159     /**
160      * Set the UserAgent for this client
161      *
162      * @param userAgent
163      */

164     public void setUserAgent(String JavaDoc userAgent)
165     {
166         userAgentHeader.setValue(userAgent);
167     }
168
169     /**
170      * Sets Authentication for this client, very basic for now user/password
171      *
172      * @param user
173      * @param password
174      */

175     public void setBasicAuthentication(String JavaDoc user, String JavaDoc password)
176     {
177         creds = new UsernamePasswordCredentials(user, password);
178         client.getState().setCredentials(null, null, creds);
179     }
180
181     /**
182      * Releases connection resources.
183      *
184      * @exception XmlRpcClientException
185      */

186     public void endClientRequest()
187         throws XmlRpcClientException
188     {
189         method.releaseConnection();
190     }
191 }
192
Popular Tags