| | |
| | | package com.smartearth.poiexcel.utils; |
| | | |
| | | import com.smartearth.poiexcel.entity.StaticData; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.http.HttpEntity; |
| | | import org.apache.http.NameValuePair; |
| | | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| | | import org.apache.http.client.methods.CloseableHttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.client.methods.HttpPost; |
| | | 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.HttpClientBuilder; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.util.EntityUtils; |
| | | import org.apache.http.ssl.SSLContexts; |
| | | import org.apache.http.ssl.TrustStrategy; |
| | | import org.springframework.http.*; |
| | | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.PrintStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import javax.net.ssl.SSLContext; |
| | | import java.lang.reflect.Field; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class RestHelper { |
| | | private static RestTemplate restTemplate; |
| | | |
| | | private static RestTemplate httpsRestTemplate; |
| | | |
| | | private final static Log log = LogFactory.getLog(RestHelper.class); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * Get请求-HttpURLConnection |
| | | * |
| | | * @param url URL地址 |
| | | * @return 字符串 |
| | | * @throws IOException IO异常 |
| | | * 获取RestTemplate |
| | | */ |
| | | public static String getForConn(String url) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | | public static RestTemplate getHttpsRestTemplate() { |
| | | if (null == httpsRestTemplate) { |
| | | try { |
| | | TrustStrategy acceptingTrustStrategy = (chain, authType) -> true; |
| | | SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); |
| | | SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); |
| | | |
| | | try { |
| | | URL restUrl = new URL(url); |
| | | HttpClientBuilder clientBuilder = HttpClients.custom(); |
| | | |
| | | conn = (HttpURLConnection) restUrl.openConnection(); |
| | | // POST,GET,PUT,DELETE |
| | | conn.setRequestMethod("GET"); |
| | | conn.setRequestProperty("Accept", "application/json"); |
| | | CloseableHttpClient httpClient = clientBuilder.setSSLSocketFactory(sslFactory).build(); |
| | | |
| | | br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| | | HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); |
| | | requestFactory.setHttpClient(httpClient); |
| | | |
| | | String line; |
| | | StringBuilder sb = new StringBuilder(); |
| | | while ((line = br.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | |
| | | return sb.toString(); |
| | | } finally { |
| | | if (br != null) { |
| | | br.close(); |
| | | } |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | httpsRestTemplate = new RestTemplate(requestFactory); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | return httpsRestTemplate; |
| | | } |
| | | |
| | | /** |
| | | * Post请求-HttpURLConnection |
| | | * |
| | | * @param url URL地址 |
| | | * @param query 查询条件 |
| | | * @return 字符串 |
| | | * @throws IOException IO异常 |
| | | * get请求(Rest) |
| | | */ |
| | | public static String postForConn(String url, String query) throws IOException { |
| | | BufferedReader br = null; |
| | | HttpURLConnection conn = null; |
| | | public static <T> T getForRest(String url, Class<T> clazz) { |
| | | // RestTemplate rest = getHttpsRestTemplate() |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | |
| | | try { |
| | | URL restUrl = new URL(url); |
| | | return rest.getForObject(url, clazz); |
| | | } |
| | | |
| | | conn = (HttpURLConnection) restUrl.openConnection(); |
| | | // POST,GET,PUT,DELETE |
| | | conn.setRequestMethod("POST"); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setDoOutput(true); |
| | | /** |
| | | * post请求(Rest) |
| | | */ |
| | | public static <T> T postForRest(String url, Object map, Class<T> clazz) { |
| | | // RestTemplate rest = getHttpsRestTemplate() |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | |
| | | PrintStream ps = new PrintStream(conn.getOutputStream()); |
| | | ps.print(query); |
| | | ps.close(); |
| | | return rest.postForObject(url, map, clazz); |
| | | } |
| | | |
| | | // OutputStream out = conn.getOutputStream() |
| | | // out.write(query.getBytes()) |
| | | // out.close() |
| | | /** |
| | | * delete请求(Rest) |
| | | */ |
| | | public static <T> T deleteForRest(String url, Map<String, T> map, Class<T> clazz) { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_JSON); |
| | | |
| | | br = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| | | HttpEntity<?> entity = new HttpEntity<>(map, headers); |
| | | |
| | | String line; |
| | | StringBuilder sb = new StringBuilder(); |
| | | while ((line = br.readLine()) != null) { |
| | | sb.append(line); |
| | | } |
| | | // RestTemplate rest = getHttpsRestTemplate() |
| | | RestTemplate rest = RestHelper.getRestTemplate(); |
| | | ResponseEntity<T> rs = rest.exchange(url, HttpMethod.DELETE, entity, clazz); |
| | | |
| | | return sb.toString(); |
| | | } finally { |
| | | if (br != null) { |
| | | br.close(); |
| | | } |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | return rs.getBody(); |
| | | } |
| | | |
| | | /** |
| | | * 获取Map数据 |
| | | */ |
| | | public static <T> Map<String, Object> getMapData(T t) { |
| | | Map<String, Object> map = new HashMap<>(1); |
| | | |
| | | Field[] fields = t.getClass().getDeclaredFields(); |
| | | for (Field field : fields) { |
| | | try { |
| | | if ("serialVersionUID".equals(field.getName())) { |
| | | continue; |
| | | } |
| | | |
| | | field.setAccessible(true); |
| | | Object obj = field.get(t); |
| | | |
| | | map.put(field.getName(), obj); |
| | | } catch (Exception ex) { |
| | | // |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Get请求-CloseableHttpClient |
| | | * |
| | | * @param uri Uri地址 |
| | | * @return 响应字符串 |
| | | */ |
| | | public static String get(String uri) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | | |
| | | HttpGet httpGet = new HttpGet(uri); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpGet); |
| | | // 取出返回体 |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Post请求-CloseableHttpClient |
| | | * |
| | | * @param uri Uri地址 |
| | | * @param postData 待发送数据 |
| | | * @return 响应字符串 |
| | | */ |
| | | public static String post(String uri, List<NameValuePair> postData) { |
| | | try { |
| | | CloseableHttpClient httpClient = HttpClients.custom().build(); |
| | | |
| | | UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(postData, StaticData.TEXT_ENCODER); |
| | | HttpPost httpPost = new HttpPost(uri); |
| | | httpPost.setEntity(postEntity); |
| | | |
| | | CloseableHttpResponse closeResponse = httpClient.execute(httpPost); |
| | | |
| | | // 取出返回体 |
| | | HttpEntity entity = closeResponse.getEntity(); |
| | | |
| | | return EntityUtils.toString(entity, StaticData.TEXT_ENCODER); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return getErrorInfo(uri, ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取错误信息 |
| | | * |
| | | * @param uri Uri地址 |
| | | * @param ex 异常 |
| | | * @return 错误信息 |
| | | */ |
| | | public static String getErrorInfo(String uri, Exception ex) { |
| | | Map<String, Object> map = new LinkedHashMap<>(); |
| | | map.put("result", null); |
| | | map.put("message", ex.getMessage()); |
| | | map.put("code", 400); |
| | | map.put("uri", uri); |
| | | //map.put("tag", StaticData.CACHE_PREFIX) |
| | | |
| | | return map.toString(); |
| | | } |
| | | |
| | | /** |
| | | * GET请求(REST) |
| | | */ |
| | | public static String getForRest(String uri) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(uri, String.class); |
| | | } |
| | | |
| | | /** |
| | | * GET请求(REST) |
| | | */ |
| | | public static <T> T getForRest(String uri, Class<T> clazz) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.getForObject(uri, clazz); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static String postForRest(String uri, Map<String, Object> map) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, map, String.class); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static <T> String postForRest(String uri, List<T> list) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, list, String.class); |
| | | } |
| | | |
| | | /** |
| | | * POST请求(REST) |
| | | */ |
| | | public static <T> String postForRest(String uri, T t) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | return rest.postForObject(uri, t, String.class); |
| | | } |
| | | |
| | | /** |
| | | * DELETE请求(REST) |
| | | */ |
| | | public static void deleteForRest(String uri) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | rest.delete(uri); |
| | | } |
| | | |
| | | /** |
| | | * DELETE请求(REST) |
| | | */ |
| | | public static void deleteForRest(String uri, Map<String, Object> map) { |
| | | RestTemplate rest = getRestTemplate(); |
| | | |
| | | rest.delete(uri, map); |
| | | return map; |
| | | } |
| | | } |