
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package com.ogosecurity.proxy.http2samples;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util.Date;

import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;

/**
 * Call of HttpClient using a website whose IP address is different from inetAddress will always woir with Sync "Classic" APIs.
 * (Will fail with Async new Http/2 APIs).
 * 
 * @author Nicolas de Pomereu
 *
 */

public class RawSSLClassicRegularTest {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {

	System.out.println(new Date() + " Starting...");

	String initHost = "www.kremote-files.com";
	String destServerNameIP ="185.74.10.195";
	String destScheme = "https";
	int port = 443;
		
	HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        PoolingHttpClientConnectionManagerBuilder poolingHttpClientConnectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder
        	.create();
       
        PoolingHttpClientConnectionManager cm = poolingHttpClientConnectionManagerBuilder.build();
        httpClientBuilder.setConnectionManager(cm);
        
        RequestConfig requestConfig = RequestConfig.custom().build();
	HttpClient proxyClient = httpClientBuilder.setDefaultRequestConfig(requestConfig).build();
	
	String pathGet = "https://" + initHost;	
	
	BasicClassicHttpRequest proxyRequest = null;
	
	proxyRequest = new BasicClassicHttpRequest("GET", pathGet);
	
	InetAddress inetAddress = InetAddress.getByName(destServerNameIP);
	final HttpHost target = new HttpHost(destScheme, inetAddress, initHost, port);
	
	CloseableHttpResponse closeableHttpResponse = (CloseableHttpResponse) proxyClient.execute(target,
		proxyRequest);
	int statusCode = closeableHttpResponse.getCode();

	System.out.println(new Date() + " statusCode: " + statusCode);

	HttpEntity entity = closeableHttpResponse.getEntity();
	InputStream instream = entity.getContent();

	ByteArrayOutputStream out = new ByteArrayOutputStream();
	copyLarge(instream, out, new byte[DEFAULT_BUFFER_SIZE]);
	    
	String content = out.toString();
	System.out.println(content);
	    
	System.out.println(new Date() + " Done.");
	
    }
    
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
    private static final int EOF = -1;
    public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
            throws IOException {
        long count = 0;
        int n = 0;
        while (EOF != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

}
