From d2e66a1ca276ceafe124d4cd931c21ffb743a824 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 26 Oct 2022 12:57:01 +0800 Subject: [PATCH] fix code --- hutool-http/pom.xml | 5 ++ .../client/engine/ClientEngineFactory.java | 53 +++++++++++++++++++ .../http/client/engine/EngineFactory.java | 13 ----- .../cn.hutool.http.client.ClientEngine | 3 ++ .../http/client/ClientEngineFactoryTest.java | 13 +++++ .../http/client/HttpClient4EngineTest.java | 2 +- .../test/resources/simplelogger.properties | 1 + 7 files changed, 76 insertions(+), 14 deletions(-) create mode 100755 hutool-http/src/main/java/cn/hutool/http/client/engine/ClientEngineFactory.java delete mode 100755 hutool-http/src/main/java/cn/hutool/http/client/engine/EngineFactory.java create mode 100755 hutool-http/src/main/resources/META-INF/services/cn.hutool.http.client.ClientEngine create mode 100755 hutool-http/src/test/java/cn/hutool/http/client/ClientEngineFactoryTest.java create mode 100755 hutool-http/src/test/resources/simplelogger.properties diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 0959c5227..d4d2c26ef 100755 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -22,6 +22,11 @@ hutool-core ${project.parent.version} + + cn.hutool + hutool-log + ${project.parent.version} + javax.xml.soap javax.xml.soap-api diff --git a/hutool-http/src/main/java/cn/hutool/http/client/engine/ClientEngineFactory.java b/hutool-http/src/main/java/cn/hutool/http/client/engine/ClientEngineFactory.java new file mode 100755 index 000000000..d29e8f259 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/client/engine/ClientEngineFactory.java @@ -0,0 +1,53 @@ +package cn.hutool.http.client.engine; + +import cn.hutool.core.lang.Singleton; +import cn.hutool.core.text.StrUtil; +import cn.hutool.core.util.ServiceLoaderUtil; +import cn.hutool.http.HttpException; +import cn.hutool.http.client.ClientEngine; +import cn.hutool.log.StaticLog; + +/** + * Http客户端引擎工厂类 + * + * @author looly + * @since 6.0.0 + */ +public class ClientEngineFactory { + + /** + * 获得单例的ClientEngine + * + * @return 单例的ClientEngine + */ + public static ClientEngine get() { + return Singleton.get(ClientEngine.class.getName(), ClientEngineFactory::of); + } + + /** + * 根据用户引入的拼音引擎jar,自动创建对应的拼音引擎对象
+ * 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎 + * + * @return {@code ClientEngine} + */ + public static ClientEngine of() { + final ClientEngine engine = doCreate(); + StaticLog.debug("Use [{}] Http Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine")); + return engine; + } + + /** + * 根据用户引入的拼音引擎jar,自动创建对应的拼音引擎对象
+ * 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎 + * + * @return {@code EngineFactory} + */ + private static ClientEngine doCreate() { + final ClientEngine engine = ServiceLoaderUtil.loadFirstAvailable(ClientEngine.class); + if (null != engine) { + return engine; + } + + throw new HttpException("No http jar found ! Please add one of it to your project !"); + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/client/engine/EngineFactory.java b/hutool-http/src/main/java/cn/hutool/http/client/engine/EngineFactory.java deleted file mode 100755 index a0db70ef8..000000000 --- a/hutool-http/src/main/java/cn/hutool/http/client/engine/EngineFactory.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.hutool.http.client.engine; - -import cn.hutool.http.client.ClientEngine; - -/** - * Http客户端引擎工厂类 - */ -public class EngineFactory { - - public static ClientEngine getEngine(){ - return null; - } -} diff --git a/hutool-http/src/main/resources/META-INF/services/cn.hutool.http.client.ClientEngine b/hutool-http/src/main/resources/META-INF/services/cn.hutool.http.client.ClientEngine new file mode 100755 index 000000000..a735c86ec --- /dev/null +++ b/hutool-http/src/main/resources/META-INF/services/cn.hutool.http.client.ClientEngine @@ -0,0 +1,3 @@ +cn.hutool.http.client.engine.httpclient5.HttpClient5Engine +cn.hutool.http.client.engine.httpclient4.HttpClient4Engine +cn.hutool.http.client.engine.okhttp.OkHttpEngine diff --git a/hutool-http/src/test/java/cn/hutool/http/client/ClientEngineFactoryTest.java b/hutool-http/src/test/java/cn/hutool/http/client/ClientEngineFactoryTest.java new file mode 100755 index 000000000..fa57e9d13 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/client/ClientEngineFactoryTest.java @@ -0,0 +1,13 @@ +package cn.hutool.http.client; + +import cn.hutool.http.client.engine.ClientEngineFactory; +import org.junit.Assert; +import org.junit.Test; + +public class ClientEngineFactoryTest { + @Test + public void getTest() { + final ClientEngine clientEngineFactory = ClientEngineFactory.get(); + Assert.assertNotNull(clientEngineFactory); + } +} diff --git a/hutool-http/src/test/java/cn/hutool/http/client/HttpClient4EngineTest.java b/hutool-http/src/test/java/cn/hutool/http/client/HttpClient4EngineTest.java index 04fed763c..2a5ef4f3e 100755 --- a/hutool-http/src/test/java/cn/hutool/http/client/HttpClient4EngineTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/client/HttpClient4EngineTest.java @@ -1,8 +1,8 @@ package cn.hutool.http.client; import cn.hutool.core.lang.Console; -import cn.hutool.http.meta.Method; import cn.hutool.http.client.engine.httpclient4.HttpClient4Engine; +import cn.hutool.http.meta.Method; import org.junit.Ignore; import org.junit.Test; diff --git a/hutool-http/src/test/resources/simplelogger.properties b/hutool-http/src/test/resources/simplelogger.properties new file mode 100755 index 000000000..beb56b2e1 --- /dev/null +++ b/hutool-http/src/test/resources/simplelogger.properties @@ -0,0 +1 @@ +org.slf4j.simpleLogger.defaultLogLevel=debug