fix: 校验 kettle状态

This commit is contained in:
taojinlong 2021-05-10 18:06:16 +08:00
parent bf638f1d95
commit 224902d222
2 changed files with 279 additions and 0 deletions

View File

@ -0,0 +1,72 @@
package io.dataease.commons.utils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import java.util.HashMap;
import java.util.Map;
public class HttpClientConfig {
// 字符集
private String charset = "UTF-8";
// 请求头
private Map<String, String> header = new HashMap<>();
// 设置连接超时时间单位毫秒
private int connectTimeout = 5000;
// 设置从connect Manager获取Connection 超时时间单位毫秒这个属性是新加的属性因为目前版本是可以共享连接池的
private int connectionRequestTimeout = 5000;
// 请求获取数据的超时时间单位毫秒 如果访问一个接口多少时间内无法返回数据就直接放弃此次调用
private int cocketTimeout = 60000;
public RequestConfig buildRequestConfig() {
Builder builder = RequestConfig.custom();
builder.setConnectTimeout(connectTimeout);
builder.setConnectionRequestTimeout(connectionRequestTimeout);
builder.setSocketTimeout(cocketTimeout);
return builder.build();
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public Map<String, String> getHeader() {
return header;
}
public void addHeader(String key, String value) {
header.put(key, value);
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public int getConnectionRequestTimeout() {
return connectionRequestTimeout;
}
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
}
public int getCocketTimeout() {
return cocketTimeout;
}
public void setCocketTimeout(int cocketTimeout) {
this.cocketTimeout = cocketTimeout;
}
}

View File

@ -0,0 +1,207 @@
package io.dataease.commons.utils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
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.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static final String HTTPS = "https";
/**
* 根据url构建HttpClient区分http和https
*
* @param url 请求地址
* @return CloseableHttpClient实例
*/
private static CloseableHttpClient buildHttpClient(String url) {
try {
if (url.startsWith(HTTPS)) {
// https 增加信任设置
TrustStrategy trustStrategy = new TrustSelfSignedStrategy();
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStrategy).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
return HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(hostnameVerifier).build();
} else {
// http
return HttpClientBuilder.create().build();
}
} catch (Exception e) {
throw new RuntimeException("HttpClient构建失败", e);
}
}
/**
* Get http请求
*
* @param url 请求地址
* @param config 配置项如果null则使用默认配置
* @return 响应结果字符串
*/
public static String get(String url, HttpClientConfig config) {
CloseableHttpClient httpClient = buildHttpClient(url);
HttpGet httpGet = new HttpGet(url);
if (config == null) {
config = new HttpClientConfig();
}
try {
httpGet.setConfig(config.buildRequestConfig());
Map<String, String> header = config.getHeader();
for (String key : header.keySet()) {
httpGet.addHeader(key, header.get(key));
}
httpGet.addHeader(HTTP.CONTENT_ENCODING, config.getCharset());
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, config.getCharset());
} catch (Exception e) {
logger.error("HttpClient查询失败", e);
throw new RuntimeException("HttpClient查询失败", e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
logger.error("HttpClient关闭连接失败", e);
}
}
}
/**
* Post请求请求内容必须为JSON格式的字符串
*
* @param url 请求地址
* @param config 配置项如果null则使用默认配置
* @param json JSON格式的字符串
* @return 响应结果字符串
*/
public static String post(String url, String json, HttpClientConfig config) {
CloseableHttpClient httpClient = buildHttpClient(url);
HttpPost httpPost = new HttpPost(url);
if (config == null) {
config = new HttpClientConfig();
}
try {
httpPost.setConfig(config.buildRequestConfig());
Map<String, String> header = config.getHeader();
for (String key : header.keySet()) {
httpPost.addHeader(key, header.get(key));
}
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
httpPost.addHeader(HTTP.CONTENT_ENCODING, config.getCharset());
EntityBuilder entityBuilder = EntityBuilder.create();
entityBuilder.setText(json);
entityBuilder.setContentType(ContentType.APPLICATION_JSON);
entityBuilder.setContentEncoding(config.getCharset());
HttpEntity requestEntity = entityBuilder.build();
httpPost.setEntity(requestEntity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, config.getCharset());
} catch (Exception e) {
logger.error("HttpClient查询失败", e);
throw new RuntimeException("HttpClient查询失败", e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
logger.error("HttpClient关闭连接失败", e);
}
}
}
/**
* Post请求请求内容必须为JSON格式的字符串
*
* @param url 请求地址
* @param json JSON格式的字符串
* @return 响应结果字符串
*/
public static String post(String url, String json) {
return HttpClientUtil.post(url, json, null);
}
/**
* Post请求请求内容必须为键值对参数
*
* @param url 请求地址
* @param config 配置项如果null则使用默认配置
* @param body 请求内容键值对参数
* @return 响应结果字符串
*/
public static String post(String url, Map<String, String> body, HttpClientConfig config) {
CloseableHttpClient httpClient = buildHttpClient(url);
HttpPost httpPost = new HttpPost(url);
if (config == null) {
config = new HttpClientConfig();
}
try {
httpPost.setConfig(config.buildRequestConfig());
Map<String, String> header = config.getHeader();
for (String key : header.keySet()) {
httpPost.addHeader(key, header.get(key));
}
httpPost.addHeader(HTTP.CONTENT_ENCODING, config.getCharset());
if (body != null && body.size() > 0) {
List<NameValuePair> nvps = new ArrayList<>();
for (String key : body.keySet()) {
nvps.add(new BasicNameValuePair(key, body.get(key)));
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, config.getCharset()));
} catch (Exception e) {
logger.error("HttpClient转换编码错误", e);
throw new RuntimeException("HttpClient转换编码错误", e);
}
}
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, config.getCharset());
} catch (Exception e) {
logger.error("HttpClient查询失败", e);
throw new RuntimeException("HttpClient查询失败", e);
} finally {
try {
httpClient.close();
} catch (Exception e) {
logger.error("HttpClient关闭连接失败", e);
}
}
}
}