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

com.ksc.kvs.KSCKVSClient Maven / Gradle / Ivy

package com.ksc.kvs;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;

import com.ksc.ClientConfiguration;
import com.ksc.ClientConfigurationFactory;
import com.ksc.KscServiceException;
import com.ksc.KscWebServiceClient;
import com.ksc.KscWebServiceRequest;
import com.ksc.KscWebServiceResponse;
import com.ksc.Request;
import com.ksc.Response;
import com.ksc.auth.AWSCredentials;
import com.ksc.auth.AWSCredentialsProvider;
import com.ksc.auth.DefaultAWSCredentialsProviderChain;
import com.ksc.http.DefaultErrorResponseHandler;
import com.ksc.http.ExecutionContext;
import com.ksc.http.HttpResponseHandler;
import com.ksc.http.StaxResponseHandler;
import com.ksc.internal.StaticCredentialsProvider;
import com.ksc.kvs.model.GetListRequest;
import com.ksc.kvs.model.KvsResult;
import com.ksc.kvs.model.transform.GetListRequestMarshaller;
import com.ksc.kvs.model.transform.KvsResultStaxUnmarshaller;
import com.ksc.transform.LegacyErrorUnmarshaller;
import com.ksc.transform.StandardErrorUnmarshaller;
import com.ksc.transform.Unmarshaller;
import com.ksc.util.CredentialUtils;
import com.ksc.util.KscRequestMetrics;
import com.ksc.util.KscRequestMetrics.Field;

public class KSCKVSClient extends KscWebServiceClient {
	/** Provider for AWS credentials. */
	private AWSCredentialsProvider kscCredentialsProvider;

	/** Default signing name for the service. */
	private static final String DEFAULT_SIGNING_NAME = "kvs";

	/** The region metadata service name for computing region endpoints. */
	private static final String DEFAULT_ENDPOINT_PREFIX = "kvs";

	/**
	 * Client configuration factory providing ClientConfigurations tailored to
	 * this client
	 */
	
	protected static final ClientConfigurationFactory configFactory = new ClientConfigurationFactory();
	
	protected final List> exceptionUnmarshallers = new ArrayList>();

	
	
	public KSCKVSClient() {
		this(new DefaultAWSCredentialsProviderChain(), configFactory.getConfig());
	}
	
	public KSCKVSClient(ClientConfiguration clientConfiguration) {
		this(new DefaultAWSCredentialsProviderChain(), clientConfiguration);
	}
	
	public KSCKVSClient(AWSCredentials awsCredentials) {
		this(awsCredentials, configFactory.getConfig());
	}
	
	
	public KSCKVSClient(AWSCredentials awsCredentials, ClientConfiguration clientConfiguration) {
		super(clientConfiguration);
		this.kscCredentialsProvider = new StaticCredentialsProvider(awsCredentials);
		init();
	}
	
	public KSCKVSClient(DefaultAWSCredentialsProviderChain defaultAWSCredentialsProviderChain, ClientConfiguration clientConfiguration) {
		super(clientConfiguration);
		// TODO Auto-generated constructor stub
	}

	private void init() {
		
		exceptionUnmarshallers.add(new StandardErrorUnmarshaller());
		exceptionUnmarshallers.add(new LegacyErrorUnmarshaller());

		setServiceNameIntern(DEFAULT_SIGNING_NAME);
		setEndpointPrefix(DEFAULT_ENDPOINT_PREFIX);
		// calling this.setEndPoint(...) will also modify the signer accordingly
		setEndpoint("http://kvs.cn-beijing-6.api.ksyun.com/");
		//HandlerChainFactory chainFactory = new HandlerChainFactory();
		/*
		 * requestHandler2s .addAll(chainFactory
		 * .newRequestHandlerChain("/com/ksc/services/kec/request.handlers"));
		 * requestHandler2s .addAll(chainFactory
		 * .newRequestHandler2Chain("/com/ksc/services/kec/request.handler2s"));
		 */
	}
	
	public KvsResult Preset(GetListRequest kvsRequest){
		ExecutionContext executionContext = createExecutionContext(kvsRequest);
		KscRequestMetrics kscRequestMetrics = executionContext.getKscRequestMetrics();
		kscRequestMetrics.startEvent(Field.ClientExecuteTime);
		Request request = null;
		Response response = null;
		
		try {
			kscRequestMetrics.startEvent(Field.RequestMarshallTime);
			try {
				request = new GetListRequestMarshaller()
						.marshall(super.beforeMarshalling(kvsRequest));
				// Binds the request metrics to the current request.
				request.setKscRequestMetrics(kscRequestMetrics);
			} finally {
				kscRequestMetrics.endEvent(Field.RequestMarshallTime);
			}
			
			StaxResponseHandler responseHandler = new StaxResponseHandler(
					new KvsResultStaxUnmarshaller());
			response = invoke(request, responseHandler, executionContext);

			return response.getKscResponse();

		} finally {

			endClientExecution(kscRequestMetrics, request, response);
		}


	}
	
	/**
	 * Normal invoke with authentication. Credentials are required and may be
	 * overriden at the request level.
	 **/
	private  Response invoke(Request request,
			HttpResponseHandler> responseHandler, ExecutionContext executionContext) {

		executionContext.setCredentialsProvider(
				CredentialUtils.getCredentialsProvider(request.getOriginalRequest(), kscCredentialsProvider));

		return doInvoke(request, responseHandler, executionContext);
	}
	
	/**
	 * Invoke the request using the http client. Assumes credentials (or lack
	 * thereof) have been configured in the ExecutionContext beforehand.
	 **/
	private  Response doInvoke(Request request,
			HttpResponseHandler> responseHandler, ExecutionContext executionContext) {
		request.setEndpoint(endpoint);
		request.setTimeOffset(timeOffset);

		DefaultErrorResponseHandler errorResponseHandler = new DefaultErrorResponseHandler(exceptionUnmarshallers);

		return client.execute(request, responseHandler, errorResponseHandler, executionContext);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy