From e457ca704e1ec66898f4b66b7fb9b167b3664849 Mon Sep 17 00:00:00 2001 From: urviljoshi Date: Fri, 16 Sep 2022 19:08:25 +0530 Subject: [PATCH] MOSIP-23945 bean defination modified --- .../pms/config/AuthRestTemplateConfig.java | 17 +++++++++- .../config/KeycloakRestTemplateConfig.java | 17 +++++++++- .../io/mosip/pms/common/util/RestUtil.java | 33 +++++++++++-------- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/config/AuthRestTemplateConfig.java b/partner/partner-management-service/src/main/java/io/mosip/pms/config/AuthRestTemplateConfig.java index 890d325f93..16c04a6fe3 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/config/AuthRestTemplateConfig.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/config/AuthRestTemplateConfig.java @@ -1,8 +1,12 @@ package io.mosip.pms.config; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.client.RestTemplate; @@ -15,10 +19,21 @@ @EnableAsync public class AuthRestTemplateConfig { + @Value("${pms.auth.httpclient.connections.max.per.host:20}") + private int maxConnectionPerRoute; + + @Value("${pms.auth.httpclient.connections.max:100}") + private int totalMaxConnection; + @Primary @Bean(name = "authRestTemplate") public RestTemplate restTemplate() { - return new RestTemplate(); + HttpClientBuilder httpClientBuilder = HttpClients.custom() + .setMaxConnPerRoute(maxConnectionPerRoute) + .setMaxConnTotal(totalMaxConnection).disableCookieManagement(); + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + requestFactory.setHttpClient(httpClientBuilder.build()); + return new RestTemplate(requestFactory); } @Bean diff --git a/partner/partner-management-service/src/main/java/io/mosip/pms/config/KeycloakRestTemplateConfig.java b/partner/partner-management-service/src/main/java/io/mosip/pms/config/KeycloakRestTemplateConfig.java index 132377c488..7f194156bf 100644 --- a/partner/partner-management-service/src/main/java/io/mosip/pms/config/KeycloakRestTemplateConfig.java +++ b/partner/partner-management-service/src/main/java/io/mosip/pms/config/KeycloakRestTemplateConfig.java @@ -2,22 +2,37 @@ import java.util.Collections; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import io.mosip.pms.partner.keycloak.service.RestInterceptor; @Configuration public class KeycloakRestTemplateConfig { + + @Value("${pms.keycloak.httpclient.connections.max.per.host:20}") + private int maxConnectionPerRoute; + + @Value("${pms.keycloak.httpclient.connections.max:100}") + private int totalMaxConnection; @Autowired private RestInterceptor restInterceptor; @Bean(name = "keycloakRestTemplate") public RestTemplate getRestTemplate() { - RestTemplate restTemplate = new RestTemplate(); + HttpClientBuilder httpClientBuilder = HttpClients.custom() + .setMaxConnPerRoute(maxConnectionPerRoute) + .setMaxConnTotal(totalMaxConnection).disableCookieManagement(); + HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); + requestFactory.setHttpClient(httpClientBuilder.build()); + RestTemplate restTemplate = new RestTemplate(requestFactory); restTemplate.setInterceptors(Collections.singletonList(restInterceptor)); return restTemplate; } diff --git a/partner/pms-common/src/main/java/io/mosip/pms/common/util/RestUtil.java b/partner/pms-common/src/main/java/io/mosip/pms/common/util/RestUtil.java index d5879ce03b..748761c40c 100644 --- a/partner/pms-common/src/main/java/io/mosip/pms/common/util/RestUtil.java +++ b/partner/pms-common/src/main/java/io/mosip/pms/common/util/RestUtil.java @@ -5,24 +5,19 @@ import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; import java.util.Iterator; import java.util.List; import java.util.Map; -import javax.net.ssl.SSLContext; - import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.TrustStrategy; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -48,10 +43,21 @@ import io.mosip.pms.common.dto.TokenRequestDTO; import io.mosip.pms.common.exception.ApiAccessibleException; + @Component public class RestUtil { private static final Logger logger = PMSLogger.getLogger(RestUtil.class); + + + + private RestTemplate localRestTemplate; + + @Value("${pms.default.httpclient.connections.max.per.host:20}") + private int maxConnectionPerRoute; + + @Value("${pms.default.httpclient.connections.max:100}") + private int totalMaxConnection; @Autowired private Environment environment; @@ -207,14 +213,15 @@ public T getApi(String apiUrl, Map pathsegments, Class re * @throws KeyStoreException */ public RestTemplate getRestTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { - TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; - SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy) - .build(); - SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); - CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build(); + if(localRestTemplate==null) { + HttpClientBuilder httpClientBuilder = HttpClients.custom() + .setMaxConnPerRoute(maxConnectionPerRoute) + .setMaxConnTotal(totalMaxConnection).disableCookieManagement(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); - requestFactory.setHttpClient(httpClient); - return new RestTemplate(requestFactory); + requestFactory.setHttpClient(httpClientBuilder.build()); + localRestTemplate= new RestTemplate(requestFactory); + } + return localRestTemplate; } /**