// 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 ai.vespa.util.http.hc4.VespaHttpClientBuilder;
import com.yahoo.container.jdisc.HttpResponse;
import java.util.logging.Level;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

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

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

    private final CloseableHttpClient client = VespaHttpClientBuilder.create().build();

    @Override
    public HttpResponse get(Params params, URL url) {
        try {
            HttpGet request = new HttpGet(url.toURI());
            request.addHeader("Connection", "Close");
            request.setConfig(
                    RequestConfig.custom()
                            .setConnectTimeout(params.readTimeoutMs)
                            .setSocketTimeout(params.readTimeoutMs)
                            .build());
            try (CloseableHttpResponse response = client.execute(request)) {
                HttpEntity entity = response.getEntity();
                return new StaticResponse(
                        response.getStatusLine().getStatusCode(),
                        entity.getContentType().getValue(),
                        EntityUtils.toString(entity));
            }
        } catch (ConnectTimeoutException | SocketTimeoutException e) {
            String message = "Timed out after " + params.readTimeoutMs + " ms reading response from " + url;
            logger.log(Level.WARNING, message, e);
            throw new RequestTimeoutException(message);
        } catch (IOException e) {
            String message = "Failed to get response from " + url;
            logger.log(Level.WARNING, message, e);
            throw new InternalServerException(message);
        } catch (URISyntaxException e) {
            String message = "Invalid URL: " + e.getMessage();
            logger.log(Level.WARNING, message, e);
            throw new InternalServerException(message, e);
        }
    }
}

@@ -189,9 +189,9 @@
 p.hc
-4
+5
 .Ves
@@ -269,66 +269,114 @@
 ort 
-java.util.logging.Level;%0Aimport org.apache.http.HttpEntity
+org.apache.hc.client5.http.classic.methods.HttpGet;%0Aimport org.apache.hc.client5.http.config.RequestConfig
 ;%0Aim
@@ -396,39 +396,55 @@
 he.h
-ttp
+c
 .client
-.config.RequestConfig
+5.http.impl.classic.CloseableHttpClient
 ;%0Aim
@@ -460,34 +460,43 @@
 apache.h
-ttp
+c
 .client
-.methods
+5.http.impl.classic
 .Closeab
@@ -534,184 +534,208 @@
 he.h
-ttp.client.methods.HttpGet;%0Aimport org.apache.http.conn.ConnectTimeoutException;%0Aimport org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.util.EntityUtils
+c.core5.http.HttpEntity;%0Aimport org.apache.hc.core5.http.ParseException;%0Aimport org.apache.hc.core5.http.io.entity.EntityUtils;%0Aimport org.apache.hc.core5.util.Timeout;%0A%0Aimport java.util.logging.Level
 ;%0A%0Ai
@@ -1437,32 +1437,55 @@
 tConnectTimeout(
+Timeout.ofMilliseconds(
 params.readTimeo
@@ -1481,32 +1481,33 @@
 s.readTimeoutMs)
+)
 %0A               
@@ -1523,29 +1523,54 @@
     .set
-SocketTimeout
+ResponseTimeout(Timeout.ofMilliseconds
 (params.
@@ -1583,16 +1583,17 @@
 meoutMs)
+)
 %0A       
@@ -1842,30 +1842,8 @@
 .get
-StatusLine().getStatus
 Code
@@ -1897,19 +1897,8 @@
 pe()
-.getValue()
 ,%0A  
@@ -1985,34 +1985,8 @@
 ch (
-ConnectTimeoutException %7C 
 Sock
@@ -2217,32 +2217,150 @@
 ption(message);%0A
+        %7D catch (ParseException e) %7B%0A            logger.log(Level.WARNING, %22Parse error of response from %22 + url, e);%0A
         %7D catch 

Unify on hc5 [run-systemtest]