import com.sun.grizzly.SSLConfig; import com.sun.grizzly.arp.AsyncExecutor; import com.sun.grizzly.arp.AsyncFilter; import com.sun.grizzly.http.embed.GrizzlyWebServer; import com.sun.grizzly.util.WorkerThread; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; /** * * @author Igor Minar */ public class TestSslServer { public static void main(String[] args) throws IOException, InterruptedException, KeyStoreException, NoSuchAlgorithmException, CertificateException, KeyManagementException { int port = 7000; String keystorePath = TestSslServer.class.getClassLoader().getResource("test.jks").getPath(); String keystorePass = "changeit"; SSLConfig sslConfig = new SSLConfig(); sslConfig.setKeyStorePass(keystorePass); sslConfig.setKeyStoreFile(keystorePath); GrizzlyWebServer gws = new GrizzlyWebServer(port, "/var/tmp/", true); gws.setSSLConfig(sslConfig); gws.addAsyncFilter(new TestFilter()); gws.start(); try { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File(keystorePath)), keystorePass.toCharArray()); TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keyStore); SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(null, tmf.getTrustManagers(), null); SSLSocketFactory sslFactory = ctx.getSocketFactory(); HttpsURLConnection con = (HttpsURLConnection) new URL("https://localhost:" + port + "/foo-that-donesnt-exist").openConnection(); con.setSSLSocketFactory(sslFactory); con.setRequestMethod("GET"); con.connect(); if (con.getResponseCode() == 404) { System.out.println("everything looks fine"); } } finally { gws.stop(); } } public static class TestFilter implements AsyncFilter { public boolean doFilter(AsyncExecutor asyncExecutor) { SSLEngine sslEngine = ((WorkerThread) Thread.currentThread()).getSSLEngine(); if (sslEngine == null) { throw new RuntimeException("SSLEngine is null!!"); } return true; } } }