com.aliyuncs.http.HttpClientFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aliyun-java-sdk-core Show documentation
Show all versions of aliyun-java-sdk-core Show documentation
Aliyun Open API SDK for Java
Copyright (C) Alibaba Cloud Computing
All rights reserved.
版权所有 (C)阿里云计算有限公司
http://www.aliyun.com
package com.aliyuncs.http;
import java.lang.reflect.Constructor;
import com.aliyuncs.http.clients.CompatibleUrlConnClient;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.utils.StringUtils;
/**
* @author VK.Gao
* @date 2018/03/28
*/
public class HttpClientFactory {
public static String HTTP_CLIENT_IMPL_KEY = "aliyuncs.sdk.httpclient";
public static String COMPATIBLE_HTTP_CLIENT_CLASS_NAME = CompatibleUrlConnClient.class.getName();
public static IHttpClient buildClient(IClientProfile profile) {
try {
HttpClientConfig clientConfig = profile.getHttpClientConfig();
if (clientConfig == null) {
clientConfig = HttpClientConfig.getDefault();
}
String customClientClassName = null;
if (clientConfig.isCompatibleMode()) {
customClientClassName = COMPATIBLE_HTTP_CLIENT_CLASS_NAME;
} else if (clientConfig.getClientType() == HttpClientType.Custom && StringUtils.isNotEmpty(clientConfig.getCustomClientClassName())) {
customClientClassName = clientConfig.getCustomClientClassName();
} else {
customClientClassName = System.getProperty(HTTP_CLIENT_IMPL_KEY);
}
if (StringUtils.isEmpty(customClientClassName)) {
customClientClassName = clientConfig.getClientType().getImplClass().getName();
}
Class httpClientClass = Class.forName(customClientClassName);
if (!IHttpClient.class.isAssignableFrom(httpClientClass)) {
throw new IllegalStateException(String.format("%s is not assignable from com.aliyuncs.http.IHttpClient", customClientClassName));
}
Constructor extends IHttpClient> constructor = httpClientClass.getConstructor(HttpClientConfig.class);
return constructor.newInstance(clientConfig);
} catch (Exception e) {
// keep compatibility
throw new IllegalStateException("HttpClientFactory buildClient failed", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy