Example usage for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport

List of usage examples for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.javanet GoogleNetHttpTransport newTrustedTransport.

Prototype

public static NetHttpTransport newTrustedTransport() throws GeneralSecurityException, IOException 

Source Link

Document

Returns a new instance of NetHttpTransport that uses GoogleUtils#getCertificateTrustStore() for the trusted certificates using com.google.api.client.http.javanet.NetHttpTransport.Builder#trustCertificates(KeyStore) .

Usage

From source file:com.google.pubsub.flic.controllers.GCEController.java

License:Apache License

/** Returns a GCEController using default application credentials. */
public static GCEController newGCEController(String projectName, Map<String, Map<ClientParams, Integer>> types,
        ScheduledExecutorService executor) {
    try {/*from  w  w w . j a  v  a  2 s.c  om*/
        HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
        if (credential.createScopedRequired()) {
            credential = credential
                    .createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"));
        }
        return new GCEController(projectName, types, executor,
                new Storage.Builder(transport, jsonFactory, credential)
                        .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(),
                new Compute.Builder(transport, jsonFactory, credential)
                        .setApplicationName("Cloud Pub/Sub Loadtest Framework").build(),
                new Pubsub.Builder(transport, jsonFactory, credential)
                        .setApplicationName("Cloud Pub/Sub Loadtest Framework").build());
    } catch (Throwable t) {
        return null;
    }
}

From source file:com.google.pubsub.flic.Driver.java

License:Apache License

