From c49f09051157a1b2b486cd0145a305635f815858 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 10 Aug 2024 19:38:53 +0800 Subject: [PATCH] add AbstractClientEngine --- hutool-http/pom.xml | 16 ---------- .../engine/httpclient4/HttpClient4Engine.java | 29 ++++++++----------- .../engine/httpclient5/HttpClient5Engine.java | 29 ++++++++----------- .../client/engine/jdk/JdkClientEngine.java | 23 ++++++++------- .../client/engine/okhttp/OkHttpEngine.java | 19 ++++++------ .../dromara/hutool/http/ContentTypeTest.java | 2 -- .../dromara/hutool/http/MockServerTest.java | 28 ------------------ 7 files changed, 47 insertions(+), 99 deletions(-) delete mode 100644 hutool-http/src/test/java/org/dromara/hutool/http/MockServerTest.java diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 6b7b9610b..d4c40b67b 100755 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -112,21 +112,5 @@ 2.0.9 test - - com.squareup.okhttp3 - mockwebserver - 4.11.0 - test - - - kotlin-stdlib-jdk8 - org.jetbrains.kotlin - - - junit - junit - - - diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java index 9c35069dc..b3bd2fe01 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java @@ -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; } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java index 29ae6b0d0..75e13aa0d 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java @@ -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; } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java index ee0854e4e..6f170fd3b 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/jdk/JdkClientEngine.java @@ -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 + } + /** * 执行发送 * diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java index e30d438c3..e7cdd67b6 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java @@ -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; } diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java index 9ca519949..7cd716518 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java @@ -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; diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/MockServerTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/MockServerTest.java deleted file mode 100644 index a851586fc..000000000 --- a/hutool-http/src/test/java/org/dromara/hutool/http/MockServerTest.java +++ /dev/null @@ -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); - } -}