package de.ganskef.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.security.GeneralSecurityException;
import java.security.KeyStore;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class Client {

    private static final int PROXY_PORT_UNDEFINED = -1;

    public File get(String url, IProxy proxy) throws Exception {
        return get(new URI(url), proxy.getProxyPort());
    }

    public File get(String url) throws Exception {
        URI uri = new URI(url);
        return get(uri, PROXY_PORT_UNDEFINED);
    }

    public File get(URI uri, int proxyPort) throws Exception {
        URLConnection con = createConnection(uri.toURL(), proxyPort);
        final boolean ssl = "https".equalsIgnoreCase(uri.getScheme());
        if (ssl) {
            return callHttpsGet(con);
        } else {
            return callHttpGet(con);
        }
    }

    private File callHttpGet(URLConnection con) throws IOException {
        con.connect();
        return read(con);
    }

    private File callHttpsGet(URLConnection con)
            throws GeneralSecurityException, IOException {
        FileInputStream is = new FileInputStream(new File(
                "littleproxy-mitm.p12"));
        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(is, "Be Your Own Lantern".toCharArray());
        is.close();

        String tma = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tma);
        tmf.init(ks);
        X509TrustManager defaultTrustManager = (X509TrustManager) tmf
                .getTrustManagers()[0];

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { defaultTrustManager }, null);
        SSLSocketFactory sslSocketFactory = context.getSocketFactory();

        ((HttpsURLConnection) con).setSSLSocketFactory(sslSocketFactory);
        con.connect();
        return read(con);
    }

    private URLConnection createConnection(URL url, int proxyPort)
            throws MalformedURLException, IOException {
        URLConnection result;
        if (proxyPort != PROXY_PORT_UNDEFINED) {
            SocketAddress sa = new InetSocketAddress("localhost", proxyPort);
            Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
            result = url.openConnection(proxy);
        } else {
            result = url.openConnection();
        }
        return result;
    }

    private File read(URLConnection con) throws IOException {
        String name = Client.class.getSimpleName();
        File result = File.createTempFile(name, ".out");
        result.deleteOnExit();
        OutputStream os = null;
        InputStream is = null;
        try {
            is = con.getInputStream();
            os = new FileOutputStream(result);
            IOUtils.copy(is, os);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        File result = new Client().get("https://localhost:8083");
        System.out.println(FileUtils.readFileToString(result));
    }

}

@@ -30,21 +30,63 @@
 ort 
-java.io.File;
+io.netty.handler.ssl.util.InsecureTrustManagerFactory;%0A
 %0Aimp
@@ -101,27 +101,16 @@
 .io.File
-InputStream
 ;%0Aimport
@@ -472,39 +472,8 @@
 ion;
-%0Aimport java.security.KeyStore;
 %0A%0Aim
@@ -621,89 +621,8 @@
 ger;
-%0Aimport javax.net.ssl.TrustManagerFactory;%0Aimport javax.net.ssl.X509TrustManager;
 %0A%0Aim
@@ -1611,24 +1611,27 @@
 on %7B%0A       
+ //
  FileInputSt
@@ -1678,23 +1678,18 @@
         
-       
+//
  %22little
@@ -1714,16 +1714,19 @@
 %0A       
+ //
  KeyStor
@@ -1771,16 +1771,19 @@
 %0A       
+ //
  ks.load
@@ -1832,16 +1832,19 @@
 %0A       
+ //
  is.clos
@@ -1848,24 +1848,34 @@
 lose();%0A
+        //
 %0A       
  String 
@@ -1858,32 +1858,35 @@
       //%0A       
+ //
  String tma = Tr
@@ -1933,16 +1933,19 @@
 %0A       
+ //
  TrustMa
@@ -2008,16 +2008,19 @@
 %0A       
+ //
  tmf.ini
@@ -2033,16 +2033,19 @@
 %0A       
+ //
  X509Tru
@@ -2107,23 +2107,18 @@
         
-       
+//
  .getTru
@@ -2134,24 +2134,34 @@
 s()%5B0%5D;%0A
+        //
 %0A       
  SSLCont
@@ -2148,24 +2148,27 @@
   //%0A       
+ //
  SSLContext 
@@ -2211,24 +2211,27 @@
 S%22);%0A       
+ //
  context.ini
@@ -2285,24 +2285,248 @@
 r %7D, null);%0A
+%0A        SSLContext context = SSLContext.getInstance(%22TLS%22);%0A        TrustManager%5B%5D trustManagers = InsecureTrustManagerFactory.INSTANCE%0A                .getTrustManagers();%0A        context.init(null, trustManagers, null);%0A%0A
         SSLS

Use insecure trust manager to connect google online to fix test failure.