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

com.aliyun.sdk.gateway.sls.BaseClientBuilder Maven / Gradle / Ivy

There is a newer version: 0.2.16-beta
Show newest version
package com.aliyun.sdk.gateway.sls;

import com.aliyun.sdk.gateway.sls.internal.interceptor.*;
import com.aliyun.sdk.gateway.sls.policy.SLSUserAgentPolicy;
import darabonba.core.TeaClientBuilder;
import darabonba.core.client.ClientConfiguration;
import darabonba.core.client.ClientOption;
import darabonba.core.client.IClientBuilder;
import darabonba.core.interceptor.InterceptorChain;

import java.util.Optional;

import static darabonba.core.EndpointType.*;

public abstract class BaseClientBuilder, ClientT> extends TeaClientBuilder {

    @Override
    protected String serviceName() {
        return "SLS";
    }

    BuilderT serviceConfiguration(Configuration serviceConfiguration) {
        clientConfiguration.setOption(ClientOption.SERVICE_CONFIGURATION, serviceConfiguration);
        return (BuilderT) this;
    }

    @Override
    protected ClientConfiguration mergeServiceDefaults(ClientConfiguration configuration) {
        return configuration.merge(ClientConfiguration.create()
                .setOption(ClientOption.SERVICE_CONFIGURATION, Configuration.create())
                .setOption(ClientOption.USER_AGENT_SERVICE_SUFFIX, SLSUserAgentPolicy.getDefaultUserAgentSuffix()));
    }

    @Override
    protected ClientConfiguration finalizeServiceConfiguration(ClientConfiguration configuration) {
        //interceptor
        InterceptorChain chain = InterceptorChain.create();
//        chain.addRequestInterceptor(new TransformRequestBodyInterceptor());
        chain.addHttpRequestInterceptor(new MakeMutableHttpRequestInterceptor());
        chain.addHttpRequestInterceptor(new GenerateUrlInterceptor());
        chain.addHttpRequestInterceptor(new SigningInterceptor());
        chain.addResponseInterceptor(new MakeMutableResponseInterceptor());
        chain.addResponseInterceptor(new ResponseBodyInterceptor());
        chain.addOutputInterceptor(new FinalizedOutputInterceptor());
        configuration.setOption(ClientOption.INTERCEPTOR_CHAIN, chain);

        configuration.setOption(ClientOption.HTTP_PROTOCOL, resolveHttpProtocol(configuration));
        //endpoint
        configuration.setOption(ClientOption.ENDPOINT, resolveEndpoint(configuration));

        return configuration;
    }

    private String resolveHttpProtocol(ClientConfiguration config) {
        return Optional.ofNullable(config.option(ClientOption.HTTP_PROTOCOL))
                .orElse("http");
    }

    private String resolveEndpoint(ClientConfiguration config) {
        return Optional.ofNullable(config.option(ClientOption.ENDPOINT))
                .orElseGet(() -> endpointFromRegion(config));
    }

    private static String getShareEndpoint(String region) {
        if ("cn-hangzhou-corp".equals(region) || "cn-shanghai-corp".equals(region)) {
            return region + ".sls.aliyuncs.com";
        } else if ("cn-zhangbei-corp".equals(region)) {
            return "zhangbei-corp-share.log.aliyuncs.com";
        } else {
            return region + "-share.log.aliyuncs.com";
        }
    }

    private String endpointFromRegion(ClientConfiguration config) {
        final String region = config.option(ClientOption.REGION);
        final String type = Optional.ofNullable(config.option(ClientOption.ENDPOINT_TYPE)).orElse(PUBLIC);
        String endpoint;
        switch (type) {
            case INTRANET:
                endpoint = region + "-intranet.log.aliyuncs.com";
                break;
            case SHARE:
                endpoint = getShareEndpoint(region);
                break;
            case ACCELERATE:
                endpoint = "log-global.aliyuncs.com";
                break;
            case PUBLIC:
            default:
                endpoint = region + ".log.aliyuncs.com";
                break;
        }
        return endpoint;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy