/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2018, Gluu
 */

package org.gluu.oxauth.fido2.client;

import java.io.IOException;

import javax.ws.rs.core.UriBuilder;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Helper class which creates proxy Fido2 services
 *
 * @author Yuriy Movchan
 * @version 12/21/2018
 */
public class Fido2ClientFactory {

    private final static Fido2ClientFactory instance = new Fido2ClientFactory();

    private ObjectMapper objectMapper;

    private Fido2ClientFactory() {
        this.objectMapper = new ObjectMapper();
    }

    public static Fido2ClientFactory instance() {
        return instance;
    }

    public ConfigurationService createMetaDataConfigurationService(String metadataUri) {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(UriBuilder.fromPath(metadataUri));
        ConfigurationService proxy = target.proxy(ConfigurationService.class);
        
        return proxy;
    }

    public AttestationService createAttestationService(String metadata) throws IOException {
        JsonNode metadataJson = objectMapper.readTree(metadata);
        String basePath = metadataJson.get("attestation").get("base_uri").asText();

        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(UriBuilder.fromPath(basePath));
        AttestationService proxy = target.proxy(AttestationService.class);
        
        return proxy;
    }

    public AssertionService createAssertionService(String metadata) throws IOException {
        JsonNode metadataJson = objectMapper.readTree(metadata);
        String basePath = metadataJson.get("assertion").get("base_uri").asText();

        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(UriBuilder.fromPath(basePath));
        AssertionService proxy = target.proxy(AssertionService.class);
        
        return proxy;
    }

}

@@ -244,16 +244,189 @@
 ilder;%0A%0A
+import org.apache.http.impl.client.CloseableHttpClient;%0Aimport org.apache.http.impl.client.HttpClients;%0Aimport org.apache.http.impl.conn.PoolingHttpClientConnectionManager;%0A
 import o
@@ -464,32 +464,32 @@
 ResteasyClient;%0A
-
 import org.jboss
@@ -591,16 +591,88 @@
 bTarget;
+%0Aimport org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
 %0A%0Aimport
@@ -989,24 +989,68 @@
 Factory();%0A%0A
+    private ApacheHttpClient4Engine engine;%0A
     private 
@@ -1075,16 +1075,21 @@
 tMapper;
+%0A    
 %0A%0A    pr
@@ -1113,24 +1113,62 @@
 Factory() %7B%0A
+        this.engine = createEngine();%0A
         this
@@ -1433,32 +1433,51 @@
 ClientBuilder().
+httpEngine(engine).
 build();%0A       
@@ -1965,32 +1965,51 @@
 ClientBuilder().
+httpEngine(engine).
 build();%0A       
@@ -2492,16 +2492,35 @@
 ilder().
+httpEngine(engine).
 build();
@@ -2673,32 +2673,32 @@
 lass);%0A        %0A
-
         return p
@@ -2710,10 +2710,528 @@
 %0A    %7D%0A%0A
+    private ApacheHttpClient4Engine createEngine() %7B%0A        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();%0A        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();%0A        cm.setMaxTotal(200); // Increase max total connection to 200%0A        cm.setDefaultMaxPerRoute(20); // Increase default max connection per route to 20%0A        ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);%0A        %0A        return engine;%0A    %7D%0A%0A
 %7D%0A

Use closable http connection pool in Fido2 client