io.mosip.kernel.packetmanager.util.RestUtil Maven / Gradle / Ivy
package io.mosip.kernel.packetmanager.util;
import com.google.gson.Gson;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.kernel.core.util.StringUtils;
import io.mosip.kernel.core.util.TokenHandlerUtil;
import io.mosip.kernel.packetmanager.dto.Metadata;
import io.mosip.kernel.packetmanager.dto.PasswordRequest;
import io.mosip.kernel.packetmanager.dto.SecretKeyRequest;
import io.mosip.kernel.packetmanager.dto.TokenRequestDTO;
import io.mosip.kernel.packetmanager.exception.ApiNotAccessibleException;
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.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
public class RestUtil {
@Autowired
private Environment environment;
private static final String AUTHORIZATION = "Authorization=";
public T postApi(String uri, MediaType mediaType, Object requestType, Class> responseClass) throws ApiNotAccessibleException {
RestTemplate restTemplate;
T result = null;
try {
restTemplate = getRestTemplate();
result = (T) restTemplate.postForObject(uri, setRequestHeader(requestType, mediaType), responseClass);
} catch (Exception e) {
throw new ApiNotAccessibleException(e);
}
return result;
}
public T getApi(URI uri, Class> responseType) throws ApiNotAccessibleException {
RestTemplate restTemplate;
T result = null;
try {
restTemplate = getRestTemplate();
result = (T) restTemplate.exchange(uri, HttpMethod.GET, setRequestHeader(null, null), responseType)
.getBody();
} catch (Exception e) {
throw new ApiNotAccessibleException(e);
}
return result;
}
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();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return new RestTemplate(requestFactory);
}
private HttpEntity
© 2015 - 2025 Weber Informatics LLC | Privacy Policy