From 11f47e5b0f163981c8a3b3f01b33f038d74d2894 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 26 Oct 2022 21:48:49 +0800 Subject: [PATCH] fix code --- .../cn/hutool/core/reflect/FieldUtil.java | 15 ++++- .../java/cn/hutool/http/HttpGlobalConfig.java | 51 ---------------- .../client/engine/jdk/HttpConnection.java | 5 +- .../engine/jdk/HttpUrlConnectionUtil.java | 58 +++++++++++++++++++ 4 files changed, 73 insertions(+), 56 deletions(-) create mode 100755 hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpUrlConnectionUtil.java diff --git a/hutool-core/src/main/java/cn/hutool/core/reflect/FieldUtil.java b/hutool-core/src/main/java/cn/hutool/core/reflect/FieldUtil.java index c52728c6e..682a30177 100644 --- a/hutool-core/src/main/java/cn/hutool/core/reflect/FieldUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/reflect/FieldUtil.java @@ -84,7 +84,7 @@ public class FieldUtil { public static Field getDeClearField(final Class beanClass, final String name) { try { return beanClass.getDeclaredField(name); - } catch (NoSuchFieldException e) { + } catch (final NoSuchFieldException e) { return null; } } @@ -124,7 +124,7 @@ public class FieldUtil { * 获得一个类中所有满足条件的字段列表,包括其父类中的字段
* 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。 * - * @param beanClass 类 + * @param beanClass 类 * @param fieldPredicate field过滤器,过滤掉不需要的field,{@link Predicate#test(Object)}为{@code true}保留,null表示全部保留 * @return 字段列表 * @throws SecurityException 安全检查异常 @@ -254,6 +254,17 @@ public class FieldUtil { setFieldValue(obj, field, value); } + /** + * 设置静态(static)字段值 + * + * @param field 字段 + * @param value 值,值类型必须与字段类型匹配,不会自动转换对象类型 + * @throws UtilException UtilException 包装IllegalAccessException异常 + */ + public static void setStaticFieldValue(final Field field, final Object value) throws UtilException { + setFieldValue(null, field, value); + } + /** * 设置字段值 * diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java index 7706f94e9..eb31277c8 100755 --- a/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpGlobalConfig.java @@ -1,17 +1,11 @@ package cn.hutool.http; -import cn.hutool.core.reflect.FieldUtil; -import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.http.client.cookie.GlobalCookieManager; import java.io.Serializable; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.net.CookieManager; import java.net.HttpURLConnection; -import java.security.AccessController; -import java.security.PrivilegedAction; /** * HTTP 全局参数配置 @@ -31,7 +25,6 @@ public class HttpGlobalConfig implements Serializable { * 底层调用:{@link HttpURLConnection#setConnectTimeout(int)} 同时设置: 连接超时 */ private static int timeout = -1; - private static boolean isAllowPatch = false; private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16); private static int maxRedirectCount = 0; private static boolean ignoreEOFError = true; @@ -182,48 +175,4 @@ public class HttpGlobalConfig implements Serializable { synchronized public static void closeCookie() { GlobalCookieManager.setCookieManager(null); } - - /** - * 增加支持的METHOD方法
- * 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
- * see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch - */ - public static void allowPatch(){ - AccessController.doPrivileged((PrivilegedAction) () -> { - doAllowPatch(); - return null; - }); - } - - /** - * 增加支持的METHOD方法
- * 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
- * see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch - * - * @since 5.7.4 - */ - synchronized private static void doAllowPatch() { - if (isAllowPatch) { - return; - } - final Field methodsField = FieldUtil.getField(HttpURLConnection.class, "methods"); - if (null == methodsField) { - throw new HttpException("None static field [methods] with Java version: [{}]", System.getProperty("java.version")); - } - - // 去除final修饰 - FieldUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL); - final String[] methods = { - "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH" - }; - FieldUtil.setFieldValue(null, methodsField, methods); - - // 检查注入是否成功 - final Object staticFieldValue = FieldUtil.getStaticFieldValue(methodsField); - if (false == ArrayUtil.equals(methods, staticFieldValue)) { - throw new HttpException("Inject value to field [methods] failed!"); - } - - isAllowPatch = true; - } } diff --git a/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpConnection.java b/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpConnection.java index 1cf9121f1..d69313ed5 100644 --- a/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpConnection.java +++ b/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpConnection.java @@ -5,10 +5,9 @@ import cn.hutool.core.net.url.URLUtil; import cn.hutool.core.reflect.FieldUtil; import cn.hutool.core.text.StrUtil; import cn.hutool.core.util.ObjUtil; -import cn.hutool.http.meta.Header; import cn.hutool.http.HttpException; -import cn.hutool.http.HttpGlobalConfig; import cn.hutool.http.HttpUtil; +import cn.hutool.http.meta.Header; import cn.hutool.http.meta.Method; import cn.hutool.http.ssl.DefaultSSLInfo; @@ -125,7 +124,7 @@ public class HttpConnection { // 增加PATCH方法支持 if (Method.PATCH.equals(method)) { - HttpGlobalConfig.allowPatch(); + HttpUrlConnectionUtil.allowPatch(); } } diff --git a/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpUrlConnectionUtil.java b/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpUrlConnectionUtil.java new file mode 100755 index 000000000..7eb60d4c4 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/client/engine/jdk/HttpUrlConnectionUtil.java @@ -0,0 +1,58 @@ +package cn.hutool.http.client.engine.jdk; + +import cn.hutool.core.reflect.FieldUtil; +import cn.hutool.core.reflect.ModifierUtil; +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.SystemUtil; +import cn.hutool.http.HttpException; + +import java.lang.reflect.Field; +import java.net.HttpURLConnection; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * 针对{@link HttpURLConnection}相关工具 + * + * @author looly + * @since 6.0.0 + */ +public class HttpUrlConnectionUtil { + + private static final String[] METHODS = { + "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH" + }; + + /** + * 增加支持的METHOD方法
+ * 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
+ * see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch + */ + public static void allowPatch() { + AccessController.doPrivileged((PrivilegedAction) () -> { + doAllowPatch(); + return null; + }); + } + + /** + * 增加支持的METHOD方法
+ * 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
+ * see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch + * + * @since 5.7.4 + */ + synchronized private static void doAllowPatch() { + final Field methodsField = FieldUtil.getField(HttpURLConnection.class, "methods"); + if (null == methodsField) { + throw new HttpException("None static field [methods] with Java version: [{}]", SystemUtil.get("java.version")); + } + + final Object staticFieldValue = FieldUtil.getStaticFieldValue(methodsField); + if (false == ArrayUtil.equals(METHODS, staticFieldValue)) { + // 去除final修饰 + ModifierUtil.removeFinalModify(methodsField); + FieldUtil.setStaticFieldValue(methodsField, METHODS); + } + } +}