1 |
|
package org.apache.xmlrpc.client; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
|
import java.io.InputStream; |
5 |
|
import java.io.OutputStream; |
6 |
|
import java.net.HttpURLConnection; |
7 |
|
import java.net.URLConnection; |
8 |
|
|
9 |
|
import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig; |
10 |
|
import org.apache.xmlrpc.util.HttpUtil; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
public class XmlRpcSunHttpTransport extends XmlRpcHttpTransport { |
17 |
294 |
private final String userAgent = super.getUserAgent() + " (Sun HTTP Transport)"; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
public XmlRpcSunHttpTransport(XmlRpcClient pClient) { |
23 |
294 |
super(pClient); |
24 |
294 |
} |
25 |
|
|
26 |
192 |
protected String getUserAgent() { return userAgent; } |
27 |
|
|
28 |
|
protected void setRequestHeader(Object pConnection, String pHeader, String pValue) { |
29 |
476 |
URLConnection conn = (URLConnection) pConnection; |
30 |
476 |
conn.setRequestProperty(pHeader, pValue); |
31 |
476 |
} |
32 |
|
|
33 |
|
protected Object newConnection(XmlRpcStreamRequestConfig pConfig) throws XmlRpcClientException { |
34 |
192 |
XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pConfig; |
35 |
|
try { |
36 |
192 |
URLConnection result = config.getServerURL().openConnection(); |
37 |
192 |
result.setUseCaches(false); |
38 |
192 |
result.setDoInput(true); |
39 |
192 |
result.setDoOutput(true); |
40 |
192 |
return result; |
41 |
0 |
} catch (IOException e) { |
42 |
0 |
throw new XmlRpcClientException("Failed to create HTTP connection object", e); |
43 |
|
} |
44 |
|
} |
45 |
|
|
46 |
|
protected void closeConnection(Object pConnection) throws XmlRpcClientException { |
47 |
192 |
if (pConnection instanceof HttpURLConnection) { |
48 |
192 |
((HttpURLConnection) pConnection).disconnect(); |
49 |
|
} |
50 |
192 |
} |
51 |
|
|
52 |
|
protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) |
53 |
|
throws XmlRpcClientException { |
54 |
|
try { |
55 |
48 |
return ((URLConnection) pConnection).getOutputStream(); |
56 |
0 |
} catch (IOException e) { |
57 |
0 |
throw new XmlRpcClientException("Failed to obtain output stream to server", e); |
58 |
|
} |
59 |
|
} |
60 |
|
|
61 |
|
protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) |
62 |
|
throws XmlRpcClientException { |
63 |
|
try { |
64 |
140 |
return ((URLConnection) pConnection).getInputStream(); |
65 |
0 |
} catch (IOException e) { |
66 |
0 |
throw new XmlRpcClientException("Failed to obtain input stream from server", e); |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection, |
71 |
|
byte[] pContent) |
72 |
|
throws XmlRpcClientException { |
73 |
92 |
URLConnection conn = (URLConnection) pConnection; |
74 |
|
try { |
75 |
92 |
OutputStream ostream = conn.getOutputStream(); |
76 |
92 |
ostream.write(pContent); |
77 |
92 |
ostream.close(); |
78 |
0 |
} catch (IOException e) { |
79 |
0 |
throw new XmlRpcClientException("Failed to send request to server: " + e.getMessage(), e); |
80 |
|
} |
81 |
92 |
return newInputStream(pConfig, pConnection); |
82 |
|
} |
83 |
|
|
84 |
|
protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig, Object pConnection) { |
85 |
140 |
if (pConnection instanceof HttpURLConnection) { |
86 |
140 |
HttpURLConnection conn = (HttpURLConnection) pConnection; |
87 |
140 |
return HttpUtil.isUsingGzipEncoding(conn.getHeaderField("Content-Encoding")); |
88 |
|
} else { |
89 |
0 |
return false; |
90 |
|
} |
91 |
|
} |
92 |
|
} |