S3Response response = new S3Response();
//
String bucketName = request.getBucketName();
SBucketVO sbucket = bucketDao.getByName(bucketName);
TransactionLegacy txn = null;
if (sbucket != null) {
txn = TransactionLegacy.open(TransactionLegacy.AWSAPI_DB);
txn.start();
S3PolicyContext context = new S3PolicyContext(PolicyActions.DeleteBucket, bucketName);
switch (verifyPolicy(context)) {
case ALLOW:
// The bucket policy can give users permission to delete a
// bucket whereas ACLs cannot
break;
case DENY:
throw new PermissionDeniedException("Access Denied - bucket policy DENY result");
case DEFAULT_DENY:
default:
// Irrespective of what the ACLs say, only the owner can delete
// a bucket
String client = UserContext.current().getCanonicalUserId();
if (!client.equals(sbucket.getOwnerCanonicalId())) {
throw new PermissionDeniedException("Access Denied - only the owner can delete a bucket");
}
break;
}
// Delete the file from its storage location
OrderedPair<SHostVO, String> host_storagelocation_pair = getBucketStorageHost(sbucket);
S3BucketAdapter bucketAdapter = getStorageHostBucketAdapter(host_storagelocation_pair.getFirst());
bucketAdapter.deleteContainer(host_storagelocation_pair.getSecond(), request.getBucketName());
// Cascade-deleting can delete related SObject/SObjectItem objects, but not SAcl, SMeta and policy objects.
// To delete SMeta & SAcl objects:
// (1)Get all the objects in the bucket,
// (2)then all the items in each object,
// (3) then all meta & acl data for each item
Set<SObjectVO> objectsInBucket = sbucket.getObjectsInBucket();
Iterator<SObjectVO> it = objectsInBucket.iterator();
while (it.hasNext()) {
SObjectVO oneObject = it.next();
Set<SObjectItemVO> itemsInObject = oneObject.getItems();
Iterator<SObjectItemVO> is = itemsInObject.iterator();
while (is.hasNext()) {
SObjectItemVO oneItem = is.next();
deleteMetaData(oneItem.getId());
deleteObjectAcls("SObjectItem", oneItem.getId());
}
}
// Delete all the policy state associated with the bucket
try {
ServiceProvider.getInstance().deleteBucketPolicy(bucketName);
bPolicyDao.deletePolicy(bucketName);
} catch (Exception e) {
logger.error("When deleting a bucket we must try to delete its policy: ", e);
}
deleteBucketAcls(sbucket.getId());
bucketDao.remove(sbucket.getId());
response.setResultCode(204);
response.setResultDescription("OK");
txn.close();
} else {
response.setResultCode(404);
response.setResultDescription("Bucket does not exist");
}
return response;