public void run(BiFunction<String, Map<ClientParams, Integer>, Controller> controllerFunction) {
    try {/*w ww  .j  av a 2  s  .c o  m*/
        Preconditions.checkArgument(
                cpsPublisherCount > 0 || cpsSubscriberCount > 0 || kafkaPublisherCount > 0
                        || kafkaSubscriberCount > 0,
                "You must set at least one type of client greater than 0.");
        Preconditions.checkArgument(broker != null || (kafkaPublisherCount == 0 && kafkaSubscriberCount == 0),
                "If using Kafka you must provide the network address of your broker using the"
                        + "--broker flag.");

        if (maxPublishLatencyTest) {
            Preconditions.checkArgument(kafkaPublisherCount > 0 ^ cpsPublisherCount > 0,
                    "If max_publish_latency is specified, there can only be one type of publisher.");
        }
        Controller.resourceDirectory = resourceDirectory;
        Map<ClientParams, Integer> clientParamsMap = new HashMap<>();
        clientParamsMap.putAll(ImmutableMap.of(new ClientParams(ClientType.CPS_GCLOUD_JAVA_PUBLISHER, null),
                cpsPublisherCount, new ClientParams(ClientType.KAFKA_PUBLISHER, null), kafkaPublisherCount,
                new ClientParams(ClientType.KAFKA_SUBSCRIBER, null), kafkaSubscriberCount));
        // Each type of client will have its own topic, so each topic will get
        // cpsSubscriberCount subscribers cumulatively among each of the subscriptions.
        for (int i = 0; i < cpsSubscriptionFanout; ++i) {
            clientParamsMap.put(
                    new ClientParams(ClientType.CPS_GCLOUD_JAVA_SUBSCRIBER, "gcloud-subscription" + i),
                    cpsSubscriberCount / cpsSubscriptionFanout);
        }
        Client.messageSize = messageSize;
        Client.requestRate = 1;
        Client.loadtestLengthSeconds = loadtestLengthSeconds;
        Client.publishBatchSize = publishBatchSize;
        Client.maxMessagesPerPull = cpsMaxMessagesPerPull;
        Client.pollLength = kafkaPollLength;
        Client.broker = broker;
        Client.requestRate = requestRate;
        Client.maxOutstandingRequests = maxOutstandingRequests;
        Client.numberOfMessages = numberOfMessages;
        Controller controller = controllerFunction.apply(project, clientParamsMap);

        // Start a thread to poll and output results.
        ScheduledExecutorService pollingExecutor = Executors.newSingleThreadScheduledExecutor();
        pollingExecutor.scheduleWithFixedDelay(() -> {
            synchronized (pollingExecutor) {
                log.info("===============================================");
                printStats(controller.getStatsForAllClientTypes());
                log.info("===============================================");
            }
        }, 5, 5, TimeUnit.SECONDS);
        Map<ClientType, Controller.LoadtestStats> statsMap;
        AtomicDouble publishLatency = new AtomicDouble(0);

        final HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
        final JsonFactory jsonFactory = new JacksonFactory();
        final GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory)
                .createScoped(Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"));
        final Monitoring monitoring = new Monitoring.Builder(transport, jsonFactory, credential)
                .setApplicationName("Cloud Pub/Sub Loadtest Framework").build();
        final SimpleDateFormat dateFormatter;
        dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
        int highestRequestRate = 0;
        long backlogSize = 0;
        do {
            Client.startTime = Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000 + 90)
                    .build();
            Client.burnInTimeMillis = (Client.startTime.getSeconds() + burnInDurationSeconds) * 1000;
            Date startDate = new Date();
            startDate.setTime(Client.startTime.getSeconds() * 1000);
            MessageTracker messageTracker = new MessageTracker(numberOfMessages, cpsPublisherCount);
            controller.startClients(messageTracker);
            if (maxSubscriberThroughputTest) {
                controller.waitForPublisherClients();
                ListTimeSeriesResponse response = monitoring.projects().timeSeries().list("projects/" + project)
                        .setFilter(
                                "metric.type = \"pubsub.googleapis.com/subscription/num_undelivered_messages\"")
                        .setIntervalStartTime(dateFormatter.format(startDate))
                        .setIntervalEndTime(dateFormatter.format(new Date())).execute();
                // Get most recent point.
                Point latestBacklogSize = null;
                for (TimeSeries timeSeries : response.getTimeSeries()) {
                    for (Point point : timeSeries.getPoints()) {
                        if (latestBacklogSize == null || dateFormatter.parse(point.getInterval().getStartTime())
                                .after(dateFormatter.parse(latestBacklogSize.getInterval().getStartTime()))) {
                            latestBacklogSize = point;
                        }
                    }
                }
                if (latestBacklogSize != null) {
                    backlogSize = latestBacklogSize.getValue().getInt64Value();
                }
                if (backlogSize > maxSubscriberThroughputTestBacklog) {
                    log.info("We accumulated a backlog during this test, refer to the last run "
                            + "for the maximum throughput attained before accumulating backlog.");
                }
            }
            // Wait for the load test to finish.
            controller.waitForClients();
            statsMap = controller.getStatsForAllClientTypes();
            if (maxPublishLatencyTest) {
                statsMap.forEach((type, stats) -> {
                    if (type.isPublisher()) {
                        publishLatency.set(LatencyDistribution.getNthPercentileUpperBound(stats.bucketValues,
                                maxPublishLatencyPercentile));
                    }
                });
            }
            if (publishLatency.get() < maxPublishLatencyMillis) {
                highestRequestRate = Client.requestRate;
            }
            Client.requestRate = (int) (Client.requestRate * 1.1);
            printStats(statsMap);
            List<MessageIdentifier> missing = messageTracker.getMissing();
            if (!missing.isEmpty()) {
                log.error("Some published messages were not received!");
                for (MessageIdentifier identifier : missing) {
                    log.error(String.format("%d:%d", identifier.getPublisherClientId(),
                            identifier.getSequenceNumber()));
                }
            }
            if (spreadsheetId.length() > 0) {
                // Output results to common Google sheet
                SheetsService service = new SheetsService(dataStoreDirectory, controller.getTypes());
                service.sendToSheets(spreadsheetId, statsMap);
            }
        } while ((maxPublishLatencyTest && publishLatency.get() < maxPublishLatencyMillis)
                || (maxSubscriberThroughputTest && backlogSize < maxSubscriberThroughputTestBacklog));
        synchronized (pollingExecutor) {
            pollingExecutor.shutdownNow();
        }
        if (maxPublishLatencyTest) {
            // This calculates the request rate of the last successful run.
            log.info("Maximum Request Rate: " + highestRequestRate);
        }
        controller.shutdown(null);
        System.exit(0);
    } catch (Throwable t) {
        log.error("An error occurred...", t);
        System.exit(1);
    }
}

