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

com.ksyun.api.sdk.regions.DescribeEndpointServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.1.8
Show newest version
package com.ksyun.api.sdk.regions;

import java.io.UnsupportedEncodingException;

import com.ksyun.api.sdk.auth.Credential;
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.http.UnmarshallerContext;
import com.ksyun.api.sdk.reponse.HttpResponse;
import com.ksyun.api.sdk.reponse.KscError;
import com.ksyun.api.sdk.request.HttpRequest;
import com.ksyun.api.sdk.utils.Reader;
import com.ksyun.api.sdk.utils.ReaderFactory;

public class DescribeEndpointServiceImpl implements DescribeEndpointService {
    private static final String DEFAULT_ENDPOINT_TYPE = "openAPI";

    private boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }

    @Override
    public DescribeEndpointResponse describeEndpoint(String regionId, String serviceCode, String endpointType,
                                                     Credential credential, LocationConfig locationConfig) {
        if (isEmpty(serviceCode)) {
            return null;
        }

        DescribeEndpointRequest request = new DescribeEndpointRequest();
        request.setAcceptFormat(FormatType.JSON);
        request.setId(regionId);
        request.setRegionId(locationConfig.getRegionId());
        request.setLocationProduct(serviceCode);
        request.setSecurityToken(credential.getSecurityToken());
        if (isEmpty(endpointType)) {
            endpointType = DEFAULT_ENDPOINT_TYPE;
        }
        request.setEndpointType(endpointType);

        ISigner signer = ShaHmac1Singleton.INSTANCE.getInstance();
        ProductDomain domain = new ProductDomain(locationConfig.getProduct(), locationConfig.getEndpoint());

        try {
            HttpRequest httpRequest = request.signRequest(signer, credential, FormatType.JSON, domain);
            HttpResponse httpResponse = HttpResponse.getResponse(httpRequest);
            if (httpResponse.isSuccess()) {
                String data = new String(httpResponse.getContent(), "utf-8");
                DescribeEndpointResponse response = getEndpointResponse(data, endpointType);
                if (response == null || isEmpty(response.getEndpoint())) {
                    return null;
                }
                return response;
            }
            KscError error = readError(httpResponse, FormatType.JSON);
            if (500 <= httpResponse.getStatus()) {
                System.out.println("Invoke_Error, requestId:" + error.getRequestId() + "; code:" + error.getErrorCode()
                        + "; Msg" + error.getErrorMessage());
                return null;
            }
            System.out.println("Invoke_Error, requestId:" + error.getRequestId() + "; code:" + error.getErrorCode()
                    + "; Msg" + error.getErrorMessage());
            return null;
        } catch (Throwable e) {
            System.out.println("Invoke Remote Error,Msg" + e.getMessage());
            return null;
        }
    }

    private DescribeEndpointResponse getEndpointResponse(String data, String endpointType) throws ClientException {
        Reader reader = ReaderFactory.createInstance(FormatType.JSON);
        UnmarshallerContext context = new UnmarshallerContext();

        context.setResponseMap(reader.read(data, "DescribeEndpointsResponse"));

        int endpointsLength = context.lengthValue("DescribeEndpointsResponse.Endpoints.Length");
        for (int i = 0; i < endpointsLength; i++) {
            if (endpointType.equalsIgnoreCase(context
                    .stringValue("DescribeEndpointsResponse.Endpoints[" + i + "].Type"))) {
                DescribeEndpointResponse response = new DescribeEndpointResponse();

                response.setRequestId(context.stringValue("DescribeEndpointsResponse.RequestId"));
                response.setProduct(context.stringValue("DescribeEndpointsResponse.Endpoints[" + i + "].SerivceCode"));
                response.setEndpoint(context.stringValue("DescribeEndpointsResponse.Endpoints[" + i + "].Endpoint"));
                response.setRegionId(context.stringValue("DescribeEndpointsResponse.Endpoints[" + i + "].Id"));
                return response;
            }
        }
        return null;
    }

    private KscError readError(HttpResponse httpResponse, FormatType format) throws ClientException {
        KscError error = new KscError();
        String responseEndpoint = "Error";
        Reader reader = ReaderFactory.createInstance(format);
        UnmarshallerContext context = new UnmarshallerContext();
        String stringContent = getResponseContent(httpResponse);
        context.setResponseMap(reader.read(stringContent, responseEndpoint));
        error = error.getInstance(context);
        if (httpResponse.getStatus() > 0) {
            error.setStatusCode(httpResponse.getStatus());
        }
        return error;
    }

    private String getResponseContent(HttpResponse httpResponse) throws ClientException {
        String stringContent = null;
        try {
            if (null == httpResponse.getEncoding()) {
                stringContent = new String(httpResponse.getContent());
            } else {
                stringContent = new String(httpResponse.getContent(), httpResponse.getEncoding());
            }
        } catch (UnsupportedEncodingException exp) {
            throw new ClientException("SDK.UnsupportedEncoding", "Can not parse response due to un supported encoding.");
        }
        return stringContent;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy