¤ñ¹ï·sÀÉ®× |
| | |
| | | package com.pollex.pam.service.util; |
| | | |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.apache.http.conn.ssl.NoopHostnameVerifier; |
| | | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.ssl.SSLContexts; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import java.security.KeyManagementException; |
| | | import java.security.KeyStoreException; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.cert.X509Certificate; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | public final class HttpRequestUtil { |
| | | private static final Logger log = LoggerFactory.getLogger(HttpRequestUtil.class); |
| | | |
| | | private HttpRequestUtil() {} |
| | | |
| | | public static <T> ResponseEntity<T> getWithJson(String url, Class<T> responseType) |
| | | throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException |
| | | { |
| | | return getWithJson(url, responseType, null); |
| | | } |
| | | |
| | | public static <T> ResponseEntity<T> getWithJson(String url, Class<T> responseType, Map<String, String> addedHeaders) |
| | | throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException |
| | | { |
| | | RestTemplate restTemplate = getTrustAllRestTemplate(); |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | setHeaders(headers, addedHeaders); |
| | | |
| | | HttpEntity<String> httpEntity = new HttpEntity<>(headers); |
| | | |
| | | log.debug("HttpRequestUtil get url: {}", url); |
| | | log.debug("httpEntity = {}", httpEntity); |
| | | return restTemplate.exchange(url, HttpMethod.GET, httpEntity, responseType); |
| | | } |
| | | |
| | | public static ResponseEntity<String> postWithJson(String url, Object jsonData, Map<String, String> headers) |
| | | throws KeyStoreException, NoSuchAlgorithmException, JsonProcessingException, KeyManagementException |
| | | { |
| | | return postWithJson(url, jsonData, String.class, headers); |
| | | } |
| | | |
| | | public static ResponseEntity<String> postWithJson(String url, Object jsonData) |
| | | throws KeyStoreException, NoSuchAlgorithmException, JsonProcessingException, KeyManagementException |
| | | { |
| | | return postWithJson(url, jsonData, String.class, null); |
| | | } |
| | | |
| | | public static <T> ResponseEntity<T> postWithJson(String url, Object jsonData, Class<T> responseType) |
| | | throws KeyStoreException, NoSuchAlgorithmException, JsonProcessingException, KeyManagementException |
| | | { |
| | | return postWithJson(url, jsonData, responseType, null); |
| | | } |
| | | |
| | | public static <T> ResponseEntity<T> postWithJson(String url, Object jsonData, Class<T> responseType, Map<String, String> addedHeaders) |
| | | throws JsonProcessingException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException { |
| | | |
| | | String parameters = new ObjectMapper().writeValueAsString(jsonData); |
| | | // é¿å
é·åº¦éé·é²èå°è´logçæ |
| | | if(parameters.length() < 1000){ |
| | | log.debug("parameters : {}",parameters); |
| | | } |
| | | |
| | | RestTemplate restTemplate = getTrustAllRestTemplate(); |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | setHeaders(headers, addedHeaders); |
| | | |
| | | HttpEntity<String> entity = new HttpEntity<>(parameters, headers); |
| | | |
| | | log.debug("rest post with json, url = {}", url); |
| | | return restTemplate.exchange(url, HttpMethod.POST, entity, responseType); |
| | | } |
| | | |
| | | private static void setHeaders(HttpHeaders headers, Map<String, String> addedHeaders) { |
| | | if(addedHeaders != null && addedHeaders.size() > 0) { |
| | | Set<String> keys = addedHeaders.keySet(); |
| | | for (String key : keys) { |
| | | String headerValue = addedHeaders.get(key); |
| | | headers.set(key, headerValue); |
| | | log.info("http cust header key: {}", key); |
| | | log.info("http cust header headerValue: {}", headerValue); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static RestTemplate getTrustAllRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { |
| | | SSLContext sslContext = SSLContexts.custom() |
| | | .loadTrustMaterial(null, (X509Certificate[] x509Certs, String s) -> true) |
| | | .build(); |
| | | SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); |
| | | CloseableHttpClient httpClient = HttpClients.custom() |
| | | .setSSLSocketFactory(csf) |
| | | .build(); |
| | | HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); |
| | | requestFactory.setHttpClient(httpClient); |
| | | requestFactory.setConnectTimeout(300000); |
| | | requestFactory.setReadTimeout(300000); |
| | | return new RestTemplate(requestFactory); |
| | | } |
| | | } |