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.services.ec2.model.transform;
 
 import java.net.URI;
 
RequestHandler that generates a pre-signed URL for copying encrypted snapshots
 
 
     @Override
     public void beforeRequest(Request<?> request) {
 
         AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
 
         if (originalRequest instanceof CopySnapshotRequest) {
 
             CopySnapshotRequest originalCopySnapshotRequest = (CopySnapshotRequestoriginalRequest;
            
             // Return if presigned url is already specified by the user. 
             if (originalCopySnapshotRequest.getPresignedUrl() != nullreturn;
 
             String serviceName = "ec2";
 
             // The source regions where the snapshot currently resides.
             String sourceRegion = originalCopySnapshotRequest.getSourceRegion();
             String sourceSnapshotId = originalCopySnapshotRequest
                     .getSourceSnapshotId();
 
             /*
              * The region where the snapshot has to be copied from the source.
              * The original copy snap shot request will have the end point set
              * as the destination region in the client before calling this
              * request.
              */
 
             URI endPointDestination = request.getEndpoint();
             String destinationRegion = originalCopySnapshotRequest
                     .getDestinationRegion() != null ? originalCopySnapshotRequest
                     .getDestinationRegion() : RegionUtils.getRegionByEndpoint(
                     endPointDestination.getHost()).toString();
 
             URI endPointSource = toURI(RegionUtils.getRegion(sourceRegion)
                     .getServiceEndpoint(serviceName));
 
             Request<CopySnapshotRequestrequestForPresigning = generateRequestForPresigning(
                     sourceSnapshotIdsourceRegiondestinationRegion);
 
             requestForPresigning.setEndpoint(endPointSource);
             requestForPresigning.setHttpMethod(.);
 
             AWS4Signer signer = new AWS4Signer();
 
             signer.presignRequest(requestForPresigningnull);
 
             originalCopySnapshotRequest
                     .setPresignedUrl(generateUrl(requestForPresigning));
             originalCopySnapshotRequest.setDestinationRegion(destinationRegion);
             request.addParameter("DestinationRegion", StringUtils
                     .fromString(originalCopySnapshotRequest
                             .getDestinationRegion()));
             request.addParameter("PresignedUrl", StringUtils
                     .fromString(originalCopySnapshotRequest.getPresignedUrl()));
         }
 
     }

    
Generates a Request object for the pre-signed URL.
 
             String sourceSnapshotIdString sourceRegion,
            String destinationRegion) {
        CopySnapshotRequest copySnapshotRequest = new CopySnapshotRequest()
                .withSourceSnapshotId(sourceSnapshotId)
                .withSourceRegion(sourceRegion)
                .withDestinationRegion(destinationRegion);
        return new CopySnapshotRequestMarshaller()
                .marshall(copySnapshotRequest);
    }
    private String generateUrl(Request<?> request) {
        URI endpoint = request.getEndpoint();
        String uri = HttpUtils.appendUri(endpoint.toString(),
                request.getResourcePath(), true);
        String encodedParams = HttpUtils.encodeParameters(request);
        if (encodedParams != null) {
            uri += "?" + encodedParams;
        }
        return uri;
    }

    
Returns the endpoint as a URI.
    private URI toURI(String endpointthrows IllegalArgumentException {
        if (endpoint.contains("://") == false) {
            endpoint = . + "://" + endpoint;
        }
        try {
            return new URI(endpoint);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }
    @Override
    public void afterResponse(Request<?> requestResponse<?> response) {
    }
    @Override
    public void afterError(Request<?> requestResponse<?> responseException e) {
    }
New to GrepCode? Check out our FAQ X