public S3Response handleRequest( S3DeleteBucketRequest request )
{
S3Response response = new S3Response();
SBucketDao bucketDao = new SBucketDao();
String bucketName = request.getBucketName();
SBucket sbucket = bucketDao.getByName( bucketName );
if ( sbucket != null )
{
S3PolicyContext context = new S3PolicyContext( PolicyActions.DeleteBucket, bucketName );
switch( verifyPolicy( context )) {
case ALLOW:
// -> bucket policy can give users permission to delete a bucket while ACLs cannot
break;
case DENY:
throw new PermissionDeniedException( "Access Denied - bucket policy DENY result" );
case DEFAULT_DENY:
default:
// -> does not matter 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
Tuple<SHost, String> tupleBucketHost = getBucketStorageHost(sbucket);
S3BucketAdapter bucketAdapter = getStorageHostBucketAdapter(tupleBucketHost.getFirst());
bucketAdapter.deleteContainer(tupleBucketHost.getSecond(), request.getBucketName());
// -> cascade-deleting can delete related SObject/SObjectItem objects, but not SAcl, SMeta and policy objects. We
// need to perform deletion of these objects related to bucket manually.
// 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<SObject> objectsInBucket = sbucket.getObjectsInBucket();
Iterator it = objectsInBucket.iterator();
while( it.hasNext())
{
SObject oneObject = (SObject)it.next();
Set<SObjectItem> itemsInObject = oneObject.getItems();
Iterator is = itemsInObject.iterator();
while( is.hasNext())
{
SObjectItem oneItem = (SObjectItem)is.next();
deleteMetaData( oneItem.getId());
deleteObjectAcls( "SObjectItem", oneItem.getId());
}
}
// -> delete all the policy state associated with the bucket
try {
ServiceProvider.getInstance().deleteBucketPolicy( bucketName );
BucketPolicyDao policyDao = new BucketPolicyDao();
policyDao.deletePolicy( bucketName );
}
catch( Exception e ) {
logger.error("When deleting a bucket we must try to delete its policy: ", e);
}
deleteBucketAcls( sbucket.getId());
bucketDao.delete( sbucket );
response.setResultCode(204);
response.setResultDescription("OK");
}
else