Skip to content

Commit d922623

Browse files
committed
Modernize I/O
1 parent 863ca19 commit d922623

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.apache.commons.httpclient.Header;
3232
import org.apache.commons.httpclient.HostConfiguration;
3333
import org.apache.commons.httpclient.HttpClient;
34-
import org.apache.commons.httpclient.HttpException;
3534
import org.apache.commons.httpclient.HttpState;
3635
import org.apache.commons.httpclient.HttpStatus;
3736
import org.apache.commons.httpclient.StatusLine;
@@ -57,7 +56,6 @@ public ClassicJiraDownloader() {}
5756

5857
/**
5958
* Execute the query on the JIRA server.
60-
*
6159
*/
6260
public void doExecute() {
6361
try {
@@ -306,22 +304,17 @@ private void determineProxy(String jiraUrl, HttpClient client) {
306304
* @param link the URL to JIRA
307305
*/
308306
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);
317307

308+
GetMethod gm = new GetMethod(link);
309+
getLog().info("Downloading from JIRA at: " + link);
310+
gm.setFollowRedirects(true);
311+
try {
318312
cl.executeMethod(gm);
319313

320314
StatusLine sl = gm.getStatusLine();
321315

322316
if (sl == null) {
323317
getLog().error("Unknown error validating link: " + link);
324-
325318
return;
326319
}
327320

@@ -333,47 +326,34 @@ private void download(final HttpClient cl, final String link) {
333326
getLog().warn("Site sent redirect, but did not set Location header");
334327
} else {
335328
String newLink = locationHeader.getValue();
336-
337329
getLog().debug("Following redirect to " + newLink);
338-
339330
download(cl, newLink);
340331
}
341332
}
342333

343334
if (gm.getStatusCode() == HttpStatus.SC_OK) {
344-
in = gm.getResponseBodyAsStream();
345-
346335
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+
}
348341
}
349342

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+
}
359348
} else {
360349
getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
361350
}
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-
}
368351
} catch (IOException e) {
369352
if (getLog().isDebugEnabled()) {
370353
getLog().error("Error downloading issues from JIRA:", e);
371354
} else {
372355
getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
373356
}
374-
} finally {
375-
IOUtil.close(out);
376-
IOUtil.close(in);
377357
}
378358
}
379359

0 commit comments

Comments
 (0)