1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
package org.apache.xmlrpc.client; |
17 |
|
|
18 |
|
import java.io.IOException; |
19 |
|
import java.io.InputStream; |
20 |
|
import java.io.OutputStream; |
21 |
|
|
22 |
|
import org.apache.commons.httpclient.Credentials; |
23 |
|
import org.apache.commons.httpclient.Header; |
24 |
|
import org.apache.commons.httpclient.HostConfiguration; |
25 |
|
import org.apache.commons.httpclient.HttpClient; |
26 |
|
import org.apache.commons.httpclient.HttpException; |
27 |
|
import org.apache.commons.httpclient.HttpVersion; |
28 |
|
import org.apache.commons.httpclient.URI; |
29 |
|
import org.apache.commons.httpclient.URIException; |
30 |
|
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
31 |
|
import org.apache.commons.httpclient.auth.AuthScope; |
32 |
|
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; |
33 |
|
import org.apache.commons.httpclient.methods.PostMethod; |
34 |
|
import org.apache.xmlrpc.XmlRpcException; |
35 |
|
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig; |
36 |
|
import org.apache.xmlrpc.util.HttpUtil; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
public class XmlRpcCommonsTransport extends XmlRpcHttpTransport { |
43 |
|
private class CommonsConnection { |
44 |
48 |
final HttpClient client = new HttpClient(); |
45 |
|
final PostMethod method; |
46 |
48 |
CommonsConnection(XmlRpcHttpClientConfig pConfig) { |
47 |
48 |
method = new PostMethod(pConfig.getServerURL().toString()); |
48 |
48 |
method.getParams().setVersion(HttpVersion.HTTP_1_1); |
49 |
48 |
} |
50 |
|
} |
51 |
|
|
52 |
22 |
private String userAgent = super.getUserAgent() + " (Jakarta Commons httpclient Transport)"; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public XmlRpcCommonsTransport(XmlRpcClient pClient) { |
58 |
22 |
super(pClient); |
59 |
22 |
} |
60 |
|
|
61 |
48 |
protected String getUserAgent() { return userAgent; } |
62 |
|
|
63 |
|
protected void setRequestHeader(Object pConnection, String pHeader, String pValue) { |
64 |
96 |
PostMethod method = ((CommonsConnection) pConnection).method; |
65 |
96 |
method.setRequestHeader(new Header(pHeader, pValue)); |
66 |
96 |
} |
67 |
|
|
68 |
|
protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig, Object pConnection) { |
69 |
35 |
Header h = ((CommonsConnection) pConnection).method.getResponseHeader( "Content-Encoding" ); |
70 |
35 |
if (h == null) { |
71 |
35 |
return false; |
72 |
|
} else { |
73 |
0 |
return HttpUtil.isUsingGzipEncoding(h.getValue()); |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
|
protected Object newConnection(XmlRpcStreamRequestConfig pConfig) throws XmlRpcClientException { |
78 |
48 |
return new CommonsConnection((XmlRpcHttpClientConfig) pConfig); |
79 |
|
} |
80 |
|
|
81 |
|
protected void closeConnection(Object pConnection) throws XmlRpcClientException { |
82 |
48 |
((CommonsConnection) pConnection).method.releaseConnection(); |
83 |
48 |
} |
84 |
|
|
85 |
|
protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcClientException { |
86 |
0 |
throw new IllegalStateException("Not implemented"); |
87 |
|
} |
88 |
|
|
89 |
48 |
protected boolean isUsingByteArrayOutput(XmlRpcStreamRequestConfig pConfig) { return true; } |
90 |
|
|
91 |
|
protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcException { |
92 |
0 |
throw new IllegalStateException("Not implemented"); |
93 |
|
} |
94 |
|
|
95 |
|
protected void setContentLength(Object pConnection, int pLength) { |
96 |
35 |
} |
97 |
|
|
98 |
|
protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection, byte[] pContents) |
99 |
|
throws XmlRpcException { |
100 |
35 |
XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pConfig; |
101 |
35 |
CommonsConnection conn = (CommonsConnection) pConnection; |
102 |
35 |
PostMethod method = conn.method; |
103 |
35 |
method.setRequestEntity(new ByteArrayRequestEntity(pContents, "text/xml")); |
104 |
|
HostConfiguration hostConfig; |
105 |
|
try { |
106 |
35 |
URI hostURI = new URI(config.getServerURL().toString(), false); |
107 |
35 |
hostConfig = new HostConfiguration(); |
108 |
35 |
hostConfig.setHost(hostURI); |
109 |
0 |
} catch (URIException e) { |
110 |
0 |
throw new XmlRpcClientException("Failed to parse URL: " + config.getServerURL().toString(), e); |
111 |
|
} |
112 |
|
try { |
113 |
35 |
conn.client.executeMethod(hostConfig, method); |
114 |
35 |
return method.getResponseBodyAsStream(); |
115 |
0 |
} catch (HttpException e) { |
116 |
0 |
throw new XmlRpcClientException("Error in HTTP transport: " + e.getMessage(), e); |
117 |
0 |
} catch (IOException e) { |
118 |
0 |
throw new XmlRpcClientException("I/O error in server communication: " + e.getMessage(), e); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
protected void setCredentials(XmlRpcHttpClientConfig pConfig, Object pConnection) throws XmlRpcClientException { |
123 |
48 |
String userName = pConfig.getBasicUserName(); |
124 |
48 |
if (userName != null) { |
125 |
0 |
Credentials creds = new UsernamePasswordCredentials(userName, pConfig.getBasicPassword()); |
126 |
0 |
AuthScope scope = new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME); |
127 |
0 |
((CommonsConnection) pConnection).client.getState().setCredentials(scope, creds); |
128 |
|
} |
129 |
48 |
} |
130 |
|
} |