Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws;
 
 import static com.amazonaws.SDKGlobalConfiguration.PROFILING_SYSTEM_PROPERTY;
 
 import java.net.URI;
 import java.util.List;
 
 
Abstract base class for Amazon Web Service Java clients.

Responsible for basic client capabilities that are the same across all AWS SDK Java clients (ex: setting the client endpoint).

 
 public abstract class AmazonWebServiceClient {
     private static final String AMAZON = "Amazon";
     private static final String AWS = "AWS";
     public static final boolean LOGGING_AWS_REQUEST_METRIC = true;
 
     private static final Log log =
         LogFactory.getLog(AmazonWebServiceClient.class);
     static {
         // Configures the internal logging of the signers and core
         // classes to use Jakarta Commons Logging to stay consistent with the
         // rest of the library.
         boolean success = com.amazonaws.log.InternalLogFactory.configureFactory(
                             new CommonsLogFactory());
         if (.isDebugEnabled())
             .debug("Internal logging succesfully configured to commons logger: "
                     + success);
     }

    
The service endpoint to which this client will send requests.

Subclass should only read but not assign to this field, at least not without synchronization on the enclosing object for thread-safety reason.

 
     protected volatile URI endpoint;

    
Used to explicitly override the internal signer region computed by the default implementation. This field is typically null.
 
     private volatile String signerRegionOverride;

    
The client configuration
 
     protected ClientConfiguration clientConfiguration;

    
Low level client for sending requests to AWS services.
 
     protected AmazonHttpClient client;

    
Optional request handlers for additional request processing.
 
     protected final List<RequestHandler2requestHandler2s;

    
Optional offset (in seconds) to use when signing requests
 
     protected int timeOffset;

    
AWS signer for authenticating requests.
 
    private volatile Signer signer;

    
The cached service abbreviation for this service, used for identifying service endpoints by region, identifying the necessary signer, etc. Thread safe so it's backward compatible.
    private volatile String serviceName;
    
Constructs a new AmazonWebServiceClient object using the specified configuration.

Parameters:
clientConfiguration The client configuration for this client.
    public AmazonWebServiceClient(ClientConfiguration clientConfiguration) {
        this(clientConfigurationnull);
    }

    
Constructs a new AmazonWebServiceClient object using the specified configuration and request metric collector.

Parameters:
clientConfiguration The client configuration for this client.
requestMetricCollector optional request metric collector to be used at the http client level; can be null.
    public AmazonWebServiceClient(ClientConfiguration clientConfiguration,
            RequestMetricCollector requestMetricCollector) {
        this. = clientConfiguration;
         = new AmazonHttpClient(clientConfigurationrequestMetricCollector);
    }

    
Returns the signer.

Note, however, the signer configured for S3 is incomplete at this stage as the information on the S3 bucket and key is not yet known.

    protected Signer getSigner() { return ; }

    
Overrides the default endpoint for this client. Callers can use this method to control which AWS region they want to work with.

This method is not threadsafe. Endpoints should be configured when the client is created and before any service requests are made. Changing it afterwards creates inevitable race conditions for any service requests in transit.

Callers can pass in just the endpoint (ex: "ec2.amazonaws.com") or a full URL, including the protocol (ex: "https://ec2.amazonaws.com"). If the protocol is not specified here, the default protocol from this client's ClientConfiguration will be used, which by default is HTTPS.

For more information on using AWS regions with the AWS SDK for Java, and a complete list of all available endpoints for all AWS services, see: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=3912

Parameters:
endpoint The endpoint (ex: "ec2.amazonaws.com") or a full URL, including the protocol (ex: "https://ec2.amazonaws.com") of the region specific AWS endpoint this client will communicate with.
Throws:
java.lang.IllegalArgumentException If any problems are detected with the specified endpoint.
    public void setEndpoint(String endpointthrows IllegalArgumentException {
        URI uri = toURI(endpoint);
        Signer signer = computeSignerByURI(urifalse);
        synchronized(this)  {
            this. = uri;
            this. = signer;
        }
    }

    
Returns the endpoint as a URI.
    private URI toURI(String endpointthrows IllegalArgumentException {
        /*
         * If the endpoint doesn't explicitly specify a protocol to use, then
         * we'll defer to the default protocol specified in the client
         * configuration.
         */
        if (endpoint.contains("://") == false) {
            endpoint = .getProtocol().toString() + "://" + endpoint;
        }
        try {
            return new URI(endpoint);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }

    
An internal method that is not expected to be normally called except for AWS internal development purposes.

Overrides the default endpoint for this client ("http://dynamodb.us-east-1.amazonaws.com/") and explicitly provides an AWS region ID and AWS service name to use when the client calculates a signature for requests. In almost all cases, this region ID and service name are automatically determined from the endpoint, and callers should use the simpler one-argument form of setEndpoint instead of this method.

Callers can pass in just the endpoint (ex: "dynamodb.us-east-1.amazonaws.com/") or a full URL, including the protocol (ex: "http://dynamodb.us-east-1.amazonaws.com/"). If the protocol is not specified here, the default protocol from this client's ClientConfiguration will be used, which by default is HTTPS.

For more information on using AWS regions with the AWS SDK for Java, and a complete list of all available endpoints for all AWS services, see: http://developer.amazonwebservices.com/connect/entry.jspa?externalID= 3912

Deprecated:
Parameters:
endpoint The endpoint (ex: "dynamodb.us-east-1.amazonaws.com/") or a full URL, including the protocol (ex: "http://dynamodb.us-east-1.amazonaws.com/") of the region specific AWS endpoint this client will communicate with.
serviceName This parameter is ignored.
regionId The ID of the region in which this service resides AND the overriding region for signing purposes.
Throws:
java.lang.IllegalArgumentException If any problems are detected with the specified endpoint.
    @Deprecated
    public void setEndpoint(String endpointString serviceNameString regionId) {
        URI uri = toURI(endpoint);
        Signer signer = computeSignerByServiceRegion(serviceNameregionId,
                regionIdtrue);
        synchronized(this)  {
            this. = signer;
            this. = uri;
            this. = regionId;
        }
    }

    

Deprecated:
this method is now a no-op, as overriding the signer from sublcass is no longer supported.
    @Deprecated protected void configSigner(URI uri) {}
    

Deprecated:
this method is now a no-op, as overriding the signer from sublcass is no longer supported.
    @Deprecated protected void configSigner(String serviceNameString regionId) {}

    
Returns the signer based on the given URI and the current AWS client configuration. Currently only the SQS client can have different region on a per request basis. For other AWS clients, the region remains the same on a per AWS client level.

Note, however, the signer returned for S3 is incomplete at this stage as the information on the S3 bucket and key is not yet known.

    public Signer getSignerByURI(URI uri) {
        return computeSignerByURI(uritrue);
    }

    
Returns the signer for the given uri and the current client configuration.

Note, however, the signer returned for S3 is incomplete at this stage as the information on the S3 bucket and key is not yet known.

Parameters:
signerRegionOverride the overriding signer region; or null if there is none.
isRegionIdAsSignerParam true if the "regionId" is used to configure the signer if applicable; false if this method is called for the purpose of purely setting the communication end point of this AWS client, and therefore the "regionId" parameter will not be used directly for configuring the signer.
    private Signer computeSignerByURI(URI uriString signerRegionOverride,
            boolean isRegionIdAsSignerParam) {
        if (uri == null) {
            throw new IllegalArgumentException(
                    "Endpoint is not set. Use setEndpoint to set an endpoint before performing any request.");
        }
        String service = getServiceNameIntern();
        String region = AwsHostNameUtils.parseRegionName(uri.getHost(), service);
        return computeSignerByServiceRegion(
                serviceregionsignerRegionOverrideisRegionIdAsSignerParam);
    }

    
Returns the signer for the given service name, region id, and the current client configuration.

Note, however, the signer returned for S3 is incomplete at this stage as the information on the S3 bucket and key is not yet known.

Parameters:
regionId the region for sending AWS requests
signerRegionOverride the overriding signer region; or null if there is none.
isRegionIdAsSignerParam true if the "regionId" is used to configure the signer if applicable; false if this method is called for the purpose of purely setting the communication end point of this AWS client, and therefore the "regionId" parameter will not be used directly for configuring the signer.
            String serviceNameString regionId,
            String signerRegionOverride,
            boolean isRegionIdAsSignerParam) {
        String signerType = .getSignerOverride();
        Signer signer = signerType == null
             ? SignerFactory.getSigner(serviceNameregionId)
             : SignerFactory.getSignerByTypeAndService(signerTypeserviceName)
             ;
         if (signer instanceof RegionAwareSigner) {
             // Overrides the default region computed
             RegionAwareSigner regionAwareSigner = (RegionAwareSigner)signer;
            // (signerRegionOverride != null) means that it is likely to be AWS
            // internal dev work, as "signerRegionOverride" is typically null
             // when used in the external release
             if (signerRegionOverride != null)
                 regionAwareSigner.setRegionName(signerRegionOverride);
             else if (regionId != null && isRegionIdAsSignerParam)
                 regionAwareSigner.setRegionName(regionId);
         }
         return signer;
    }

    
An alternative to setEndpoint(java.lang.String), sets the regional endpoint for this client's service calls. Callers can use this method to control which AWS region they want to work with.

This method is not threadsafe. A region should be configured when the client is created and before any service requests are made. Changing it afterwards creates inevitable race conditions for any service requests in transit or retrying.

By default, all service endpoints in all regions use the https protocol. To use http instead, specify it in the ClientConfiguration supplied at construction.

    public void setRegion(Region regionthrows IllegalArgumentException {
        if (region == null) {
            throw new IllegalArgumentException("No region provided");
        }
        final String serviceNameForEndpoint = getServiceNameIntern();
        URI uri = new DefaultServiceEndpointBuilder(serviceNameForEndpoint.getProtocol()
                .toString()).withRegion(region).getServiceEndpoint();
        Signer signer = computeSignerByServiceRegion(serviceNameForEndpointregion.getName(), false);
        synchronized (this) {
            this. = uri;
            this. = signer;
        }
    }

    

Deprecated:
to be removed from this class
    @Deprecated
    public final void setRegion(Regions region) {
        if (region == null)
            throw new IllegalArgumentException("No region provided");
        this.setRegion(Region.getRegion(region));
    }

    
Convenient method for setting region.

Parameters:
region region to set to; must not be null.
See also:
setRegion(com.amazonaws.regions.Region)
    public final void configureRegion(Regions region) {
        if (region == null)
            throw new IllegalArgumentException("No region provided");
        this.setRegion(Region.getRegion(region));
    }

    

Deprecated:
by client configuration via the constructor. This method will be removed later on.
    @Deprecated
    public void setConfiguration(ClientConfiguration clientConfiguration) {
        AmazonHttpClient existingClient = this.;
        RequestMetricCollector requestMetricCollector = null;
        if (existingClient != null) {
            requestMetricCollector = existingClient.getRequestMetricCollector();
            existingClient.shutdown();
        }
        this. = clientConfiguration;
        this. = new AmazonHttpClient(clientConfigurationrequestMetricCollector);
    }

    
Shuts down this client object, releasing any resources that might be held open. This is an optional method, and callers are not expected to call it, but can if they want to explicitly release any open resources. Once a client has been shutdown, it should not be used to make any more requests.
    public void shutdown() {
        .shutdown();
    }

    
Converts a Request<T> object into an HttpRequest object. Copies all the headers, parameters, etc. from the Request into the new HttpRequest.

Parameters:
request The request to convert.
methodName The HTTP method (GET, PUT, DELETE, HEAD) to use in the converted HttpRequest object.
Returns:
A new HttpRequest object created from the details of the specified Request<T> object.
    @Deprecated
    protected <T> HttpRequest convertToHttpRequest(Request<T> requestHttpMethodName methodName) {
        HttpRequest httpRequest = new HttpRequest(methodName);
        for (Entry<StringStringparameter : request.getParameters().entrySet()) {
            httpRequest.addParameter(parameter.getKey(), parameter.getValue());
        }
        for (Entry<StringStringparameter : request.getHeaders().entrySet()) {
            httpRequest.addHeader(parameter.getKey(), parameter.getValue());
        }
        httpRequest.setServiceName(request.getServiceName());
        httpRequest.setEndpoint(request.getEndpoint());
        httpRequest.setResourcePath(request.getResourcePath());
        httpRequest.setOriginalRequest(request.getOriginalRequest());
        return httpRequest;
    }

    

Deprecated:
by addRequestHandler(com.amazonaws.handlers.RequestHandler2). Appends a request handler to the list of registered handlers that are run as part of a request's lifecycle.
Parameters:
requestHandler The new handler to add to the current list of request handlers.
    @Deprecated
    public void addRequestHandler(RequestHandler requestHandler) {
        .add(RequestHandler2.adapt(requestHandler));
    }

    
Appends a request handler to the list of registered handlers that are run as part of a request's lifecycle.

Parameters:
requestHandler2 The new handler to add to the current list of request handlers.
    public void addRequestHandler(RequestHandler2 requestHandler2) {
        .add(requestHandler2);
    }

    
Removes a request handler from the list of registered handlers that are run as part of a request's lifecycle.

Parameters:
requestHandler The handler to remove from the current list of request handlers.
    public void removeRequestHandler(RequestHandler requestHandler) {
        .remove(RequestHandler2.adapt(requestHandler));
    }
    public void removeRequestHandler(RequestHandler2 requestHandler2) {
        .remove(requestHandler2);
    }

    
Runs the beforeMarshalling method of any RequestHandler2s associated with this client.

Parameters:
request the request passed in from the user
Returns:
the (possibly different) request to marshal
    @SuppressWarnings("unchecked")
    protected final <T extends AmazonWebServiceRequest> T beforeMarshalling(
            T request) {
        T local = request;
        for (RequestHandler2 handler : ) {
            local = (T) handler.beforeMarshalling(local);
        }
        return local;
    }
        boolean isMetricsEnabled = isRequestMetricsEnabled(req) || isProfilingEnabled();
        return new ExecutionContext(isMetricsEnabledthis);
    }
    protected final ExecutionContext createExecutionContext(Request<?> req) {
        return createExecutionContext(req.getOriginalRequest());
    }

    

Deprecated:
by createExecutionContext(com.amazonaws.AmazonWebServiceRequest). This method exists only for backward compatiblity reason, so that clients compiled against the older version of this class won't get java.lang.NoSuchMethodError at runtime. However, calling this methods would effectively ignore and disable the request metric collector, if any, specified at the request level. Request metric collector specified at the service client or AWS SDK level will still be honored.
    @Deprecated
    protected final ExecutionContext createExecutionContext() {
        boolean isMetricsEnabled = isRMCEnabledAtClientOrSdkLevel() || isProfilingEnabled();
        return new ExecutionContext(isMetricsEnabledthis);
    }
    /* Check the profiling system property and return true if set */
    protected static boolean isProfilingEnabled() {
        return System.getProperty() != null;
    }

    
Returns true if request metric collection is applicable to the given request; false otherwise.
    protected final boolean isRequestMetricsEnabled(AmazonWebServiceRequest req) {
        RequestMetricCollector c = req.getRequestMetricCollector(); // request level collector
        if (c != null && c.isEnabled()) {
            return true;
        }
        return isRMCEnabledAtClientOrSdkLevel();
    }

    
Returns true if request metric collection is enabled at the service client or AWS SDK level request; false otherwise.
    private boolean isRMCEnabledAtClientOrSdkLevel() {
        return c != null && c.isEnabled();
    }

    
Sets the optional value for time offset for this client. This value will be applied to all requests processed through this client. Value is in seconds, positive values imply the current clock is "fast", negative values imply clock is slow.

Parameters:
timeOffset The optional value for time offset (in seconds) for this client.
    public void setTimeOffset(int timeOffset) {
        this. = timeOffset;
    }

    
Sets the optional value for time offset for this client. This value will be applied to all requests processed through this client. Value is in seconds, positive values imply the current clock is "fast", negative values imply clock is slow.

Parameters:
timeOffset The optional value for time offset (in seconds) for this client.
Returns:
the updated web service client
    public AmazonWebServiceClient withTimeOffset(int timeOffset) {
        setTimeOffset(timeOffset);
        return this;
    }

    
Returns the optional value for time offset for this client. This value will be applied to all requests processed through this client. Value is in seconds, positive values imply the current clock is "fast", negative values imply clock is slow.

Returns:
The optional value for time offset (in seconds) for this client.
    public int getTimeOffset() {
        return ;
    }

    
Returns the client specific com.amazonaws.metrics.RequestMetricCollector; or null if there is none.
        return .getRequestMetricCollector();
    }

    
Returns the client specific request metric collector if there is one; or the one at the AWS SDK level otherwise.
        return mc == null ? AwsSdkMetrics.getRequestMetricCollector() : mc;
    }

    
Returns the most specific request metric collector, starting from the request level, then client level, then finally the AWS SDK level.
    protected final RequestMetricCollector findRequestMetricCollector(Request<?> req) {
        AmazonWebServiceRequest origReq = req.getOriginalRequest();
        RequestMetricCollector mc = origReq.getRequestMetricCollector();
        if (mc != null) {
            return mc;
        }
        mc = getRequestMetricsCollector();
        return mc == null ? AwsSdkMetrics.getRequestMetricCollector() : mc;
    }

    
Convenient method to end the client execution without logging the awsRequestMetrics.
    protected final void endClientExecution(
            AWSRequestMetrics awsRequestMetricsRequest<?> request,
            Response<?> response) {
        this.endClientExecution(awsRequestMetricsrequestresponse,
                !);
    }

    
Common routine to end a client AWS request/response execution and collect the request metrics. Caller of this routine is responsible for starting the event for com.amazonaws.util.AWSRequestMetrics.Field.ClientExecuteTime and call this method in a try-finally block.

Parameters:
loggingAwsRequestMetrics deprecated and ignored
    protected final void endClientExecution(
            AWSRequestMetrics awsRequestMetricsRequest<?> request,
            Response<?> response, @Deprecated boolean loggingAwsRequestMetrics) {
        if (request != null) {
            awsRequestMetrics.endEvent(.);
            awsRequestMetrics.getTimingInfo().endTiming();
            RequestMetricCollector c = findRequestMetricCollector(request);
            c.collectMetrics(requestresponse);
            awsRequestMetrics.log();
        }
    }

    

Deprecated:
by getServiceName().
    @Deprecated
    protected String getServiceAbbreviation() {
        return getServiceNameIntern();
    }

    
Returns the service abbreviation for this service, used for identifying service endpoints by region, identifying the necessary signer, etc. Used to be call "getServiceAbbreviation".
    public String getServiceName() {
        return getServiceNameIntern();
    }

    
Internal method for implementing getServiceName(). Method is protected by intent so peculiar subclass that don't follow the class naming convention can choose to return whatever service name as needed.
    protected String getServiceNameIntern() {
        if ( == null) {
            synchronized(this) {
                if ( == null) {
                    return  = computeServiceName();
                }
            }
        }
        return ;
    }

    
An internal method used to explicitly override the service name computed by the default implementation. This method is not expected to be normally called except for AWS internal development purposes.
    public final void setServiceNameIntern(String serviceName) {
        this. = serviceName;
    }

    
Returns the service name of this AWS http client by first looking it up from the SDK internal configuration, and if not found, derive it from the class name of the immediate subclass of AmazonWebServiceClient. No configuration is necessary if the simple class name of the http client follows the convention of (Amazon|AWS).*(JavaClient|Client).
    private String computeServiceName() {
        Class<?> httpClientClass = Classes.childClassOf(
                AmazonWebServiceClient.classthis);
        final String httpClientName = httpClientClass.getSimpleName();
        String service = ServiceNameFactory.getServiceName(httpClientName);
        if (service != null) {
            return service// only if it is so explicitly configured
        }
        // Otherwise, make use of convention over configuration
        int j = httpClientName.indexOf("JavaClient");
        if (j == -1) {
            j = httpClientName.indexOf("Client");
            if (j == -1) {
                throw new IllegalStateException(
                        "Unrecognized suffix for the AWS http client class name "
                                + httpClientName);
            }
        }
        int i = httpClientName.indexOf();
        int len;
        if (i == -1) {
            i = httpClientName.indexOf();
            if (i == -1) {
                throw new IllegalStateException(
                        "Unrecognized prefix for the AWS http client class name "
                                + httpClientName);
            }
            len = .length();
        } else {
            len = .length();
        }
        if (i >= j) {
            throw new IllegalStateException(
                    "Unrecognized AWS http client class name " + httpClientName);
        }
        String serviceName = httpClientName.substring(i + lenj);
        return serviceName.toLowerCase();
    }

    
Returns the signer region override.

See also:
setSignerRegionOverride(String).
    public final String getSignerRegionOverride() {
        return ;
    }

    
An internal method used to explicitly override the internal signer region computed by the default implementation. This method is not expected to be normally called except for AWS internal development purposes.
    public final void setSignerRegionOverride(String signerRegionOverride) {
        Signer signer = computeSignerByURI(signerRegionOverridetrue);
        synchronized(this)  {
            this. = signer;
            this. = signerRegionOverride;
        }
    }

    
Fluent method for setRegion(com.amazonaws.regions.Region).
 Example:

   AmazonDynamoDBClient client = new AmazonDynamoDBClient(...).<AmazonDynamoDBClient>withRegion(...);

    public <T extends AmazonWebServiceClient> T withRegion(Region region) {
        setRegion(region);
        @SuppressWarnings("unchecked") T t= (T)this;
        return t;
    }

    
Convenient fluent method for setting region.

Parameters:
region region to set to; must not be null.
See also:
withRegion(com.amazonaws.regions.Region)
    public <T extends AmazonWebServiceClient> T withRegion(Regions region) {
        configureRegion(region);
        @SuppressWarnings("unchecked") T t= (T)this;
        return t;
    }
    
Fluent method for setEndpoint(java.lang.String).
 Example:

   AmazonDynamoDBClient client = new AmazonDynamoDBClient(...).<AmazonDynamoDBClient>withEndPoint(...);

    public <T extends AmazonWebServiceClient> T withEndpoint(String endpoint) {
        setEndpoint(endpoint);
        @SuppressWarnings("unchecked") T t= (T)this;
        return t;
    }
New to GrepCode? Check out our FAQ X