From source file:com.google.pubsub.flic.output.SheetsService.java

License:Apache License

private Sheets authorize() throws Exception {
    InputStream in = new FileInputStream(new File(System.getenv("GOOGLE_OATH2_CREDENTIALS")));
    JsonFactory factory = new JacksonFactory();
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(factory, new InputStreamReader(in));
    HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
    FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(dataStoreDirectory));
    List<String> scopes = Collections.singletonList(SheetsScopes.SPREADSHEETS);
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, factory,
            clientSecrets, scopes).setAccessType("offline").setDataStoreFactory(dataStoreFactory).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    return new Sheets.Builder(transport, factory, credential).setApplicationName(APPLICATION_NAME).build();
}

From source file:com.google.sites.liberation.export.Main.java

License:Apache License

private void doMain(String[] args) {
    CmdLineParser parser = new CmdLineParser(this);
    Injector injector = Guice.createInjector(new SiteExporterModule());
    SiteExporter siteExporter = injector.getInstance(SiteExporter.class);
    try {/*from   w  ww.ja v  a  2  s  .com*/
        parser.parseArgument(args);
        if (webspace == null) {
            throw new CmdLineException("Webspace of site not specified!");
        }
        SitesService sitesService = new SitesService("google-sites-liberation");
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        Credential credential = authorize();
        oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

        sitesService.setOAuth2Credentials(credential);

        siteExporter.exportSite(host, domain, webspace, exportRevisions, sitesService, directory,
                new StdOutProgressListener());
    } catch (CmdLineException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
        parser.printUsage(System.err);
        return;
    } catch (GeneralSecurityException e) {
        LOGGER.log(Level.SEVERE, "Error while setting up the security");
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Error handling resources files");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error while getting the AccesToken");
    }
}

From source file:com.google.sites.liberation.imprt.Main.java

License:Apache License

private void doMain(String[] args) {
    CmdLineParser parser = new CmdLineParser(this);
    Injector injector = Guice.createInjector(new SiteImporterModule());
    SiteImporter siteImporter = injector.getInstance(SiteImporter.class);
    try {//from   w  w  w  .ja v  a2s .co  m
        parser.parseArgument(args);
        if (webspace == null) {
            throw new CmdLineException("Webspace of site not specified!");
        }
        SitesService sitesService = new SitesService("google-sites-liberation");

        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        Credential credential = authorize();
        oauth2 = new Oauth2.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

        sitesService.setOAuth2Credentials(credential);

        siteImporter.importSite(host, domain, webspace, importRevisions, sitesService, directory,
                new StdOutProgressListener());
    } catch (CmdLineException e) {
        LOGGER.log(Level.SEVERE, e.getMessage());
        parser.printUsage(System.err);
        return;
    } catch (GeneralSecurityException e) {
        LOGGER.log(Level.SEVERE, "Error while setting up the security");
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Error handling resources files");
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error while getting the AccesToken");
    }
}

From source file:com.infield.googlesearch.GoogleSearchService.java

License:Apache License

