com.google.cloud.TransportOptions Java Examples

The following examples show how to use com.google.cloud.TransportOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractGCPProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Builds the Transport Options containing the proxy configuration
 * @param context Context to get properties
 * @return Transport options object with proxy configuration
 */
protected TransportOptions getTransportOptions(ProcessContext context) {
    final ProxyConfiguration proxyConfiguration = ProxyConfiguration.getConfiguration(context, () -> {
        final String proxyHost = context.getProperty(PROXY_HOST).evaluateAttributeExpressions().getValue();
        final Integer proxyPort = context.getProperty(PROXY_PORT).evaluateAttributeExpressions().asInteger();
        if (proxyHost != null && proxyPort != null && proxyPort > 0) {
            final ProxyConfiguration componentProxyConfig = new ProxyConfiguration();
            final String proxyUser = context.getProperty(HTTP_PROXY_USERNAME).evaluateAttributeExpressions().getValue();
            final String proxyPassword = context.getProperty(HTTP_PROXY_PASSWORD).evaluateAttributeExpressions().getValue();
            componentProxyConfig.setProxyType(Proxy.Type.HTTP);
            componentProxyConfig.setProxyServerHost(proxyHost);
            componentProxyConfig.setProxyServerPort(proxyPort);
            componentProxyConfig.setProxyUserName(proxyUser);
            componentProxyConfig.setProxyUserPassword(proxyPassword);
            return componentProxyConfig;
        }
        return ProxyConfiguration.DIRECT_CONFIGURATION;
    });

    final ProxyAwareTransportFactory transportFactory = new ProxyAwareTransportFactory(proxyConfiguration);
    return HttpTransportOptions.newBuilder().setHttpTransportFactory(transportFactory).build();
}
 
Example #2
Source File: BeastFactory.java    From beast with Apache License 2.0 5 votes vote down vote up
private BigQuery getBigQueryInstance() {
    final TransportOptions transportOptions = BigQueryOptions.getDefaultHttpTransportOptions().toBuilder()
            .setConnectTimeout(Integer.parseInt(bqConfig.getBqClientConnectTimeout()))
            .setReadTimeout(Integer.parseInt(bqConfig.getBqClientReadTimeout()))
            .build();
    return BigQueryOptions.newBuilder()
            .setTransportOptions(transportOptions)
            .setCredentials(getGoogleCredentials())
            .setProjectId(bqConfig.getGCPProject())
            .build().getService();
}
 
Example #3
Source File: GoogleBinaryStore.java    From usergrid with Apache License 2.0 5 votes vote down vote up
private synchronized Storage getService() throws IOException, GeneralSecurityException {

        logger.trace("Getting Google Cloud Storage service");

        // leave this here for tests because they do things like manipulate properties dynamically to test invalid values
        this.bucketName = properties.getProperty( "usergrid.binary.bucketname" );


        if (instance == null) {

            // Google provides different authentication types which are different based on if the application is
            // running within GCE(Google Compute Engine) or GAE (Google App Engine). If Usergrid is running in
            // GCE or GAE, the SDK will automatically authenticate and get access to
            // cloud storage. Else, the full path to a credential file should be provided in the following environment variable
            //
            //     GOOGLE_APPLICATION_CREDENTIALS
            //
            // The SDK will attempt to load the credential file for a service account. See the following
            // for more info: https://developers.google.com/identity/protocols/application-default-credentials#howtheywork
            GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(StorageScopes.all());


            final TransportOptions transportOptions = HttpTransportOptions.newBuilder()
                .setConnectTimeout(30000) // in milliseconds
                .setReadTimeout(30000) // in milliseconds
                .build();

            instance = StorageOptions.newBuilder()
                .setCredentials(credentials)
                .setTransportOptions(transportOptions)
                .build()
                .getService();
        }

        return instance;
    }
 
Example #4
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
        try
        {
            
            /*
          JacksonFactory jsonFactory = new JacksonFactory(); 
              
            Authenticator.setDefault(
                new Authenticator() {
                   @Override
                   public PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(
                            "user1", "user1".toCharArray());
                   }
                }
             );                          
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
            NetHttpTransport mHttpTransport = new NetHttpTransport.Builder().setProxy(proxy).build();
            */
           

            HttpHost proxy = new HttpHost("127.0.0.1",3128);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            
            httpClient.addRequestInterceptor(new HttpRequestInterceptor(){            
                @Override
                public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException {
                    //if (request.getRequestLine().getMethod().equals("CONNECT"))                 
                    //  request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE="));
                }
            });

           mHttpTransport =  new ApacheHttpTransport(httpClient);



/*
            com.google.api.client.googleapis.auth.oauth2.GoogleCredential credential = com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(mHttpTransport,jsonFactory);
            if (credential.createScopedRequired())
                credential = credential.createScoped(Arrays.asList(StorageScopes.DEVSTORAGE_READ_ONLY));

            com.google.api.services.storage.Storage service = new com.google.api.services.storage.Storage.Builder(mHttpTransport, jsonFactory, credential)
                .setApplicationName("oauth client")   
                .build(); 
                
            com.google.api.services.storage.model.Buckets dl = service.buckets().list("mineral-minutia-820").execute();
            for (com.google.api.services.storage.model.Bucket bucket: dl.getItems()) 
                System.out.println(bucket.getName());
*/

            
           // System.setProperty("https.proxyHost", "localhost");
           // System.setProperty("https.proxyPort", "3128");
/*
            Authenticator.setDefault(
                new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            "user1", "user1".toCharArray());
                }
                }
            );
*/


            HttpTransportFactory hf = new HttpTransportFactory(){
                @Override
                public HttpTransport create() {
                    return mHttpTransport;
                }
            };            

            com.google.auth.oauth2.GoogleCredentials credential = com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(hf);
            if (credential.createScopedRequired())
                credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/devstorage.read_write"));

            TransportOptions options = HttpTransportOptions.newBuilder().setHttpTransportFactory(hf).build();            
            com.google.cloud.storage.Storage storage = com.google.cloud.storage.StorageOptions.newBuilder()
                .setCredentials(credential)
                .setProjectId("mineral-minutia-820")
                .setTransportOptions(options)
                .build().getService();
            
            System.out.println("My buckets:");        
            for (com.google.cloud.storage.Bucket bucket : storage.list().iterateAll()) 
              System.out.println(bucket);              
          
        } 
        catch (Exception ex) {
            System.out.println("Error:  " + ex);
        }
    
    }
 
Example #5
Source File: AbstractGoogleClientFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Provide a {@link TransportOptions} backed by Apache HTTP Client.
 *
 * @see ApacheHttpTransport
 * @return customized {@link TransportOptions} to use for our Google client instances
 */
TransportOptions transportOptions() {
  return HttpTransportOptions.newBuilder()
      .setHttpTransportFactory(() -> new ApacheHttpTransport(newHttpClient()))
      .build();
}