Skip to content

Commit 7534a3c

Browse files
committed
updated rest client httpclient to 4.1.2
1 parent a823a0a commit 7534a3c

File tree

3 files changed

+96
-68
lines changed

3 files changed

+96
-68
lines changed

framework/bundles/org.eclipse.ecf.remoteservice.rest/META-INF/MANIFEST.MF

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ Export-Package: org.eclipse.ecf.internal.remoteservice.rest;x-internal:=true,
1414
org.eclipse.ecf.remoteservice.rest.client;version="2.2.200",
1515
org.eclipse.ecf.remoteservice.rest.identity;version="2.2.200",
1616
org.eclipse.ecf.remoteservice.rest.util;version="2.2.200"
17-
Import-Package: org.apache.commons.httpclient;version="3.0.1",
18-
org.apache.commons.httpclient.auth;version="3.0.1",
19-
org.apache.commons.httpclient.methods;version="3.1.0",
20-
org.apache.commons.httpclient.params;version="3.0.1",
21-
org.apache.commons.httpclient.util,
17+
Import-Package: org.apache.http;version="4.1.2",
18+
org.apache.http.client;version="4.1.2",
19+
org.apache.http.client.entity;version="4.1.2",
20+
org.apache.http.client.params;version="4.1.2",
21+
org.apache.http.impl.auth;version="4.1.2",
22+
org.apache.http.impl.client;version="4.1.2",
23+
org.apache.http.entity;version="4.1.2",
24+
org.apache.http.message;version="4.1.2",
25+
org.apache.http.auth;version="4.1.2",
26+
org.apache.http.client.methods;version="4.1.2",
27+
org.apache.http.params;version="4.1.2",
28+
org.apache.http.util,
2229
org.eclipse.ecf.remoteservice;version="6.0.0",
2330
org.eclipse.ecf.remoteservice.client,
2431
org.eclipse.ecf.remoteservice.events,

