Search in sources :

Example 1 with IotHubSizeExceededException

use of com.microsoft.azure.sdk.iot.device.exceptions.IotHubSizeExceededException in project azure-iot-sdk-java by Azure.

the class HttpsIotHubConnection method sendMessage.

/**
 * Sends an event message.
 *
 * @param message the event message.
 *
 * @return the IotHubStatusCode from sending the event message.
 *
 * @throws TransportException if the IoT Hub could not be reached.
 */
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 (IotHubSizeExceededException 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();
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_002: [The function shall send a request to the URL 'https://[iotHubHostname]/devices/[deviceId]/messages/events?api-version=2016-02-03'.]
        IotHubEventUri iotHubEventUri = new IotHubEventUri(iotHubHostname, deviceId, moduleId);
        URL eventUrl = this.buildUrlFromString(HTTPS_HEAD_TAG + iotHubEventUri.toString());
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_003: [The function shall send a POST request.]
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_004: [The function shall set the request body to the message body.]
        HttpsRequest request = new HttpsRequest(eventUrl, HttpsMethod.POST, httpsMessage.getBody(), this.config.getProductInfo().getUserAgentString(), config.getProxySettings());
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_005: [The function shall write each message property as a request header.]
        for (MessageProperty property : httpsMessage.getProperties()) {
            request.setHeaderField(property.getName(), property.getValue());
        }
        if (message.getContentEncoding() != null) {
            // Codes_SRS_HTTPSIOTHUBCONNECTION_34_073: [If the provided message has a content encoding, this function shall set the request header to include that value with the key "iothub-contentencoding".]
            request.setHeaderField(MessageProperty.IOTHUB_CONTENT_ENCODING, message.getContentEncoding());
        }
        if (message.getContentType() != null) {
            // Codes_SRS_HTTPSIOTHUBCONNECTION_34_074: [If the provided message has a content type, this function shall set the request header to include that value with the key "iothub-contenttype".]
            request.setHeaderField(MessageProperty.IOTHUB_CONTENT_TYPE, message.getContentType());
        }
        if (message.getCreationTimeUTC() != null) {
            // Codes_SRS_HTTPSIOTHUBCONNECTION_34_075: [If the provided message has a creation time utc, this function shall set the request header to include that value with the key "iothub-contenttype".]
            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));
        }
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_008: [The function shall set the header field 'iothub-to' to be '/devices/[deviceId]/messages/events'.]
        request.setHeaderField(HTTPS_PROPERTY_IOTHUB_TO_TAG, iotHubEventUri.getPath()).setHeaderField(HTTPS_PROPERTY_CONTENT_TYPE_TAG, httpsMessage.getContentType());
        // Codes_SRS_HTTPSIOTHUBCONNECTION_25_040: [The function shall set the IotHub SSL context by calling setSSLContext on the request.]
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_007: [The function shall set the header field 'authorization' to be a valid SAS token generated from the configuration parameters.]
        // Codes_SRS_HTTPSIOTHUBCONNECTION_34_059: [If this config is using x509 authentication, this function shall retrieve its sslcontext from its x509 Authentication object.]
        // Codes_SRS_HTTPSIOTHUBCONNECTION_11_006: [The function shall set the request read timeout to be the configuration parameter readTimeoutMillis.]
        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 || status == IotHubStatusCode.OK_EMPTY) {
            // Codes_SRS_HTTPSIOTHUBCONNECTION_34_067: [If the response from the service is OK or OK_EMPTY, this function shall notify its listener that a message was sent with no exception.]
            this.listener.onMessageSent(transportMessage, this.config.getDeviceId(), null);
        }
        return status;
    }
}
Also used : IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) ArrayList(java.util.ArrayList) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) URL(java.net.URL) IotHubSizeExceededException(com.microsoft.azure.sdk.iot.device.exceptions.IotHubSizeExceededException)

Aggregations

IotHubSizeExceededException (com.microsoft.azure.sdk.iot.device.exceptions.IotHubSizeExceededException)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1