org.dasein.cloud.aws.platform.CloudFront Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dasein-cloud-aws Show documentation
Show all versions of dasein-cloud-aws Show documentation
Implementation of the Dasein Cloud API for AWS.
/**
* Copyright (C) 2009-2015 Dell, Inc.
* See annotations for authorship information
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.dasein.cloud.aws.platform;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.ProviderContext;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.aws.AWSCloud;
import org.dasein.cloud.aws.platform.CloudFrontMethod.CloudFrontResponse;
import org.dasein.cloud.identity.ServiceAction;
import org.dasein.cloud.platform.CDNCapabilities;
import org.dasein.cloud.platform.CDNSupport;
import org.dasein.cloud.platform.Distribution;
import org.dasein.cloud.storage.Blob;
import org.dasein.cloud.util.APITrace;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class CloudFront implements CDNSupport {
static private final Logger logger = AWSCloud.getLogger(CloudFront.class);
private volatile transient CloudFrontCapabilities capabilities;
private AWSCloud provider = null;
CloudFront(AWSCloud provider) {
this.provider = provider;
}
@Override
public @Nonnull String create(@Nonnull String bucket, @Nonnull String name, boolean active, @CheckForNull String ... cnames) throws InternalException, CloudException {
APITrace.begin(provider, "CDN.create");
try {
ProviderContext ctx = provider.getContext();
if( ctx == null ) {
throw new CloudException("No context was established for this request");
}
CloudFrontResponse response;
CloudFrontMethod method;
NodeList blocks;
provider.getStorageServices().getBlobStoreSupport().makePublic(bucket);
for( Blob file : provider.getStorageServices().getBlobStoreSupport().list(bucket) ) {
if( !file.isContainer() ) {
provider.getStorageServices().getBlobStoreSupport().makePublic(file.getBucketName(), file.getObjectName());
}
}
method = new CloudFrontMethod(provider, CloudFrontAction.CREATE_DISTRIBUTION, null, toConfigXml(bucket, name, null, null, null, active, cnames));
try {
response = method.invoke();
}
catch( CloudFrontException e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = response.document.getElementsByTagName("Distribution");
for( int i=0; i headers = new HashMap();
CloudFrontMethod method;
String etag;
etag = (String)getDistributionWithEtag(distributionId)[1];
headers.put("If-Match", etag);
method = new CloudFrontMethod(provider, CloudFrontAction.DELETE_DISTRIBUTION, headers, null);
try {
method.invoke(distributionId);
return;
}
catch( CloudFrontException e ) {
String code = e.getCode();
if( code != null && code.equals("DistributionNotDisabled") ) {
try { Thread.sleep(10000L); }
catch( InterruptedException interrupt ) { /* ignore */ }
}
else {
logger.error(e.getSummary());
throw new CloudException(e);
}
}
}
}
finally {
APITrace.end();
}
}
@Override
public @Nonnull CDNCapabilities getCapabilities() throws InternalException, CloudException {
if( capabilities == null ) {
capabilities = new CloudFrontCapabilities(provider);
}
return capabilities;
}
@Override
public @Nullable Distribution getDistribution(@Nonnull String distributionId) throws InternalException, CloudException {
APITrace.begin(provider, "CDN.getDistribution");
try {
Object[] parts = getDistributionWithEtag(distributionId);
if( parts.length < 1 ) {
return null;
}
return (Distribution)parts[0];
}
finally {
APITrace.end();
}
}
private @Nonnull Object[] getDistributionWithEtag(@Nonnull String distributionId) throws InternalException, CloudException {
APITrace.begin(provider, "CDN.getDistributionWithEtag");
try {
ProviderContext ctx = provider.getContext();
if( ctx == null ) {
throw new CloudException("No context was established for this request");
}
CloudFrontMethod method = new CloudFrontMethod(provider, CloudFrontAction.GET_DISTRIBUTION, null, null);
CloudFrontResponse response;
NodeList blocks;
try {
response = method.invoke(distributionId);
}
catch( CloudFrontException e ) {
String code = e.getCode();
if( code != null && code.equals("NoSuchDistribution") ) {
return new Object[] { null, null, null };
}
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = response.document.getElementsByTagName("Distribution");
for( int i=0; i 0 ) {
callerReference = blocks.item(0).getFirstChild().getNodeValue();
}
return new Object[] { dist, response.etag, callerReference };
}
}
return new Object[0];
}
finally {
APITrace.end();
}
}
@Override
@Deprecated
public @Nonnull String getProviderTermForDistribution(@Nonnull Locale locale) {
try {
return getCapabilities().getProviderTermForDistribution(locale);
} catch( InternalException e ) {
} catch( CloudException e ) {
}
return "distribution"; // legacy
}
@Override
public boolean isSubscribed() throws InternalException, CloudException {
APITrace.begin(provider, "CDN.isSubscribed");
try {
CloudFrontMethod method = new CloudFrontMethod(provider, CloudFrontAction.LIST_DISTRIBUTIONS, null, null);
try {
method.invoke();
return true;
}
catch( CloudFrontException e ) {
if( e.getStatus() == HttpServletResponse.SC_UNAUTHORIZED || e.getStatus() == HttpServletResponse.SC_FORBIDDEN ) {
return false;
}
String code = e.getCode();
if( code != null && (code.equals("SubscriptionCheckFailed") || code.equals("AuthFailure") || code.equals("SignatureDoesNotMatch") || code.equals("InvalidClientTokenId") || code.equals("OptInRequired")) ) {
return false;
}
logger.warn(e.getSummary());
if( logger.isDebugEnabled() ) {
e.printStackTrace();
}
throw new CloudException(e);
}
}
finally {
APITrace.end();
}
}
@Override
public @Nonnull Collection list() throws InternalException, CloudException {
APITrace.begin(provider, "CDN.list");
try {
CloudFrontMethod method = new CloudFrontMethod(provider, CloudFrontAction.LIST_DISTRIBUTIONS, null, null);
ArrayList list = new ArrayList();
CloudFrontResponse response;
NodeList blocks;
try {
response = method.invoke();
}
catch( CloudFrontException e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = response.document.getElementsByTagName("DistributionSummary");
for( int i=0; i listDistributionStatus() throws InternalException, CloudException {
APITrace.begin(provider, "CDN.listDistributionStatus");
try {
CloudFrontMethod method = new CloudFrontMethod(provider, CloudFrontAction.LIST_DISTRIBUTIONS, null, null);
ArrayList list = new ArrayList();
CloudFrontResponse response;
NodeList blocks;
try {
response = method.invoke();
}
catch( CloudFrontException e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = response.document.getElementsByTagName("DistributionSummary");
for( int i=0; i headers = new HashMap();
Object[] distData = getDistributionWithEtag(distributionId);
Distribution distribution = (Distribution)distData[0];
String location = (distribution == null ? null : distribution.getLocation());
String[] parts = (location == null ? new String[0] : location.split("\\."));
CloudFrontResponse response;
String bucket = parts[0];
CloudFrontMethod method;
NodeList blocks;
headers.put("If-Match", (String)distData[1]);
String logDirectory = (distribution == null ? null : distribution.getLogDirectory());
String logName = (distribution == null ? null : distribution.getLogName());
method = new CloudFrontMethod(provider, CloudFrontAction.UPDATE_DISTRIBUTION, headers, toConfigXml(bucket, name, (String)distData[2], logDirectory, logName, active, cnames));
try {
response = method.invoke(distributionId, "config");
}
catch( CloudFrontException e ) {
logger.error(e.getSummary());
throw new CloudException(e);
}
blocks = response.document.getElementsByTagName("Distribution");
for( int i=0; i\r\n");
xml.append("\r\n");
xml.append("");
xml.append(toXml(bucket));
xml.append(".s3.amazonaws.com");
xml.append(" \r\n");
xml.append("");
xml.append(callerReference == null ? String.valueOf(System.currentTimeMillis()) : callerReference);
xml.append(" \r\n");
if( cnames != null ) {
for( String cname : cnames ) {
xml.append("");
xml.append(toXml(cname));
xml.append(" \r\n");
}
}
xml.append("");
xml.append(toXml(name));
xml.append(" \r\n");
xml.append("");
xml.append(String.valueOf(active));
xml.append(" ");
if( logDirectory != null && logName != null ) {
xml.append("\r\n");
xml.append("");
xml.append(logDirectory);
xml.append(" \r\n");
xml.append("");
xml.append(logName);
xml.append(" \r\n");
xml.append(" \r\n");
}
xml.append(" \r\n");
return xml.toString();
}
private @Nullable Distribution toDistributionFromInfo(@Nonnull ProviderContext ctx, @Nullable Node node) {
if( node == null ) {
return null;
}
ArrayList cnames = new ArrayList();
Distribution distribution = new Distribution();
NodeList attrs = node.getChildNodes();
distribution.setProviderOwnerId(ctx.getAccountNumber());
for( int i=0; i cnames = new ArrayList();
Distribution distribution = new Distribution();
NodeList attrs = node.getChildNodes();
//noinspection ConstantConditions
distribution.setProviderOwnerId(provider.getContext().getAccountNumber());
for( int i=0; i", ">").replaceAll("\"", """).replaceAll("'", "'");
}
}