All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.whaleal.icefrog.http.HttpGlobalConfig Maven / Gradle / Ivy

The newest version!
package com.whaleal.icefrog.http;

import com.whaleal.icefrog.core.util.ArrayUtil;
import com.whaleal.icefrog.core.util.ReflectUtil;
import com.whaleal.icefrog.http.cookie.GlobalCookieManager;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.CookieManager;
import java.net.HttpURLConnection;

/**
 * HTTP 全局参数配置
 *
 * @author Looly
 * @author wh
 * @since 1.0.0
 */
public class HttpGlobalConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    protected static int timeout = -1;
    private static boolean isAllowPatch = false;

    /**
     * 获取全局默认的超时时长
     *
     * @return 全局默认的超时时长
     */
    public static int getTimeout() {
        return timeout;
    }

    /**
     * 设置默认的连接和读取超时时长
     *
     * @param customTimeout 超时时长
     */
    synchronized public static void setTimeout( int customTimeout ) {
        timeout = customTimeout;
    }

    /**
     * 获取Cookie管理器,用于自定义Cookie管理
     *
     * @return {@link CookieManager}
     * @see GlobalCookieManager#getCookieManager()
     * @since 1.0.0
     */
    public static CookieManager getCookieManager() {
        return GlobalCookieManager.getCookieManager();
    }

    /**
     * 自定义{@link CookieManager}
     *
     * @param customCookieManager 自定义的{@link CookieManager}
     * @see GlobalCookieManager#setCookieManager(CookieManager)
     * @since 1.0.0
     */
    public static void setCookieManager( CookieManager customCookieManager ) {
        GlobalCookieManager.setCookieManager(customCookieManager);
    }

    /**
     * 关闭Cookie
     *
     * @see GlobalCookieManager#setCookieManager(CookieManager)
     * @since 1.0.0
     */
    public static void closeCookie() {
        GlobalCookieManager.setCookieManager(null);
    }

    /**
     * 增加支持的METHOD方法
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch * * @since 1.0.0 */ synchronized public static void allowPatch() { if (isAllowPatch) { return; } final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods"); if (null == methodsField) { throw new HttpException("None static field [methods] with Java version: [{}]", System.getProperty("java.version")); } // 去除final修饰 ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL); final String[] methods = { "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH" }; ReflectUtil.setFieldValue(null, methodsField, methods); // 检查注入是否成功 final Object staticFieldValue = ReflectUtil.getStaticFieldValue(methodsField); if (false == ArrayUtil.equals(methods, staticFieldValue)) { throw new HttpException("Inject value to field [methods] failed!"); } isAllowPatch = true; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy