in iothub/device/iot-device-client/src/main/java/com/microsoft/azure/sdk/iot/device/transport/https/HttpsIotHubConnection.java [76:165]
public IotHubStatusCode sendMessage(Message message) throws TransportException
{
synchronized (HTTPS_CONNECTION_LOCK)
{
// Here we check if it's a bulk message and serialize it.
HttpsMessage httpsMessage;
if (message instanceof BatchMessage)
{
try
{
List<HttpsSingleMessage> httpsMessageList = new ArrayList<>();
for (Message msg : ((BatchMessage)message).getNestedMessages())
{
httpsMessageList.add(HttpsSingleMessage.parseHttpsMessage(msg));
}
httpsMessage = new HttpsBatchMessage(httpsMessageList);
}
catch (IllegalArgumentException e)
{
throw new TransportException("Failed to create HTTPS batch message", e);
}
}
else
{
httpsMessage = HttpsSingleMessage.parseHttpsMessage(message);
}
String iotHubHostname = getHostName();
String deviceId = this.config.getDeviceId();
String moduleId = this.config.getModuleId();
IotHubEventUri iotHubEventUri = new IotHubEventUri(iotHubHostname, deviceId, moduleId);
URL eventUrl = this.buildUrlFromString(HTTPS_HEAD_TAG + iotHubEventUri.toString());
HttpsRequest request = new HttpsRequest(eventUrl, HttpsMethod.POST, httpsMessage.getBody(), this.config.getProductInfo().getUserAgentString(), config.getProxySettings());
for (MessageProperty property : httpsMessage.getProperties())
{
request.setHeaderField(property.getName(),
property.getValue());
}
if (message.getContentEncoding() != null)
{
request.setHeaderField(MessageProperty.IOTHUB_CONTENT_ENCODING, message.getContentEncoding());
}
if (message.getContentType() != null)
{
request.setHeaderField(MessageProperty.IOTHUB_CONTENT_TYPE, message.getContentType());
}
if (message.getCreationTimeUTC() != null)
{
request.setHeaderField(MessageProperty.IOTHUB_CREATION_TIME_UTC, message.getCreationTimeUTCString());
}
if (message.isSecurityMessage())
{
request.setHeaderField(MessageProperty.IOTHUB_SECURITY_INTERFACE_ID, MessageProperty.IOTHUB_SECURITY_INTERFACE_ID_VALUE);
}
Map<String, String> systemProperties = httpsMessage.getSystemProperties();
for (String systemProperty : systemProperties.keySet())
{
request.setHeaderField(systemProperty, systemProperties.get(systemProperty));
}
request.setHeaderField(HTTPS_PROPERTY_IOTHUB_TO_TAG, iotHubEventUri.getPath())
.setHeaderField(HTTPS_PROPERTY_CONTENT_TYPE_TAG, httpsMessage.getContentType());
log.trace("Sending message using http request ({})", message);
HttpsResponse response = this.sendRequest(request);
IotHubStatusCode status = IotHubStatusCode.getIotHubStatusCode(response.getStatus());
log.trace("Iot Hub responded to http message for iot hub message ({}) with status code {}", message, status);
IotHubTransportMessage transportMessage = new IotHubTransportMessage(httpsMessage.getBody(), message.getMessageType(), message.getMessageId(), message.getCorrelationId(), message.getProperties());
if (status == IotHubStatusCode.OK)
{
this.listener.onMessageSent(transportMessage, this.config.getDeviceId(), null);
}
// Status codes other than 200 and 204 have their errors handled in the IotHubTransport layer once this method returns,
// so there is no need to call "this.listener.onMessageSent(transportMessage, someException)" from this layer.
return status;
}
}