/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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.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 java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collection;

/**
 * This class manages the details of creating a Storage service, including auth.
 */
public class StorageFactory {

  private static Storage instance = null;

  public static synchronized Storage getService() throws IOException, GeneralSecurityException {
    if (instance == null) {
      instance = buildService();
    }
    return instance;
  }

  private static Storage buildService() throws IOException, GeneralSecurityException {
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

    if (credential.createScopedRequired()) {
      Collection<String> bigqueryScopes = StorageScopes.all();
      credential = credential.createScoped(bigqueryScopes);
    }

    return new Storage.Builder(transport, jsonFactory, credential)
        .setApplicationName("GCS Samples")
        .build();
  }
}

@@ -1200,16 +1200,74 @@
 th.%0A */%0A
+// %5BSTART authentication_application_default_credentials%5D%0A
 public c
@@ -1288,17 +1288,16 @@
 ctory %7B%0A
-%0A
   privat
@@ -1835,16 +1835,297 @@
 tory);%0A%0A
+    // Depending on the environment that provides the default credentials (for%0A    // example: Compute Engine, App Engine), the credentials may require us to%0A    // specify the scopes we need explicitly.  Check for this case, and inject%0A    // the Cloud Storage scope if required.%0A
     if (
@@ -2186,25 +2186,17 @@
 String%3E 
-bigqueryS
+s
 copes = 
@@ -2263,17 +2263,9 @@
 ped(
-bigqueryS
+s
 cope
@@ -2409,8 +2409,64 @@
 ;%0A  %7D%0A%7D%0A
+// %5BEND authentication_application_default_credentials%5D%0A

Add missing Application Default Credentials region tags.