Skip to content

Commit

Permalink
HTTPSUtil: improve logging
Browse files Browse the repository at this point in the history
Make the code a little more DRY. And in debug mode, emit stack traces.
  • Loading branch information
ctrueden committed Jul 11, 2020
1 parent 48a0f1f commit a23f815
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/net/imagej/updater/util/HTTPSUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,40 @@ public static void checkHTTPSSupport(LogService log) {
connection.setRequestMethod("HEAD");
connection.setConnectTimeout(10000);
} catch (ProtocolException e) {
e.printStackTrace();
if (log != null) log.error(e);
else e.printStackTrace();
}
try {
connection.getResponseCode();
secureMode = true;
} catch (UnknownHostException e) {
String msg = "Could not determine the IP address of https://imagej.net. "
+ "Make sure you are connected to a network.";
if (log != null) log.warn(msg);
else System.out.println("[WARNING] " + msg);
warn(log, msg, e);
offlineMode = true;
} catch (SSLHandshakeException e) {
secureMode = false;
String msg = "Your Java might be too old to handle updates via HTTPS. This is a security risk. " +
"Depending on your setup please download a recent version of this software or update your local Java installation.";
if (log != null) log.warn(msg);
else System.out.println("[WARNING] " + msg);
warn(log, msg, e);
} catch (SocketTimeoutException e) {
secureMode = false;
String msg = "Timeout while trying to update securely via HTTPS. Will fall back to HTTP. This is a security risk. " +
"Please contact your system administrator to enable communication via HTTPS.";
if (log != null) log.warn(msg);
else System.out.println("[WARNING] " + msg);
warn(log, msg, e);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void warn(final LogService log, final String msg, final Throwable t) {
if (log != null) {
if (log.isDebug()) log.debug(msg, t);
else log.warn(msg);
}
else System.out.println("[WARNING] " + msg);
}

/**
* @return whether this ImageJ instance can handle HTTPS
*/
Expand Down

0 comments on commit a23f815

Please sign in to comment.