Example usage for com.amazonaws.services.cloudfront CloudFrontUrlSigner getSignedURLWithCustomPolicy

List of usage examples for com.amazonaws.services.cloudfront CloudFrontUrlSigner getSignedURLWithCustomPolicy

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudfront CloudFrontUrlSigner getSignedURLWithCustomPolicy.

Prototype

public static String getSignedURLWithCustomPolicy(final Protocol protocol, final String distributionDomain,
        final File privateKeyFile, final String s3ObjectKey, final String keyPairId, final Date dateLessThan,
        final Date dateGreaterThan, final String ipRange) throws InvalidKeySpecException, IOException 

Source Link

Document

Returns a signed URL that provides tailored access to private content based on an access time window and an ip range.

Usage

From source file:org.duracloud.s3task.streaming.GetSignedUrlTaskRunner.java

License:Apache License

public String performTask(String taskParameters) {
    GetSignedUrlTaskParameters taskParams = GetSignedUrlTaskParameters.deserialize(taskParameters);

    String spaceId = taskParams.getSpaceId();
    String contentId = taskParams.getContentId();
    String resourcePrefix = taskParams.getResourcePrefix();
    String ipAddress = taskParams.getIpAddress();
    int minutesToExpire = taskParams.getMinutesToExpire();
    if (minutesToExpire <= 0) {
        minutesToExpire = DEFAULT_MINUTES_TO_EXPIRE;
    }/* w w w.  j  av a2s .  c  o  m*/

    log.info("Performing " + TASK_NAME + " task with parameters: spaceId=" + spaceId + ", contentId="
            + contentId + ", resourcePrefix=" + resourcePrefix + ", minutesToExpire=" + minutesToExpire
            + ", ipAddress=" + ipAddress);

    // Will throw if bucket does not exist
    String bucketName = unwrappedS3Provider.getBucketName(spaceId);
    GetSignedUrlTaskResult taskResult = new GetSignedUrlTaskResult();

    // Ensure that streaming service is on
    checkThatStreamingServiceIsEnabled(spaceId, TASK_NAME);

    // Retrieve the existing distribution for the given space
    StreamingDistributionSummary existingDist = getExistingDistribution(bucketName);
    if (null == existingDist) {
        throw new UnsupportedTaskException(TASK_NAME,
                "The " + TASK_NAME + " task can only be used after a space "
                        + "has been configured to enable secure streaming. Use "
                        + StorageTaskConstants.ENABLE_STREAMING_TASK_NAME
                        + " to enable secure streaming on this space.");
    }
    String domainName = existingDist.getDomainName();

    // Verify that this is a secure distribution
    if (existingDist.getTrustedSigners().getItems().isEmpty()) {
        throw new UnsupportedTaskException(TASK_NAME,
                "The " + TASK_NAME + " task cannot be used to request a "
                        + "stream from an open distribution. Use " + StorageTaskConstants.GET_URL_TASK_NAME
                        + " instead.");
    }

    // Make sure resourcePrefix is a valid string
    if (null == resourcePrefix) {
        resourcePrefix = "";
    }

    // Define expiration date/time
    Calendar expireCalendar = Calendar.getInstance();
    expireCalendar.add(Calendar.MINUTE, minutesToExpire);

    try {

        File cfKeyPathFile = getCfKeyPathFile(this.cfKeyPath);
        String signedUrl = CloudFrontUrlSigner.getSignedURLWithCustomPolicy(SignerUtils.Protocol.rtmp,
                domainName, cfKeyPathFile, contentId, cfKeyId, expireCalendar.getTime(), null, ipAddress);
        taskResult.setSignedUrl("rtmp://" + domainName + "/cfx/st/" + resourcePrefix + signedUrl);
    } catch (InvalidKeySpecException | IOException e) {
        throw new RuntimeException(
                "Error encountered attempting to sign URL for" + " task " + TASK_NAME + ": " + e.getMessage(),
                e);
    }

    String toReturn = taskResult.serialize();
    log.info("Result of " + TASK_NAME + " task: " + toReturn);
    return toReturn;
}