forked from karlmdavis/ksoap2-android
/
OkHttpServiceConnectionSE.java
150 lines (125 loc) · 4.93 KB
/
OkHttpServiceConnectionSE.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.ksoap2.transport;
import okhttp3.OkHttpClient;
import okhttp3.internal.huc.OkHttpURLConnection;
import org.ksoap2.HeaderProperty;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* A simple ServiceConnection based on OkHttp3's URLConnection implementation
* as a workaround for Android's timeout issues
* ref. https://code.google.com/p/android/issues/detail?id=40874
*/
public class OkHttpServiceConnectionSE implements ServiceConnection {
private HttpURLConnection connection;
private OkHttpClient client;
/**
* Constructor taking the url to the endpoint for this soap communication
* @param url the url to open the connection to.
* @throws IOException
*/
public OkHttpServiceConnectionSE(String url) throws IOException {
this(null, url, ServiceConnection.DEFAULT_TIMEOUT);
}
public OkHttpServiceConnectionSE(Proxy proxy, String url) throws IOException {
this(proxy, url, ServiceConnection.DEFAULT_TIMEOUT);
}
/**
* Constructor taking the url to the endpoint for this soap communication
* @param url the url to open the connection to.
* @param timeout the connection and read timeout for the http connection in milliseconds
* @throws IOException // 20 seconds
*/
public OkHttpServiceConnectionSE(String url, int timeout) throws IOException {
this(null, url, timeout);
}
public OkHttpServiceConnectionSE(Proxy proxy, String url, int timeout) throws IOException {
this(new OkHttpClient.Builder()
.connectTimeout(timeout, TimeUnit.MILLISECONDS)
.readTimeout(timeout, TimeUnit.MILLISECONDS)
.build(),
proxy, url, timeout);
}
public OkHttpServiceConnectionSE(OkHttpClient client, Proxy proxy, String url, int timeout) throws IOException {
this.client = client;
connection = new OkHttpURLConnection(new URL(url), client);
// connection = (proxy == null)
// ? new HttpURLConnectionImpl(new URL(url), client)
// : (HttpURLConnectionImpl) new URL(url).openConnection(proxy);
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout); // even if we connect fine we want to time out if we cant read anything..
}
public void connect() throws IOException {
connection.connect();
}
public void disconnect() {
connection.disconnect();
}
public List getResponseProperties() throws IOException {
List retList = new LinkedList();
Map properties = connection.getHeaderFields();
if(properties != null) {
Set keys = properties.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
List values = (List) properties.get(key);
for (int j = 0; j < values.size(); j++) {
retList.add(new HeaderProperty(key, (String) values.get(j)));
}
}
}
return retList;
}
public int getResponseCode() throws IOException {
return connection.getResponseCode();
}
public void setRequestProperty(String string, String soapAction) {
connection.setRequestProperty(string, soapAction);
}
public void setRequestMethod(String requestMethod) throws IOException {
connection.setRequestMethod(requestMethod);
}
/**
* If the length of a HTTP request body is known ahead, sets fixed length
* to enable streaming without buffering. Sets after connection will cause an exception.
*
* @param contentLength the fixed length of the HTTP request body
* @see http://developer.android.com/reference/java/net/HttpURLConnection.html
**/
public void setFixedLengthStreamingMode(int contentLength) {
connection.setFixedLengthStreamingMode(contentLength);
}
public void setChunkedStreamingMode() {
connection.setChunkedStreamingMode(0);
}
public OutputStream openOutputStream() throws IOException {
return connection.getOutputStream();
}
public InputStream openInputStream() throws IOException {
return connection.getInputStream();
}
public InputStream getErrorStream() {
return connection.getErrorStream();
}
public String getHost() {
return connection.getURL().getHost();
}
public int getPort() {
return connection.getURL().getPort();
}
public String getPath() {
return connection.getURL().getPath();
}
}