Commit 62b4a5a7 by inrgihc

修正接口调用

parent 9003b935
package com.gitee.sqlrest.core.executor;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
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.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
@Slf4j
public class AlarmHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
private static final String SSL_PROTOCOL = "SSL";
private static final TrustStrategy trustStrategy = (x509Certificates, authType) -> true;
private int connectTimeout = 2 * 1000;
private int socketTimeout = 60 * 1000;
private int connectionRequestTimeout = 60 * 1000;
private int maxConnectionSize = 20;
private int maxPerRoute = 2;
public AlarmHttpRequestFactory() {
super();
init();
}
private void init() {
try {
SSLContext sslContext = SSLContexts.custom().setProtocol(SSL_PROTOCOL)
.loadTrustMaterial(null, trustStrategy).build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.setConnectionManager(poolingConnectionManager(csf))
.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout)
.build())
.build();
setHttpClient(httpClient);
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
log.warn("Failed to build CloseableHttpClient support https for RestTemplate. message:{}", e.getMessage());
}
}
private PoolingHttpClientConnectionManager poolingConnectionManager(SSLConnectionSocketFactory csf) {
Registry<ConnectionSocketFactory> registry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", csf)
.build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
manager.setMaxTotal(maxConnectionSize);
manager.setDefaultMaxPerRoute(maxPerRoute);
return manager;
}
}
......@@ -32,6 +32,11 @@ public class UnifyAlarmOpsService {
@Resource
private UnifyAlarmDao unifyAlarmDao;
private final RestTemplate restTemplate;
public UnifyAlarmOpsService() {
this.restTemplate = new RestTemplate(new AlarmHttpRequestFactory());
}
public UnifyAlarmEntity getUnifyAlarmConfig() {
return unifyAlarmDao.getUnifyAlarmConfig();
......@@ -110,9 +115,8 @@ public class UnifyAlarmOpsService {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType(config.getContentType().replace(";", "") + "; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
headers.add("Accept", MediaType.ALL_VALUE.toString());
HttpEntity<String> httpEntity = new HttpEntity<>(bodyStr, headers);
RestTemplate restTemplate = new RestTemplate();
return restTemplate.exchange(config.getEndpoint(), HttpMethod.POST, httpEntity, String.class);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment