Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-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.internal;
 
 import static com.amazonaws.SDKGlobalConfiguration.EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY;
 
 import java.net.URL;
 
 
Simple client for accessing the Amazon EC2 Instance Metadata Service.
 
 public class EC2MetadataClient {


    
Default endpoint for the Amazon EC2 Instance Metadata Service.
 
     private static final String EC2_METADATA_SERVICE_URL = "http://169.254.169.254";

    
Default resource path for credentials in the Amazon EC2 Instance Metadata Service.
 
     public static final String SECURITY_CREDENTIALS_RESOURCE = "/latest/meta-data/iam/security-credentials/";
 
     private static final Log log = LogFactory.getLog(EC2MetadataClient.class);

    
Connects to the Amazon EC2 Instance Metadata Service to retrieve the default credential information (if any).

Returns:
The response from the Amazon EC2 Instance Metadata Service, or null if no credential information was available.
Throws:
java.io.IOException If any problems are encountered while connecting to the Amazon EC2 Instance Metadata Service.
 
     public String getDefaultCredentials() throws IOException {
         String securityCredentialsList = readResource();
 
         securityCredentialsList = securityCredentialsList.trim();
         String[] securityCredentials = securityCredentialsList.split("\n");
         if (securityCredentials.length == 0) return null;
 
         String securityCredentialsName = securityCredentials[0];
 
         return readResource( + securityCredentialsName);
     }

    
Connects to the metadata service to read the specified resource and returns the text contents.

Parameters:
resourcePath The resource
Returns:
The text payload returned from the Amazon EC2 Instance Metadata service for the specified resource path.
Throws:
java.io.IOException If any problems were encountered while connecting to metadata service for the requested resource path.
com.amazonaws.AmazonClientException If the requested metadata service is not found.
 
     public String readResource(String resourcePaththrows IOExceptionAmazonClientException {
         URL url = getEc2MetadataServiceUrlForResource(resourcePath);
         .debug("Connecting to EC2 instance metadata service at URL: " + url.toString());
 
         HttpURLConnection connection = (HttpURLConnection)url.openConnection();
         connection.setConnectTimeout(1000 * 2);
         connection.setRequestMethod("GET");
         connection.setDoOutput(true);
         connection.connect();
 
         return readResponse(connection);
     }


    
Reads a response from the Amazon EC2 Instance Metadata Service and returns the content as a string.

Parameters:
connection The connection to the Amazon EC2 Instance Metadata Service.
Returns:
The content contained in the response from the Amazon EC2 Instance Metadata Service.
Throws:
java.io.IOException If any problems ocurred while reading the response.
    private String readResponse(HttpURLConnection connectionthrows IOException {
    	if (connection.getResponseCode() == .)
    		throw new AmazonClientException("The requested metadata is not found at " + connection.getURL());
    	
        InputStream inputStream = connection.getInputStream();
        try {
            StringBuilder buffer = new StringBuilder();
            while (true) {
                int c = inputStream.read();
                if (c == -1) break;
                buffer.append((char)c);
            }
            return buffer.toString();
        } finally {
            inputStream.close();
        }
    }

    
Constructs a URL to the EC2 metadata service for the specified resource path.

Parameters:
resourcePath The resource portion of the URL.
Returns:
A URL to the EC2 metadata service for the specified resource path.
Throws:
java.io.IOException If a valid URL could not be constructed.
    private URL getEc2MetadataServiceUrlForResource(String resourcePaththrows IOException {
        String endpoint = ;
        if (System.getProperty() != null) {
            endpoint = System.getProperty();
        }
        return new URL(endpoint + resourcePath);
    }
New to GrepCode? Check out our FAQ X