package de.fau.amos.virtualledger.server.banking.adorsys.api.bankAccessEndpoint;

import de.fau.amos.virtualledger.server.banking.adorsys.api.BankingApiUrlProvider;
import de.fau.amos.virtualledger.server.banking.adorsys.api.JerseyClientUtility;
import de.fau.amos.virtualledger.server.banking.adorsys.api.json.BankAccessJSONBankingModel;
import de.fau.amos.virtualledger.server.banking.model.BankAccessBankingModel;
import de.fau.amos.virtualledger.server.banking.model.BankingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;

@Component

@Qualifier("default")
public class HttpBankAccessEndpoint implements BankAccessEndpoint {
    private static final int HTTP_OK_RESPONSE = 201;
    private static final int HTTP_OK = 200;

    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private final BankingApiUrlProvider urlProvider;

    @Autowired
    public HttpBankAccessEndpoint(final BankingApiUrlProvider urlProvider) {
        this.urlProvider = urlProvider;
    }

    @Override
    public List<BankAccessBankingModel> getBankAccesses(final String userId) throws BankingException {

        // Create Jersey client
        final Client client = JerseyClientUtility.getLoggingClient(LOGGER);

        String url = urlProvider.getBankAccessEndpointUrl();
        WebTarget webTarget = client.target(url);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.get();

        if (response.getStatus() != HTTP_OK) {
            throw new BankingException("No connection to Adorsys Server!");
        }
        BankAccessJSONBankingModel reponseModel = response.readEntity(BankAccessJSONBankingModel.class);
        if (reponseModel == null || reponseModel.getEmbedded() == null) {
            LOGGER.info("No access found!");
            return new ArrayList<BankAccessBankingModel>();
        }
        List<BankAccessBankingModel> result = reponseModel.getEmbedded().getBankAccessEntityList();
        return result;
    }

    @Override
    public void addBankAccess(String userId, BankAccessBankingModel bankAccess) throws BankingException {

        // Create Jersey client
        Client client = ClientBuilder.newClient();

        String url = urlProvider.getBankAccessEndpointUrl();
        WebTarget webTarget = client.target(url);
        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.post(Entity.entity(bankAccess, MediaType.APPLICATION_JSON_TYPE));

        if (response.getStatus() != HTTP_OK_RESPONSE) {
            throw new BankingException("Creating Banking Access failed!");
        }
    }
}

@@ -753,49 +753,8 @@
 nt;%0A
-import javax.ws.rs.client.ClientBuilder;%0A
 impo
@@ -1599,22 +1599,21 @@
  String 
-userId
+token
 ) throws
@@ -1742,32 +1742,38 @@
 GGER);%0A%0A        
+final 
 String url = url
@@ -1809,32 +1809,38 @@
 tUrl();%0A        
+final 
 WebTarget webTar
@@ -1865,32 +1865,38 @@
 t(url);%0A        
+final 
 Invocation.Build
@@ -1960,32 +1960,63 @@
 ATION_JSON_TYPE)
+.header(%22Authorization%22, token)
 ;%0A        Respon
@@ -2001,32 +2001,38 @@
 token);%0A        
+final 
 Response respons
@@ -2202,16 +2202,22 @@
         
+final 
 BankAcce
@@ -2237,16 +2237,17 @@
 Model re
+s
 ponseMod
@@ -2320,16 +2320,17 @@
   if (re
+s
 ponseMod
@@ -2345,16 +2345,17 @@
 ll %7C%7C re
+s
 ponseMod
@@ -2460,38 +2460,16 @@
 rayList%3C
-BankAccessBankingModel
 %3E();%0A   
@@ -2487,48 +2487,18 @@
     
-List%3CBankAccessBankingModel%3E result =
+return
  re
+s
 pons
@@ -2549,31 +2549,8 @@
 ();%0A
-        return result;%0A
     
@@ -2596,16 +2596,22 @@
 kAccess(
+final 
 String u
@@ -2616,16 +2616,22 @@
  userId,
+ final
  BankAcc
@@ -2725,16 +2725,22 @@
         
+final 
 Client c
@@ -2751,32 +2751,51 @@
 t = 
-ClientBuilder.new
+JerseyClientUtility.getLogging
 Client(
+LOGGER
 );%0A%0A
@@ -2802,16 +2802,22 @@
         
+final 
 String u
@@ -2869,16 +2869,22 @@
         
+final 
 WebTarge
@@ -2925,16 +2925,22 @@
         
+final 
 Invocati
@@ -3030,16 +3030,22 @@
         
+final 
 Response

Fix warnings in HttpBankAccessEndpoint.java