Skip to content

Commit

Permalink
MOSIP-32842 Migrated javax
Browse files Browse the repository at this point in the history
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr committed May 16, 2024
1 parent b33f327 commit 935ef37
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;

import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

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.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.Header;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.ssl.TrustStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
Expand All @@ -35,9 +35,7 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import com.google.gson.Gson;

import io.mosip.kernel.core.logger.spi.Logger;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.kernel.core.util.StringUtils;
Expand All @@ -47,6 +45,7 @@
import io.mosip.pms.common.dto.SecretKeyRequest;
import io.mosip.pms.common.dto.TokenRequestDTO;
import io.mosip.pms.common.exception.ApiAccessibleException;
import org.apache.hc.core5.ssl.SSLContexts;

@Component
public class RestUtil {
Expand Down Expand Up @@ -248,12 +247,17 @@ public <T> T getApi(String apiUrl, Map<String, String> pathsegments, Class<?> re
*/
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)
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient((org.apache.hc.client5.http.classic.HttpClient) httpClient);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

var connnectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create();
connnectionManagerBuilder.setSSLSocketFactory(csf);
var connectionManager = connnectionManagerBuilder.build();
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setConnectionManager(connectionManager);
requestFactory.setHttpClient(httpClientBuilder.build());
return new RestTemplate(requestFactory);
}

Expand All @@ -265,7 +269,7 @@ public RestTemplate getRestTemplate() throws KeyManagementException, NoSuchAlgor
* @throws IOException
*/
@SuppressWarnings("unchecked")
private HttpEntity<Object> setRequestHeader(Object requestType, MediaType mediaType) throws IOException {
private HttpEntity<Object> setRequestHeader(Object requestType, MediaType mediaType) throws IOException, NoSuchAlgorithmException, KeyStoreException {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
final String token = getToken();
headers.add("Cookie",token);
Expand Down Expand Up @@ -300,7 +304,7 @@ private HttpEntity<Object> setRequestHeader(Object requestType, MediaType mediaT
* @return
* @throws IOException
*/
private String getToken() throws IOException {
private String getToken() throws IOException, NoSuchAlgorithmException, KeyStoreException {
String token = System.getProperty("token");
boolean isValid = false;

Expand All @@ -317,13 +321,13 @@ private String getToken() throws IOException {
tokenRequestDTO.setRequest(setSecretKeyRequestDTO());

Gson gson = new Gson();
HttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(environment.getProperty("pms.cert.service.token.request.issuerUrl"));
try {
StringEntity postingString = new StringEntity(gson.toJson(tokenRequestDTO));
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
CloseableHttpResponse response = httpClient.execute(post);
Header[] cookie = response.getHeaders("Set-Cookie");
if (cookie.length == 0)
throw new IOException("cookie is empty. Could not generate new token.");
Expand Down
4 changes: 0 additions & 4 deletions partner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
Expand Down

0 comments on commit 935ef37

Please sign in to comment.