// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.log.LogLevel;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.logging.Logger;

public class SimpleHttpFetcher implements HttpFetcher {
    private static final Logger logger = Logger.getLogger(SimpleHttpFetcher.class.getName());

    @Override
    public HttpResponse get(Params params, URL url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(params.readTimeoutMs);
            int code = connection.getResponseCode();
            String contentType = connection.getContentType();
            InputStream inputStream = connection.getInputStream();
            StaticResponse response = new StaticResponse(code, contentType, inputStream);
            return response;
        } catch (SocketTimeoutException e) {
            String message = "Timed out after " + params.readTimeoutMs + " ms reading response from " + url;
            logger.log(LogLevel.WARNING, message, e);
            throw new RequestTimeoutException(message);
        } catch (IOException e) {
            String message = "Failed to get response from " + url;
            logger.log(LogLevel.WARNING, message, e);
            throw new InternalServerException(message);
        }
    }
}

@@ -157,16 +157,66 @@
 .http;%0A%0A
+import ai.vespa.util.http.VespaHttpClientBuilder;%0A
 import c
@@ -289,94 +289,376 @@
 el;%0A
-%0A
 import 
-java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.net.HttpURLConnec
+org.apache.http.HttpEntity;%0Aimport org.apache.http.client.config.RequestConfig;%0Aimport org.apache.http.client.methods.CloseableHttpResponse;%0Aimport org.apache.http.client.methods.HttpGet;%0Aimport org.apache.http.conn.ConnectTimeoutException;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.util.EntityUtils;%0A%0Aimport java.io.IOExcep
 tion
@@ -691,32 +691,68 @@
 meoutException;%0A
+import java.net.URISyntaxException;%0A
 import java.net.
@@ -941,16 +941,105 @@
 me());%0A%0A
+    private final CloseableHttpClient client = VespaHttpClientBuilder.create().build();%0A%0A
     @Ove
@@ -1132,107 +1132,215 @@
 Http
-URLConnection connection = (HttpURLConnection) url.openConnection();%0A            connection.setRead
+Get request = new HttpGet(url.toURI());%0A            request.addHeader(%22Connection%22, %22Close%22);%0A            request.setConfig(%0A                    RequestConfig.custom()%0A                            .setConnect
 Time
@@ -1364,17 +1364,16 @@
 meoutMs)
-;
 %0A       
@@ -1381,46 +1381,100 @@
     
-int code = connection.getResponseCode(
+                .setSocketTimeout(params.readTimeoutMs)%0A                            .build()
 );%0A 
@@ -1488,261 +1488,412 @@
     
-S
 tr
-ing contentType = connection.getContentType();%0A            InputStream inputStream = connection.getInputStream();%0A            StaticResponse response = new StaticResponse(code, contentType, inputStream);%0A            return response;%0A        %7D catch (
+y (CloseableHttpResponse response = client.execute(request)) %7B%0A                HttpEntity entity = response.getEntity();%0A                return new StaticResponse(%0A                        response.getStatusLine().getStatusCode(),%0A                        entity.getContentType().getValue(),%0A                        EntityUtils.toString(entity));%0A            %7D%0A        %7D catch (ConnectTimeoutException %7C 
 Sock
@@ -2338,32 +2338,249 @@
 ption(message);%0A
+        %7D catch (URISyntaxException e) %7B%0A            String message = %22Invalid URL: %22 + e.getMessage();%0A            logger.log(LogLevel.WARNING, message, e);%0A            throw new InternalServerException(message, e);%0A
         %7D%0A    %7D%0A

Use VespaHttpClientBuilder in SimpleHttpFetcher