hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1633170 - in /httpcomponents/httpcore/trunk: httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java httpcore/src/examples/org/apache/http/examples/HttpFileServer.java
Date Mon, 20 Oct 2014 15:25:35 GMT
Author: olegk
Date: Mon Oct 20 15:25:35 2014
New Revision: 1633170

URL: http://svn.apache.org/r1633170
Log:
Simplifed SSLContext initialization code in examples

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java
    httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/HttpFileServer.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java?rev=1633170&r1=1633169&r2=1633170&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpFileServer.java
Mon Oct 20 15:25:35 2014
@@ -30,12 +30,9 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URLDecoder;
-import java.security.KeyStore;
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
-import javax.net.ssl.KeyManager;
-import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
 
 import org.apache.http.ExceptionLogger;
@@ -58,6 +55,7 @@ import org.apache.http.nio.protocol.Http
 import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpCoreContext;
+import org.apache.http.ssl.SSLContexts;
 
 /**
  * Embedded HTTP/1.1 file server based on a non-blocking I/O model and capable of direct
channel
@@ -80,20 +78,14 @@ public class NHttpFileServer {
         SSLContext sslcontext = null;
         if (port == 8443) {
             // Initialize SSL context
-            ClassLoader cl = NHttpFileServer.class.getClassLoader();
-            URL url = cl.getResource("my.keystore");
+            URL url = NHttpFileServer.class.getResource("/my.keystore");
             if (url == null) {
                 System.out.println("Keystore not found");
                 System.exit(1);
             }
-            KeyStore keystore  = KeyStore.getInstance("jks");
-            keystore.load(url.openStream(), "secret".toCharArray());
-            KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
-                    KeyManagerFactory.getDefaultAlgorithm());
-            kmfactory.init(keystore, "secret".toCharArray());
-            KeyManager[] keymanagers = kmfactory.getKeyManagers();
-            sslcontext = SSLContext.getInstance("TLS");
-            sslcontext.init(keymanagers, null, null);
+            sslcontext = SSLContexts.custom()
+                    .loadKeyMaterial(url, "secret".toCharArray(), "secret".toCharArray())
+                    .build();
         }
 
         IOReactorConfig config = IOReactorConfig.custom()

Modified: httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/HttpFileServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/HttpFileServer.java?rev=1633170&r1=1633169&r2=1633170&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/HttpFileServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/examples/org/apache/http/examples/HttpFileServer.java
Mon Oct 20 15:25:35 2014
@@ -33,12 +33,9 @@ import java.net.SocketTimeoutException;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
-import java.security.KeyStore;
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
 
-import javax.net.ssl.KeyManager;
-import javax.net.ssl.KeyManagerFactory;
 import javax.net.ssl.SSLContext;
 
 import org.apache.http.ConnectionClosedException;
@@ -60,6 +57,7 @@ import org.apache.http.impl.bootstrap.Se
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.ssl.SSLContexts;
 import org.apache.http.util.EntityUtils;
 
 /**
@@ -82,20 +80,14 @@ public class HttpFileServer {
         SSLContext sslcontext = null;
         if (port == 8443) {
             // Initialize SSL context
-            ClassLoader cl = HttpFileServer.class.getClassLoader();
-            URL url = cl.getResource("my.keystore");
+            URL url = HttpFileServer.class.getResource("/my.keystore");
             if (url == null) {
                 System.out.println("Keystore not found");
                 System.exit(1);
             }
-            KeyStore keystore  = KeyStore.getInstance("jks");
-            keystore.load(url.openStream(), "secret".toCharArray());
-            KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
-                    KeyManagerFactory.getDefaultAlgorithm());
-            kmfactory.init(keystore, "secret".toCharArray());
-            KeyManager[] keymanagers = kmfactory.getKeyManagers();
-            sslcontext = SSLContext.getInstance("TLS");
-            sslcontext.init(keymanagers, null, null);
+            sslcontext = SSLContexts.custom()
+                    .loadKeyMaterial(url, "secret".toCharArray(), "secret".toCharArray())
+                    .build();
         }
 
         SocketConfig socketConfig = SocketConfig.custom()



Mime
View raw message