private @Nullable Distribution toDistribution(
      @Nonnull ProviderContext ctx, @Nullable String container)
      throws CloudException, InternalException {
    if (container == null) {
      return null;
    }
    NovaMethod method = new NovaMethod(provider);
    Map<String, String> headers = method.headResource(SERVICE, RESOURCE, container);

    if (headers == null) {
      return null;
    }
    String enabled = null, uriString = null;
    for (String key : headers.keySet()) {
      if (key.equalsIgnoreCase("X-CDN-Enabled")) {
        enabled = headers.get(key);
      } else if (key.equalsIgnoreCase("X-CDN-URI")) {
        if (uriString == null) {
          uriString = headers.get(key);
        }
      } else if (key.equalsIgnoreCase("X-CDN-SSL-URI")) {
        uriString = headers.get(key);
      }
    }
    if (uriString == null) {
      return null;
    }
    String dns;

    try {
      URI uri = new URI(uriString);

      dns = uri.getHost();
      if (uri.getPort() > 0) {
        if (dns.startsWith("https:") && uri.getPort() != 443) {
          dns = dns + ":" + uri.getPort();
        }
        if (dns.startsWith("http:") && uri.getPort() != 80) {
          dns = dns + ":" + uri.getPort();
        }
      }
    } catch (URISyntaxException e) {
      throw new CloudException(e);
    }

    Distribution distribution = new Distribution();

    distribution.setName(container);
    distribution.setActive(enabled != null && enabled.equalsIgnoreCase("true"));
    distribution.setAliases(new String[0]);
    distribution.setDeployed(enabled != null && enabled.equalsIgnoreCase("true"));
    distribution.setDnsName(dns);
    distribution.setLocation(uriString);
    distribution.setLogDirectory(null);
    distribution.setProviderDistributionId(container);
    distribution.setProviderOwnerId(getTenantId());
    return distribution;
  }
  @Override
  public @Nullable Distribution getDistribution(@Nonnull String distributionId)
      throws InternalException, CloudException {
    APITrace.begin(provider, "CDN.getDistribution");
    try {
      ProviderContext ctx = provider.getContext();

      if (ctx == null) {
        throw new InternalException("No context exists for this request");
      }
      NovaMethod method = new NovaMethod(provider);
      Map<String, String> metaData = method.headResource(SERVICE, RESOURCE, distributionId);

      if (metaData == null) {
        return null;
      }
      Distribution distribution = new Distribution();

      distribution.setAliases(new String[0]);

      String dnsName = null, cdnUri = null;
      boolean enabled = false;

      for (String key : metaData.keySet()) {
        if (key.equalsIgnoreCase("X-CDN-Enabled")) {
          String value = metaData.get(key);

          enabled = (value != null && value.equalsIgnoreCase("true"));
        } else if (key.equalsIgnoreCase("X-CDN-SSL-URI")) {
          dnsName = metaData.get(key);
        } else if (key.equalsIgnoreCase("X-CDN-URI")) {
          cdnUri = metaData.get(key);
        }
      }
      distribution.setActive(enabled);
      distribution.setDeployed(enabled);

      String prefix = "http://";

      if (dnsName == null) {
        dnsName = cdnUri;
        if (dnsName == null) {
          return null;
        }
        if (dnsName.startsWith("http://")) {
          dnsName = dnsName.substring("http://".length());
        }
      } else {
        if (dnsName.startsWith("https://")) {
          dnsName = dnsName.substring("https://".length());
          prefix = "https://";
        }
      }
      distribution.setDnsName(dnsName);
      distribution.setLocation(prefix + distribution.getDnsName() + "/" + distributionId);
      distribution.setLogDirectory(null);
      distribution.setLogName(null);
      distribution.setName(distributionId);
      distribution.setProviderDistributionId(distributionId);
      distribution.setProviderOwnerId(getTenantId());
      return distribution;
    } finally {
      APITrace.end();
    }
  }