public ResultList getResults(String q, String currentTab, long num, long pagesToShow) {

    this.num = num;
    this.pagesToShow = pagesToShow;

    ResultList resultList = new ResultList().setResultItems(new LinkedList<ResultItem>());

    this.pagesLeft = (this.pagesToShow - (pagesToShow % 2)) / 2;
    this.pagesRight = this.pagesToShow - this.pagesLeft;

    // currentPage starts from 1
    Long currentPage = Long.valueOf(currentTab);

    Customsearch.Cse.List list;/* w ww.  ja  v a  2 s .  c o  m*/
    Search results = null;

    try {
        customsearch = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY,
                null).setApplicationName(applicationname)
                        .setGoogleClientRequestInitializer(new CustomsearchRequestInitializer(apikey)).build();

        list = customsearch.cse().list(q).setNum(this.num).setStart((this.num * (currentPage - 1)) + 1)
                .setCx(cx);

        results = list.execute();

    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (GeneralSecurityException e) {
        log.error(e.getMessage(), e);
    } finally {

        SearchInformation searchInformation = (results != null) ? results.getSearchInformation() : null;

        this.resultItems = new LinkedList<ResultItem>();

        if (searchInformation != null) {
            Long totalResults = searchInformation.getTotalResults();

            Long totalPages = (totalResults - (totalResults % this.num)) / this.num + 1;

            this.pagesToShow = (totalPages > this.pagesToShow) ? this.pagesToShow : totalPages;

            resultList.setTotalPages(totalPages).setPagesToShow(this.pagesToShow)
                    .setTotalResults(searchInformation.getTotalResults())
                    .setFormattedSearchTime(searchInformation.getFormattedSearchTime())
                    .setFormattedTotalResults(searchInformation.getFormattedTotalResults())
                    .setSearchTime(searchInformation.getSearchTime()).setStartPage(getStartPage(currentPage))
                    .setEndPage(getEndPage(currentPage)).setCurrentTab(currentPage);

            //TODO: Labels and Images 
            List<Result> searchResults = results.getItems() != null ? results.getItems()
                    : new LinkedList<Result>();

            for (Result searchResult : searchResults) {
                ResultItem resultItem = new ResultItem();
                resultItem.setCacheId(searchResult.getCacheId());
                resultItem.setDisplayLink(searchResult.getDisplayLink());
                resultItem.setFileFormat(searchResult.getFileFormat());
                resultItem.setFormattedUrl(searchResult.getFormattedUrl());
                resultItem.setHtmlFormattedUrl(searchResult.getHtmlFormattedUrl());
                resultItem.setHtmlSnippet(searchResult.getHtmlSnippet());
                resultItem.setHtmlTitle(searchResult.getHtmlTitle());
                resultItem.setKind(searchResult.getKind());
                //resultItem.setLabels(searchResult.getLabels());
                resultItem.setLink(searchResult.getLink());
                resultItem.setMime(searchResult.getMime());
                resultItem.setSnippet(searchResult.getSnippet());
                resultItem.setTitle(searchResult.getTitle());
                resultItems.add(resultItem);
            }

            resultList.setResultItems(resultItems);

        }
    }

    return resultList;
}

From source file:com.jayway.maven.plugins.android.common.AndroidPublisherHelper.java

License:Open Source License

private static void newTrustedTransport() throws GeneralSecurityException, IOException {
    if (null == httpTransport) {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    }//from  www.jav a 2 s. com
}

From source file:com.jrgoogledata.JrSession.java

private HttpTransport getTransport() throws GeneralSecurityException, IOException {
    return GoogleNetHttpTransport.newTrustedTransport();
}

From source file:com.kenshoo.integrations.plugins.connectors.GCSConnector.java

License:Apache License

public Storage connectGCS(Configuration conf) {
    HttpTransport httpTransport;/*from w  w  w  .j  ava 2s  .co  m*/
    JsonFactory jsonFactory;

    jsonFactory = JacksonFactory.getDefaultInstance();
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    GoogleClientSecrets clientSecrets = buildGoogleClientSecrets(conf);

    TokenResponse tokenResponse = buildTokenResponse(conf);

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory).setClientSecrets(clientSecrets).build()
            .setFromTokenResponse(tokenResponse);

    return new Storage.Builder(httpTransport, jsonFactory, null).setHttpRequestInitializer(credential).build();
}

