changeset 9:d69b7a6d408e

Fix issue #10 ("Cannot access an Orthanc running with a self-signed HTTPS certificate")
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 15 Apr 2016 11:12:05 +0200 (2016-04-15)
parents 1411a4ecd113
children 212ff5f65ce9 ca2ee69520eb
files CMakeLists.txt NEWS com/orthancserver/OrthancConfigurationDialog.java com/orthancserver/OrthancConnection.java
diffstat 4 files changed, 32 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Apr 15 10:45:09 2016 +0200
+++ b/CMakeLists.txt	Fri Apr 15 11:12:05 2016 +0200
@@ -18,6 +18,7 @@
   ${CMAKE_SOURCE_DIR}/com/orthancserver/OrthancConnection.java
   ${CMAKE_SOURCE_DIR}/com/orthancserver/PreviewPanel.java
   ${CMAKE_SOURCE_DIR}/com/orthancserver/SelectImageDialog.java
+  ${CMAKE_SOURCE_DIR}/com/orthancserver/HttpsTrustModifier.java
 
   # Sources of the "json-simple" toolkit
   ${CMAKE_SOURCE_DIR}/org/json/simple/ItemList.java
--- a/NEWS	Fri Apr 15 10:45:09 2016 +0200
+++ b/NEWS	Fri Apr 15 11:12:05 2016 +0200
@@ -1,7 +1,8 @@
 Pending changes in the mainline
 ===============================
 
-* Fix issue #9 ("ImageJ - Error while importing this image: null")
+* Fix issue #9 ("Error while importing this image: null")
+* Fix issue #10 ("Cannot access an Orthanc running with a self-signed HTTPS certificate")
 
 
 Version 1.0.0 (2014/12/08)
--- a/com/orthancserver/OrthancConfigurationDialog.java	Fri Apr 15 10:45:09 2016 +0200
+++ b/com/orthancserver/OrthancConfigurationDialog.java	Fri Apr 15 11:12:05 2016 +0200
@@ -57,6 +57,7 @@
   private OrthancConnection CreateConnection()
   {
     OrthancConnection orthanc = new OrthancConnection();
+    orthanc.SetInsecure(true);  // Fix issue 9 (cannot connect to self-signed certificates)
     orthanc.SetName(name_.getText());
     orthanc.SetBaseUrl(url_.getText());
 
--- a/com/orthancserver/OrthancConnection.java	Fri Apr 15 10:45:09 2016 +0200
+++ b/com/orthancserver/OrthancConnection.java	Fri Apr 15 11:12:05 2016 +0200
@@ -38,16 +38,19 @@
 	private String baseUrl_;
 	private String authentication_;
   private String name_;
+  private boolean insecure_;
 	
 	OrthancConnection()
 	{
 		baseUrl_ = "http://localhost:8042/";
 		authentication_ = null;
     name_ = "Default Orthanc server";
+    insecure_ = false;
 	}
 
 
 	private static InputStream OpenUrl(String urlString,
+                                     boolean isInsecure,
 																		 String authentication,
                                      String accept) throws IOException
 	{
@@ -55,6 +58,18 @@
 
     URLConnection uc = url.openConnection();
 
+    if (isInsecure)
+    {
+      try
+      {
+        HttpsTrustModifier.Trust(uc);
+      }
+      catch (Exception e)
+      {
+        throw new IOException("Cannot allow self-signed certificates");
+      }
+    }
+
     if (authentication != null)
     {
       // http://blogs.deepal.org/2008/01/sending-basic-authentication-using-url.html
@@ -72,7 +87,7 @@
     }
     catch (ConnectException e)
     {
-      throw new IOException();
+      throw new IOException("Cannot read URL: " + urlString);
     }
 	}
 
@@ -90,7 +105,7 @@
                                 String acceptHeader) throws IOException
   {
     String url = baseUrl_ + uri;
-    return OpenUrl(url, authentication_, acceptHeader);
+    return OpenUrl(url, insecure_, authentication_, acceptHeader);
   }
 
 
@@ -117,6 +132,16 @@
     return baseUrl_;
   }
 
+  public void SetInsecure(boolean insecure)
+  {
+    insecure_ = true;
+  }
+
+  public boolean IsInsecure()
+  {
+    return insecure_;
+  }
+
   public String ReadString(String uri) throws IOException
   {
     InputStream stream = OpenStream(uri, null);
@@ -199,6 +224,7 @@
   public static OrthancConnection Unserialize(JSONObject json)
   {
     OrthancConnection c = new OrthancConnection();
+    c.SetInsecure(true);  // Fix issue 9 (cannot connect to self-signed certificates)
     c.SetName((String) json.get("Name"));
     c.SetBaseUrl((String) json.get("Url"));