use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method patchEs256ForAuth.
// [END iot_list_registries]
// [START iot_patch_es]
/**
* Patch the device to add an ES256 key for authentication.
*/
protected static void patchEs256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, 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(Collections.singletonList(devCredential));
Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method patchRsa256ForAuth.
// [END iot_patch_es]
// [START iot_patch_rsa]
/**
* Patch the device to add an RSA256 key for authentication.
*/
protected static void patchRsa256ForAuth(String deviceId, String publicKeyFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String devicePath = String.format("projects/%s/locations/%s/registries/%s/devices/%s", projectId, cloudRegion, registryName, 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(Collections.singletonList(devCredential));
Device patchedDevice = service.projects().locations().registries().devices().patch(devicePath, device).setUpdateMask("credentials").execute();
System.out.println("Patched device is " + patchedDevice.toPrettyString());
}
use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createGateway.
/**
* Create a gateway to bind devices to.
*/
protected static void createGateway(String projectId, String cloudRegion, String registryName, String gatewayId, String certificateFilePath, String algorithm) throws GeneralSecurityException, IOException {
// [START iot_create_gateway]
GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
System.out.println("Creating gateway with id: " + gatewayId);
Device device = new Device();
device.setId(gatewayId);
GatewayConfig gwConfig = new GatewayConfig();
gwConfig.setGatewayType("GATEWAY");
gwConfig.setGatewayAuthMethod("ASSOCIATION_ONLY");
String keyFormat = "RSA_X509_PEM";
if ("ES256".equals(algorithm)) {
keyFormat = "ES256_PEM";
}
PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
byte[] keyBytes = java.nio.file.Files.readAllBytes(Paths.get(certificateFilePath));
publicKeyCredential.setKey(new String(keyBytes, StandardCharsets.US_ASCII));
publicKeyCredential.setFormat(keyFormat);
DeviceCredential deviceCredential = new DeviceCredential();
deviceCredential.setPublicKey(publicKeyCredential);
device.setGatewayConfig(gwConfig);
device.setCredentials(Collections.singletonList(deviceCredential));
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created gateway: " + createdDevice.toPrettyString());
// [END iot_create_gateway]
}
use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method listGateways.
protected static void listGateways(String projectId, String cloudRegion, String registryName) throws IOException, GeneralSecurityException {
// [START iot_list_gateways]
GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
List<Device> gateways = service.projects().locations().registries().devices().list(registryPath).setGatewayListOptionsGatewayType("GATEWAY").execute().getDevices();
if (gateways != null) {
System.out.println("Found " + gateways.size() + " devices");
for (Device d : gateways) {
System.out.println("Id: " + d.getId());
if (d.getConfig() != null) {
// Note that this will show the device config in Base64 encoded format.
System.out.println("Config: " + d.getGatewayConfig().toPrettyString());
}
System.out.println();
}
} else {
System.out.println("Registry has no devices.");
}
// [END iot_list_gateways]
}
use of org.hl7.fhir.dstu3.model.Device in project java-docs-samples by GoogleCloudPlatform.
the class DeviceRegistryExample method createDeviceWithRs256.
// [END iot_create_es_device]
// [START iot_create_rsa_device]
/**
* Create a device that is authenticated using RS256.
*/
protected static void createDeviceWithRs256(String deviceId, String certificateFilePath, String projectId, String cloudRegion, String registryName) throws GeneralSecurityException, IOException {
GoogleCredentials credential = GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
final CloudIot service = new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init).setApplicationName(APP_NAME).build();
final String registryPath = String.format("projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName);
PublicKeyCredential publicKeyCredential = new PublicKeyCredential();
String key = Files.toString(new File(certificateFilePath), Charsets.UTF_8);
publicKeyCredential.setKey(key);
publicKeyCredential.setFormat("RSA_X509_PEM");
DeviceCredential devCredential = new DeviceCredential();
devCredential.setPublicKey(publicKeyCredential);
System.out.println("Creating device with id: " + deviceId);
Device device = new Device();
device.setId(deviceId);
device.setCredentials(Collections.singletonList(devCredential));
Device createdDevice = service.projects().locations().registries().devices().create(registryPath, device).execute();
System.out.println("Created device: " + createdDevice.toPrettyString());
}
Aggregations