framework/bundles/org.eclipse.ecf.remoteservice.rest/src/org/eclipse/ecf/remoteservice/rest/client/AbstractEntityRequestType.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import java.io.*;
1313
import java.util.Map;
14-
import org.apache.commons.httpclient.methods.*;
14+
import org.apache.http.HttpEntity;
15+
import org.apache.http.entity.*;
1516
import org.eclipse.ecf.remoteservice.IRemoteCall;
1617
import org.eclipse.ecf.remoteservice.client.IRemoteCallParameter;
1718
import org.eclipse.ecf.remoteservice.client.IRemoteCallable;
@@ -23,13 +24,14 @@ public abstract class AbstractEntityRequestType extends AbstractRequestType {
2324
public static final int STRING_REQUEST_ENTITY = 1;
2425
public static final int BYTEARRAY_REQUEST_ENTITY = 2;
2526
public static final int FILE_REQUEST_ENTITY = 3;
27+
public static final long CONTENT_LENGTH_AUTO = -2;
2628

2729
public static final String CHARSET_PARAM_NAME = "charset"; //$NON-NLS-1$
2830
public static final String CONTENT_TYPE_PARAM_NAME = "contentType"; //$NON-NLS-1$
2931
public static final String CONTENT_LENGTH_PARAM_NAME = "contentLength"; //$NON-NLS-1$
3032

3133
protected int requestEntityType = NO_REQUEST_ENTITY;
32-
protected long defaultContentLength = InputStreamRequestEntity.CONTENT_LENGTH_AUTO;
34+
protected long defaultContentLength = AbstractEntityRequestType.CONTENT_LENGTH_AUTO;
3335
protected String defaultContentType = null;
3436
protected String defaultCharset = null;
3537

@@ -54,7 +56,7 @@ public AbstractEntityRequestType(int requestEntityType, String defaultContentTyp
5456
}
5557

5658
public AbstractEntityRequestType(int requestEntityType, String defaultContentType, Map defaultRequestHeaders) {
57-
this(requestEntityType, defaultContentType, InputStreamRequestEntity.CONTENT_LENGTH_AUTO, null, defaultRequestHeaders);
59+
this(requestEntityType, defaultContentType, AbstractEntityRequestType.CONTENT_LENGTH_AUTO, null, defaultRequestHeaders);
5860
}
5961

6062
public AbstractEntityRequestType(int requestEntityType, String defaultContentType) {
@@ -77,32 +79,32 @@ public boolean useRequestEntity() {
7779
return requestEntityType > -1;
7880
}
7981

80-
public RequestEntity generateRequestEntity(String uri, IRemoteCall call, IRemoteCallable callable, IRemoteCallParameter paramDefault, Object paramToSerialize) throws NotSerializableException {
81-
if (paramToSerialize instanceof RequestEntity)
82-
return (RequestEntity) paramToSerialize;
82+
public HttpEntity generateRequestEntity(String uri, IRemoteCall call, IRemoteCallable callable, IRemoteCallParameter paramDefault, Object paramToSerialize) throws NotSerializableException {
83+
if (paramToSerialize instanceof HttpEntity)
84+
return (HttpEntity) paramToSerialize;
8385
switch (requestEntityType) {
8486
case INPUT_STREAM_REQUEST_ENTITY :
8587
if (paramToSerialize instanceof InputStream) {
86-
return new InputStreamRequestEntity((InputStream) paramToSerialize, getContentLength(call, callable, paramDefault), getContentType(call, callable, paramDefault));
88+
return new InputStreamEntity((InputStream) paramToSerialize, getContentLength(call, callable, paramDefault));
8789
}
8890
throw new NotSerializableException("Cannot generate request entity. Expecting InputStream and got class=" + paramToSerialize.getClass().getName()); //$NON-NLS-1$
8991
case STRING_REQUEST_ENTITY :
9092
if (paramToSerialize instanceof String) {
9193
try {
92-
return new StringRequestEntity((String) paramToSerialize, getContentType(call, callable, paramDefault), getCharset(call, callable, paramDefault));
94+
return new StringEntity((String) paramToSerialize, getContentType(call, callable, paramDefault), getCharset(call, callable, paramDefault));
9395
} catch (UnsupportedEncodingException e) {
9496
throw new NotSerializableException("Could not create request entity from call parameters: " + e.getMessage()); //$NON-NLS-1$
9597
}
9698
}
9799
throw new NotSerializableException("Cannot generate request entity. Expecting String and got class=" + paramToSerialize.getClass().getName()); //$NON-NLS-1$
98100
case BYTEARRAY_REQUEST_ENTITY :
99101
if (paramToSerialize instanceof byte[]) {
100-
return new ByteArrayRequestEntity((byte[]) paramToSerialize, getContentType(call, callable, paramDefault));
102+
return new ByteArrayEntity((byte[]) paramToSerialize);
101103
}
102104
throw new NotSerializableException("Cannot generate request entity. Expecting byte[] and got class=" + paramToSerialize.getClass().getName()); //$NON-NLS-1$
103105
case FILE_REQUEST_ENTITY :
104106
if (paramToSerialize instanceof File) {
105-
return new FileRequestEntity((File) paramToSerialize, getContentType(call, callable, paramDefault));
107+
return new FileEntity((File) paramToSerialize, getContentType(call, callable, paramDefault));
106108
}
107109
throw new NotSerializableException("Remote call parameter with name=" + paramDefault.getName() + " is incorrect type for creating request entity."); //$NON-NLS-1$ //$NON-NLS-2$
108110
default :
@@ -139,7 +141,7 @@ protected long getContentLength(IRemoteCall call, IRemoteCallable callable, IRem
139141
try {
140142
return Integer.parseInt((String) o);
141143
} catch (NumberFormatException e) {
142-
return InputStreamRequestEntity.CONTENT_LENGTH_AUTO;
144+
return AbstractEntityRequestType.CONTENT_LENGTH_AUTO;
143145
}
144146
}
145147
}

framework/bundles/org.eclipse.ecf.remoteservice.rest/src/org/eclipse/ecf/remoteservice/rest/client/RestClientService.java

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
*******************************************************************************/
1010
package org.eclipse.ecf.remoteservice.rest.client;
1111

12-
import java.io.IOException;
13-
import java.io.NotSerializableException;
12+
import java.io.*;
1413
import java.util.*;
15-
import org.apache.commons.httpclient.*;
16-
import org.apache.commons.httpclient.auth.AuthScope;
17-
import org.apache.commons.httpclient.methods.*;
18-
import org.apache.commons.httpclient.params.HttpClientParams;
19-
import org.apache.commons.httpclient.params.HttpMethodParams;
14+
import org.apache.http.*;
15+
import org.apache.http.auth.*;
16+
import org.apache.http.client.HttpClient;
17+
import org.apache.http.client.entity.UrlEncodedFormEntity;
18+
import org.apache.http.client.methods.*;
19+
import org.apache.http.client.params.ClientPNames;
20+
import org.apache.http.impl.auth.BasicScheme;
21+
import org.apache.http.impl.client.DefaultHttpClient;
22+
import org.apache.http.message.AbstractHttpMessage;
23+
import org.apache.http.message.BasicNameValuePair;
24+
import org.apache.http.params.CoreConnectionPNames;
25+
import org.apache.http.params.CoreProtocolPNames;
2026
import org.eclipse.core.runtime.IStatus;
2127
import org.eclipse.core.runtime.Status;
2228
import org.eclipse.ecf.core.security.*;
@@ -39,16 +45,18 @@ public class RestClientService extends AbstractClientService {
3945

4046
protected final static String DEFAULT_HTTP_CONTENT_CHARSET = "UTF-8"; //$NON-NLS-1$
4147

48+
private static final String CONNECTION_MANAGER_TIMEOUT = "http.connection-manager.timeout"; //$NON-NLS-1$
49+
4250
protected HttpClient httpClient;
4351
protected int responseBufferSize = DEFAULT_RESPONSE_BUFFER_SIZE;
4452

4553
public RestClientService(RestClientContainer container, RemoteServiceClientRegistration registration) {
4654
super(container, registration);
47-
this.httpClient = new HttpClient();
55+
this.httpClient = new DefaultHttpClient();
4856
}
4957

50-
private boolean isResponseOk(int httpResponseCode) {
51-
int isOkCode = httpResponseCode - 200;
58+
private boolean isResponseOk(HttpResponse response) {
59+
int isOkCode = response.getStatusLine().getStatusCode() - 200;
5260
return (isOkCode >= 0 && isOkCode < 100);
5361
}
5462

@@ -65,44 +73,46 @@ private boolean isResponseOk(int httpResponseCode) {
6573
*/
6674
protected Object invokeRemoteCall(final IRemoteCall call, final IRemoteCallable callable) throws ECFException {
6775
String uri = prepareEndpointAddress(call, callable);
68-
HttpMethod httpMethod = createAndPrepareHttpMethod(uri, call, callable);
76+
HttpRequestBase httpMethod = createAndPrepareHttpMethod(uri, call, callable);
6977
// execute method
7078
byte[] responseBody = null;
71-
int responseCode = -1;
79+
int responseCode = 500;
80+
HttpResponse response = null;
7281
try {
73-
responseCode = httpClient.executeMethod(httpMethod);
74-
if (isResponseOk(responseCode)) {
82+
response = httpClient.execute(httpMethod);
83+
responseCode = response.getStatusLine().getStatusCode();
84+
if (isResponseOk(response)) {
7585
// Get responseBody as String
76-
responseBody = getResponseAsBytes(httpMethod);
86+
responseBody = getResponseAsBytes(response);
7787
} else {
7888
// If this method returns true, we should retrieve the response body
79-
if (retrieveErrorResponseBody(responseCode)) {
80-
responseBody = getResponseAsBytes(httpMethod);
89+
if (retrieveErrorResponseBody(response)) {
90+
responseBody = getResponseAsBytes(response);
8191
}
8292
// Now pass to the exception handler
8393
handleException("Http response not OK. URL=" + uri + " responseCode=" + new Integer(responseCode), null, responseCode, responseBody); //$NON-NLS-1$ //$NON-NLS-2$
8494
}
85-
} catch (HttpException e) {
86-
handleException("Transport HttpException", e, responseCode); //$NON-NLS-1$
8795
} catch (IOException e) {
8896
handleException("Transport IOException", e, responseCode); //$NON-NLS-1$
8997
}
9098
Object result = null;
9199
try {
92-
result = processResponse(uri, call, callable, convertResponseHeaders(httpMethod.getResponseHeaders()), responseBody);
100+
result = processResponse(uri, call, callable, convertResponseHeaders(response.getAllHeaders()), responseBody);
93101
} catch (NotSerializableException e) {
94102
handleException("Exception deserializing response. URL=" + uri + " responseCode=" + new Integer(responseCode), e, responseCode); //$NON-NLS-1$ //$NON-NLS-2$
95103
}
96104
return result;
97105
}
98106

99-
protected boolean retrieveErrorResponseBody(int responseCode) {
107+
protected boolean retrieveErrorResponseBody(HttpResponse response) {
100108
// XXX this needs to be defined differently for
101109
return false;
102110
}
103111

104-
protected byte[] getResponseAsBytes(HttpMethod httpMethod) throws IOException {
105-
return httpMethod.getResponseBody();
112+
protected byte[] getResponseAsBytes(HttpResponse response) throws IOException {
113+
ByteArrayOutputStream os = new ByteArrayOutputStream();
114+
response.getEntity().writeTo(os);
115+
return os.toByteArray();
106116
}
107117

108118
protected void handleException(String message, Throwable e, int responseCode, byte[] responseBody) throws RestException {
@@ -120,9 +130,9 @@ protected void setupTimeouts(HttpClient httpClient, IRemoteCall call, IRemoteCal
120130
callTimeout = callable.getDefaultTimeout();
121131

122132
int timeout = (int) callTimeout;
123-
httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);
124-
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
125-
httpClient.getParams().setConnectionManagerTimeout(timeout);
133+
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
134+
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
135+
httpClient.getParams().setIntParameter(CONNECTION_MANAGER_TIMEOUT, timeout);
126136
}
127137

128138
private Map convertResponseHeaders(Header[] headers) {
@@ -137,7 +147,7 @@ private Map convertResponseHeaders(Header[] headers) {
137147
return result;
138148
}
139149

140-
protected void addRequestHeaders(HttpMethod httpMethod, IRemoteCall call, IRemoteCallable callable) {
150+
protected void addRequestHeaders(AbstractHttpMessage httpMethod, IRemoteCall call, IRemoteCallable callable) {
141151
// Add request headers from the callable
142152
Map requestHeaders = (callable.getRequestType() instanceof AbstractRequestType) ? ((AbstractRequestType) callable.getRequestType()).getDefaultRequestHeaders() : new HashMap();
143153
if (requestHeaders == null)
@@ -154,12 +164,12 @@ protected void addRequestHeaders(HttpMethod httpMethod, IRemoteCall call, IRemot
154164
for (int i = 0; i < headers.length; i++) {
155165
String key = (String) headers[i];
156166
String value = (String) requestHeaders.get(key);
157-
httpMethod.addRequestHeader(key, value);
167+
httpMethod.addHeader(key, value);
158168
}
159169
}
160170

161-
protected HttpMethod createAndPrepareHttpMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws RestException {
162-
HttpMethod httpMethod = null;
171+
protected HttpRequestBase createAndPrepareHttpMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws RestException {
172+
HttpRequestBase httpMethod = null;
163173

164174
IRemoteCallableRequestType requestType = callable.getRequestType();
165175
if (requestType == null)
@@ -180,75 +190,84 @@ protected HttpMethod createAndPrepareHttpMethod(String uri, IRemoteCall call, IR
180190
String message = "Could not serialize parameters for uri=" + uri + " call=" + call + " callable=" + callable; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
181191
logException(message, e);
182192
throw new RestException(message);
193+
} catch (UnsupportedEncodingException e) {
194+
String message = "Could not serialize parameters for uri=" + uri + " call=" + call + " callable=" + callable; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
195+
logException(message, e);
196+
throw new RestException(message);
183197
}
184198
// add additional request headers
185199
addRequestHeaders(httpMethod, call, callable);
186200
// handle authentication
187201
setupAuthenticaton(httpClient, httpMethod);
188202
// needed because a resource can link to another resource
189-
httpClient.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, new Boolean(true));
190-
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, DEFAULT_HTTP_CONTENT_CHARSET);
203+
httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, new Boolean(true));
204+
httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, DEFAULT_HTTP_CONTENT_CHARSET);
191205
setupTimeouts(httpClient, call, callable);
192206
return httpMethod;
193207
}
194208

195209
/**
196210
* @throws RestException
197211
*/
198-
protected HttpMethod prepareDeleteMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws RestException {
199-
return new DeleteMethod(uri);
212+
protected HttpRequestBase prepareDeleteMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws RestException {
213+
return new HttpDelete(uri);
200214
}
201215

202-
protected HttpMethod preparePutMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws NotSerializableException {
203-
PutMethod result = new PutMethod(uri);
216+
protected HttpRequestBase preparePutMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws NotSerializableException, UnsupportedEncodingException {
217+
HttpPut result = new HttpPut(uri);
204218
HttpPutRequestType putRequestType = (HttpPutRequestType) callable.getRequestType();
205219

206220
IRemoteCallParameter[] defaultParameters = callable.getDefaultParameters();
207221
Object[] parameters = call.getParameters();
208222

209223
if (putRequestType.useRequestEntity()) {
210224
if (defaultParameters != null && defaultParameters.length > 0 && parameters != null && parameters.length > 0) {
211-
RequestEntity requestEntity = putRequestType.generateRequestEntity(uri, call, callable, defaultParameters[0], parameters[0]);
212-
result.setRequestEntity(requestEntity);
225+
HttpEntity requestEntity = putRequestType.generateRequestEntity(uri, call, callable, defaultParameters[0], parameters[0]);
226+
result.setEntity(requestEntity);
213227
}
214228
} else {
215229
NameValuePair[] params = toNameValuePairs(uri, call, callable);
216-
if (params != null)
217-
result.setQueryString(params);
230+
if (params != null) {
231+
result.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
232+
}
218233
}
219234
return result;
220235
}
221236

222237
/**
238+
* @throws UnsupportedEncodingException
223239
* @throws ECFException
224240
*/
225-
protected HttpMethod preparePostMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws NotSerializableException {
226-
PostMethod result = new PostMethod(uri);
241+
protected HttpRequestBase preparePostMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws NotSerializableException, UnsupportedEncodingException {
242+
HttpPost result = new HttpPost(uri);
227243
HttpPostRequestType postRequestType = (HttpPostRequestType) callable.getRequestType();
228244

229245
IRemoteCallParameter[] defaultParameters = callable.getDefaultParameters();
230246
Object[] parameters = call.getParameters();
231247
if (postRequestType.useRequestEntity()) {
232248
if (defaultParameters != null && defaultParameters.length > 0 && parameters != null && parameters.length > 0) {
233-
RequestEntity requestEntity = postRequestType.generateRequestEntity(uri, call, callable, defaultParameters[0], parameters[0]);
234-
result.setRequestEntity(requestEntity);
249+
HttpEntity requestEntity = postRequestType.generateRequestEntity(uri, call, callable, defaultParameters[0], parameters[0]);
250+
result.setEntity(requestEntity);
235251
}
236252
} else {
237253
NameValuePair[] params = toNameValuePairs(uri, call, callable);
238254
if (params != null)
239-
result.setQueryString(params);
255+
result.setEntity(new UrlEncodedFormEntity(Arrays.asList(params)));
240256
}
241257
return result;
242258
}
243259

244260
/**
245261
* @throws ECFException
246262
*/
247-
protected HttpMethod prepareGetMethod(String uri, IRemoteCall call, IRemoteCallable callable) throws NotSerializableException {
248-
HttpMethod result = new GetMethod(uri);
263+
protected HttpRequestBase prepareGetMethod(String uri, IRemoteCall call, IRemoteCallable callable) {
264+
HttpRequestBase result = new HttpGet(uri);
265+
/**
266+
* FIXME: is this still supported in httpclient 4.0?
249267
NameValuePair[] params = toNameValuePairs(uri, call, callable);
250268
if (params != null)
251269
result.setQueryString(params);
270+
**/
252271
return result;
253272
}
254273

@@ -265,14 +284,14 @@ protected NameValuePair[] toNameValuePairs(String uri, IRemoteCall call, IRemote
265284
parameterValue = o.toString();
266285
}
267286
if (parameterValue != null) {
268-
nameValueList.add(new NameValuePair(restParameters[i].getName(), parameterValue));
287+
nameValueList.add(new BasicNameValuePair(restParameters[i].getName(), parameterValue));
269288
}
270289
}
271290
}
272291
return (NameValuePair[]) nameValueList.toArray(new NameValuePair[nameValueList.size()]);
273292
}
274293

275-
protected void setupAuthenticaton(HttpClient httpClient, HttpMethod method) {
294+
protected void setupAuthenticaton(HttpClient httpClient, HttpRequestBase method) {
276295
IConnectContext connectContext = container.getConnectContextForAuthentication();
277296
if (connectContext != null) {
278297
NameCallback nameCallback = new NameCallback(""); //$NON-NLS-1$
@@ -285,14 +304,14 @@ protected void setupAuthenticaton(HttpClient httpClient, HttpMethod method) {
285304
callbackHandler.handle(callbacks);
286305
String username = nameCallback.getName();
287306
String password = (String) passwordCallback.getObject();
288-
AuthScope authscope = new AuthScope(null, -1);
289307
Credentials credentials = new UsernamePasswordCredentials(username, password);
290-
httpClient.getState().setCredentials(authscope, credentials);
291-
method.setDoAuthentication(true);
308+
method.addHeader(new BasicScheme().authenticate(credentials, method));
292309
} catch (IOException e) {
293310
logException("IOException setting credentials for rest httpclient", e); //$NON-NLS-1$
294311
} catch (UnsupportedCallbackException e) {
295312
logException("UnsupportedCallbackException setting credentials for rest httpclient", e); //$NON-NLS-1$
313+
} catch (AuthenticationException e) {
314+
logException("AuthenticationException setting credentials for rest httpclient", e); //$NON-NLS-1$
296315
}
297316

298317
}

0 commit comments

Comments
 (0)