in provisioning/provisioning-device-client/src/main/java/com/microsoft/azure/sdk/iot/provisioning/device/internal/contract/http/ContractAPIHttp.java [179:250]
public synchronized void requestNonceForTPM(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException
{
//SRS_ContractAPIHttp_25_003: [If either registrationId, sslcontext or responseCallback is null or if registrationId is empty then this method shall throw ProvisioningDeviceClientException.]
if (requestData.getRegistrationId() == null || requestData.getRegistrationId().isEmpty())
{
throw new ProvisioningDeviceClientException(new IllegalArgumentException("registration Id cannot be null or empty"));
}
if (requestData.getEndorsementKey() == null)
{
throw new ProvisioningDeviceClientException(new IllegalArgumentException("Endorsement key cannot be null"));
}
if (requestData.getStorageRootKey() == null)
{
throw new ProvisioningDeviceClientException(new IllegalArgumentException("Storage root key cannot be null"));
}
if (requestData.getSslContext() == null)
{
throw new ProvisioningDeviceClientException(new IllegalArgumentException("sslContext cannot be null"));
}
if (responseCallback == null)
{
throw new ProvisioningDeviceClientException(new IllegalArgumentException("responseCallback cannot be null"));
}
try
{
//SRS_ContractAPIHttp_25_004: [This method shall retrieve the Url by calling 'generateRegisterUrl' on an object for UrlPathBuilder.]
String url = new UrlPathBuilder(this.hostName, this.idScope, ProvisioningDeviceClientTransportProtocol.HTTPS).generateRegisterUrl(requestData.getRegistrationId());
String base64EncodedEk = new String(encodeBase64(requestData.getEndorsementKey()), StandardCharsets.UTF_8);
String base64EncodedSrk = new String(encodeBase64(requestData.getStorageRootKey()), StandardCharsets.UTF_8);
//SRS_ContractAPIHttp_25_025: [ This method shall build the required Json input using parser. ]
byte[] payload = new DeviceRegistrationParser(requestData.getRegistrationId(), requestData.getPayload(), base64EncodedEk, base64EncodedSrk).toJson().getBytes(StandardCharsets.UTF_8);
//SRS_ContractAPIHttp_25_005: [This method shall prepare the PUT request by setting following headers on a HttpRequest 1. User-Agent : User Agent String for the SDK 2. Accept : "application/json" 3. Content-Type: "application/json; charset=utf-8".]
HttpRequest httpRequest = this.prepareRequest(new URL(url), HttpMethod.PUT, payload, null);
//SRS_ContractAPIHttp_25_006: [This method shall set the SSLContext for the Http Request.]
httpRequest.setSSLContext(requestData.getSslContext());
HttpResponse httpResponse = null;
try
{
//SRS_ContractAPIHttp_25_007: [This method shall send http request and verify the status by calling 'ProvisioningDeviceClientExceptionManager.verifyHttpResponse'.]
httpResponse = httpRequest.send();
ProvisioningDeviceClientExceptionManager.verifyHttpResponse(httpResponse);
}
catch (ProvisioningDeviceHubException e)
{
//SRS_ContractAPIHttp_25_008: [If service return a status as 404 then this method shall trigger the callback to the user with the response message.]
if (httpResponse.getStatus() == ACCEPTABLE_NONCE_HTTP_STATUS)
{
String tpmRegistrationResultJson = new String(httpResponse.getErrorReason(), StandardCharsets.UTF_8);
TpmRegistrationResultParser registerResponseTPMParser = TpmRegistrationResultParser.createFromJson(tpmRegistrationResultJson);
byte[] base64DecodedAuthKey = decodeBase64(registerResponseTPMParser.getAuthenticationKey().getBytes(StandardCharsets.UTF_8));
responseCallback.run(new ResponseData(base64DecodedAuthKey, ContractState.DPS_REGISTRATION_RECEIVED, 0), dpsAuthorizationCallbackContext);
return;
}
else
{
//SRS_ContractAPIHttp_25_009: [If service return any other status other than 404 then this method shall throw ProvisioningDeviceTransportException in case of 202 or ProvisioningDeviceHubException on any other status.]
throw e;
}
}
}
catch (IOException e)
{
throw new ProvisioningDeviceTransportException(e);
}
throw new ProvisioningDeviceTransportException("Service did not return any authorization request");
}