List of usage examples for com.amazonaws.services.cloudfront.model DistributionSummary getDomainName
public String getDomainName()
The domain name that corresponds to the distribution, for example, d111111abcdef8.cloudfront.net
.
From source file:ch.cyberduck.core.cloudfront.CloudFrontDistributionConfiguration.java
License:Open Source License
private Distribution readDownloadDistribution(final AmazonCloudFront client, final DistributionSummary summary, final Path container, final Distribution.Method method) throws BackgroundException { // Retrieve distributions configuration to access current logging status settings. try {/* w w w . j a v a 2s. c o m*/ final GetDistributionConfigResult response = client .getDistributionConfig(new GetDistributionConfigRequest(summary.getId())); final DistributionConfig configuration = response.getDistributionConfig(); final Distribution distribution = new Distribution(this.getOrigin(container, method), method, summary.isEnabled()); distribution.setId(summary.getId()); distribution.setDeployed("Deployed".equals(summary.getStatus())); distribution.setUrl(URI.create( String.format("%s://%s%s", method.getScheme(), summary.getDomainName(), method.getContext()))); distribution.setSslUrl(method.equals(Distribution.DOWNLOAD) || method.equals(Distribution.CUSTOM) ? URI.create(String.format("https://%s%s", summary.getDomainName(), method.getContext())) : null); distribution.setReference(configuration.getCallerReference()); distribution.setEtag(response.getETag()); distribution.setStatus(LocaleFactory.localizedString(summary.getStatus(), "S3")); distribution.setCNAMEs(configuration.getAliases().getItems() .toArray(new String[configuration.getAliases().getItems().size()])); distribution.setLogging(configuration.getLogging().isEnabled()); distribution .setLoggingContainer(StringUtils.isNotBlank(configuration.getLogging().getBucket()) ? ServiceUtils.findBucketNameInHostname(configuration.getLogging().getBucket(), new S3Protocol().getDefaultHostname()) : null); if (StringUtils.isNotBlank(configuration.getDefaultRootObject())) { distribution.setIndexDocument(configuration.getDefaultRootObject()); } if (this.getFeature(Purge.class, method) != null) { distribution.setInvalidationStatus(this.readInvalidationStatus(client, distribution)); } if (this.getFeature(DistributionLogging.class, method) != null) { distribution.setContainers(new S3BucketListService(session).list( new Path(String.valueOf(Path.DELIMITER), EnumSet.of(Path.Type.volume, Path.Type.directory)), new DisabledListProgressListener()).toList()); } return distribution; } catch (AmazonClientException e) { throw new AmazonServiceExceptionMappingService().map("Cannot read CDN configuration", e); } }
From source file:org.duracloud.s3task.streaminghls.EnableHlsTaskRunner.java
License:Apache License
public String performTask(String taskParameters) { EnableStreamingTaskParameters taskParams = EnableStreamingTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); boolean secure = taskParams.isSecure(); List<String> allowedOrigins = taskParams.getAllowedOrigins(); log.info("Performing " + TASK_NAME + " task on space " + spaceId + ". Secure streaming set to " + secure); // Will throw if bucket does not exist String bucketName = unwrappedS3Provider.getBucketName(spaceId); String domainName = null;//from w ww . j a va2s.co m String distId = null; String oaIdentityId = getOriginAccessId(); EnableStreamingTaskResult taskResult = new EnableStreamingTaskResult(); DistributionSummary existingDist = getExistingDistribution(bucketName); if (existingDist != null) { // There is an existing distribution // Ensure that this is not an attempt to change the security type // of this existing distribution boolean existingSecure = !existingDist.getDefaultCacheBehavior().getTrustedSigners().getItems() .isEmpty(); if ((secure && !existingSecure) || (!secure && existingSecure)) { throw new UnsupportedTaskException(TASK_NAME, "The space " + spaceId + " is already configured to stream as " + (secure ? "OPEN" : "SECURE") + " and cannot be updated to stream as " + (secure ? "SECURE" : "OPEN") + ". To do this, you must first execute the " + StorageTaskConstants.DELETE_HLS_TASK_NAME + " task."); } distId = existingDist.getId(); if (!existingDist.isEnabled()) { // Distribution is disabled, enable it setDistributionState(distId, true); } domainName = existingDist.getDomainName(); } else { // No existing distribution, need to create one // Create S3 Origin S3OriginConfig s3OriginConfig = new S3OriginConfig() .withOriginAccessIdentity(S3_ORIGIN_OAI_PREFIX + oaIdentityId); Origin s3Origin = new Origin().withDomainName(bucketName + S3_ORIGIN_SUFFIX) .withS3OriginConfig(s3OriginConfig).withId("S3-" + bucketName); // Only include trusted signers on secure distributions TrustedSigners signers = new TrustedSigners(); if (secure) { signers.setItems(Collections.singletonList(cfAccountId)); signers.setEnabled(true); signers.setQuantity(1); } else { signers.setEnabled(false); signers.setQuantity(0); } DefaultCacheBehavior defaultCacheBehavior = new DefaultCacheBehavior(); defaultCacheBehavior.setTrustedSigners(signers); defaultCacheBehavior.setViewerProtocolPolicy(ViewerProtocolPolicy.RedirectToHttps); // Forwarding headers to support CORS, see: // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-cors defaultCacheBehavior.setAllowedMethods( new AllowedMethods().withItems(Method.GET, Method.HEAD, Method.OPTIONS).withQuantity(3)); defaultCacheBehavior.setForwardedValues(new ForwardedValues().withQueryString(false) .withCookies(new CookiePreference().withForward(ItemSelection.None)) .withHeaders(new Headers() .withItems("Origin", "Access-Control-Request-Headers", "Access-Control-Request-Method") .withQuantity(3))); // Setting other cache behaviors required by the client defaultCacheBehavior.setMinTTL(0l); defaultCacheBehavior.setTargetOriginId(s3Origin.getId()); // Create origins list Origins origins; CacheBehaviors cacheBehaviors = new CacheBehaviors(); if (secure) { // Create Origin to allow signed cookies to be set through a CloudFront call CustomOriginConfig cookiesOriginConfig = new CustomOriginConfig() .withOriginProtocolPolicy(OriginProtocolPolicy.HttpsOnly).withHTTPPort(80) .withHTTPSPort(443); String getCookiesPath = "/durastore/aux"; String cookiesOriginId = "Custom origin - " + dcHost + getCookiesPath; Origin cookiesOrigin = new Origin().withDomainName(dcHost).withOriginPath(getCookiesPath) .withId(cookiesOriginId).withCustomOriginConfig(cookiesOriginConfig); origins = new Origins().withItems(s3Origin, cookiesOrigin).withQuantity(2); // Create behavior for cookies origin CookiePreference cookiePreference = new CookiePreference().withForward(ItemSelection.All); CacheBehavior cookiesCacheBehavior = new CacheBehavior().withPathPattern("/cookies") .withTargetOriginId(cookiesOriginId) .withViewerProtocolPolicy(ViewerProtocolPolicy.RedirectToHttps) .withAllowedMethods(new AllowedMethods().withItems(Method.GET, Method.HEAD).withQuantity(2)) .withForwardedValues( new ForwardedValues().withQueryString(true).withCookies(cookiePreference)) .withTrustedSigners(new TrustedSigners().withEnabled(false).withQuantity(0)).withMinTTL(0l); cacheBehaviors = cacheBehaviors.withItems(cookiesCacheBehavior).withQuantity(1); } else { origins = new Origins().withItems(s3Origin).withQuantity(1); } // Build distribution DistributionConfig distributionConfig = new DistributionConfig() .withCallerReference("" + System.currentTimeMillis()).withOrigins(origins).withEnabled(true) .withComment("HLS streaming for space: " + spaceId) .withDefaultCacheBehavior(defaultCacheBehavior); if (secure) { distributionConfig.setCacheBehaviors(cacheBehaviors); } Distribution dist = cfClient.createDistribution(new CreateDistributionRequest(distributionConfig)) .getDistribution(); domainName = dist.getDomainName(); } // Set bucket policy to accept origin access identity setBucketAccessPolicy(bucketName, oaIdentityId); // Set CORS policy on bucket setCorsPolicy(bucketName, allowedOrigins, dcHost); // Update bucket tags to include streaming host Map<String, String> spaceProps = s3Provider.getSpaceProperties(spaceId); spaceProps.put(HLS_STREAMING_HOST_PROP, domainName); spaceProps.put(HLS_STREAMING_TYPE_PROP, secure ? STREAMING_TYPE.SECURE.name() : STREAMING_TYPE.OPEN.name()); unwrappedS3Provider.setNewSpaceProperties(spaceId, spaceProps); taskResult.setResult(TASK_NAME + " task completed successfully"); // Return results taskResult.setStreamingHost(domainName); String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; }
From source file:org.duracloud.s3task.streaminghls.GetHlsSignedCookiesUrlTaskRunner.java
License:Apache License
public String performTask(String taskParameters) { GetSignedCookiesUrlTaskParameters taskParams = GetSignedCookiesUrlTaskParameters .deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); String ipAddress = taskParams.getIpAddress(); int minutesToExpire = taskParams.getMinutesToExpire(); if (minutesToExpire <= 0) { minutesToExpire = DEFAULT_MINUTES_TO_EXPIRE; }// ww w. ja v a 2s. c om String redirectUrl = taskParams.getRedirectUrl(); log.info("Performing " + TASK_NAME + " task with parameters: spaceId=" + spaceId + ", minutesToExpire=" + minutesToExpire + ", ipAddress=" + ipAddress + ", redirectUrl=" + redirectUrl); // Will throw if bucket does not exist String bucketName = unwrappedS3Provider.getBucketName(spaceId); // Ensure that streaming service is on checkThatStreamingServiceIsEnabled(spaceId, TASK_NAME); // Retrieve the existing distribution for the given space DistributionSummary 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(); // Define expiration date/time Calendar expireCalendar = Calendar.getInstance(); expireCalendar.add(Calendar.MINUTE, minutesToExpire); Map<String, String> signedCookies = new HashMap<>(); try { File cfKeyPathFile = getCfKeyPathFile(this.cfKeyPath); // Generate signed cookies CloudFrontCookieSigner.CookiesForCustomPolicy cookies = CloudFrontCookieSigner .getCookiesForCustomPolicy(SignerUtils.Protocol.https, domainName, cfKeyPathFile, "*", cfKeyId, expireCalendar.getTime(), null, ipAddress); signedCookies.put(cookies.getPolicy().getKey(), cookies.getPolicy().getValue()); signedCookies.put(cookies.getSignature().getKey(), cookies.getSignature().getValue()); signedCookies.put(cookies.getKeyPairId().getKey(), cookies.getKeyPairId().getValue()); } catch (InvalidKeySpecException | IOException e) { throw new RuntimeException("Error encountered attempting to create signed cookies in task " + TASK_NAME + ": " + e.getMessage(), e); } String token = storeCookies(signedCookies, domainName, redirectUrl); GetSignedCookiesUrlTaskResult taskResult = new GetSignedCookiesUrlTaskResult(); taskResult.setSignedCookiesUrl("https://" + domainName + "/cookies?token=" + token); String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; }
From source file:org.duracloud.s3task.streaminghls.GetUrlHlsTaskRunner.java
License:Apache License
public String performTask(String taskParameters) { GetHlsUrlTaskParameters taskParams = GetHlsUrlTaskParameters.deserialize(taskParameters); String spaceId = taskParams.getSpaceId(); String contentId = taskParams.getContentId(); log.info("Performing " + TASK_NAME + " task with parameters: spaceId=" + spaceId + ", contentId=" + contentId);//from w w w . j a v a 2s . c o m // Will throw if bucket does not exist String bucketName = unwrappedS3Provider.getBucketName(spaceId); GetUrlTaskResult taskResult = new GetUrlTaskResult(); // Ensure that streaming service is on checkThatStreamingServiceIsEnabled(spaceId, TASK_NAME); // Retrieve the existing distribution for the given space DistributionSummary 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 HLS streaming. Use " + StorageTaskConstants.ENABLE_HLS_TASK_NAME + " to enable HLS streaming on this space."); } String domainName = existingDist.getDomainName(); taskResult.setStreamUrl("https://" + domainName + "/" + contentId); String toReturn = taskResult.serialize(); log.info("Result of " + TASK_NAME + " task: " + toReturn); return toReturn; }