From source file:com.liberologico.cloudesire.gce.ComputeSample.java

License:Apache License

public static void main(String[] args) {
    try {//w  w w  . jav a2  s.  c  om
        // initialize the transport
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        // authorization
        Credential credential = authorize();

        // set up global Compute instance
        client = new Compute.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

        System.out.println("Success! Now add code here.");

        // List<Instance> items = client.instances().list(PROJECT_NAME,
        // ZONE).execute().getItems();
        // for (Instance instance : items)
        // {
        // System.out.println(instance.toPrettyString());
        // }
        // RegionList regions =
        // client.regions().list(PROJECT_NAME).execute();
        // System.out.println(regions.toPrettyString());

        // AddressList addressList = client.addresses()
        // .list(PROJECT_NAME, Regions.EUROPE_WEST).execute();
        // System.out.println(addressList.toPrettyString());

        // AddressAggregatedList aggregatedList = client.addresses()
        // .aggregatedList(PROJECT_NAME).execute();
        // System.out.println(aggregatedList.toPrettyString());

        // Instance instance = client.instances()
        // .get(PROJECT_NAME, ZONE, "test009").execute();
        // System.out.println(instance.toPrettyString());

        // Region regionUS = client.regions()
        // .get(PROJECT_NAME, Regions.US_CENTRAL).execute();
        // System.out.println(regionUS.toPrettyString());

        // Region regionEU = client.regions()
        // .get(PROJECT_NAME, Regions.EUROPE_WEST).execute();
        // System.out.println(regionEU.toPrettyString());
        //
        // Zone zoneEU1 = client.zones().get(PROJECT_NAME, ZONE).execute();
        // System.out.println(zoneEU1.toPrettyString());

        // createVM("test009", 10L);
        // createDisk("dtest07", 10L);
        // attachDisk("test07", "dtest07");
        // deleteVM("test009");
        // createNetwork("test01");
        // deleteNetwork("test009-network");
        // getValidImages(PROJECT_DEBIAN, "backports-debian-7-wheezy");
        // getValidMachineTypes(PROJECT_NAME, ZONE);

        // Instance instance = client.instances()
        // .get(PROJECT_NAME, ZONE, "test007").execute();
        // System.out.println(instance.toPrettyString());
        // List<String> tags = instance.getTags().getItems();
        // tags.add("pippo-geronimo-pasticcio");
        // setInstanceTags("test007", tags);
        // Network network = client.networks()
        // .get(PROJECT_NAME, "test01-network").execute();
        // System.out.println(network.toPrettyString());
        // Firewall firewall = client.firewalls()
        // .get(PROJECT_NAME, "test01-firewall").execute();
        // System.out.println(firewall.toPrettyString());
        // DiskList diskList = client.disks().list(PROJECT_NAME, ZONE)
        // .execute();
        // if (diskList == null)
        // System.out.println("diskList is null");
        // else if (diskList.getItems() == null)
        // System.out.println("diskList.getItems() returns null");
        // else
        // for (Disk d : diskList.getItems())
        // System.out.println(d.toPrettyString());

        // try {
        // Network network = client.networks().get(PROJECT_NAME, "pippo")
        // .execute();
        // } catch (Exception e) {
        // System.out.println(e.getMessage());
        // }
        // NetworkList networkList = client.networks().list(PROJECT_NAME)
        // .execute();
        // if (networkList == null)
        // System.out.println("networkList is null");
        // else
        // for (Network net : networkList.getItems())
        // System.out.println(net.toPrettyString());
        //
        // FirewallList firewallList = client.firewalls().list(PROJECT_NAME)
        // .execute();
        // for (Firewall fw : firewallList.getItems()) {
        // System.out.println(getResourceNameFromUrl(fw.getNetwork()));
        // }

    } catch (IOException e) {
        System.err.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}