/*
 * DownloadClient Geodateninfrastruktur Bayern
 *
 * (c) 2016 GSt. GDI-BY (gdi.bayern.de)
 *
 * Licensed 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.
 */
package de.bayern.gdi.utils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/** Helper for HTTP. */
public final class HTTP {

    private static final Logger log
        = Logger.getLogger(HTTP.class.getName());

    private HTTP() {
    }

    /**
     * Returns a HTTP client doing Basic Auth if needed.
     * @param url The URL to browse.
     * @param user The user. Maybe null.
     * @param password The password. Maybe null.
     * @return The HTTP client.
     */
    public static CloseableHttpClient getClient(
        URL url, String user, String password
    ) {
        if (user == null || password == null) {
            return HttpClients.createDefault();
        }

        UsernamePasswordCredentials defaultCreds
            = new UsernamePasswordCredentials(user, password);

        HttpClientContext context = HttpClientContext.create();
        BasicCredentialsProvider credsProv = new BasicCredentialsProvider();

        credsProv.setCredentials(
            new AuthScope(url.getHost(), url.getPort()), defaultCreds);

        context.setCredentialsProvider(credsProv);

        BasicAuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        HttpHost target = new HttpHost(url.getHost(), url.getPort());

        authCache.put(target, basicAuth);
        context.setAuthCache(authCache);

        return HttpClients
            .custom()
            .setDefaultCredentialsProvider(credsProv)
            .build();
    }

    /**
     * Closes a client without throwing exceptions.
     * @param client The client to close.
     */
    public static void closeGraceful(CloseableHttpClient client) {
        if (client == null) {
            return;
        }
        try {
            client.close();
        } catch (IOException ioe) {
            // Only log this.
            log.log(Level.SEVERE,
                "Closing HTTP client failed: " + ioe.getMessage(), ioe);
        }
    }

    private static RequestConfig getProxyConfig(String proto) {
        String proxy = System.getProperty(proto + ".proxyHost");
        Integer port = Integer.getInteger(proto + ".proxyPort");
        HttpHost proxyHost = new HttpHost(proxy, port, proto);
        return RequestConfig.custom()
                .setProxy(proxyHost)
                .build();
    }

    private static void configureProxy(HttpGet request, String proto) {
        String proxy = System.getProperty(proto + ".proxyHost");
        if (proxy != null) {
            request.setConfig(getProxyConfig(proto));
        }
    }

    private static void configureProxy(HttpPost request, String proto) {
        String proxy = System.getProperty(proto + ".proxyHost");
        if (proxy != null) {
            request.setConfig(getProxyConfig(proto));
        }
    }

    /**
     * Returns a configured GET request.
     * @param url The URL to browse.
     * @return The GET requst object.
     * @throws URISyntaxException If the URL is not valid.
     */
    public static HttpGet getGetRequest(URL url) throws URISyntaxException {
        HttpGet request = new HttpGet(url.toURI());
        configureProxy(request, url.getProtocol().equals("https")
            ? "https"
            : "http");
        return request;
    }

    /**
     * Ret urns a configured Post request.
     * @param url the URL to post to
     * @return the POST Request Object
     * @throws URISyntaxException if the url is not valid
     */
    public static HttpPost getPostRequest(URL url)  throws URISyntaxException {
        HttpPost request = new HttpPost(url.toURI());
        configureProxy(request, url.getProtocol().equals("https")
                ? "https"
                : "http");
        return request;
    }
}

@@ -699,32 +699,63 @@
 io.IOException;%0A
+import java.net.ProxySelector;%0A
 import java.net.
@@ -766,32 +766,32 @@
 yntaxException;%0A
-
 import java.net.
@@ -856,24 +856,25 @@
 ing.Logger;%0A
+%0A
 import org.a
@@ -994,60 +994,8 @@
 ls;%0A
-import org.apache.http.client.config.RequestConfig;%0A
 impo
@@ -1404,16 +1404,76 @@
 Clients;
+%0Aimport org.apache.http.impl.conn.SystemDefaultRoutePlanner;
 %0A%0A/** He
@@ -2747,24 +2747,180 @@
 uthCache);%0A%0A
+        // Use JVM proxy settings.%0A        SystemDefaultRoutePlanner routePlanner%0A            = new SystemDefaultRoutePlanner(ProxySelector.getDefault());%0A%0A
         retu
@@ -3010,16 +3010,59 @@
 dsProv)%0A
+            .setRoutePlanner(routePlanner)%0A
         
@@ -3555,848 +3555,8 @@
  %7D%0A%0A
-    private static RequestConfig getProxyConfig(String proto) %7B%0A        String proxy = System.getProperty(proto + %22.proxyHost%22);%0A        Integer port = Integer.getInteger(proto + %22.proxyPort%22);%0A        HttpHost proxyHost = new HttpHost(proxy, port, proto);%0A        return RequestConfig.custom()%0A                .setProxy(proxyHost)%0A                .build();%0A    %7D%0A%0A    private static void configureProxy(HttpGet request, String proto) %7B%0A        String proxy = System.getProperty(proto + %22.proxyHost%22);%0A        if (proxy != null) %7B%0A            request.setConfig(getProxyConfig(proto));%0A        %7D%0A    %7D%0A%0A    private static void configureProxy(HttpPost request, String proto) %7B%0A        String proxy = System.getProperty(proto + %22.proxyHost%22);%0A        if (proxy != null) %7B%0A            request.setConfig(getProxyConfig(proto));%0A        %7D%0A    %7D%0A%0A
     
@@ -3831,25 +3831,14 @@
     
-HttpGet request =
+return
  new
@@ -3864,143 +3864,8 @@
 ));%0A
-        configureProxy(request, url.getProtocol().equals(%22https%22)%0A            ? %22https%22%0A            : %22http%22);%0A        return request;%0A
     
@@ -4152,26 +4152,14 @@
     
-HttpPost request =
+return
  new
@@ -4186,151 +4186,8 @@
 ));%0A
-        configureProxy(request, url.getProtocol().equals(%22https%22)%0A                ? %22https%22%0A                : %22http%22);%0A        return request;%0A
     

Use JVM setting for proxy configuration.