forked from github/dataease
fix: 校验 kettle状态
This commit is contained in:
parent
bf638f1d95
commit
224902d222
72
backend/src/main/java/io/dataease/commons/utils/HttpClientConfig.java
Executable file
72
backend/src/main/java/io/dataease/commons/utils/HttpClientConfig.java
Executable 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
207
backend/src/main/java/io/dataease/commons/utils/HttpClientUtil.java
Executable file
207
backend/src/main/java/io/dataease/commons/utils/HttpClientUtil.java
Executable 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user