Search in sources :

Example 1 with ConfigurationParser

use of com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method addConfiguration.

/**
 * Add configuration using the given Configuration object
 * Return with the response configuration object from IotHub
 *
 * @param configuration The configuration object to add
 * @return The configuration object for the requested operation
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
public Configuration addConfiguration(Configuration configuration) throws IOException, IotHubException, JsonSyntaxException {
    if (configuration == null) {
        throw new IllegalArgumentException("configuration cannot be null");
    }
    String configurationJson = configuration.toConfigurationParser().toJson();
    URL url = IotHubConnectionString.getUrlConfiguration(this.hostName, configuration.getId());
    HttpRequest request = CreateRequest(url, HttpMethod.PUT, configurationJson.getBytes(StandardCharsets.UTF_8));
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    return new Configuration(new ConfigurationParser(bodyStr));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) ConfigurationParser(com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 2 with ConfigurationParser

use of com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser in project azure-iot-sdk-java by Azure.

the class RegistryManagerTest method commonConfigExpectations.

private void commonConfigExpectations(String connectionString, String configId) throws Exception {
    new NonStrictExpectations() {

        {
            IotHubConnectionStringBuilder.createIotHubConnectionString(connectionString);
            result = iotHubConnectionString;
            iotHubConnectionString.getHostName();
            result = "aaa.bbb.ccc";
            IotHubConnectionString.getUrlConfiguration(anyString, configId);
            result = mockUrl;
            mockHttpRequest.send();
            result = mockHttpResponse;
            IotHubExceptionManager.httpResponseVerification((HttpResponse) any);
            mockHttpResponse.getBody();
            result = configJson.getBytes(StandardCharsets.UTF_8);
            Deencapsulation.invoke(config, "toConfigurationParser");
            result = new ConfigurationParser();
        }
    };
}
Also used : ConfigurationParser(com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser)

Example 3 with ConfigurationParser

use of com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method getConfiguration.

/**
 * Get configuration by configuration Id from IotHub
 *
 * @param configurationId The id of requested configuration
 * @return The configuration object of requested configuration on the specific device
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
public Configuration getConfiguration(String configurationId) throws IOException, IotHubException, JsonSyntaxException {
    if (Tools.isNullOrEmpty(configurationId)) {
        throw new IllegalArgumentException("configurationId cannot be null or empty");
    }
    URL url = IotHubConnectionString.getUrlConfiguration(this.hostName, configurationId);
    HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0]);
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    return new Configuration(new ConfigurationParser(bodyStr));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) ConfigurationParser(com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 4 with ConfigurationParser

use of com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method updateConfiguration.

/**
 * Update configuration with forceUpdate input parameter
 * @deprecated the forceUpdate argument does nothing so this method will always behave the same as @link #updateConfiguration(Configuration)}
 *
 * @param configuration The configuration object containing updated data
 * @param forceUpdate This value is not used
 * @return The updated configuration object
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
@Deprecated
public Configuration updateConfiguration(Configuration configuration, Boolean forceUpdate) throws IOException, IotHubException, JsonSyntaxException {
    if (configuration == null) {
        throw new IllegalArgumentException("configuration cannot be null");
    }
    configuration.setForceUpdate(forceUpdate);
    URL url = IotHubConnectionString.getUrlConfiguration(this.hostName, configuration.getId());
    HttpRequest request = CreateRequest(url, HttpMethod.PUT, configuration.toConfigurationParser().toJson().getBytes(StandardCharsets.UTF_8));
    request.setHeaderField("If-Match", "*");
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    return new Configuration(new ConfigurationParser(bodyStr));
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) ConfigurationParser(com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) URL(java.net.URL)

Example 5 with ConfigurationParser

use of com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser in project azure-iot-sdk-java by Azure.

the class RegistryManager method getConfigurations.

/**
 * Get list of Configuration
 *
 * @param maxCount The requested count of configurations
 * @return The array of requested configuration objects
 * @throws IOException This exception is thrown if the IO operation failed
 * @throws IotHubException This exception is thrown if the response verification failed
 */
public List<Configuration> getConfigurations(Integer maxCount) throws IOException, IotHubException, JsonSyntaxException {
    if (maxCount < 1) {
        throw new IllegalArgumentException("maxCount cannot be less then 1");
    }
    URL url = IotHubConnectionString.getUrlConfigurationsList(this.hostName, maxCount);
    HttpRequest request = CreateRequest(url, HttpMethod.GET, new byte[0]);
    HttpResponse response = request.send();
    IotHubExceptionManager.httpResponseVerification(response);
    String bodyStr = new String(response.getBody(), StandardCharsets.UTF_8);
    try (JsonReader jsonReader = Json.createReader(new StringReader(bodyStr))) {
        List<Configuration> configurationList = new ArrayList<>();
        JsonArray deviceArray = jsonReader.readArray();
        for (int i = 0; i < deviceArray.size(); i++) {
            JsonObject jsonObject = deviceArray.getJsonObject(i);
            Configuration iotHubConfiguration = new Configuration(new ConfigurationParser(jsonObject.toString()));
            configurationList.add(iotHubConfiguration);
        }
        return configurationList;
    }
}
Also used : HttpRequest(com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest) ArrayList(java.util.ArrayList) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) JsonObject(javax.json.JsonObject) URL(java.net.URL) JsonArray(javax.json.JsonArray) StringReader(java.io.StringReader) ConfigurationParser(com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser) JsonReader(javax.json.JsonReader)

Aggregations

ConfigurationParser (com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationParser)6 HttpRequest (com.microsoft.azure.sdk.iot.service.transport.http.HttpRequest)4 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)4 URL (java.net.URL)4 ConfigurationContentParser (com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationContentParser)1 ConfigurationMetricsParser (com.microsoft.azure.sdk.iot.deps.serializer.ConfigurationMetricsParser)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 JsonArray (javax.json.JsonArray)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1