mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add AbstractClientEngine
This commit is contained in:
@@ -36,7 +36,7 @@ import org.dromara.hutool.http.client.ClientConfig;
|
||||
import org.dromara.hutool.http.client.Request;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.body.HttpBody;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.AbstractClientEngine;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
import org.dromara.hutool.http.proxy.HttpProxy;
|
||||
import org.dromara.hutool.http.ssl.SSLInfo;
|
||||
@@ -54,9 +54,8 @@ import java.util.Map;
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class HttpClient4Engine implements ClientEngine {
|
||||
public class HttpClient4Engine extends AbstractClientEngine {
|
||||
|
||||
private ClientConfig config;
|
||||
private CloseableHttpClient engine;
|
||||
|
||||
/**
|
||||
@@ -68,15 +67,6 @@ public class HttpClient4Engine implements ClientEngine {
|
||||
Assert.notNull(CloseableHttpClient.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClient4Engine init(final ClientConfig config) {
|
||||
this.config = config;
|
||||
// 重置客户端
|
||||
IoUtil.closeQuietly(this.engine);
|
||||
this.engine = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response send(final Request message) {
|
||||
initEngine();
|
||||
@@ -99,13 +89,18 @@ public class HttpClient4Engine implements ClientEngine {
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.engine.close();
|
||||
IoUtil.nullSafeClose(this.engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化引擎
|
||||
*/
|
||||
private void initEngine() {
|
||||
@Override
|
||||
protected void reset() {
|
||||
// 重置客户端
|
||||
IoUtil.closeQuietly(this.engine);
|
||||
this.engine = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initEngine() {
|
||||
if (null != this.engine) {
|
||||
return;
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ import org.dromara.hutool.http.client.ClientConfig;
|
||||
import org.dromara.hutool.http.client.Request;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.body.HttpBody;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.AbstractClientEngine;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
import org.dromara.hutool.http.proxy.HttpProxy;
|
||||
import org.dromara.hutool.http.ssl.SSLInfo;
|
||||
@@ -58,9 +58,8 @@ import java.util.concurrent.TimeUnit;
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class HttpClient5Engine implements ClientEngine {
|
||||
public class HttpClient5Engine extends AbstractClientEngine {
|
||||
|
||||
private ClientConfig config;
|
||||
private CloseableHttpClient engine;
|
||||
|
||||
/**
|
||||
@@ -72,15 +71,6 @@ public class HttpClient5Engine implements ClientEngine {
|
||||
Assert.notNull(CloseableHttpClient.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClient5Engine init(final ClientConfig config) {
|
||||
this.config = config;
|
||||
// 重置客户端
|
||||
IoUtil.closeQuietly(this.engine);
|
||||
this.engine = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response send(final Request message) {
|
||||
initEngine();
|
||||
@@ -103,13 +93,18 @@ public class HttpClient5Engine implements ClientEngine {
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
this.engine.close();
|
||||
IoUtil.nullSafeClose(this.engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化引擎
|
||||
*/
|
||||
private void initEngine() {
|
||||
@Override
|
||||
protected void reset() {
|
||||
// 重置客户端
|
||||
IoUtil.closeQuietly(this.engine);
|
||||
this.engine = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initEngine() {
|
||||
if (null != this.engine) {
|
||||
return;
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ import org.dromara.hutool.http.client.Request;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.body.HttpBody;
|
||||
import org.dromara.hutool.http.client.cookie.GlobalCookieManager;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.AbstractClientEngine;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
import org.dromara.hutool.http.meta.HttpStatus;
|
||||
|
||||
@@ -38,20 +38,13 @@ import java.util.List;
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class JdkClientEngine implements ClientEngine {
|
||||
|
||||
private ClientConfig config;
|
||||
public class JdkClientEngine extends AbstractClientEngine {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public JdkClientEngine() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public JdkClientEngine init(final ClientConfig config) {
|
||||
this.config = config;
|
||||
return this;
|
||||
// 无需检查类是否存在
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,6 +82,16 @@ public class JdkClientEngine implements ClientEngine {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void reset() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initEngine() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行发送
|
||||
*
|
||||
|
@@ -20,7 +20,7 @@ import org.dromara.hutool.http.client.ClientConfig;
|
||||
import org.dromara.hutool.http.client.Request;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.body.HttpBody;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.AbstractClientEngine;
|
||||
import org.dromara.hutool.http.proxy.HttpProxy;
|
||||
import org.dromara.hutool.http.ssl.SSLInfo;
|
||||
|
||||
@@ -36,9 +36,8 @@ import java.util.concurrent.TimeUnit;
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class OkHttpEngine implements ClientEngine {
|
||||
public class OkHttpEngine extends AbstractClientEngine {
|
||||
|
||||
private ClientConfig config;
|
||||
private OkHttpClient client;
|
||||
|
||||
/**
|
||||
@@ -53,8 +52,6 @@ public class OkHttpEngine implements ClientEngine {
|
||||
@Override
|
||||
public OkHttpEngine init(final ClientConfig config) {
|
||||
this.config = config;
|
||||
// 重置客户端
|
||||
this.client = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -82,10 +79,14 @@ public class OkHttpEngine implements ClientEngine {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化引擎
|
||||
*/
|
||||
private void initEngine() {
|
||||
@Override
|
||||
protected void reset() {
|
||||
// 重置客户端
|
||||
this.client = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initEngine() {
|
||||
if (null != this.client) {
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user