26
26
import org .apache .commons .io .IOUtils ;
27
27
import org .apache .commons .lang .StringUtils ;
28
28
import org .apache .http .HttpEntity ;
29
- import org .apache .http .HttpResponse ;
30
29
import org .apache .http .HttpStatus ;
31
- import org .apache .http .StatusLine ;
30
+ import org .apache .http .client . methods . CloseableHttpResponse ;
32
31
import org .apache .http .client .methods .HttpGet ;
33
32
import org .apache .http .client .methods .HttpPut ;
34
33
import org .apache .http .util .EntityUtils ;
37
36
38
37
import java .io .IOException ;
39
38
import java .io .InputStream ;
39
+ import java .nio .charset .StandardCharsets ;
40
40
41
41
import static org .apache .commons .codec .binary .StringUtils .getBytesUtf8 ;
42
42
import static org .apache .commons .codec .binary .StringUtils .newStringUsAscii ;
@@ -182,49 +182,43 @@ public PreemptiveHttpClient getHttpClient(int connectionTimeout) {
182
182
183
183
public ArtifactoryVersion getVersion () throws IOException {
184
184
String versionUrl = artifactoryUrl + VERSION_INFO_URL ;
185
- HttpResponse response = executeGetRequest (versionUrl );
186
- int statusCode = response .getStatusLine ().getStatusCode ();
187
- if (statusCode == HttpStatus .SC_NOT_FOUND ) {
188
- consumeEntity (response );
189
- return ArtifactoryVersion .NOT_FOUND ;
190
- }
191
- if (statusCode != HttpStatus .SC_OK ) {
192
- throw new IOException (getMessageFromEntity (response .getEntity ()));
193
- }
194
- HttpEntity httpEntity = response .getEntity ();
195
- if (httpEntity != null ) {
185
+ try (CloseableHttpResponse response = executeGetRequest (versionUrl )) {
186
+ HttpEntity httpEntity = response .getEntity ();
187
+ int statusCode = response .getStatusLine ().getStatusCode ();
188
+ if (statusCode == HttpStatus .SC_NOT_FOUND ) {
189
+ EntityUtils .consumeQuietly (httpEntity );
190
+ return ArtifactoryVersion .NOT_FOUND ;
191
+ }
192
+ if (statusCode != HttpStatus .SC_OK ) {
193
+ String message = getMessageFromEntity (httpEntity );
194
+ EntityUtils .consumeQuietly (httpEntity );
195
+ throw new IOException (message );
196
+ }
197
+ if (httpEntity == null ) {
198
+ return ArtifactoryVersion .NOT_FOUND ;
199
+ }
196
200
try (InputStream content = httpEntity .getContent ()) {
197
201
JsonNode result = getJsonNode (content );
198
202
log .debug ("Version result: " + result );
199
203
String version = result .get ("version" ).asText ();
200
204
JsonNode addonsNode = result .get ("addons" );
201
205
boolean hasAddons = (addonsNode != null ) && addonsNode .iterator ().hasNext ();
202
206
return new ArtifactoryVersion (version , hasAddons );
203
- } finally {
204
- EntityUtils .consume (httpEntity );
205
207
}
206
208
}
207
- return ArtifactoryVersion .NOT_FOUND ;
208
209
}
209
210
210
211
public JsonNode getJsonNode (InputStream content ) throws IOException {
211
212
JsonParser parser = createJsonParser (content );
212
213
return parser .readValueAsTree ();
213
214
}
214
215
215
- private HttpResponse executeGetRequest (String lastModifiedUrl ) throws IOException {
216
+ private CloseableHttpResponse executeGetRequest (String lastModifiedUrl ) throws IOException {
216
217
PreemptiveHttpClient client = getHttpClient ();
217
218
HttpGet httpGet = new HttpGet (lastModifiedUrl );
218
219
return client .execute (httpGet );
219
220
}
220
221
221
- private void consumeEntity (HttpResponse response ) throws IOException {
222
- HttpEntity httpEntity = response .getEntity ();
223
- if (httpEntity != null ) {
224
- EntityUtils .consume (httpEntity );
225
- }
226
- }
227
-
228
222
public JsonParser createJsonParser (InputStream in ) throws IOException {
229
223
JsonFactory jsonFactory = createJsonFactory ();
230
224
return jsonFactory .createParser (in );
@@ -250,45 +244,41 @@ public ArtifactoryUploadResponse upload(HttpPut httpPut, HttpEntity fileEntity)
250
244
}
251
245
252
246
public ArtifactoryUploadResponse execute (HttpPut httpPut ) throws IOException {
253
- HttpResponse response = getHttpClient ().execute (httpPut );
254
-
255
- ArtifactoryUploadResponse artifactoryResponse = null ;
256
- if (response .getEntity () != null && response .getEntity ().getContent () != null ) {
257
- InputStream in = response .getEntity ().getContent ();
258
- String content = IOUtils .toString (in , "UTF-8" );
259
- if (StringUtils .isNotEmpty (content )) {
247
+ ArtifactoryUploadResponse artifactoryResponse = new ArtifactoryUploadResponse ();
248
+ try (CloseableHttpResponse response = getHttpClient ().execute (httpPut )) {
249
+ artifactoryResponse .setStatusLine (response .getStatusLine ());
250
+ if (response .getEntity () == null || response .getEntity ().getContent () == null ) {
251
+ return artifactoryResponse ;
252
+ }
253
+ try (InputStream in = response .getEntity ().getContent ()) {
254
+ String content = IOUtils .toString (in , StandardCharsets .UTF_8 );
255
+ if (StringUtils .isEmpty (content )) {
256
+ return artifactoryResponse ;
257
+ }
260
258
try {
261
259
JsonParser parser = createJsonParser (content );
262
260
artifactoryResponse = parser .readValueAs (ArtifactoryUploadResponse .class );
261
+ artifactoryResponse .setStatusLine (response .getStatusLine ());
263
262
} catch (Exception e ) {
264
263
// Displays the response received from the client and the stacktrace in case an Exception caught.
265
264
log .info ("Response received: \n \n " + content + "\n \n " );
266
265
log .error ("Failed while reading the response from: " + httpPut , e );
267
- } finally {
268
- in .close ();
269
266
}
270
267
}
268
+ return artifactoryResponse ;
271
269
}
272
-
273
- if (artifactoryResponse == null ) {
274
- artifactoryResponse = new ArtifactoryUploadResponse ();
275
- }
276
- StatusLine statusLine = response .getStatusLine ();
277
- artifactoryResponse .setStatusLine (statusLine );
278
- return artifactoryResponse ;
279
270
}
280
271
281
272
/**
282
- * @param entity the entity to retrive the message from.
273
+ * @param entity the entity to retrieve the message from.
283
274
* @return response entity content.
284
- * @throws IOException
275
+ * @throws IOException if entity couldn't serialize
285
276
*/
286
277
287
278
public String getMessageFromEntity (HttpEntity entity ) throws IOException {
288
279
String responseMessage = "" ;
289
280
if (entity != null ) {
290
281
responseMessage = getResponseEntityContent (entity );
291
- EntityUtils .consume (entity );
292
282
if (StringUtils .isNotBlank (responseMessage )) {
293
283
responseMessage = " Response message: " + responseMessage ;
294
284
}
@@ -306,7 +296,7 @@ public String getMessageFromEntity(HttpEntity entity) throws IOException {
306
296
private String getResponseEntityContent (HttpEntity responseEntity ) throws IOException {
307
297
InputStream in = responseEntity .getContent ();
308
298
if (in != null ) {
309
- return IOUtils .toString (in , "UTF-8" );
299
+ return IOUtils .toString (in , StandardCharsets . UTF_8 );
310
300
}
311
301
return "" ;
312
302
}
0 commit comments