private Distribution convert(
     final org.jets3t.service.model.cloudfront.Distribution d, Distribution.Method method)
     throws IOException, CloudFrontServiceException {
   // Retrieve distributions configuration to access current logging status settings.
   final DistributionConfig distributionConfig = this.getDistributionConfig(d);
   final String loggingTarget;
   if (null == distributionConfig.getLoggingStatus()) {
     // Default logging target to origin itself
     loggingTarget =
         ServiceUtils.findBucketNameInHostname(
             d.getConfig().getOrigin().getDomainName(), Protocol.S3_SSL.getDefaultHostname());
   } else {
     loggingTarget =
         ServiceUtils.findBucketNameInHostname(
             distributionConfig.getLoggingStatus().getBucket(),
             Protocol.S3_SSL.getDefaultHostname());
   }
   final Distribution distribution =
       new Distribution(
           d.getId(),
           distributionConfig.getEtag(),
           distributionConfig.getCallerReference(),
           d.getConfig().getOrigin().getDomainName(),
           method,
           d.getConfig().isEnabled(),
           d.isDeployed(),
           // CloudFront URL
           String.format("%s://%s%s", method.getScheme(), d.getDomainName(), method.getContext()),
           method.equals(Distribution.DOWNLOAD) || method.equals(Distribution.CUSTOM)
               ? String.format("https://%s%s", d.getDomainName(), method.getContext())
               : null, // No SSL
           null,
           Locale.localizedString(d.getStatus(), "S3"),
           distributionConfig.getCNAMEs(),
           distributionConfig.getLoggingStatus().isEnabled(),
           loggingTarget,
           distributionConfig.getDefaultRootObject());
   if (this.isInvalidationSupported(method)) {
     distribution.setInvalidationStatus(this.readInvalidationStatus(distribution));
   }
   if (this.isLoggingSupported(method)) {
     distribution.setContainers(this.getContainers());
   }
   return distribution;
 }
 @Override
 protected void prompt(LoginController controller) throws LoginCanceledException {
   // Configure with the same host as S3 to get the same credentials from the keychain.
   controller.check(
       new Host(Protocol.S3_SSL, Protocol.S3_SSL.getDefaultHostname(), host.getCredentials()),
       this.getName(),
       null,
       true,
       false,
       false);
 }
 protected List<String> getContainers() {
   // List S3 containers
   final Session session =
       SessionFactory.createSession(
           new Host(Protocol.S3_SSL, Protocol.S3_SSL.getDefaultHostname(), host.getCredentials()));
   if (session.getHost().getCredentials().validate(session.getHost().getProtocol())) {
     List<String> buckets = new ArrayList<String>();
     for (Path bucket : session.mount().children()) {
       buckets.add(bucket.getName());
     }
     Collections.sort(buckets);
     return buckets;
   }
   return Collections.emptyList();
 }
  @Override
  public void write(
      boolean enabled,
      String origin,
      Distribution.Method method,
      String[] cnames,
      boolean logging,
      String loggingBucket,
      String defaultRootObject) {
    try {
      this.check();

      // Configure CDN
      LoggingStatus loggingStatus = null;
      if (logging) {
        if (this.isLoggingSupported(method)) {
          final String loggingDestination =
              StringUtils.isNotBlank(loggingBucket)
                  ? ServiceUtils.generateS3HostnameForBucket(
                      loggingBucket, false, Protocol.S3_SSL.getDefaultHostname())
                  : origin;
          loggingStatus =
              new LoggingStatus(
                  loggingDestination,
                  Preferences.instance().getProperty("cloudfront.logging.prefix"));
        }
      }
      StringBuilder name =
          new StringBuilder(Locale.localizedString("Amazon CloudFront", "S3"))
              .append(" ")
              .append(method.toString());
      if (enabled) {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Enable {0} Distribution", "Status"), name));
      } else {
        this.message(
            MessageFormat.format(
                Locale.localizedString("Disable {0} Distribution", "Status"), name));
      }
      Distribution d = distributionStatus.get(method).get(origin);
      if (null == d) {
        if (log.isDebugEnabled()) {
          log.debug(String.format("No existing distribution found for method %s", method));
        }
        this.createDistribution(enabled, method, origin, cnames, loggingStatus, defaultRootObject);
      } else {
        boolean modified = false;
        if (d.isEnabled() != enabled) {
          modified = true;
        }
        if (!Arrays.equals(d.getCNAMEs(), cnames)) {
          modified = true;
        }
        if (d.isLogging() != logging) {
          modified = true;
        }
        // Compare default root object for possible change
        if (!StringUtils.equals(d.getDefaultRootObject(), defaultRootObject)) {
          modified = true;
        }
        // Compare logging target for possible change
        if (!StringUtils.equals(d.getLoggingTarget(), loggingBucket)) {
          modified = true;
        }
        if (modified) {
          this.updateDistribution(
              enabled,
              method,
              origin,
              d.getId(),
              d.getEtag(),
              d.getReference(),
              cnames,
              loggingStatus,
              defaultRootObject);
        } else {
          log.info("Skip updating distribution not modified.");
        }
      }
    } catch (CloudFrontServiceException e) {
      this.error("Cannot write CDN configuration", e);
    } catch (IOException e) {
      this.error("Cannot write CDN configuration", e);
    } finally {
      distributionStatus.get(method).clear();
    }
  }
 @Override
 protected void login(LoginController controller) throws IOException {
   super.login(controller);
   controller.success(
       new Host(Protocol.S3_SSL, Protocol.S3_SSL.getDefaultHostname(), host.getCredentials()));
 }