31
31
import org .apache .commons .httpclient .Header ;
32
32
import org .apache .commons .httpclient .HostConfiguration ;
33
33
import org .apache .commons .httpclient .HttpClient ;
34
- import org .apache .commons .httpclient .HttpException ;
35
34
import org .apache .commons .httpclient .HttpState ;
36
35
import org .apache .commons .httpclient .HttpStatus ;
37
36
import org .apache .commons .httpclient .StatusLine ;
@@ -57,7 +56,6 @@ public ClassicJiraDownloader() {}
57
56
58
57
/**
59
58
* Execute the query on the JIRA server.
60
- *
61
59
*/
62
60
public void doExecute () {
63
61
try {
@@ -306,22 +304,17 @@ private void determineProxy(String jiraUrl, HttpClient client) {
306
304
* @param link the URL to JIRA
307
305
*/
308
306
private void download (final HttpClient cl , final String link ) {
309
- InputStream in = null ;
310
- OutputStream out = null ;
311
- try {
312
- GetMethod gm = new GetMethod (link );
313
-
314
- getLog ().info ("Downloading from JIRA at: " + link );
315
-
316
- gm .setFollowRedirects (true );
317
307
308
+ GetMethod gm = new GetMethod (link );
309
+ getLog ().info ("Downloading from JIRA at: " + link );
310
+ gm .setFollowRedirects (true );
311
+ try {
318
312
cl .executeMethod (gm );
319
313
320
314
StatusLine sl = gm .getStatusLine ();
321
315
322
316
if (sl == null ) {
323
317
getLog ().error ("Unknown error validating link: " + link );
324
-
325
318
return ;
326
319
}
327
320
@@ -333,47 +326,34 @@ private void download(final HttpClient cl, final String link) {
333
326
getLog ().warn ("Site sent redirect, but did not set Location header" );
334
327
} else {
335
328
String newLink = locationHeader .getValue ();
336
-
337
329
getLog ().debug ("Following redirect to " + newLink );
338
-
339
330
download (cl , newLink );
340
331
}
341
332
}
342
333
343
334
if (gm .getStatusCode () == HttpStatus .SC_OK ) {
344
- in = gm .getResponseBodyAsStream ();
345
-
346
335
if (!output .getParentFile ().exists ()) {
347
- output .getParentFile ().mkdirs ();
336
+ if (!output .getParentFile ().mkdirs ()) {
337
+ getLog ().error ("Downloading issues from JIRA failed. Could not create "
338
+ + output .getParentFile ());
339
+ return ;
340
+ }
348
341
}
349
342
350
- // write the response to file
351
- out = new FileOutputStream (output );
352
- IOUtil .copy (in , out );
353
- out .close ();
354
- out = null ;
355
- in .close ();
356
- in = null ;
357
-
358
- getLog ().debug ("Downloading from JIRA was successful" );
343
+ try (InputStream in = gm .getResponseBodyAsStream ();
344
+ OutputStream out = new FileOutputStream (output )) {
345
+ IOUtil .copy (in , out );
346
+ getLog ().debug ("Downloading from JIRA was successful" );
347
+ }
359
348
} else {
360
349
getLog ().warn ("Downloading from JIRA failed. Received: [" + gm .getStatusCode () + "]" );
361
350
}
362
- } catch (HttpException e ) {
363
- if (getLog ().isDebugEnabled ()) {
364
- getLog ().error ("Error downloading issues from JIRA:" , e );
365
- } else {
366
- getLog ().error ("Error downloading issues from JIRA url: " + e .getLocalizedMessage ());
367
- }
368
351
} catch (IOException e ) {
369
352
if (getLog ().isDebugEnabled ()) {
370
353
getLog ().error ("Error downloading issues from JIRA:" , e );
371
354
} else {
372
355
getLog ().error ("Error downloading issues from JIRA. Cause is " + e .getLocalizedMessage ());
373
356
}
374
- } finally {
375
- IOUtil .close (out );
376
- IOUtil .close (in );
377
357
}
378
358
}
379
359
0 commit comments