10
10
import java .util .Map ;
11
11
12
12
import org .apache .http .Header ;
13
+ import org .apache .http .StatusLine ;
13
14
import org .apache .http .annotation .NotThreadSafe ;
14
- import org .apache .http .client .ClientProtocolException ;
15
15
import org .apache .http .client .ResponseHandler ;
16
16
import org .apache .http .client .methods .CloseableHttpResponse ;
17
17
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
18
18
import org .apache .http .client .methods .HttpGet ;
19
19
import org .apache .http .client .methods .HttpPatch ;
20
20
import org .apache .http .client .methods .HttpPost ;
21
21
import org .apache .http .client .methods .HttpPut ;
22
+ import org .apache .http .client .methods .HttpRequestBase ;
22
23
import org .apache .http .client .utils .URIBuilder ;
23
24
import org .apache .http .entity .StringEntity ;
24
25
import org .apache .http .impl .client .CloseableHttpClient ;
@@ -143,8 +144,6 @@ public Response getResponse(CloseableHttpResponse response) throws IOException {
143
144
* response headers.
144
145
*/
145
146
public Response get (Request request ) throws URISyntaxException , IOException {
146
- CloseableHttpResponse serverResponse = null ;
147
- Response response = new Response ();
148
147
URI uri = null ;
149
148
HttpGet httpGet = null ;
150
149
@@ -160,28 +159,14 @@ public Response get(Request request) throws URISyntaxException, IOException {
160
159
httpGet .setHeader (entry .getKey (), entry .getValue ());
161
160
}
162
161
}
163
-
164
- try {
165
- serverResponse = httpClient .execute (httpGet );
166
- response = getResponse (serverResponse );
167
- } catch (IOException ex ) {
168
- throw ex ;
169
- } finally {
170
- if (serverResponse != null ) {
171
- serverResponse .close ();
172
- }
173
- }
174
-
175
- return response ;
162
+ return executeApiCall (httpGet );
176
163
}
177
164
178
165
/**
179
166
* Make a POST request and provide the status code, response body and
180
167
* response headers.
181
168
*/
182
169
public Response post (Request request ) throws URISyntaxException , IOException {
183
- CloseableHttpResponse serverResponse = null ;
184
- Response response = new Response ();
185
170
URI uri = null ;
186
171
HttpPost httpPost = null ;
187
172
@@ -203,28 +188,14 @@ public Response post(Request request) throws URISyntaxException, IOException {
203
188
httpPost .setHeader ("Content-Type" , "application/json" );
204
189
}
205
190
206
- try {
207
- serverResponse = httpClient .execute (httpPost );
208
- response = getResponse (serverResponse );
209
- serverResponse .close ();
210
- } catch (IOException ex ) {
211
- throw ex ;
212
- } finally {
213
- if (serverResponse != null ) {
214
- serverResponse .close ();
215
- }
216
- }
217
-
218
- return response ;
191
+ return executeApiCall (httpPost );
219
192
}
220
193
221
194
/**
222
195
* Make a PATCH request and provide the status code, response body and
223
196
* response headers.
224
197
*/
225
198
public Response patch (Request request ) throws URISyntaxException , IOException {
226
- CloseableHttpResponse serverResponse = null ;
227
- Response response = new Response ();
228
199
URI uri = null ;
229
200
HttpPatch httpPatch = null ;
230
201
@@ -245,29 +216,14 @@ public Response patch(Request request) throws URISyntaxException, IOException {
245
216
if (request .body != "" ) {
246
217
httpPatch .setHeader ("Content-Type" , "application/json" );
247
218
}
248
-
249
- try {
250
- serverResponse = httpClient .execute (httpPatch );
251
- response = getResponse (serverResponse );
252
- serverResponse .close ();
253
- } catch (IOException ex ) {
254
- throw ex ;
255
- } finally {
256
- if (serverResponse != null ) {
257
- serverResponse .close ();
258
- }
259
- }
260
-
261
- return response ;
219
+ return executeApiCall (httpPatch );
262
220
}
263
221
264
222
/**
265
223
* Make a PUT request and provide the status code, response body and
266
224
* response headers.
267
225
*/
268
226
public Response put (Request request ) throws URISyntaxException , IOException {
269
- CloseableHttpResponse serverResponse = null ;
270
- Response response = new Response ();
271
227
URI uri = null ;
272
228
HttpPut httpPut = null ;
273
229
@@ -289,27 +245,13 @@ public Response put(Request request) throws URISyntaxException, IOException {
289
245
httpPut .setHeader ("Content-Type" , "application/json" );
290
246
}
291
247
292
- try {
293
- serverResponse = httpClient .execute (httpPut );
294
- response = getResponse (serverResponse );
295
- serverResponse .close ();
296
- } catch (IOException ex ) {
297
- throw ex ;
298
- } finally {
299
- if (serverResponse != null ) {
300
- serverResponse .close ();
301
- }
302
- }
303
-
304
- return response ;
248
+ return executeApiCall (httpPut );
305
249
}
306
250
307
251
/**
308
252
* Make a DELETE request and provide the status code and response headers.
309
253
*/
310
254
public Response delete (Request request ) throws URISyntaxException , IOException {
311
- CloseableHttpResponse serverResponse = null ;
312
- Response response = new Response ();
313
255
URI uri = null ;
314
256
HttpDeleteWithBody httpDelete = null ;
315
257
@@ -331,18 +273,26 @@ public Response delete(Request request) throws URISyntaxException, IOException {
331
273
httpDelete .setHeader ("Content-Type" , "application/json" );
332
274
}
333
275
276
+ return executeApiCall (httpDelete );
277
+ }
278
+
279
+ private Response executeApiCall (HttpRequestBase httpPost ) throws IOException {
280
+ CloseableHttpResponse serverResponse = null ;
281
+ Response response = new Response ();
334
282
try {
335
- serverResponse = httpClient .execute (httpDelete );
283
+ serverResponse = httpClient .execute (httpPost );
336
284
response = getResponse (serverResponse );
337
- serverResponse .close ();
338
- } catch (IOException ex ) {
339
- throw ex ;
285
+ final StatusLine statusLine = serverResponse .getStatusLine ();
286
+ if (statusLine .getStatusCode ()>=300 ){
287
+ //throwing IOException here to not break API behavior.
288
+ throw new IOException ("Request returned status Code " +statusLine .getStatusCode ()+"Body:" +(response !=null ?response .body :null ));
289
+ }
290
+
340
291
} finally {
341
292
if (serverResponse != null ) {
342
293
serverResponse .close ();
343
294
}
344
295
}
345
-
346
296
return response ;
347
297
}
348
298
0 commit comments