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.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Retrieves device metadata from a registry. **/
public static Device getDevice(String deviceId, String projectId, String cloudRegion, String registryName)
        throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;

    String devicePath = registryPath + "/devices/" + deviceId;
    System.out.println("Retrieving device " + devicePath);
    return service.projects().locations().registries().devices().get(devicePath).execute();
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Retrieves device metadata from a registry. **/
public static List<DeviceState> getDeviceStates(String deviceId, String projectId, String cloudRegion,
        String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/" + registryName;

    String devicePath = registryPath + "/devices/" + deviceId;
    System.out.println("Retrieving device states " + devicePath);

    ListDeviceStatesResponse resp = service.projects().locations().registries().devices().states()
            .list(devicePath).execute();
    return resp.getDeviceStates();
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Retrieves registry metadata from a project. **/
public static DeviceRegistry getRegistry(String projectId, String cloudRegion, String registryName)
        throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
            + registryName;/*  w  w  w .  j a v a2  s  .c o  m*/
    return service.projects().locations().registries().get(registryPath).execute();
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** List all of the configs for the given device. */
public static void listDeviceConfigs(String deviceId, String projectId, String cloudRegion, String registryName)
        throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
            + registryName;//from w  ww  .  j a  v  a 2  s .  c  om

    final String devicePath = registryPath + "/devices/" + deviceId;
    System.out.println("Listing device configs for " + devicePath);
    List<DeviceConfig> deviceConfigs = service.projects().locations().registries().devices().configVersions()
            .list(devicePath).execute().getDeviceConfigs();

    for (DeviceConfig config : deviceConfigs) {
        System.out.println("Config version: " + config.getVersion());
        System.out.println("Contents: " + config.getBinaryData());
        System.out.println();
    }
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Lists all of the registries associated with the given project. */
public static void listRegistries(String projectId, String cloudRegion)
        throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;

    List<DeviceRegistry> registries = service.projects().locations().registries().list(projectPath).execute()
            .getDeviceRegistries();/*from  w w w . java2  s. c o  m*/

    if (registries != null) {
        System.out.println("Found " + registries.size() + " registries");
        for (DeviceRegistry r : registries) {
            System.out.println("Id: " + r.getId());
            System.out.println("Name: " + r.getName());
            if (r.getMqttConfig() != null) {
                System.out.println("Config: " + r.getMqttConfig().toPrettyString());
            }
            System.out.println();
        }
    } else {
        System.out.println("Project has no registries.");
    }
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Modify the latest cloud to device config for the given device, with the config data. */
public static void modifyCloudToDeviceConfig(String deviceId, String configData, String projectId,
        String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
            + registryName;//from   w ww  . ja  v  a2  s  .c om
    final String devicePath = registryPath + "/devices/" + deviceId;
    ModifyCloudToDeviceConfigRequest request = new ModifyCloudToDeviceConfigRequest();
    request.setVersionToUpdate(0L); // 0L indicates update all versions
    request.setBinaryData(DatatypeConverter.printBase64Binary(configData.getBytes(Charsets.UTF_8)));
    DeviceConfig config = service.projects().locations().registries().devices()
            .modifyCloudToDeviceConfig(devicePath, request).execute();

    System.out.println("Created device config: " + config.toPrettyString());
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Patch the device to add an ES256 key for authentication. */
public static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, String projectId,
        String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
            + registryName;/* w w  w  .  j  a va2  s.  co m*/

    final String devicePath = registryPath + "/devices/" + deviceId;
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("ES256_PEM");

    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);

    Device device = new Device();
    device.setCredentials(Arrays.asList(devCredential));

    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device)
            .setUpdateMask("credentials").execute();

    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}

From source file:com.example.cloud.iot.examples.DeviceRegistryExample.java

License:Apache License

/** Patch the device to add an RSA256 key for authentication. */
public static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, String projectId,
        String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
    GoogleCredential credential = GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential);
    final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory,
            init).setApplicationName(APP_NAME).build();

    final String registryPath = "projects/" + projectId + "/locations/" + cloudRegion + "/registries/"
            + registryName;//w  w w . j ava 2s  .co  m

    final String devicePath = registryPath + "/devices/" + deviceId;
    PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
    String key = Files.toString(new File(publicKeyFilePath), Charsets.UTF_8);
    publicKeyCredential.setKey(key);
    publicKeyCredential.setFormat("RSA_X509_PEM");

    DeviceCredential devCredential = new DeviceCredential();
    devCredential.setPublicKey(publicKeyCredential);

    Device device = new Device();
    device.setCredentials(Arrays.asList(devCredential));

    Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device)
            .setUpdateMask("credentials").execute();

    System.out.println("Patched device is " + patchedDevice.toPrettyString());
}

From source file:com.example.irvinmundo.weirdwords.FusionTablesSample.java

License:Apache License

public static void main(String[] args) {
    try {//w  ww .j  a v a 2s .c  om
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        Credential credential = authorize();
        // set up global FusionTables instance
        fusiontables = new Fusiontables.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();
        // run commands
        listTables();
        String tableId = createTable();
        insertData(tableId);
        showRows(tableId);
        deleteTable(tableId);
        // success!
        return;
    } catch (IOException e) {
        System.err.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}

From source file:com.example.samcarey.InsertFusionTables.java

License:Apache License

public void fusion_upload() {
    try {/*from  w ww .j  a  va  2 s  . c  om*/
        System.out.println("Fusion Table Start Uploading...");

        //facilities to interface with a website
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        // authorization
        Credential credential = authorize();

        // set up global FusionTables instance
        fusiontables = new Fusiontables.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();

        // run commands
        // listTables();
        // Change here to insert data to the existing table
        String tableId = "1gzuL5N-2-_7uVF8F_LNBIBRio6bjKUdAzfb6wLl6"; //insert to existing table
        //String tableId = createTable();     //create a table
        insertData(tableId); //insert data into table

        //showRows(tableId);                  //not quite working yet
        System.out.println("Fusion Table Done Uploading...");
        return;
    } catch (IOException e) {
        System.err.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
    System.exit(1);
}