Showing
6 changed files
with
665 additions
and
2 deletions
... | ... | @@ -50,6 +50,26 @@ |
50 | 50 | <artifactId>spring-boot-starter-test</artifactId> |
51 | 51 | <scope>test</scope> |
52 | 52 | </dependency> |
53 | + <dependency> | |
54 | + <groupId>org.apache.httpcomponents</groupId> | |
55 | + <artifactId>httpclient</artifactId> | |
56 | + <version>4.5.13</version> | |
57 | + </dependency> | |
58 | + <dependency> | |
59 | + <groupId>commons-collections</groupId> | |
60 | + <artifactId>commons-collections</artifactId> | |
61 | + <version>3.2.2</version> | |
62 | + </dependency> | |
63 | + <dependency> | |
64 | + <groupId>org.apache.commons</groupId> | |
65 | + <artifactId>commons-lang3</artifactId> | |
66 | + <version>3.8.1</version> | |
67 | + </dependency> | |
68 | + <dependency> | |
69 | + <groupId>com.alibaba</groupId> | |
70 | + <artifactId>fastjson</artifactId> | |
71 | + <version>1.2.37</version> | |
72 | + </dependency> | |
53 | 73 | </dependencies> |
54 | 74 | |
55 | 75 | <build> | ... | ... |
1 | +package com.mass.controller; | |
2 | + | |
3 | +import com.mass.service.UserSyncService; | |
4 | +import lombok.extern.slf4j.Slf4j; | |
5 | +import org.springframework.data.repository.query.Param; | |
6 | +import org.springframework.scheduling.annotation.Scheduled; | |
7 | +import org.springframework.web.bind.annotation.GetMapping; | |
8 | +import org.springframework.web.bind.annotation.RequestMapping; | |
9 | +import org.springframework.web.bind.annotation.RestController; | |
10 | + | |
11 | +import javax.annotation.Resource; | |
12 | +import java.io.IOException; | |
13 | + | |
14 | +@Slf4j | |
15 | +@RestController | |
16 | +@RequestMapping(value = "/sync") | |
17 | +public class UserSyncController { | |
18 | + | |
19 | + @Resource | |
20 | + private UserSyncService userSyncService; | |
21 | + | |
22 | + @GetMapping("/syncAllUser") | |
23 | + public String syncAllUser() throws IOException { | |
24 | + userSyncService.syncAllUser(); | |
25 | + return "ok"; | |
26 | + } | |
27 | + | |
28 | + @GetMapping("/syncUpdateUser") | |
29 | + public String syncUpdateUser() throws IOException { | |
30 | + userSyncService.syncUpdateUser(); | |
31 | + return "ok"; | |
32 | + } | |
33 | + | |
34 | + @GetMapping("/syncAllOrg") | |
35 | + public String syncAllOrg() throws IOException { | |
36 | + userSyncService.syncAllOrg(); | |
37 | + return "ok"; | |
38 | + } | |
39 | + | |
40 | + @GetMapping("/syncUpdateOrg") | |
41 | + public String syncUpdateOrg() throws IOException { | |
42 | + userSyncService.syncUpdateOrg(); | |
43 | + return "ok"; | |
44 | + } | |
45 | + | |
46 | + @GetMapping("/getLastExecTime") | |
47 | + public String getLastExecTime(@Param("fileName") String fileName) throws IOException { | |
48 | + userSyncService.getLastExecTime(fileName); | |
49 | + return "ok"; | |
50 | + } | |
51 | +} | ... | ... |
1 | 1 | package com.mass.service; |
2 | 2 | |
3 | +import com.alibaba.fastjson.JSONArray; | |
4 | +import com.alibaba.fastjson.JSONObject; | |
5 | +import com.mass.util.HttpClientUtils; | |
3 | 6 | import lombok.extern.slf4j.Slf4j; |
7 | +import org.apache.http.entity.ContentType; | |
8 | +import org.apache.http.entity.StringEntity; | |
9 | +import org.springframework.beans.factory.annotation.Value; | |
4 | 10 | import org.springframework.scheduling.annotation.Scheduled; |
5 | 11 | import org.springframework.stereotype.Service; |
6 | 12 | |
13 | +import java.io.*; | |
14 | +import java.text.SimpleDateFormat; | |
15 | +import java.time.LocalDateTime; | |
16 | +import java.time.format.DateTimeFormatter; | |
17 | +import java.util.Date; | |
18 | +import java.util.HashMap; | |
19 | +import java.util.Map; | |
20 | + | |
7 | 21 | @Service |
8 | 22 | @Slf4j |
9 | 23 | public class UserSyncService { |
24 | + | |
25 | + @Value("${mass.domain}") | |
26 | + private String massDomain; | |
27 | + | |
28 | + @Value("${mass.userListUrl}") | |
29 | + private String massUserListUrl; | |
30 | + | |
31 | + @Value("${mass.userUpdateListUrl}") | |
32 | + private String massUserUpdateListUrl; | |
33 | + | |
34 | + @Value("${mass.orgListUrl}") | |
35 | + private String massOrgListUrl; | |
36 | + | |
37 | + @Value("${mass.orgListUpdateUrl}") | |
38 | + private String massOrgListUpdateUrl; | |
39 | + | |
40 | + @Value("${mass.syscode}") | |
41 | + private String massSyscode; | |
42 | + | |
43 | + @Value("${mass.tmpFilePath}") | |
44 | + private String tmpFilePath; | |
45 | + | |
46 | + private static final Integer PAGE_SIZE = 1000; | |
47 | + private static final String USER_DATE_FILE = "userDate.txt"; | |
48 | + private static final String ORG_DATE_FILE = "orgDate.txt"; | |
49 | + | |
10 | 50 | @Scheduled(cron = "*/5 * * * * ?") |
11 | - public void snyc(){ | |
12 | - log.info("test"); | |
51 | + public void snyc() { | |
52 | + log.info("====test"); | |
53 | + } | |
54 | + | |
55 | + | |
56 | + public void syncAllOrg() throws IOException { | |
57 | + String url = massDomain + massOrgListUrl; | |
58 | + Map<String, Object> paramsMap = new HashMap<>(); | |
59 | + Integer pageNo = 1; | |
60 | + paramsMap.put("page", pageNo); | |
61 | + paramsMap.put("limit", PAGE_SIZE); | |
62 | + JSONArray dataList = getData(url, paramsMap, pageNo); | |
63 | + getLastExecTime(ORG_DATE_FILE); | |
64 | + log.info("=======================syncAllOrg data:" + JSONObject.toJSONString(dataList)); | |
65 | + } | |
66 | + | |
67 | + public void syncUpdateOrg() throws IOException { | |
68 | + String url = massDomain + massOrgListUpdateUrl; | |
69 | + Map<String, Object> paramsMap = new HashMap<>(); | |
70 | + Integer pageNo = 1; | |
71 | + paramsMap.put("page", pageNo); | |
72 | + paramsMap.put("limit", PAGE_SIZE); | |
73 | + String startTime = getLastExecTime(USER_DATE_FILE); | |
74 | + LocalDateTime now = LocalDateTime.now(); | |
75 | + String endTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
76 | + paramsMap.put("startTime", startTime); | |
77 | + paramsMap.put("endTime", endTime); | |
78 | + JSONArray dataList = getData(url, paramsMap, pageNo); | |
79 | + getLastExecTime(ORG_DATE_FILE); | |
80 | + log.info("=======================syncAllOrg data:" + JSONObject.toJSONString(dataList)); | |
81 | + } | |
82 | + | |
83 | + public void syncAllUser() throws IOException { | |
84 | + String url = massDomain + massUserListUrl; | |
85 | + Map<String, Object> paramsMap = new HashMap<>(); | |
86 | + Integer pageNo = 1; | |
87 | + paramsMap.put("page", pageNo); | |
88 | + paramsMap.put("limit", PAGE_SIZE); | |
89 | + JSONArray dataList = getData(url, paramsMap, pageNo); | |
90 | + getLastExecTime(USER_DATE_FILE); | |
91 | + log.info("=======================syncAllUser data:" + JSONObject.toJSONString(dataList)); | |
92 | + } | |
93 | + | |
94 | + public void syncUpdateUser() throws IOException { | |
95 | + String url = massDomain + massUserUpdateListUrl; | |
96 | + Map<String, Object> paramsMap = new HashMap<>(); | |
97 | + Integer pageNo = 1; | |
98 | + paramsMap.put("page", pageNo); | |
99 | + paramsMap.put("limit", PAGE_SIZE); | |
100 | + String startTime = getLastExecTime(USER_DATE_FILE); | |
101 | + LocalDateTime now = LocalDateTime.now(); | |
102 | + String endTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
103 | + paramsMap.put("startTime", startTime); | |
104 | + paramsMap.put("endTime", endTime); | |
105 | + JSONArray dataList = getData(url, paramsMap, pageNo); | |
106 | + log.info("=======================syncUpdateUser data:" + JSONObject.toJSONString(dataList)); | |
107 | + } | |
108 | + | |
109 | + public String getLastExecTime(String fileName) throws IOException { | |
110 | + String filePath = tmpFilePath + fileName; | |
111 | + File file = new File(filePath); | |
112 | + if (!file.getParentFile().exists()) { | |
113 | + if (!file.getParentFile().mkdirs()) { | |
114 | + log.error("create dateFileDir error!"); | |
115 | + return null; | |
116 | + } | |
117 | + } | |
118 | + if (!file.exists()) { | |
119 | + if (!file.createNewFile()) { | |
120 | + log.error("create dateFile error!"); | |
121 | + } | |
122 | + return setNowDateFile(filePath); | |
123 | + } else { | |
124 | + String lastDate = readFile(filePath); | |
125 | + setNowDateFile(filePath); | |
126 | + return lastDate; | |
127 | + } | |
128 | + } | |
129 | + | |
130 | + private String setNowDateFile(String filePath) { | |
131 | + LocalDateTime now = LocalDateTime.now(); | |
132 | + String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
133 | + writeFile(filePath, nowStr); | |
134 | + return nowStr; | |
135 | + } | |
136 | + | |
137 | + private void writeFile(String path, String content) { | |
138 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) { | |
139 | + writer.write(content); | |
140 | + } catch (IOException e) { | |
141 | + e.printStackTrace(); | |
142 | + } | |
143 | + } | |
144 | + | |
145 | + | |
146 | + private String readFile(String path) { | |
147 | + StringBuilder content = new StringBuilder(); | |
148 | + try (BufferedReader br = new BufferedReader(new FileReader(path))) { | |
149 | + String line; | |
150 | + while ((line = br.readLine()) != null) { | |
151 | + content.append(line); | |
152 | + } | |
153 | + } catch (IOException e) { | |
154 | + e.printStackTrace(); | |
155 | + } | |
156 | + return content.toString(); | |
157 | + } | |
158 | + | |
159 | + private JSONArray getData(String url, Map<String, Object> paramsMap, Integer pageNo) { | |
160 | + JSONArray dataList = new JSONArray(); | |
161 | + JSONObject result = invokeMassInterface(url, paramsMap); | |
162 | + if (result != null) { | |
163 | + Integer count = result.getInteger("count"); | |
164 | + JSONArray data = result.getJSONArray("data"); | |
165 | + dataList.addAll(data); | |
166 | + int pageNum = count / PAGE_SIZE; | |
167 | + pageNum = count % PAGE_SIZE != 0 ? pageNum + 1 : pageNum; | |
168 | + while (pageNo < pageNum) { | |
169 | + pageNo++; | |
170 | + paramsMap.put("page", pageNo); | |
171 | + result = invokeMassInterface(url, paramsMap); | |
172 | + if (result != null) { | |
173 | + JSONArray currentData = result.getJSONArray("data"); | |
174 | + dataList.addAll(currentData); | |
175 | + } | |
176 | + } | |
177 | + } | |
178 | + return dataList; | |
179 | + } | |
180 | + | |
181 | + private JSONObject invokeMassInterface(String url, Map<String, Object> paramsMap) { | |
182 | + Map<String, String> header = new HashMap<>(); | |
183 | + header.put("syscode", massSyscode); | |
184 | + String toJson = JSONObject.toJSONString(paramsMap); | |
185 | + StringEntity myEntity = new StringEntity(toJson, ContentType.APPLICATION_JSON); | |
186 | + String sResult = HttpClientUtils.doPostRequest(url, header, null, myEntity); | |
187 | + JSONObject result = JSONObject.parseObject(sResult); | |
188 | + Integer code = result.getInteger("code"); | |
189 | + if (code == 200) { | |
190 | + return result; | |
191 | + } else { | |
192 | + log.info("========invoke error! url:" + url + " params:" + JSONObject.toJSONString(paramsMap) + " result:" + JSONObject.toJSONString(result)); | |
193 | + return null; | |
194 | + } | |
13 | 195 | } |
14 | 196 | } | ... | ... |
1 | +package com.mass.util; | |
2 | + | |
3 | +import lombok.extern.slf4j.Slf4j; | |
4 | +import org.apache.commons.collections.MapUtils; | |
5 | +import org.apache.commons.lang3.StringUtils; | |
6 | +import org.apache.http.*; | |
7 | +import org.apache.http.client.ClientProtocolException; | |
8 | +import org.apache.http.client.config.RequestConfig; | |
9 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | |
10 | +import org.apache.http.client.methods.CloseableHttpResponse; | |
11 | +import org.apache.http.client.methods.HttpGet; | |
12 | +import org.apache.http.client.methods.HttpPost; | |
13 | +import org.apache.http.client.methods.HttpPut; | |
14 | +import org.apache.http.entity.StringEntity; | |
15 | +import org.apache.http.impl.client.CloseableHttpClient; | |
16 | +import org.apache.http.message.BasicNameValuePair; | |
17 | +import org.apache.http.util.EntityUtils; | |
18 | + | |
19 | +import java.io.IOException; | |
20 | +import java.util.ArrayList; | |
21 | +import java.util.List; | |
22 | +import java.util.Map; | |
23 | + | |
24 | +@Slf4j | |
25 | +public class HttpClientUtils { | |
26 | + | |
27 | + | |
28 | + /** | |
29 | + * 发送post请求 | |
30 | + * | |
31 | + * @param url:请求地址 | |
32 | + * @param header:请求头参数 | |
33 | + * @param params:表单参数 form提交 | |
34 | + * @param httpEntity json/xml参数 | |
35 | + * @return | |
36 | + */ | |
37 | + public static String doPostRequest(String url, Map<String, String> header, Map<String, String> params, HttpEntity httpEntity) { | |
38 | + String resultStr = ""; | |
39 | + if (StringUtils.isEmpty(url)) { | |
40 | + return resultStr; | |
41 | + } | |
42 | + CloseableHttpClient httpClient = null; | |
43 | + CloseableHttpResponse httpResponse = null; | |
44 | + try { | |
45 | + httpClient = SSLClientCustom.getHttpClinet(); | |
46 | + HttpPost httpPost = new HttpPost(url); | |
47 | + //请求头header信息 | |
48 | + if (MapUtils.isNotEmpty(header)) { | |
49 | + for (Map.Entry<String, String> stringStringEntry : header.entrySet()) { | |
50 | + httpPost.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue()); | |
51 | + log.info("请求header信息,key:{},value:{}", stringStringEntry.getKey(), stringStringEntry.getValue()); | |
52 | + } | |
53 | + } | |
54 | + //请求参数信息 | |
55 | + if (MapUtils.isNotEmpty(params)) { | |
56 | + List<NameValuePair> paramList = new ArrayList<NameValuePair>(); | |
57 | + for (Map.Entry<String, String> stringStringEntry : params.entrySet()) { | |
58 | + paramList.add(new BasicNameValuePair(stringStringEntry.getKey(), stringStringEntry.getValue())); | |
59 | + log.info("请求参数信息,key:{},value:{}", stringStringEntry.getKey(), stringStringEntry.getValue()); | |
60 | + } | |
61 | + UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramList, Consts.UTF_8); | |
62 | + httpPost.setEntity(urlEncodedFormEntity); | |
63 | + } | |
64 | + //实体设置 | |
65 | + if (httpEntity != null) { | |
66 | + String json = EntityUtils.toString(httpEntity); | |
67 | + log.info("请求参数信息{}", json); | |
68 | + httpPost.setEntity(httpEntity); | |
69 | + } | |
70 | +// httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); | |
71 | + //发起请求 | |
72 | + httpResponse = httpClient.execute(httpPost); | |
73 | + int statusCode = httpResponse.getStatusLine().getStatusCode(); | |
74 | + if (statusCode == HttpStatus.SC_OK||statusCode == HttpStatus.SC_UNAUTHORIZED) { | |
75 | + HttpEntity httpResponseEntity = httpResponse.getEntity(); | |
76 | + resultStr = EntityUtils.toString(httpResponseEntity); | |
77 | + //log.info("请求正常,请求地址:{},响应结果:{}", url, statusCode+";"+resultStr); | |
78 | + } else { | |
79 | + StringBuffer stringBuffer = new StringBuffer(); | |
80 | + HeaderIterator headerIterator = httpResponse.headerIterator(); | |
81 | + while (headerIterator.hasNext()) { | |
82 | + stringBuffer.append("\t" + headerIterator.next()); | |
83 | + } | |
84 | + log.info("异常信息:请求地址:{},响应状态和结果:{}",url,statusCode+";"+stringBuffer); | |
85 | + } | |
86 | + | |
87 | + } catch (Exception e) { | |
88 | + log.info("请求地址:{}", url); | |
89 | + e.printStackTrace(); | |
90 | + } finally { | |
91 | + HttpClientUtils.closeConnection(httpClient, httpResponse); | |
92 | + } | |
93 | + return resultStr; | |
94 | + } | |
95 | + | |
96 | + public static String doGetRequest(String url, Map<String, String> header, Map<String, Object> params) { | |
97 | + String resultStr = ""; | |
98 | + if (StringUtils.isEmpty(url)) { | |
99 | + return resultStr; | |
100 | + } | |
101 | + CloseableHttpClient httpClient = null; | |
102 | + CloseableHttpResponse httpResponse = null; | |
103 | + try { | |
104 | + httpClient = SSLClientCustom.getHttpClinet(); | |
105 | + //请求参数信息 | |
106 | + if (MapUtils.isNotEmpty(params)) { | |
107 | + url = url + buildUrl(params); | |
108 | + } | |
109 | + HttpGet httpGet = new HttpGet(url); | |
110 | + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)//连接超时 | |
111 | + .setConnectionRequestTimeout(25000)//请求超时 | |
112 | + .setSocketTimeout(25000)//套接字连接超时 | |
113 | + .setRedirectsEnabled(true).build();//允许重定向 | |
114 | + httpGet.setConfig(requestConfig); | |
115 | + if (MapUtils.isNotEmpty(header)) { | |
116 | + for (Map.Entry<String, String> stringStringEntry : header.entrySet()) { | |
117 | + httpGet.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue()); | |
118 | + } | |
119 | + } | |
120 | + //发起请求 | |
121 | + httpResponse = httpClient.execute(httpGet); | |
122 | + int statusCode = httpResponse.getStatusLine().getStatusCode(); | |
123 | + if (statusCode == HttpStatus.SC_OK) { | |
124 | + resultStr = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8); | |
125 | + log.info("请求地址:{},响应结果:{}", url, resultStr); | |
126 | + } else { | |
127 | + StringBuffer stringBuffer = new StringBuffer(); | |
128 | + HeaderIterator headerIterator = httpResponse.headerIterator(); | |
129 | + while (headerIterator.hasNext()) { | |
130 | + stringBuffer.append("\t" + headerIterator.next()); | |
131 | + } | |
132 | + resultStr = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8); | |
133 | + log.info("异常信息请求地址:{}", url, resultStr); | |
134 | + log.info("异常信息:请求响应状态:{},请求返回结果:{}", httpResponse.getStatusLine().getStatusCode(),StringUtils.isBlank(resultStr)?stringBuffer:resultStr); | |
135 | + } | |
136 | + | |
137 | + } catch (Exception e) { | |
138 | + log.info("请求地址:{}", url); | |
139 | + e.printStackTrace(); | |
140 | + | |
141 | + } finally { | |
142 | + HttpClientUtils.closeConnection(httpClient, httpResponse); | |
143 | + } | |
144 | + return resultStr; | |
145 | + } | |
146 | + | |
147 | + public static String doGetRequest(String url, Map<String, String> header, Map<String, Object> params,StringBuffer tracecode) { | |
148 | + String resultStr = ""; | |
149 | + if (StringUtils.isEmpty(url)) { | |
150 | + return resultStr; | |
151 | + } | |
152 | + CloseableHttpClient httpClient = null; | |
153 | + CloseableHttpResponse httpResponse = null; | |
154 | + try { | |
155 | + httpClient = SSLClientCustom.getHttpClinet(); | |
156 | + //请求参数信息 | |
157 | + if (MapUtils.isNotEmpty(params)) { | |
158 | + url = url + buildUrl(params); | |
159 | + } | |
160 | + HttpGet httpGet = new HttpGet(url); | |
161 | + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)//连接超时 | |
162 | + .setConnectionRequestTimeout(30000)//请求超时 | |
163 | + .setSocketTimeout(30000)//套接字连接超时 | |
164 | + .setRedirectsEnabled(true).build();//允许重定向 | |
165 | + httpGet.setConfig(requestConfig); | |
166 | + if (MapUtils.isNotEmpty(header)) { | |
167 | + for (Map.Entry<String, String> stringStringEntry : header.entrySet()) { | |
168 | + httpGet.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue()); | |
169 | + } | |
170 | + } | |
171 | + //发起请求 | |
172 | + httpResponse = httpClient.execute(httpGet); | |
173 | + int statusCode = httpResponse.getStatusLine().getStatusCode(); | |
174 | + if (statusCode == HttpStatus.SC_OK) { | |
175 | + Header[] tracecodes = httpResponse.getHeaders("Tracecode"); | |
176 | + tracecode.append(tracecodes[0].getValue()); | |
177 | + resultStr = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8); | |
178 | + log.info("请求地址:{},响应结果:{}", url, resultStr); | |
179 | + } else { | |
180 | + StringBuffer stringBuffer = new StringBuffer(); | |
181 | + HeaderIterator headerIterator = httpResponse.headerIterator(); | |
182 | + while (headerIterator.hasNext()) { | |
183 | + stringBuffer.append("\t" + headerIterator.next()); | |
184 | + } | |
185 | + log.info("异常信息:请求地址:{},请求响应状态:{},请求返回结果:{}",url,httpResponse.getStatusLine().getStatusCode(), stringBuffer); | |
186 | + } | |
187 | + | |
188 | + } catch (Exception e) { | |
189 | + e.printStackTrace(); | |
190 | + } finally { | |
191 | + HttpClientUtils.closeConnection(httpClient, httpResponse); | |
192 | + } | |
193 | + return resultStr; | |
194 | + } | |
195 | + | |
196 | + | |
197 | + public static String doPutRequest(String url, Map<String, String> header,StringEntity entity) { | |
198 | + | |
199 | + CloseableHttpClient closeableHttpClient = null; | |
200 | + CloseableHttpResponse closeableHttpResponse=null; | |
201 | + try { | |
202 | + closeableHttpClient = SSLClientCustom.getHttpClinet(); | |
203 | + } catch (Exception e) { | |
204 | + e.printStackTrace(); | |
205 | + } | |
206 | + | |
207 | + | |
208 | + RequestConfig requestConfig = RequestConfig.custom() | |
209 | + .setConnectTimeout(5000) | |
210 | + .setConnectionRequestTimeout(5000) | |
211 | + .setRedirectsEnabled(true) | |
212 | + .build(); | |
213 | + | |
214 | + HttpPut httpPost = new HttpPut(url); | |
215 | + | |
216 | + httpPost.setConfig(requestConfig); | |
217 | + httpPost.setHeader("Content-Type", "application/json"); | |
218 | + | |
219 | + if (MapUtils.isNotEmpty(header)) { | |
220 | + for (Map.Entry<String, String> stringStringEntry : header.entrySet()) { | |
221 | + httpPost.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue()); | |
222 | + log.info("请求header信息,key:{},value:{}", stringStringEntry.getKey(), stringStringEntry.getValue()); | |
223 | + } | |
224 | + } | |
225 | + | |
226 | + String strRequest = ""; | |
227 | + try { | |
228 | +// StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8"); | |
229 | + entity.setContentEncoding("utf-8"); | |
230 | + entity.setContentType("application/json"); | |
231 | + httpPost.setEntity(entity); | |
232 | + | |
233 | + | |
234 | + closeableHttpResponse = closeableHttpClient.execute(httpPost); | |
235 | + | |
236 | + if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) { | |
237 | + int statusCode = closeableHttpResponse.getStatusLine().getStatusCode(); | |
238 | + if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | |
239 | + HttpEntity httpEntity = closeableHttpResponse.getEntity(); | |
240 | + strRequest = EntityUtils.toString(httpEntity); | |
241 | + log.info("请求正常,请求地址:{},响应结果:{}", url, statusCode+";"+strRequest); | |
242 | + } else { | |
243 | + StringBuffer stringBuffer = new StringBuffer(); | |
244 | + HeaderIterator headerIterator = closeableHttpResponse.headerIterator(); | |
245 | + while (headerIterator.hasNext()) { | |
246 | + stringBuffer.append("\t" + headerIterator.next()); | |
247 | + } | |
248 | + strRequest = "Error Response" + statusCode; | |
249 | + log.info("异常信息:请求地址:{},响应状态和结果:{}",url,strRequest+";"+stringBuffer); | |
250 | + } | |
251 | + } | |
252 | + | |
253 | + } catch (ClientProtocolException e) { | |
254 | + e.printStackTrace(); | |
255 | + } catch (ParseException e) { | |
256 | + e.printStackTrace(); | |
257 | + } catch (IOException e) { | |
258 | + e.printStackTrace(); | |
259 | + } finally { | |
260 | + try { | |
261 | + if (closeableHttpClient != null) { | |
262 | + closeableHttpClient.close(); | |
263 | + } | |
264 | + | |
265 | + if(closeableHttpResponse!=null){ | |
266 | + closeableHttpResponse.close(); | |
267 | + } | |
268 | + } catch (IOException e) { | |
269 | + e.printStackTrace(); | |
270 | + } | |
271 | + } | |
272 | + | |
273 | + return strRequest; | |
274 | + } | |
275 | + | |
276 | + | |
277 | + | |
278 | + /** | |
279 | + * 关掉连接释放资源 | |
280 | + */ | |
281 | + private static void closeConnection(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) { | |
282 | + if (httpClient != null) { | |
283 | + try { | |
284 | + httpClient.close(); | |
285 | + } catch (IOException e) { | |
286 | + e.printStackTrace(); | |
287 | + } | |
288 | + } | |
289 | + if (httpResponse != null) { | |
290 | + try { | |
291 | + httpResponse.close(); | |
292 | + } catch (IOException e) { | |
293 | + e.printStackTrace(); | |
294 | + } | |
295 | + } | |
296 | + | |
297 | + } | |
298 | + | |
299 | + /** | |
300 | + * 构造get请求的参数 | |
301 | + * | |
302 | + * @return | |
303 | + */ | |
304 | + private static String buildUrl(Map<String, Object> map) { | |
305 | + if (MapUtils.isEmpty(map)) { | |
306 | + return ""; | |
307 | + } | |
308 | + StringBuffer stringBuffer = new StringBuffer("?"); | |
309 | + for (Map.Entry<String, Object> stringStringEntry : map.entrySet()) { | |
310 | + stringBuffer.append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("&"); | |
311 | + } | |
312 | + String result = stringBuffer.toString(); | |
313 | + if (StringUtils.isNotEmpty(result)) { | |
314 | + result = result.substring(0, result.length() - 1);//去掉结尾的&连接符 | |
315 | + } | |
316 | + return result; | |
317 | + } | |
318 | + | |
319 | +} | ... | ... |
1 | +package com.mass.util; | |
2 | + | |
3 | +import org.apache.http.config.Registry; | |
4 | +import org.apache.http.config.RegistryBuilder; | |
5 | +import org.apache.http.conn.socket.ConnectionSocketFactory; | |
6 | +import org.apache.http.conn.socket.PlainConnectionSocketFactory; | |
7 | +import org.apache.http.conn.ssl.NoopHostnameVerifier; | |
8 | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
9 | +import org.apache.http.conn.ssl.TrustStrategy; | |
10 | +import org.apache.http.impl.client.CloseableHttpClient; | |
11 | +import org.apache.http.impl.client.HttpClients; | |
12 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
13 | +import org.apache.http.ssl.SSLContextBuilder; | |
14 | + | |
15 | +import java.security.KeyManagementException; | |
16 | +import java.security.KeyStoreException; | |
17 | +import java.security.NoSuchAlgorithmException; | |
18 | +import java.security.cert.CertificateException; | |
19 | +import java.security.cert.X509Certificate; | |
20 | + | |
21 | +public class SSLClientCustom { | |
22 | + private static final String HTTP = "http"; | |
23 | + private static final String HTTPS = "https"; | |
24 | + private static SSLConnectionSocketFactory sslConnectionSocketFactory = null; | |
25 | + private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;//连接池管理类 | |
26 | + private static SSLContextBuilder sslContextBuilder = null;//管理Https连接的上下文类 | |
27 | + | |
28 | + static { | |
29 | + try { | |
30 | + sslContextBuilder = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { | |
31 | + @Override | |
32 | + public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { | |
33 | +// 信任所有站点 直接返回true | |
34 | + return true; | |
35 | + } | |
36 | + }); | |
37 | + sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE); | |
38 | + Registry<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create() | |
39 | + .register(HTTP, new PlainConnectionSocketFactory()) | |
40 | + .register(HTTPS, sslConnectionSocketFactory) | |
41 | + .build(); | |
42 | + poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registryBuilder); | |
43 | + poolingHttpClientConnectionManager.setMaxTotal(200); | |
44 | + } catch (NoSuchAlgorithmException e) { | |
45 | + e.printStackTrace(); | |
46 | + } catch (KeyStoreException e) { | |
47 | + e.printStackTrace(); | |
48 | + } catch (KeyManagementException e) { | |
49 | + e.printStackTrace(); | |
50 | + } | |
51 | + | |
52 | + } | |
53 | + | |
54 | + /** | |
55 | + * 获取连接 | |
56 | + * | |
57 | + * @return | |
58 | + * @throws Exception | |
59 | + */ | |
60 | + public static CloseableHttpClient getHttpClinet() throws Exception { | |
61 | + CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory) | |
62 | + .setConnectionManager(poolingHttpClientConnectionManager) | |
63 | + .setConnectionManagerShared(true) | |
64 | + .build(); | |
65 | + return httpClient; | |
66 | + } | |
67 | +} | ... | ... |
src/main/resources/application.yml
0 → 100644
1 | +server: | |
2 | + port: 8091 | |
3 | +spring: | |
4 | + application: | |
5 | + name: massSync | |
6 | + main: | |
7 | + allow-bean-definition-overriding: true | |
8 | + profiles: | |
9 | + active: default | |
10 | + | |
11 | +logging: | |
12 | + level: | |
13 | + root: INFO | |
14 | + file: | |
15 | + name: logs/massSync.log | |
16 | + | |
17 | +mass: | |
18 | + domain: http://53.1.230.5:8159 | |
19 | + userListUrl: /UUDB/user/list | |
20 | + userUpdateListUrl: /UUDB/user/updateList | |
21 | + orgListUrl: /UUDB/org/list | |
22 | + orgListUpdateUrl: /UUDB/org/updateList | |
23 | + tmpFilePath: /tmp/syncFile/ | |
24 | + | ... | ... |