Example usage for com.amazonaws.services.iot.model DescribeEndpointRequest DescribeEndpointRequest

List of usage examples for com.amazonaws.services.iot.model DescribeEndpointRequest DescribeEndpointRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.iot.model DescribeEndpointRequest DescribeEndpointRequest.

Prototype

DescribeEndpointRequest

Source Link

Usage

From source file:com.erudika.para.iot.AWSIoTService.java

License:Apache License

@Override
public Thing createThing(Thing thing) {
    if (thing == null || StringUtils.isBlank(thing.getName()) || StringUtils.isBlank(thing.getAppid())
            || existsThing(thing)) {/*w w w .  j av a2 s.  c  o m*/
        return null;
    }
    thing.setId(Utils.getNewId());
    String id = cloudIDForThing(thing);
    String appid = thing.getAppid();

    // STEP 1: Create thing
    CreateThingResult resp1 = getClient().createThing(new CreateThingRequest().withThingName(id)
            .withAttributePayload(new AttributePayload().addAttributesEntry(Config._APPID, appid)));

    // STEP 2: Create certificate
    CreateKeysAndCertificateResult resp2 = getClient()
            .createKeysAndCertificate(new CreateKeysAndCertificateRequest().withSetAsActive(true));

    String accountId = getAccountIdFromARN(resp1.getThingArn());
    String policyString = (String) (thing.getDeviceMetadata().containsKey("policyJSON")
            ? thing.getDeviceMetadata().get("policyJSON")
            : getDefaultPolicyDocument(accountId, id));

    // STEP 3: Create policy
    getClient().createPolicy(
            new CreatePolicyRequest().withPolicyDocument(policyString).withPolicyName(id + "-Policy"));

    // STEP 4: Attach policy to certificate
    getClient().attachPrincipalPolicy(new AttachPrincipalPolicyRequest()
            .withPrincipal(resp2.getCertificateArn()).withPolicyName(id + "-Policy"));

    // STEP 5: Attach thing to certificate
    getClient().attachThingPrincipal(
            new AttachThingPrincipalRequest().withPrincipal(resp2.getCertificateArn()).withThingName(id));

    thing.getDeviceMetadata().remove("policyJSON");

    thing.setServiceBroker("AWS");
    thing.getDeviceMetadata().put("thingId", thing.getId());
    thing.getDeviceMetadata().put("thingName", id);
    thing.getDeviceMetadata().put("thingARN", resp1.getThingArn());
    thing.getDeviceMetadata().put("clientId", id);
    thing.getDeviceMetadata().put("clientCertId", resp2.getCertificateId());
    thing.getDeviceMetadata().put("clientCertARN", resp2.getCertificateArn());
    thing.getDeviceMetadata().put("clientCert", resp2.getCertificatePem());
    thing.getDeviceMetadata().put("privateKey", resp2.getKeyPair().getPrivateKey());
    thing.getDeviceMetadata().put("publicKey", resp2.getKeyPair().getPublicKey());
    thing.getDeviceMetadata().put("region", Config.AWS_REGION);
    thing.getDeviceMetadata().put("port", 8883);
    thing.getDeviceMetadata().put("host",
            getClient().describeEndpoint(new DescribeEndpointRequest()).getEndpointAddress());

    return thing;
}