List of usage examples for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport
public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException
From source file:com.google.appengine.tools.pipeline.impl.backend.CloudTaskQueue.java
License:Apache License
public CloudTaskQueue() { final String rootUrl = System.getProperty(CLOUDTASKS_API_ROOT_URL_PROPERTY); final String apiKey = System.getProperty(CLOUDTASKS_API_KEY_PROPERTY); this.apiKey = apiKey; try {// w w w. ja va 2 s .co m HttpRequestInitializer credential; try { credential = GoogleCredential.getApplicationDefault().createScoped(CloudTasksScopes.all()); } catch (IOException e) { logger.info( "Fallback to HttpRequestInitializer, cause cannot create Application default credentials: " + e.getMessage()); credential = new HttpRequestInitializer() { @Override public void initialize(final HttpRequest request) throws IOException { } }; } CloudTasks.Builder builder = new CloudTasks.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential).setApplicationName("appengine-pipeline"); if (rootUrl != null) { builder.setRootUrl(rootUrl); } cloudTask = builder.build(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
From source file:com.google.cloud.bigtable.config.CredentialFactory.java
License:Open Source License
/** * Returns shared httpTransport instance; initializes httpTransport if it hasn't already been * initialized./*w w w . j a v a2s . c o m*/ */ public static synchronized HttpTransport getHttpTransport() throws IOException, GeneralSecurityException { if (httpTransport == null) { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); } return httpTransport; }
From source file:com.google.cloud.bigtable.hbase.CredentialFactory.java
License:Open Source License
/** * Returns shared httpTransport instance; initializes httpTransport if it hasn't already been * initialized.//from w w w.j av a2 s .co m */ private static synchronized HttpTransport getHttpTransport() throws IOException, GeneralSecurityException { if (httpTransport == null) { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); } return httpTransport; }
From source file:com.google.cloud.dataflow.examples.NewsInjector.java
License:Apache License
/** * Creates a Cloud Pub/Sub client./* ww w .ja v a 2s . c o m*/ */ public Pubsub createPubsubClient() throws IOException, GeneralSecurityException { HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault(); HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); return new Pubsub.Builder(transport, JSON_FACTORY, initializer).build(); }
From source file:com.google.cloud.dataflow.samples.daily_precipitation_sample.ReadDataWithFileName.java
License:Apache License
/** * Get all precipitation files in a specified bucket within the specified date range. * @return A set of fully qualified file names for all precipitation data files. * All names are in the format "gs://sub/dir/.../precip_YYYYMMDD.json" *///from w ww . j a va 2 s . c om private static HashSet<String> getPrecipitationFiles(String project, String bucket, String startDate, String endDate) { HashSet<String> files = new HashSet<>(); // Prevents duplicate data files for the same date. HashSet<String> visitedFiles = new HashSet<>(); try { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault(); Collection<String> bigqueryScopes = BigqueryScopes.all(); if (credential.createScopedRequired()) { credential = credential.createScoped(bigqueryScopes); } Storage client = new Storage.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(project).build(); // Get the contents of the bucket. Storage.Objects.List listObjects = client.objects().list(bucket); com.google.api.services.storage.model.Objects objects; do { objects = listObjects.execute(); List<StorageObject> items = objects.getItems(); if (items == null) { break; } for (StorageObject object : items) { String fileName = PathUtil.basename(object.getName()); if (matchesFileTemplate(fileName) && isInRange(fileName, startDate, endDate) && !visitedFiles.contains(fileName)) { visitedFiles.add(fileName); files.add("gs://" + PathUtil.join(bucket, object.getName())); } } listObjects.setPageToken(objects.getNextPageToken()); } while (objects.getNextPageToken() != null); } catch (IOException | GeneralSecurityException e) { throw new RuntimeException("Exception while constructing ReadDataWithFileName reader.", e); } return files; }
From source file:com.google.cloud.dataflow.sdk.util.Credentials.java
License:Apache License
/** * Loads OAuth2 credential from client secrets, which may require an * interactive authorization prompt./* ww w.java 2 s. com*/ */ private static Credential getCredentialFromClientSecrets(GcpOptions options, Collection<String> scopes) throws IOException, GeneralSecurityException { String clientSecretsFile = options.getSecretsFile(); Preconditions.checkArgument(clientSecretsFile != null); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleClientSecrets clientSecrets; try { clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(clientSecretsFile)); } catch (IOException e) { throw new RuntimeException("Could not read the client secrets from file: " + clientSecretsFile, e); } FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory( new java.io.File(options.getCredentialDir())); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory) .setTokenServerUrl(new GenericUrl(options.getTokenServerUrl())) .setAuthorizationServerEncodedUrl(options.getAuthorizationServerEncodedUrl()).build(); // The credentialId identifies the credential if we're using a persistent // credential store. Credential credential = new AuthorizationCodeInstalledApp(flow, new PromptReceiver()) .authorize(options.getCredentialId()); LOG.info("Got credential from client secret"); return credential; }
From source file:com.google.cloud.genomics.cba.StorageFactory.java
License:Apache License
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> scopes = StorageScopes.all(); credential = credential.createScoped(scopes); }/*from w ww . j a va 2 s . co m*/ return new Storage.Builder(transport, jsonFactory, credential).setApplicationName("AnnotationHive").build(); }
From source file:com.google.cloud.genomics.gatk.common.GenomicsApiDataSource.java
License:Open Source License
private Genomics initGenomicsApi() throws GeneralSecurityException, IOException { LOG.info("Initializing Genomics API for " + rootUrl); if (!clientSecretsFilename.isEmpty()) { File clientSecrets = new File(clientSecretsFilename); if (!clientSecrets.exists()) { throw new IOException("Client secrets file " + clientSecretsFilename + " does not exist." + " Visit https://cloud.google.com/genomics/install-genomics-tools#authenticate to learn how" + " to install a client_secrets.json file. If you have installed a client_secrets.json" + " in a specific location, use --client_secrets_filename <path>/client_secrets.json."); }//from ww w.j a va 2 s.co m LOG.info("Using client secrets file " + clientSecretsFilename); VerificationCodeReceiver receiver = noLocalServer ? new GooglePromptReceiver() : new LocalServerReceiver(); GenomicsFactory genomicsFactory = GenomicsFactory.builder("genomics_java_client").setRootUrl(rootUrl) .setServicePath("/").setVerificationCodeReceiver(Suppliers.ofInstance(receiver)).build(); return genomicsFactory.fromClientSecretsFile(clientSecrets); } else { final Genomics.Builder builder = new Genomics.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() { @Override public void initialize(HttpRequest httpRequest) throws IOException { httpRequest.setReadTimeout(20000); httpRequest.setConnectTimeout(20000); } }).setApplicationName("genomics_java_client").setRootUrl(rootUrl).setServicePath("/"); return builder.build(); } }
From source file:com.google.cloud.genomics.gatk.common.rest.GenomicsDataSource.java
License:Open Source License
private Genomics initGenomicsApi() throws GeneralSecurityException, IOException { LOG.info("Initializing Genomics API for " + rootUrl); checkParamsForAuth(AUTH_REQUIREMENTS.NONE); if (!clientSecretsFilename.isEmpty() || !apiKey.isEmpty()) { if (!clientSecretsFilename.isEmpty()) { LOG.info("Using client secrets file " + clientSecretsFilename); return getFactory().fromClientSecretsFile(new File(clientSecretsFilename)); } else {/*from w w w . ja v a2s. co m*/ LOG.info("Using API key"); return getFactory().fromApiKey(apiKey); } } else { final Genomics.Builder builder = new Genomics.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), new HttpRequestInitializer() { @Override public void initialize(HttpRequest httpRequest) throws IOException { httpRequest.setReadTimeout(20000); httpRequest.setConnectTimeout(20000); } }).setApplicationName("genomics_java_client").setRootUrl(rootUrl).setServicePath("/"); return builder.build(); } }
From source file:com.google.cloud.genomics.utils.CredentialFactory.java
License:Apache License
/** * Creates an OAuth2 credential from client secrets, which may require an interactive authorization prompt. * * Use this method when the Application Default Credential is not sufficient. * * @param clientSecretsFile The {@code client_secrets.json} file path. * @param credentialId The credentialId for use in identifying the credential in the persistent credential store. * @return The user credential//w ww . j av a 2s . c o m */ public static Credential getCredentialFromClientSecrets(String clientSecretsFile, String credentialId) { Preconditions.checkArgument(clientSecretsFile != null); Preconditions.checkArgument(credentialId != null); HttpTransport httpTransport; try { httpTransport = GoogleNetHttpTransport.newTrustedTransport(); } catch (IOException | GeneralSecurityException e) { throw new RuntimeException("Could not create HTTPS transport for use in credential creation", e); } JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleClientSecrets clientSecrets; try { clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(clientSecretsFile)); } catch (IOException e) { throw new RuntimeException("Could not read the client secrets from file: " + clientSecretsFile, e); } FileDataStoreFactory dataStoreFactory; try { dataStoreFactory = new FileDataStoreFactory(CREDENTIAL_STORE); } catch (IOException e) { throw new RuntimeException("Could not create persisten credential store " + CREDENTIAL_STORE, e); } GoogleAuthorizationCodeFlow flow; try { flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, SCOPES) .setDataStoreFactory(dataStoreFactory).build(); } catch (IOException e) { throw new RuntimeException("Could not build credential authorization flow", e); } // The credentialId identifies the credential in the persistent credential store. Credential credential; try { credential = new AuthorizationCodeInstalledApp(flow, new PromptReceiver()).authorize(credentialId); } catch (IOException e) { throw new RuntimeException("Could not perform credential authorization flow", e); } return credential; }