Package eu.mosaic.cloud.amazon

Source Code of eu.mosaic.cloud.amazon.AmazonS3Helper

package eu.mosaic.cloud.amazon;

/**
* This is a helper class to be used in conjunction with AmazonConnection in order to
*  provide useful methods and to simplify the code
*
* @author Adrian Copie
*
*/

import java.util.Iterator;
import java.util.Set;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.Grant;
import com.amazonaws.services.s3.model.Permission;

public class AmazonS3Helper {

  private static AmazonS3Helper instance = null;
  private AmazonS3Client s3client = null;

  private AmazonS3Helper(AmazonS3Client client) {
    this.s3client = client;
  }

  /**
   * Returns and instance and only one of the AmazonS3Helper class
   *
   * @param client
   *            the {@link com.amazonaws.services.s3.AmazonS3Client}
   * @return the instance of AmazonS3Helper class
   */
  public static AmazonS3Helper getInstance(AmazonS3Client client) {
    if (instance == null) {
      instance = new AmazonS3Helper(client);
    }

    return instance;
  }

  /**
   * Helper method for checking the existence of a bucket
   *
   * @param bucketName
   *            the name of the bucked who's existence is checked
   * @return <code>true</code> if the specified bucket exists,
   *         <code>false</code> otherwise
   */
  public boolean bucketExists(String bucketName) {
    return s3client.doesBucketExist(bucketName);
  }

  /**
   * Helper method for checking if a specified bucket has the Write
   * permission, i.e. objects could be stored inside
   *
   * @param bucketName
   * @return <code>true</code> if the specified bucket has Write permission,
   *         <code>false</code> otherwise
   */
  public boolean hasWritePermission(String bucketName) {
    boolean bWritePermission = false;

    AccessControlList aclList = s3client.getBucketAcl(bucketName);
    Set<Grant> grants = aclList.getGrants();

    Iterator<Grant> i = grants.iterator();
    while (i.hasNext()) {
      Permission permission = i.next().getPermission();
      if (permission.equals(Permission.Write)) {
        bWritePermission = true;
        break;
      }
    }

    return bWritePermission;
  }

}
TOP

Related Classes of eu.mosaic.cloud.amazon.AmazonS3Helper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.