Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOSIP-23945 bean defination modified #490

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -207,14 +213,15 @@ public <T> T getApi(String apiUrl, Map<String, String> 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;
}

/**
Expand Down