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

import com.yahoo.vespa.applicationmodel.HostName;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.proxy.WebResourceFactory;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;

/**
 * @author bakksjo
 */
public class JerseyJaxRsClientFactory implements JaxRsClientFactory {
    private static final int DEFAULT_CONNECT_TIMEOUT_MS = 30000;
    private static final int DEFAULT_READ_TIMEOUT_MS = 30000;

    private final int connectTimeoutMs;
    private final int readTimeoutMs;

    public JerseyJaxRsClientFactory() {
        this(DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS);
    }

    public JerseyJaxRsClientFactory(final int connectTimeoutMs, final int readTimeoutMs) {
        this.connectTimeoutMs = connectTimeoutMs;
        this.readTimeoutMs = readTimeoutMs;
    }

    /**
     * Contains some workarounds for HTTP/JAX-RS/Jersey issues. See:
     *   https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/ClientProperties.html#SUPPRESS_HTTP_COMPLIANCE_VALIDATION
     *   https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/client/HttpUrlConnectorProvider.html#SET_METHOD_WORKAROUND
     */
    @Override
    public <T> T createClient(final Class<T> apiClass, final HostName hostName, final int port, final String pathPrefix, String scheme) {
        final UriBuilder uriBuilder = UriBuilder.fromPath(pathPrefix).host(hostName.s()).port(port).scheme(scheme);
        final Client webClient = ClientBuilder.newClient()
                .property(ClientProperties.CONNECT_TIMEOUT, connectTimeoutMs)
                .property(ClientProperties.READ_TIMEOUT, readTimeoutMs)
                .property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true) // Allow empty PUT. TODO: Fix API.
                .property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true) // Allow e.g. PATCH method.
                .property(ClientProperties.FOLLOW_REDIRECTS, true);
        final WebTarget target = webClient.target(uriBuilder);
        // TODO: Check if this fills up non-heap memory with loaded classes.
        return WebResourceFactory.newResource(apiClass, target);
    }
}

@@ -377,16 +377,49 @@
 ctory;%0A%0A
+import javax.net.ssl.SSLContext;%0A
 import j
@@ -511,25 +511,109 @@
 .client.
-WebTarget
+ClientRequestFilter;%0Aimport javax.ws.rs.client.WebTarget;%0Aimport javax.ws.rs.core.HttpHeaders
 ;%0Aimport
@@ -641,16 +641,46 @@
 Builder;
+%0Aimport java.util.Collections;
 %0A%0A/**%0A *
@@ -974,16 +974,93 @@
 meoutMs;
+%0A    private final SSLContext sslContext;%0A    private final String userAgent;
 %0A%0A    pu
@@ -1207,59 +1207,421 @@
 ory(
-final int connectTimeoutMs, final int readTimeoutMs
+SSLContext sslContext, String userAgent) %7B%0A        this(DEFAULT_CONNECT_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS, sslContext, userAgent);%0A    %7D%0A%0A    public JerseyJaxRsClientFactory(final int connectTimeoutMs, final int readTimeoutMs) %7B%0A        this(connectTimeoutMs, readTimeoutMs, null, null);%0A    %7D%0A%0A    public JerseyJaxRsClientFactory(int connectTimeoutMs, int readTimeoutMs, SSLContext sslContext, String userAgent
 ) %7B%0A
@@ -1710,24 +1710,98 @@
 dTimeoutMs;%0A
+        this.sslContext = sslContext;%0A        this.userAgent = userAgent;%0A
     %7D%0A%0A    /
@@ -2436,30 +2436,29 @@
     
-final Client webClient
+ClientBuilder builder
  = C
@@ -2473,22 +2473,23 @@
 lder.new
-Client
+Builder
 ()%0A     
@@ -2926,24 +2926,326 @@
 CTS, true);%0A
+        if (sslContext != null) %7B%0A            builder.sslContext(sslContext);%0A        %7D%0A        if (userAgent != null) %7B%0A            builder.register((ClientRequestFilter) context -%3E%0A                    context.getHeaders().put(HttpHeaders.USER_AGENT, Collections.singletonList(userAgent)));%0A        %7D%0A
         fina
@@ -3269,17 +3269,23 @@
 t = 
-webClient
+builder.build()
 .tar

Allow custom SSLContext and user agent