mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add AbstractClientEngine
This commit is contained in:
parent
c657a4f9f4
commit
c49f090511
@ -112,21 +112,5 @@
|
||||
<version>2.0.9</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>mockwebserver</artifactId>
|
||||
<version>4.11.0</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>junit</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -14,8 +14,6 @@ package org.dromara.hutool.http;
|
||||
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.http.meta.ContentType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -1,28 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2024 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.http;
|
||||
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class MockServerTest {
|
||||
public static void main(final String[] args) throws IOException {
|
||||
//noinspection resource
|
||||
final MockWebServer server = new MockWebServer();
|
||||
final MockResponse mockResponse = new MockResponse().setBody("hello, world!");
|
||||
server.enqueue(mockResponse);
|
||||
server.start(8080);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user