<commit_before>package com.ciandt.techgallery.persistence.dao.storage;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.StorageObject;
import com.google.appengine.api.utils.SystemProperty;

import com.ciandt.techgallery.persistence.dao.StorageDAO;
import com.ciandt.techgallery.utils.TechGalleryUtil;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Arrays;

/**
 * Class that implements the StorageDAO.
 *
 * @author <a href="mailto:<EMAIL>"> <NAME> </a>
 * @since 13/10/2015
 *
 */
public class StorageDAOImpl implements StorageDAO {

  /*
   * Constants --------------------------------------------
   */
  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
  private static final String APPLICATION_NAME = SystemProperty.applicationId.get();
  private static final String LOCATION = "US";

  /*
   * Attributes --------------------------------------------
   */
  private static Storage storageService;
  private static StorageDAOImpl instance;

  /*
   * Constructors --------------------------------------------
   */
  public StorageDAOImpl() {}

  /**
   * Singleton method for the DAO.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return StorageDAOImpl instance.
   */
  public static StorageDAOImpl getInstance() {
    if (instance == null) {
      instance = new StorageDAOImpl();
    }
    return instance;
  }

  /*
   * Methods --------------------------------------------
   */
  @Override
  public String insertImage(String name, InputStream stream)
      throws IOException, GeneralSecurityException {
    InputStreamContent contentStream = new InputStreamContent("image/png", stream);
    StorageObject objectMetadata = new StorageObject()
        // Set the destination object name
        .setName(name)
        // Set the access control list to publicly read-only
        .setAcl(Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER")));

    Storage client = getService();
    Storage.Objects.Insert insertRequest =
        client.objects().insert(getBucket().getName(), objectMetadata, contentStream);

    return insertRequest.execute().getSelfLink();
  }

  /**
   * Method to get the bucket of the application version.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return the bucket of this application version.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private Bucket getBucket() throws IOException, GeneralSecurityException {
    String applicationVersion = TechGalleryUtil.getApplicationVersion();
    Bucket createdBucket = getExistingBucket(applicationVersion);
    if (createdBucket == null) {
      return createBucket(applicationVersion);
    }
    return createdBucket;
  }

  /**
   * Method to create a new bucket on cloud storage.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @param applicationVersion to create the bucket.
   *
   * @return the bucket created.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private static Bucket createBucket(String applicationVersion)
      throws IOException, GeneralSecurityException {
    Storage client = getService();
    Bucket newBucket = new Bucket().setName(applicationVersion).setLocation(LOCATION)
        .setAcl(Arrays.asList(new BucketAccessControl().setEntity("allUsers").setRole("READER")));
    Storage.Buckets.Insert bucketToCreate = client.buckets().insert(APPLICATION_NAME, newBucket);
    return bucketToCreate.execute();
  }

  /**
   * Method to get a bucket that already exists.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @param bucketName to be founded.
   *
   * @return the bucket founded.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  public static Bucket getExistingBucket(String bucketName)
      throws IOException, GeneralSecurityException {
    Storage client = getService();
    Storage.Buckets.Get bucketRequest = client.buckets().get(bucketName);
    // Fetch the full set of the bucket's properties (e.g. include the ACLs in the response)
    bucketRequest.setProjection("full");
    return bucketRequest.execute();
  }

  /**
   * Method to create the service or get the service if is already created.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return the Storage service already created.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private static Storage getService() throws IOException, GeneralSecurityException {
    if (null == storageService) {
      GoogleCredential credential = GoogleCredential.getApplicationDefault();
      // Depending on the environment that provides the default credentials (e.g. Compute Engine,
      // App Engine), the credentials may require us to specify the scopes we need explicitly.
      // Check for this case, and inject the Cloud Storage scope if required.
      if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
      }
      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
      storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
          .setApplicationName(APPLICATION_NAME).build();
    }
    return storageService;
  }
}
<commit_msg>TECG-286 Bug fix<commit_after>package com.ciandt.techgallery.persistence.dao.storage;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.BucketAccessControl;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.StorageObject;
import com.google.appengine.api.utils.SystemProperty;

import com.ciandt.techgallery.persistence.dao.StorageDAO;
import com.ciandt.techgallery.utils.TechGalleryUtil;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.logging.Logger;

/**
 * Class that implements the StorageDAO.
 *
 * @author <a href="mailto:<EMAIL>"> <NAME> </a>
 * @since 13/10/2015
 *
 */
public class StorageDAOImpl implements StorageDAO {

  /*
   * Constants --------------------------------------------
   */
  private static final Logger logger = Logger.getLogger(StorageDAOImpl.class.getName());
  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
  private static final String APPLICATION_NAME = SystemProperty.applicationId.get();
  private static final String LOCATION = "US";

  /*
   * Attributes --------------------------------------------
   */
  private static Storage storageService;
  private static StorageDAOImpl instance;

  /*
   * Constructors --------------------------------------------
   */
  public StorageDAOImpl() {}

  /**
   * Singleton method for the DAO.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return StorageDAOImpl instance.
   */
  public static StorageDAOImpl getInstance() {
    if (instance == null) {
      instance = new StorageDAOImpl();
    }
    return instance;
  }

  /*
   * Methods --------------------------------------------
   */
  @Override
  public String insertImage(String name, InputStream stream)
      throws IOException, GeneralSecurityException {
    InputStreamContent contentStream = new InputStreamContent("image/png", stream);
    StorageObject objectMetadata = new StorageObject()
        // Set the destination object name
        .setName(name)
        // Set the access control list to publicly read-only
        .setAcl(Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER")));

    Storage client = getService();
    Storage.Objects.Insert insertRequest =
        client.objects().insert(getBucket().getName(), objectMetadata, contentStream);

    return insertRequest.execute().getSelfLink();
  }

  /**
   * Method to get the bucket of the application version.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return the bucket of this application version.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private Bucket getBucket() throws IOException, GeneralSecurityException {
    String applicationVersion = TechGalleryUtil.getApplicationVersion();
    Bucket createdBucket = getExistingBucket(applicationVersion);
    if (createdBucket == null) {
      return createBucket(applicationVersion);
    }
    return createdBucket;
  }

  /**
   * Method to create a new bucket on cloud storage.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @param applicationVersion to create the bucket.
   *
   * @return the bucket created.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private static Bucket createBucket(String applicationVersion)
      throws IOException, GeneralSecurityException {
    Storage client = getService();
    Bucket newBucket =
        new Bucket().setName("cit-tech-gallery-" + applicationVersion).setLocation(LOCATION).setAcl(
            Arrays.asList(new BucketAccessControl().setEntity("allUsers").setRole("READER")));
    Storage.Buckets.Insert bucketToCreate = client.buckets().insert(APPLICATION_NAME, newBucket);
    return bucketToCreate.execute();
  }

  /**
   * Method to get a bucket that already exists.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @param bucketName to be founded.
   *
   * @return the bucket founded.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  public static Bucket getExistingBucket(String bucketName)
      throws IOException, GeneralSecurityException {
    Storage client = getService();
    Storage.Buckets.Get bucketRequest = client.buckets().get("cit-tech-gallery-" + bucketName);
    // Fetch the full set of the bucket's properties (e.g. include the ACLs in the response)
    bucketRequest.setProjection("full");
    try {
      return bucketRequest.execute();
    } catch (Exception e) {
      if (e.getCause() == null) {

      }
    }
    return null;
  }

  /**
   * Method to create the service or get the service if is already created.
   *
   * @author <a href="mailto:<EMAIL>"> <NAME> </a>
   * @since 13/10/2015
   *
   * @return the Storage service already created.
   *
   * @throws IOException in case a IO problem.
   * @throws GeneralSecurityException in case a security problem.
   */
  private static Storage getService() throws IOException, GeneralSecurityException {
    if (null == storageService) {
      // GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
      // new InputStreamReader(StorageDAOImpl.class.getResourceAsStream("/client_secret.json")));

      HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
      GoogleCredential credential = GoogleCredential.getApplicationDefault();
      // GoogleCredential credential = new
      // GoogleCredential.Builder().setClientSecrets(clientSecrets)
      // .setTransport(httpTransport).setJsonFactory(JSON_FACTORY).build();
      // Depending on the environment that provides the default credentials (e.g. Compute Engine,
      // App Engine), the credentials may require us to specify the scopes we need explicitly.
      // Check for this case, and inject the Cloud Storage scope if required.
      if (credential.createScopedRequired()) {
        credential = credential.createScoped(StorageScopes.all());
      }
      storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential)
          .setApplicationName(APPLICATION_NAME).build();
    }
    return storageService;
  }
}

{'fraction_non_alphanumeric': 0.12980518026423826, 'fraction_numerical': 0.0076882884227812195, 'mean_word_length': 3.2736842105263158, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2500759', 'n_tokens_mistral': 3759, 'n_tokens_neox': 3629, 'n_words': 955}