Package eu.mosaic.cloud.amazon

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

package eu.mosaic.cloud.amazon;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import eu.mosaic.cloud.driver.DriverApiException;
import eu.mosaic.cloud.driver.DriverConnection;
import eu.mosaic.cloud.driver.ObjectInfo;
import eu.mosaic.cloud.driver.Repository;
public class AmazonConnection extends AmazonS3Client implements eu.mosaic.cloud.driver.DriverConnection {
  private static final String OPERATION_NOT_POSSIBLE =  "Write Permission missing";

  private static final String OBJECT_ALREADY_EXISTS =  "The specified object already exists.";

  /**
   * Constructs an AmazonConenction object based on the AWSCredentials
   * parameter
   *
   * @param awsCredentials
   */
  public AmazonConnection(AWSCredentials awsCredentials) {
    super(awsCredentials);
  }

  /**
   * Stores an object inside the Amazon S3 database
   *
   * @param bucket
   *            the bucket inside which the object will be stored
   * @param key
   *            the key that identifies the object that will be stored
   * @param object
   *            the object that will be stored inside the Amazon S3 storage
   *            system
   * @return <code>true</code> if the object is successfully stored,
   *         <code>false</code> otherwise
   *
   * @throws AmazonApiException
   *             if the bucket does not exist or the Write Permission is
   *             missing
   */
  public boolean store(String bucket, String key, Object object) throws AmazonApiException {

    AmazonS3Helper helper = AmazonS3Helper.getInstance(this);

    if (!helper.bucketExists(bucket) || helper.hasWritePermission(bucket)) {
      throw new AmazonApiException(OPERATION_NOT_POSSIBLE);
    }

    boolean bResult = false;

    try {
      putObject(bucket, key, (File) object);
      bResult = true;
    } catch (AmazonServiceException ase) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ase.getMessage());
    } catch (AmazonClientException ace) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ace.getMessage());
    } catch (Exception e) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(e.getMessage());
    }

    return bResult;

  }

  /**
   * Deletes an object inside the Amazon S3 database
   *
   * @param bucket
   *            the bucket inside which the object will be stored
   * @param key
   *            the key that identifies the object that will be stored
   * @return <code>true</code> if the object is successfully deleted,
   *         <code>false</code> otherwise
   * @throws AmazonApiException
   */
  public boolean delete(String bucket, String key) throws AmazonApiException {

    AmazonS3Helper helper = AmazonS3Helper.getInstance(this);

    if (!helper.bucketExists(bucket) || helper.hasWritePermission(bucket)) {
      throw new AmazonApiException(OPERATION_NOT_POSSIBLE);
    }

    boolean bResult = false;

    try {
      deleteObject(bucket, key);
      bResult = true;
    } catch (AmazonServiceException ase) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ase.getMessage());
    } catch (AmazonClientException ace) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ace.getMessage());
    } catch (Exception e) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(e.getMessage());
    }

    return bResult;
  }

  /**
   * This method creates a repository for objects in the storage system. In
   * this case it creates a bucked with the name specified as parameter.
   *
   * @param repositoryName
   *            the name of the repository wanted to be created
   *
   * @return <code>true</code> if the repository is successfully created,
   *         <code>false</code> otherwise
   *
   * @throws AmazonApiException
   *             if an object with the specified name already exists
   */
  public boolean createRepository(String repositoryName) throws AmazonApiException {

    AmazonS3Helper helper = AmazonS3Helper.getInstance(this);

    if (helper.bucketExists(repositoryName)) {
      throw new AmazonApiException(OBJECT_ALREADY_EXISTS);
    }

    boolean bResult = false;

    try {
      createBucket(repositoryName);
      bResult = true;
    } catch (AmazonServiceException ase) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ase.getMessage());
    } catch (AmazonClientException ace) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ace.getMessage());
    } catch (Exception e) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(e.getMessage());
    }

    return bResult;
  }

  /**
   * Deletes a repository from the storage system. The name of the repository
   * intended to be deleted is passed as parameter in the method
   *
   * @param repositoryName
   *            the name of the repository intended to be deleted
   * @return <code>true</code> in case of success, <code>false</code>
   *         otherwise
   * @throws AmazonApiException
   */
  public boolean deleteRepository(String repositoryName) throws AmazonApiException {
    boolean bResult = false;

    // TODO: add the security checks here

    try {
      deleteBucket(repositoryName);
      bResult = true;
    } catch (AmazonServiceException ase) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ase.getMessage());
    } catch (AmazonClientException ace) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(ace.getMessage());
    } catch (Exception e) {
      // rethrow the caught exception, this time wrapped as
      // AmazonApiException
      throw new AmazonApiException(e.getMessage());
    }
    return bResult;
  }

  /**
   * Copies a specified object from a Amazon S3 bucket to another Amazon S3
   * bucket
   *
   * @param sourceBucket
   *            the name of the source bucket
   * @param sourceKey
   *            the key from the source bucket under which the object is
   *            identified
   * @param destinationBucket
   *            the destination bucket
   * @param destinationKey
   *            the key under the destination bucket under which the object
   *            will be stored
   * @return <code>true</code> if the copy operation succeeded,
   *         <code>false</code> otherwise
   * @throws AmazonApiException
   */
  public boolean copy(String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) throws AmazonApiException {
    boolean bResult = false;
    try {
      copyObject(sourceBucket, sourceKey, destinationBucket, destinationKey);
      bResult = true;
    } catch (Exception e) {
      e.printStackTrace();
      return bResult;
    }

    return bResult;
  }

  /**
   * Moves a specified object from a Amazon S3 bucket to another Amazon S3
   * bucket
   *
   * @param sourceBucket
   *            the name of the source bucket
   * @param sourceKey
   *            the key from the source bucket under which the object is
   *            identified
   * @param destinationBucket
   *            the destination bucket
   * @param destinationKey
   *            the key under the destination bucket under which the object
   *            will be stored
   * @return <code>true</code> if the copy operation succeeded,
   *         <code>false</code> otherwise
   * @throws AmazonApiException
   */
  public boolean move(String sourceBucket, String sourceKey, String destinationBucket, String destinationKey) throws AmazonApiException {
    boolean bResult = false;
    if (copy(sourceBucket, sourceKey, destinationBucket, destinationKey)) {
      deleteObject(sourceBucket, sourceKey);
      bResult = true;
    }
    return bResult;
  }

  /**
   * Lists all the repositories that the authenticated user of the request
   * owns
   *
   * @return the list with all the Repository objects the authenticated user
   *         owns
   */
  public List<eu.mosaic.cloud.driver.Repository> getRepositories() throws eu.mosaic.cloud.driver.DriverApiException {
    List<Repository> repos = new ArrayList<Repository>();
    List<Bucket> buckets = listBuckets();

    for (Bucket crtBucket : buckets) {
      Repository newRep = new AmazonRepository();
      newRep.setCreationDate(crtBucket.getCreationDate());
      newRep.setName(crtBucket.getName());
      repos.add(newRep);
    }

    return (List<Repository>) repos;
  }

  /**
   * List all the objects owned by the authenticated user and located in a
   * specified bucket
   *
   * @param repositoryName
   *            the name of the bucket in which the objects reside
   * @return the list of the objects owned by the authenticated user and
   *         located in the specified bucket
   */
  public List<eu.mosaic.cloud.driver.ObjectInfo> getObjects(String repositoryName) throws eu.mosaic.cloud.driver.DriverApiException {
    List<ObjectInfo> objects = new ArrayList<ObjectInfo>();

    ObjectListing amazonObjects = listObjects(repositoryName);

    List<S3ObjectSummary> objSummaries = amazonObjects.getObjectSummaries();

    for (S3ObjectSummary crtObject : objSummaries) {
      String fileName = crtObject.getKey();
      long fileSize = crtObject.getSize();
      Date lastModifDate = crtObject.getLastModified();

      ObjectInfo crtDsObj = new ObjectInfo(fileName, fileSize, lastModifDate);
      objects.add(crtDsObj);
    }

    return objects;
  }

  public InputStream retrieve(String repository, String key) throws eu.mosaic.cloud.driver.DriverApiException {
    // TODO Auto-generated method stub
    S3Object s3obj = getObject(repository, key);
    if (s3obj == null) {
      return null;
    }
    return s3obj.getObjectContent();
  }

}
TOP

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

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.