99 *******************************************************************************/
1010package org .eclipse .ecf .remoteservice .rest .client ;
1111
12- import java .io .IOException ;
13- import java .io .NotSerializableException ;
12+ import java .io .*;
1413import 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 ;
2026import org .eclipse .core .runtime .IStatus ;
2127import org .eclipse .core .runtime .Status ;
2228import 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