/** * @param file File in origin container * @param origin Distribution URL * @return URL to file in distribution */ private URI toUrl(final Path file, final URI origin) { final StringBuilder b = new StringBuilder(String.format("%s://%s", origin.getScheme(), origin.getHost())); if(distribution.getMethod().equals(Distribution.CUSTOM)) { b.append(Path.DELIMITER).append(URIEncoder.encode(PathRelativizer.relativize(origin.getRawPath(), file.getAbsolute()))); } else { if(StringUtils.isNotEmpty(origin.getRawPath())) { b.append(origin.getRawPath()); } if(StringUtils.isNotEmpty(containerService.getKey(file))) { b.append(Path.DELIMITER).append(URIEncoder.encode(containerService.getKey(file))); } } return URI.create(b.toString()).normalize(); }
/** * @param file File in origin container * @return CNAME to distribution */ private List<DescriptiveUrl> toCnameUrl(final Path file) { final List<DescriptiveUrl> urls = new ArrayList<DescriptiveUrl>(); for(String cname : distribution.getCNAMEs()) { final StringBuilder b = new StringBuilder(); b.append(String.format("%s://%s", distribution.getMethod().getScheme(), cname)).append(distribution.getMethod().getContext()); if(StringUtils.isNotEmpty(containerService.getKey(file))) { b.append(Path.DELIMITER).append(URIEncoder.encode(containerService.getKey(file))); } urls.add(new DescriptiveUrl(URI.create(b.toString()).normalize(), DescriptiveUrl.Type.cname, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))); } return urls; } }
protected void deleteDownloadDistribution(final Path container, final Distribution distribution) throws BackgroundException { final URI origin = this.getOrigin(container, distribution.getMethod()); if(log.isDebugEnabled()) { log.debug(String.format("Update %s distribution with origin %s", distribution.getMethod().toString(), origin)); } final AmazonCloudFront client = client(container); client.deleteDistribution(new DeleteDistributionRequest(distribution.getId(), distribution.getEtag())); }
protected void deleteStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException { final URI origin = this.getOrigin(container, distribution.getMethod()); if(log.isDebugEnabled()) { log.debug(String.format("Update %s distribution with origin %s", distribution.getMethod().toString(), origin)); } final AmazonCloudFront client = client(container); client.deleteStreamingDistribution(new DeleteStreamingDistributionRequest(distribution.getId(), distribution.getEtag())); }
log.debug(String.format("No existing distribution found for method %s", distribution.getMethod())); if(distribution.getMethod().equals(Distribution.STREAMING)) { distribution.setId(createStreamingDistribution(container, distribution).getId()); else if(distribution.getMethod().equals(Distribution.DOWNLOAD)) { distribution.setId(createDownloadDistribution(container, distribution).getId()); else if(distribution.getMethod().equals(Distribution.CUSTOM) || distribution.getMethod().equals(Distribution.WEBSITE_CDN)) { distribution.setId(createCustomDistribution(container, distribution).getId()); if(distribution.getMethod().equals(Distribution.DOWNLOAD)) { distribution.setEtag(updateDownloadDistribution(container, distribution).getETag()); else if(distribution.getMethod().equals(Distribution.STREAMING)) { distribution.setEtag(updateStreamingDistribution(container, distribution).getETag()); else if(distribution.getMethod().equals(Distribution.CUSTOM) || distribution.getMethod().equals(Distribution.WEBSITE_CDN)) { distribution.setEtag(updateCustomDistribution(container, distribution).getETag());
@Override public DescriptiveUrlBag toUrl(final Path file) { final DescriptiveUrlBag list = new DescriptiveUrlBag(); list.add(new DescriptiveUrl(this.toUrl(file, distribution.getOrigin()), DescriptiveUrl.Type.origin, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString("Origin", "Info")))); if(distribution.getUrl() != null) { list.add(new DescriptiveUrl(this.toUrl(file, distribution.getUrl()), DescriptiveUrl.Type.cdn, MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString(distribution.getMethod().toString(), "S3")))); } if(distribution.getSslUrl() != null) { list.add(new DescriptiveUrl(this.toUrl(file, distribution.getSslUrl()), DescriptiveUrl.Type.cdn, String.format("%s (SSL)", MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"))))); } if(distribution.getStreamingUrl() != null) { list.add(new DescriptiveUrl(this.toUrl(file, distribution.getStreamingUrl()), DescriptiveUrl.Type.cdn, String.format("%s (Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"))))); } if(distribution.getiOSstreamingUrl() != null) { list.add(new DescriptiveUrl(this.toUrl(file, distribution.getiOSstreamingUrl()), DescriptiveUrl.Type.cdn, String.format("%s (iOS Streaming)", MessageFormat.format(LocaleFactory.localizedString("{0} URL"), LocaleFactory.localizedString(distribution.getMethod().toString(), "S3"))))); } list.addAll(this.toCnameUrl(file)); return list; }
protected UpdateStreamingDistributionResult updateStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException { final URI origin = this.getOrigin(container, distribution.getMethod()); if(log.isDebugEnabled()) { log.debug(String.format("Update %s distribution with origin %s", distribution.getMethod().toString(), origin)); } final AmazonCloudFront client = client(container); final GetStreamingDistributionConfigResult response = client.getStreamingDistributionConfig(new GetStreamingDistributionConfigRequest(distribution.getId())); final StreamingDistributionConfig config = response.getStreamingDistributionConfig() .withEnabled(distribution.isEnabled()) .withS3Origin(new S3Origin(origin.getHost(), StringUtils.EMPTY)) .withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length)); if(distribution.isLogging()) { // Make bucket name fully qualified final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname()); if(log.isDebugEnabled()) { log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget)); } config.setLogging(new StreamingLoggingConfig() .withEnabled(distribution.isLogging()) .withBucket(loggingTarget) .withPrefix(preferences.getProperty("cloudfront.logging.prefix")) ); } return client.updateStreamingDistribution(new UpdateStreamingDistributionRequest(config, distribution.getId(), response.getETag())); }
@Override public void write(final Path container, final Distribution distribution, final LoginCallback prompt) throws BackgroundException { if(distribution.getMethod().equals(Distribution.WEBSITE)) { try { if(distribution.isEnabled()) { String suffix = "index.html"; if(StringUtils.isNotBlank(distribution.getIndexDocument())) { suffix = PathNormalizer.name(distribution.getIndexDocument()); } // Enable website endpoint session.getClient().setWebsiteConfig(container.getName(), new S3WebsiteConfig(suffix)); } else { // Disable website endpoint session.getClient().deleteWebsiteConfig(container.getName()); } } catch(S3ServiceException e) { throw new S3ExceptionMappingService().map("Cannot write website configuration", e); } } else { super.write(container, distribution, prompt); } }
protected UpdateDistributionResult updateCustomDistribution(final Path container, final Distribution distribution) throws BackgroundException { final URI origin = this.getOrigin(container, distribution.getMethod()); if(log.isDebugEnabled()) { log.debug(String.format("Update %s distribution with origin %s", distribution.getMethod().toString(), origin)); } final AmazonCloudFront client = client(container); final GetDistributionConfigResult response = client.getDistributionConfig(new GetDistributionConfigRequest(distribution.getId())); final DistributionConfig config = response.getDistributionConfig() .withEnabled(distribution.isEnabled()) .withDefaultRootObject(distribution.getIndexDocument() != null ? distribution.getIndexDocument() : StringUtils.EMPTY) .withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length)); // Make bucket name fully qualified final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname()); if(log.isDebugEnabled()) { log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget)); } config.setLogging(new LoggingConfig() .withEnabled(distribution.isLogging()) .withIncludeCookies(true) .withBucket(loggingTarget) .withPrefix(preferences.getProperty("cloudfront.logging.prefix")) ); return client.updateDistribution(new UpdateDistributionRequest(config, distribution.getId(), response.getETag())); }
throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Create new %s distribution", distribution.getMethod().toString())); final URI origin = this.getOrigin(container, distribution.getMethod()); final String originId = String.format("%s-%s", preferences.getProperty("application.name"), new AlphanumericRandomStringService().random()); final StreamingDistributionConfig config = new StreamingDistributionConfig(new AlphanumericRandomStringService().random(),
@Override public Distribution run(final Session<?> session) throws BackgroundException { final DistributionConfiguration cdn = session.getFeature(DistributionConfiguration.class); for(Path file : this.getContainers(files)) { if(this.isCanceled()) { throw new ConnectionCanceledException(); } final Distribution distribution = cdn.read(file, method, prompt); if(cdn.getFeature(Index.class, distribution.getMethod()) != null) { // Make sure container items are cached for default root object. distribution.setRootDocuments(session.getFeature(ListService.class).list(containerService.getContainer(file), new DisabledListProgressListener()).toList()); } return distribution; } return this.initialize(); }
int httpPort = 80; int httpsPort = 443; final URI origin = this.getOrigin(container, distribution.getMethod()); if(origin.getPort() != -1) { if(origin.getScheme().equals(Scheme.http.name())) { .withHTTPSPort(httpsPort) .withOriginSslProtocols(new OriginSslProtocols().withQuantity(2).withItems("TLSv1.1", "TLSv1.2")) .withOriginProtocolPolicy(this.getPolicy(distribution.getMethod()))
protected com.amazonaws.services.cloudfront.model.Distribution createDownloadDistribution(final Path container, final Distribution distribution) throws BackgroundException { if(log.isDebugEnabled()) { log.debug(String.format("Create new %s distribution", distribution.getMethod().toString())); final URI origin = this.getOrigin(container, distribution.getMethod()); final String originId = String.format("%s-%s", preferences.getProperty("application.name"), new AlphanumericRandomStringService().random()); final DistributionConfig config = new DistributionConfig(new AlphanumericRandomStringService().random(), distribution.isEnabled())
@Override public void write(final Path file, final Distribution distribution, final LoginCallback prompt) throws BackgroundException { final Path container = containerService.getContainer(file); try { if(distribution.isEnabled()) { String suffix = "index.html"; if(StringUtils.isNotBlank(distribution.getIndexDocument())) { suffix = PathNormalizer.name(distribution.getIndexDocument()); } // Enable website endpoint session.getClient().setWebsiteConfigImpl(container.getName(), new GSWebsiteConfig(suffix)); final DistributionLogging logging = this.getFeature(DistributionLogging.class, distribution.getMethod()); if(logging != null) { new GoogleStorageLoggingFeature(session).setConfiguration(container, new LoggingConfiguration( distribution.isEnabled(), distribution.getLoggingContainer())); } } else { // Disable website endpoint session.getClient().setWebsiteConfigImpl(container.getName(), new GSWebsiteConfig()); } } catch(ServiceException e) { throw new S3ExceptionMappingService().map("Cannot write website configuration", e); } }
final DistributionConfiguration cdn = session.getFeature(DistributionConfiguration.class); distributionEnableButton.setTitle(MessageFormat.format(LocaleFactory.localizedString("Enable {0} Distribution", "Status"), cdn.getName(distribution.getMethod()))); distributionEnableButton.setState(distribution.isEnabled() ? NSCell.NSOnState : NSCell.NSOffState); distributionStatusField.setAttributedStringValue(NSMutableAttributedString.create(distribution.getStatus(), TRUNCATE_MIDDLE_ATTRIBUTES)); distributionAnalyticsSetupUrlField.setAttributedStringValue( HyperlinkAttributedStringFactory.create(analyticsFeature.getSetup(cdn.getHostname(), distribution.getMethod().getScheme(), file, credentials)) ); if(cdn.getFeature(Index.class, distribution.getMethod()) != null) { for(Path next : distribution.getRootDocuments()) { if(next.isFile()) {