adding support for https in UriUtils
authorAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Wed, 17 Jun 2015 14:23:38 +0000 (16:23 +0200)
committerAndreas Kohlbecker <a.kohlbecker@bgbm.org>
Wed, 17 Jun 2015 14:29:25 +0000 (16:29 +0200)
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/UriUtils.java
cdmlib-commons/src/test/java/eu/etaxonomy/cdm/common/media/ImageInfoTest.java

index 1cd87c2d65e1ab1530206d3a47a7463e27aed9a5..eafd8c81bd3ac71720eb0f24bca8939d8a4aa174 100644 (file)
@@ -21,12 +21,19 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
@@ -42,6 +49,8 @@ import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.ssl.SSLSocketFactory;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.log4j.Logger;
@@ -219,6 +228,19 @@ public class UriUtils {
         // Create an instance of HttpClient.
         HttpClient  client = new DefaultHttpClient();
 
+        try {
+            SSLContext sc = SSLContext.getInstance("SSL");
+            sc.init(null, getTrustingManager(), new java.security.SecureRandom());
+            SSLSocketFactory socketFactory = new SSLSocketFactory(sc);
+            Scheme sch = new Scheme("https", 443, socketFactory);
+            client.getConnectionManager().getSchemeRegistry().register(sch);
+        } catch (KeyManagementException e1) {
+            throw new RuntimeException("Registration of ssl support failed", e1);
+        } catch (NoSuchAlgorithmException e2) {
+            throw new RuntimeException("Registration of ssl support failed", e2);
+        }
+
+
         HttpUriRequest method;
         switch (httpMethod) {
         case GET:
@@ -454,4 +476,25 @@ public class UriUtils {
         }
         return success;
     }
+
+    private static TrustManager[] getTrustingManager() {
+        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
+            @Override
+            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                return null;
+            }
+
+            @Override
+            public void checkClientTrusted(X509Certificate[] certs, String authType) {
+                // Do nothing
+            }
+
+            @Override
+            public void checkServerTrusted(X509Certificate[] certs, String authType) {
+                // Do nothing
+            }
+
+        } };
+        return trustAllCerts;
+    }
 }
index 8e9766fcafea8363b7f66caf894ba7825e4ddae7..37e069974e1edc6cc15a18e69a4405857c286545 100644 (file)
@@ -17,8 +17,6 @@ import org.junit.Before;
 import org.junit.Test;
 
 import eu.etaxonomy.cdm.common.UriUtils;
-import eu.etaxonomy.cdm.common.media.ImageInfo;
-import eu.etaxonomy.cdm.common.media.MimeType;
 
 /**
  * @author n.hoffmann
@@ -49,7 +47,7 @@ public class ImageInfoTest {
         URL tiffUrl = ImageInfoTest.class.getResource("/images/OregonScientificDS6639-DSC_0307-small.tif");
         tiffUri = tiffUrl.toURI();
 
-        remotePngUri = URI.create("http://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
+        remotePngUri = URI.create("https://dev.e-taxonomy.eu/trac_htdocs/logo_edit.png");
     }
 
     @Test