From 25411330ebb968607b22939f8fa9f5730b25f1c2 Mon Sep 17 00:00:00 2001 From: SimonBrandhof Date: Fri, 5 Apr 2013 15:00:05 +0200 Subject: [PATCH] SONARPLUGINS-2574 improve handling of connection failure --- .../org/sonar/runner/impl/FileDownloader.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/FileDownloader.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/FileDownloader.java index 285673c..2e66b50 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/FileDownloader.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/FileDownloader.java @@ -28,8 +28,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; +import java.net.UnknownHostException; import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; @@ -66,13 +68,22 @@ class FileDownloader { Logs.debug("Download " + fullUrl + " to " + toFile.getAbsolutePath()); } HttpURLConnection connection = newHttpConnection(new URL(fullUrl)); + int statusCode = connection.getResponseCode(); + if (statusCode != HttpURLConnection.HTTP_OK) { + throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); + } output = new FileOutputStream(toFile, false); input = connection.getInputStream(); IOUtils.copyLarge(input, output); - } catch (IOException e) { + + } catch (Exception e) { + if (e instanceof ConnectException || e instanceof UnknownHostException) { + Logs.error("Sonar server '" + serverUrl + "' can not be reached"); + } IOUtils.closeQuietly(output); FileUtils.deleteQuietly(toFile); throw new IllegalStateException("Fail to download: " + fullUrl, e); + } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); @@ -86,13 +97,20 @@ class FileDownloader { if (charset == null || "".equals(charset)) { charset = "UTF-8"; } - Reader reader = new InputStreamReader(conn.getInputStream(), charset); + Reader reader = null; try { int statusCode = conn.getResponseCode(); if (statusCode != HttpURLConnection.HTTP_OK) { throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); } + reader = new InputStreamReader(conn.getInputStream(), charset); return IOUtils.toString(reader); + } catch (IOException e) { + if (e instanceof ConnectException || e instanceof UnknownHostException) { + Logs.error("Sonar server '" + serverUrl + "' can not be reached"); + } + throw e; + } finally { IOUtils.closeQuietly(reader); conn.disconnect(); -- 2.39.5