com.ksyun.api.sdk.client.DefaultKscProfile Maven / Gradle / Ivy
package com.ksyun.api.sdk.client;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.ksyun.api.sdk.auth.Credential;
import com.ksyun.api.sdk.auth.ICredentialProvider;
import com.ksyun.api.sdk.auth.ISigner;
import com.ksyun.api.sdk.auth.ShaHmac1Singleton;
import com.ksyun.api.sdk.exceptions.ClientException;
import com.ksyun.api.sdk.http.FormatType;
import com.ksyun.api.sdk.regions.CustomizedEndpointsParser;
import com.ksyun.api.sdk.regions.Endpoint;
import com.ksyun.api.sdk.regions.IEndpointsProvider;
import com.ksyun.api.sdk.regions.InternalEndpointsParser;
import com.ksyun.api.sdk.regions.KscEndpointsParser;
import com.ksyun.api.sdk.regions.LocationConfig;
import com.ksyun.api.sdk.regions.ProductDomain;
public class DefaultKscProfile implements IClientProfile {
private static DefaultKscProfile profile = null;
private static List endpoints = null;
private Credential credential = null;
private String regionId = null;
private FormatType acceptFormat = null;
private ISigner isigner = null;
private IEndpointsProvider iendpoints = null;
private IEndpointsProvider remoteProvider = null;
private ICredentialProvider icredential = null;
private LocationConfig locationConfig = new LocationConfig();
private DefaultKscProfile() {
this.locationConfig = new LocationConfig();
this.iendpoints = new InternalEndpointsParser();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
}
private DefaultKscProfile(String region, Credential creden) {
this.iendpoints = new InternalEndpointsParser();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
this.credential = creden;
this.regionId = region;
this.locationConfig = new LocationConfig();
}
private DefaultKscProfile(String region, Credential creden, IEndpointsProvider provider) {
this.iendpoints = provider;
this.credential = creden;
this.regionId = region;
this.locationConfig = new LocationConfig();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
}
private DefaultKscProfile(ICredentialProvider icredential) {
this.icredential = icredential;
this.iendpoints = new InternalEndpointsParser();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
this.locationConfig = new LocationConfig();
}
private DefaultKscProfile(String region, ICredentialProvider icredential) {
this.regionId = region;
this.icredential = icredential;
this.iendpoints = new InternalEndpointsParser();
this.locationConfig = new LocationConfig();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
}
private DefaultKscProfile(ICredentialProvider icredential, String region, FormatType format) {
this.regionId = region;
this.acceptFormat = format;
this.icredential = icredential;
this.iendpoints = new InternalEndpointsParser();
this.remoteProvider = KscEndpointsParser.initRemoteEndpointsParser();
this.locationConfig = new LocationConfig();
}
@Override
public synchronized ISigner getSigner() {
if (null == isigner)
this.isigner = ShaHmac1Singleton.INSTANCE.getInstance();
return isigner;
}
@Override
public synchronized String getRegionId() {
return regionId;
}
@Override
public synchronized FormatType getFormat() {
return acceptFormat;
}
@Override
public synchronized Credential getCredential() {
if (null == credential && null != icredential)
credential = icredential.fresh();
return credential;
}
@Override
public synchronized void setLocationConfig(String regionId, String product, String endpoint) {
this.locationConfig = LocationConfig.createLocationConfig(regionId, product, endpoint);
}
@Override
public List getEndpoints() throws ClientException {
throw new UnsupportedOperationException();
}
@Override
public synchronized List getEndpoints(String regionId, String product) throws ClientException {
if (null == endpoints) {
Endpoint endpoint = iendpoints.getEndpoint(regionId, product);
if (endpoint != null) {
endpoints = new ArrayList();
endpoints.add(endpoint);
}
}
return endpoints;
}
// public synchronized List getEndpoints(String product,String serviceCode, String endpointType) throws ClientException {
// if (null == endpoints || Endpoint.findProductDomain(regionId, product, endpoints) == null) {
// if(serviceCode!=null){
// endpoints = remoteProvider.getEndpoints(regionId, serviceCode, endpointType, credential, locationConfig);
// }
// if (endpoints == null|| Endpoint.findProductDomain(regionId, product, endpoints) == null) {
//
// endpoints = iendpoints.getEndpoints();
// }
// }
// return endpoints;
// }
@Override
public synchronized List getEndpoints(String product, String regionId, String serviceCode,
String endpointType) throws ClientException {
if (null == endpoints) {
Endpoint endpoint = null;
if (serviceCode != null) {
locationConfig.setServiceCode(serviceCode);
locationConfig.setRegionId(regionId);
endpoint = remoteProvider.getEndpoint(regionId, product, serviceCode, endpointType, credential,
locationConfig);
}
if (endpoint == null) {
endpoint = iendpoints.getEndpoint(regionId, product);
}
if (endpoint != null) {
endpoints = new ArrayList();
endpoints.add(endpoint);
}
} else if (Endpoint.findProductDomain(regionId, product, endpoints) == null) {
Endpoint endpoint = null;
if (serviceCode != null) {
endpoint = remoteProvider.getEndpoint(regionId, product, serviceCode, endpointType,
credential, locationConfig);
}
if (endpoint == null) {
endpoint = iendpoints.getEndpoint(regionId, product);
}
if (endpoint != null) {
for (String region : endpoint.getRegionIds()) {
for (ProductDomain productDomain : endpoint.getProductDomains()) {
addEndpoint(endpoint.getName(), region, product, productDomain.getDomianName());
}
}
}
}
return endpoints;
}
public synchronized static DefaultKscProfile getProfile() {
if (null == profile)
profile = new DefaultKscProfile();
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, ICredentialProvider icredential) {
profile = new DefaultKscProfile(regionId, icredential);
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, String accessKeyId, String secret) {
Credential creden = new Credential(accessKeyId, secret);
profile = new DefaultKscProfile(regionId, creden);
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, String accessKeyId, String secret,
String stsToken) {
Credential creden = new Credential(accessKeyId, secret, stsToken);
profile = new DefaultKscProfile(regionId, creden);
return profile;
}
/**
*
* 给个性化用户使用的,CustomizedEndpointsParser 通过这个去解析endpoint,
* 非本地和location;所以一般情况不要使用这个 regionId,
*
* eg: cn-hangzhou productDomainMap,eg: Map
* productDomainMap= new HashMap();
* productDomainMap.put("ecs","ecs.aliyuncs.com") accessKeyId secret
*/
public synchronized static DefaultKscProfile getProfile(String regionId, Map productDomainMap,
String accessKeyId, String secret) {
Credential creden = new Credential(accessKeyId, secret);
IEndpointsProvider provider = CustomizedEndpointsParser.initParser(regionId, productDomainMap);
profile = new DefaultKscProfile(regionId, creden, provider);
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, Map productDomainMap,
String accessKeyId, String secret, String stsToken) {
Credential creden = new Credential(accessKeyId, secret, stsToken);
IEndpointsProvider provider = CustomizedEndpointsParser.initParser(regionId, productDomainMap);
profile = new DefaultKscProfile(regionId, creden, provider);
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, IEndpointsProvider provider,
String accessKeyId, String secret) {
Credential creden = new Credential(accessKeyId, secret);
profile = new DefaultKscProfile(regionId, creden, provider);
return profile;
}
public synchronized static DefaultKscProfile getProfile(String regionId, IEndpointsProvider provider,
String accessKeyId, String secret, String stsToken) {
Credential creden = new Credential(accessKeyId, secret, stsToken);
profile = new DefaultKscProfile(regionId, creden, provider);
return profile;
}
public synchronized static void addEndpoint(String endpointName, String regionId, String product, String domain)
throws ClientException {
if (null == endpoints) {
endpoints = getProfile().getEndpoints(regionId, product);
}
Endpoint endpoint = findEndpointByRegionId(regionId);
if (null == endpoint) {
addEndpoint_(endpointName, regionId, product, domain);
} else {
updateEndpoint(regionId, product, domain, endpoint);
}
}
private static void addEndpoint_(String endpointName, String regionId, String product, String domain) {
Set regions = new HashSet();
regions.add(regionId);
List productDomains = new ArrayList();
productDomains.add(new ProductDomain(product, domain));
Endpoint endpoint = new Endpoint(endpointName, regions, productDomains);
if (endpoints == null) {
endpoints = new ArrayList();
}
endpoints.add(endpoint);
}
private static void updateEndpoint(String regionId, String product, String domain, Endpoint endpoint) {
Set regionIds = endpoint.getRegionIds();
regionIds.add(regionId);
List productDomains = endpoint.getProductDomains();
ProductDomain productDomain = findProductDomain(productDomains, product);
if (null == productDomain) {
ProductDomain newProductDomain = new ProductDomain(product, domain);
productDomains.add(newProductDomain);
} else {
productDomain.setDomianName(domain);
}
}
private static Endpoint findEndpointByRegionId(String regionId) {
if (null == endpoints) {
return null;
}
for (Endpoint endpoint : endpoints) {
if (endpoint.getRegionIds().contains(regionId)) {
return endpoint;
}
}
return null;
}
private static ProductDomain findProductDomain(List productDomains, String product) {
for (ProductDomain productDomain : productDomains) {
if (productDomain.getProductName().equals(product)) {
return productDomain;
}
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy