-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathHttpClient.java
More file actions
262 lines (235 loc) · 8.87 KB
/
HttpClient.java
File metadata and controls
262 lines (235 loc) · 8.87 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.rallydev.rest.client;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DecompressingHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
* A HttpClient implementation providing connectivity to Rally. This class does not
* provide any authentication on its own but instead relies on a concrete subclass to do so.
*/
public class HttpClient extends DefaultHttpClient
implements Closeable {
protected URI server;
protected String wsapiVersion = "v2.0";
protected org.apache.http.client.HttpClient client;
private enum Header {
Library,
Name,
Vendor,
Version
}
private Map<Header, String> headers = new HashMap<Header, String>() {
{
put(Header.Library, "Rally Rest API for Java v2.2.1");
put(Header.Name, "Rally Rest API for Java");
put(Header.Vendor, "Rally Software, Inc.");
put(Header.Version, "2.2.1");
}
};
protected HttpClient(URI server) {
this.server = server;
client = new DecompressingHttpClient(this);
}
/**
* Allow the user to specify a compliant HttpClient
*
* @param server The URI of the remote server
* @param client A pre-configured HttpClient implementation
*/
public HttpClient(URI server, org.apache.http.client.HttpClient client) {
this.server = server;
this.client = client;
}
/**
* Set the unauthenticated proxy server to use. By default no proxy is configured.
*
* @param proxy The proxy server, e.g. {@code new URI("http://my.proxy.com:8000")}
*/
public void setProxy(URI proxy) {
this.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme()));
}
/**
* Set the authenticated proxy server to use. By default no proxy is configured.
*
* @param proxy The proxy server, e.g. {@code new URI("http://my.proxy.com:8000")}
* @param userName The username to be used for authentication.
* @param password The password to be used for authentication.
*/
public void setProxy(URI proxy, String userName, String password) {
setProxy(proxy);
setClientCredentials(proxy, userName, password);
}
/**
* Set the value of the X-RallyIntegrationVendor header included on all requests.
* This should be set to your company name.
*
* @param value The vendor header to be included on all requests.
*/
public void setApplicationVendor(String value) {
headers.put(Header.Vendor, value);
}
/**
* Set the value of the X-RallyIntegrationVersion header included on all requests.
* This should be set to the version of your application.
*
* @param value The vendor header to be included on all requests.
*/
public void setApplicationVersion(String value) {
headers.put(Header.Version, value);
}
/**
* Set the value of the X-RallyIntegrationName header included on all requests.
* This should be set to the name of your application.
*
* @param value The vendor header to be included on all requests.
*/
public void setApplicationName(String value) {
headers.put(Header.Name, value);
}
/**
* Get the current server being targeted.
*
* @return the current server.
*/
public String getServer() {
return server.toString();
}
/**
* Get the current version of the WSAPI being targeted.
*
* @return the current WSAPI version.
*/
public String getWsapiVersion() {
return wsapiVersion;
}
/**
* Set the current version of the WSAPI being targeted.
*
* @param wsapiVersion the new version, e.g. {@code "1.30"}
*/
public void setWsapiVersion(String wsapiVersion) {
this.wsapiVersion = wsapiVersion;
}
/**
* Execute a request against the WSAPI
*
* @param request the request to be executed
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
protected String doRequest(HttpRequestBase request) throws IOException {
for (Map.Entry<Header, String> header : headers.entrySet()) {
request.setHeader("X-RallyIntegration" + header.getKey().name(), header.getValue());
}
return this.executeRequest(request);
}
/**
* Execute a request against the WSAPI
*
* @param request the request to be executed
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
protected String executeRequest(HttpRequestBase request) throws IOException {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(entity, "utf-8");
} else {
EntityUtils.consumeQuietly(entity);
throw new IOException(response.getStatusLine().toString());
}
}
/**
* Perform a post against the WSAPI
*
* @param url the request url
* @param body the body of the post
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
public String doPost(String url, String body) throws IOException {
HttpPost httpPost = new HttpPost(getWsapiUrl() + url);
httpPost.setEntity(new StringEntity(body, "utf-8"));
return doRequest(httpPost);
}
/**
* Perform a put against the WSAPI
*
* @param url the request url
* @param body the body of the put
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
public String doPut(String url, String body) throws IOException {
HttpPut httpPut = new HttpPut(getWsapiUrl() + url);
httpPut.setEntity(new StringEntity(body, "utf-8"));
return doRequest(httpPut);
}
/**
* Perform a delete against the WSAPI
*
* @param url the request url
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
public String doDelete(String url) throws IOException {
HttpDelete httpDelete = new HttpDelete(getWsapiUrl() + url);
return doRequest(httpDelete);
}
/**
* Perform a get against the WSAPI
*
* @param url the request url
* @return the JSON encoded string response
* @throws IOException if a non-200 response code is returned or if some other
* problem occurs while executing the request
*/
public String doGet(String url) throws IOException {
HttpGet httpGet = new HttpGet(getWsapiUrl() + url);
return doRequest(httpGet);
}
/**
* Release all resources associated with this instance.
*
* @throws IOException if an error occurs releasing resources
*/
public void close() throws IOException {
client.getConnectionManager().shutdown();
}
protected Credentials setClientCredentials(URI server, String userName, String password) {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
this.getCredentialsProvider().setCredentials(new AuthScope(server.getHost(), server.getPort()), credentials);
return credentials;
}
/**
* Get the WSAPI base url based on the current server and WSAPI version
*
* @return the fully qualified WSAPI base url, e.g. https://rally1.rallydev.com/slm/webservice/1.33
*/
public String getWsapiUrl() {
return getServer() + "/slm/webservice/" + getWsapiVersion();
}
}