Search in sources :

Example 1 with QueryRequestParser

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

the class Query method sendQueryRequest.

/**
 * Sends request for the query to the IotHub.
 *
 * @param credentialCache The RBAC authorization token provider. May be null if azureSasCredential or iotHubConnectionString is not.
 * @param azureSasCredential The SAS authorization token provider. May be null if credential or iotHubConnectionString is not.
 * @param iotHubConnectionString The iot hub connection string that SAS tokens will be derived from. May be null if azureSasCredential or credential is not.
 * @param url URL to Query on.
 * @param method HTTP Method for the requesting a query.
 * @param httpConnectTimeout the http connect timeout to use for this request.
 * @param httpReadTimeout the http read timeout to use for this request.
 * @param proxy the proxy to use, or null if no proxy should be used.
 * @return QueryResponse object which holds the response Iterator.
 * @throws IOException If any of the input parameters are not valid.
 * @throws IotHubException If HTTP response other then status ok is received.
 */
public QueryResponse sendQueryRequest(TokenCredentialCache credentialCache, AzureSasCredential azureSasCredential, IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, int httpConnectTimeout, int httpReadTimeout, Proxy proxy) throws IOException, IotHubException {
    this.url = url;
    this.httpMethod = method;
    this.azureSasCredential = azureSasCredential;
    this.credentialCache = credentialCache;
    this.iotHubConnectionString = iotHubConnectionString;
    this.httpConnectTimeout = httpConnectTimeout;
    this.httpReadTimeout = httpReadTimeout;
    this.proxy = proxy;
    byte[] payload;
    Map<String, String> queryHeaders = new HashMap<>();
    if (this.requestContinuationToken != null) {
        queryHeaders.put(CONTINUATION_TOKEN_KEY, requestContinuationToken);
    }
    queryHeaders.put(PAGE_SIZE_KEY, String.valueOf(pageSize));
    DeviceOperations.setHeaders(queryHeaders);
    if (isSqlQuery) {
        QueryRequestParser requestParser = new QueryRequestParser(this.query);
        payload = requestParser.toJson().getBytes(StandardCharsets.UTF_8);
    } else {
        payload = new byte[0];
    }
    String authorizationToken;
    if (credentialCache != null) {
        authorizationToken = this.credentialCache.getTokenString();
    } else if (azureSasCredential != null) {
        authorizationToken = azureSasCredential.getSignature();
    } else {
        authorizationToken = new IotHubServiceSasToken(iotHubConnectionString).toString();
    }
    HttpResponse httpResponse = DeviceOperations.request(authorizationToken, url, method, payload, null, httpConnectTimeout, httpReadTimeout, proxy);
    this.responseContinuationToken = null;
    Map<String, String> headers = httpResponse.getHeaderFields();
    for (Map.Entry<String, String> header : headers.entrySet()) {
        switch(header.getKey()) {
            case CONTINUATION_TOKEN_KEY:
                this.responseContinuationToken = header.getValue();
                break;
            case ITEM_TYPE_KEY:
                this.responseQueryType = QueryType.fromString(header.getValue());
                break;
            default:
                break;
        }
    }
    if (this.responseQueryType == null || this.responseQueryType == QueryType.UNKNOWN) {
        throw new IotHubException("Query response type is not defined by IotHub");
    }
    if (this.requestQueryType != this.responseQueryType) {
        throw new IotHubException("Query response does not match query request");
    }
    this.queryResponse = new QueryResponse(new String(httpResponse.getBody(), StandardCharsets.UTF_8));
    return this.queryResponse;
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryRequestParser(com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser) HashMap(java.util.HashMap) Map(java.util.Map) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 2 with QueryRequestParser

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

the class Query method sendQueryRequest.

/**
 * Sends request for the query to the IotHub.
 *
 * @param iotHubConnectionString Hub Connection String.
 * @param url URL to Query on.
 * @param method HTTP Method for the requesting a query.
 * @param httpConnectTimeout the http connect timeout to use for this request.
 * @param httpReadTimeout the http read timeout to use for this request.
 * @param proxy the proxy to use, or null if no proxy should be used.
 * @return QueryResponse object which holds the response Iterator.
 * @throws IOException If any of the input parameters are not valid.
 * @throws IotHubException If HTTP response other then status ok is received.
 */
// Public method
@SuppressWarnings("UnusedReturnValue")
public QueryResponse sendQueryRequest(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, int httpConnectTimeout, int httpReadTimeout, Proxy proxy) throws IOException, IotHubException {
    if (iotHubConnectionString == null || url == null || method == null) {
        throw new IllegalArgumentException("Input parameters cannot be null");
    }
    this.iotHubConnectionString = iotHubConnectionString;
    this.url = url;
    this.httpMethod = method;
    this.httpConnectTimeout = httpConnectTimeout;
    this.httpReadTimeout = httpReadTimeout;
    this.proxy = proxy;
    byte[] payload;
    Map<String, String> queryHeaders = new HashMap<>();
    if (this.requestContinuationToken != null) {
        queryHeaders.put(CONTINUATION_TOKEN_KEY, requestContinuationToken);
    }
    queryHeaders.put(PAGE_SIZE_KEY, String.valueOf(pageSize));
    DeviceOperations.setHeaders(queryHeaders);
    if (isSqlQuery) {
        QueryRequestParser requestParser = new QueryRequestParser(this.query);
        payload = requestParser.toJson().getBytes(StandardCharsets.UTF_8);
    } else {
        payload = new byte[0];
    }
    HttpResponse httpResponse = DeviceOperations.request(iotHubConnectionString, url, method, payload, null, httpConnectTimeout, httpReadTimeout, proxy);
    this.responseContinuationToken = null;
    Map<String, String> headers = httpResponse.getHeaderFields();
    for (Map.Entry<String, String> header : headers.entrySet()) {
        switch(header.getKey()) {
            case CONTINUATION_TOKEN_KEY:
                this.responseContinuationToken = header.getValue();
                break;
            case ITEM_TYPE_KEY:
                this.responseQueryType = QueryType.fromString(header.getValue());
                break;
            default:
                break;
        }
    }
    if (this.responseQueryType == null || this.responseQueryType == QueryType.UNKNOWN) {
        throw new IotHubException("Query response type is not defined by IotHub");
    }
    if (this.requestQueryType != this.responseQueryType) {
        throw new IotHubException("Query response does not match query request");
    }
    this.queryResponse = new QueryResponse(new String(httpResponse.getBody(), StandardCharsets.UTF_8));
    return this.queryResponse;
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryRequestParser(com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser) HashMap(java.util.HashMap) Map(java.util.Map) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 3 with QueryRequestParser

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

the class Query method sendQueryRequest.

/**
 * Sends request for the query to the IotHub.
 *
 * @param iotHubConnectionString Hub Connection String.
 * @param url URL to Query on.
 * @param method HTTP Method for the requesting a query.
 * @param timeoutInMs Unused.
 * @return QueryResponse object which holds the response Iterator.
 * @throws IOException If any of the input parameters are not valid.
 * @throws IotHubException If HTTP response other then status ok is received.
 * @deprecated use {@link #sendQueryRequest(IotHubConnectionString, URL, HttpMethod, int, int, Proxy)} instead.
 */
@Deprecated
public QueryResponse sendQueryRequest(IotHubConnectionString iotHubConnectionString, URL url, HttpMethod method, Long timeoutInMs) throws IOException, IotHubException {
    if (iotHubConnectionString == null || url == null || method == null) {
        throw new IllegalArgumentException("Input parameters cannot be null");
    }
    this.iotHubConnectionString = iotHubConnectionString;
    this.url = url;
    this.httpMethod = method;
    this.httpConnectTimeout = DeviceTwinClientOptions.DEFAULT_HTTP_CONNECT_TIMEOUT_MS;
    this.httpReadTimeout = DeviceTwinClientOptions.DEFAULT_HTTP_READ_TIMEOUT_MS;
    byte[] payload;
    Map<String, String> queryHeaders = new HashMap<>();
    if (this.requestContinuationToken != null) {
        queryHeaders.put(CONTINUATION_TOKEN_KEY, requestContinuationToken);
    }
    queryHeaders.put(PAGE_SIZE_KEY, String.valueOf(pageSize));
    DeviceOperations.setHeaders(queryHeaders);
    if (isSqlQuery) {
        QueryRequestParser requestParser = new QueryRequestParser(this.query);
        payload = requestParser.toJson().getBytes(StandardCharsets.UTF_8);
    } else {
        payload = new byte[0];
    }
    HttpResponse httpResponse = DeviceOperations.request(iotHubConnectionString, url, method, payload, null, this.httpConnectTimeout, this.httpReadTimeout, null);
    this.responseContinuationToken = null;
    Map<String, String> headers = httpResponse.getHeaderFields();
    for (Map.Entry<String, String> header : headers.entrySet()) {
        switch(header.getKey()) {
            case CONTINUATION_TOKEN_KEY:
                this.responseContinuationToken = header.getValue();
                break;
            case ITEM_TYPE_KEY:
                this.responseQueryType = QueryType.fromString(header.getValue());
                break;
            default:
                break;
        }
    }
    if (this.responseQueryType == null || this.responseQueryType == QueryType.UNKNOWN) {
        throw new IotHubException("Query response type is not defined by IotHub");
    }
    if (this.requestQueryType != this.responseQueryType) {
        throw new IotHubException("Query response does not match query request");
    }
    this.queryResponse = new QueryResponse(new String(httpResponse.getBody(), StandardCharsets.UTF_8));
    return this.queryResponse;
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) QueryRequestParser(com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser) HashMap(java.util.HashMap) Map(java.util.Map) IotHubException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)

Example 4 with QueryRequestParser

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

the class QueryCollection method sendQueryRequest.

/**
 * Retrieves a page of results for a query.
 *
 * @param options the options for the query. If a continuation token is set in these options, it shall override any
 *                previously saved tokens. The page size of these options shall override any previously saved page size.
 * @return The QueryCollectionResponse containing the full page of results and the continuation token for the next query.
 * @throws IOException If an IOException occurs when calling the Service API, or if the results of that call are unexpected.
 * @throws IotHubException If an IotHubException occurs when calling the Service API.
 */
private QueryCollectionResponse<String> sendQueryRequest(QueryOptions options) throws IOException, IotHubException {
    DeviceOperations.setHeaders(buildQueryHeaders(options));
    byte[] payload;
    if (isSqlQuery) {
        QueryRequestParser requestParser = new QueryRequestParser(this.query);
        payload = requestParser.toJson().getBytes(StandardCharsets.UTF_8);
    } else {
        payload = new byte[0];
    }
    String authorizationToken;
    if (this.credentialCache != null) {
        authorizationToken = this.credentialCache.getTokenString();
    } else if (this.azureSasCredential != null) {
        authorizationToken = this.azureSasCredential.getSignature();
    } else {
        authorizationToken = new IotHubServiceSasToken(iotHubConnectionString).toString();
    }
    HttpResponse httpResponse = DeviceOperations.request(authorizationToken, this.url, this.httpMethod, payload, null, this.httpConnectTimeout, this.httpReadTimeout, this.proxy);
    handleQueryResponse(httpResponse);
    this.isInitialQuery = false;
    return new QueryCollectionResponse<>(new String(httpResponse.getBody(), StandardCharsets.UTF_8), this.responseContinuationToken);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) HttpResponse(com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse) QueryRequestParser(com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Aggregations

QueryRequestParser (com.microsoft.azure.sdk.iot.deps.serializer.QueryRequestParser)4 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)4 HttpResponse (com.microsoft.azure.sdk.iot.service.transport.http.HttpResponse)4 IotHubException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)2