Search in sources :

Example 1 with InputStreamConsumer

use of com.ibm.streamsx.rest.internal.InputStreamConsumer in project streamsx.topology by IBMStreams.

the class StreamsRestUtils method rawStreamingGet.

/**
 * Gets an entity in streaming mode.
 * @param executor
 * @param auth
 * @param url
 * @param streamConsumer
 * @return The return of {@link InputStreamConsumer#getResult()}
 * @throws IOException
 */
static <T> T rawStreamingGet(Executor executor, String auth, String url, final InputStreamConsumer<T> streamConsumer) throws IOException {
    TRACE.fine("HTTP GET: " + url);
    Request request = Request.Get(url).addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()).useExpectContinue().socketTimeout(// throw Exception when we do not read data for more than x millis
    STREAMING_GET_SO_TIMEOUT_MILLIS);
    if (null != auth) {
        request = request.addHeader(AUTH.WWW_AUTH_RESP, auth);
    }
    Response response = executor.execute(request);
    response.handleResponse(new ResponseHandler<InputStreamConsumer<T>>() {

        @Override
        public InputStreamConsumer<T> handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();
            int rcResponse = statusLine.getStatusCode();
            HttpEntity entity = response.getEntity();
            if (HttpStatus.SC_OK == rcResponse) {
                if (entity != null) {
                    try (InputStream is = entity.getContent()) {
                        streamConsumer.consume(is);
                    }
                    return streamConsumer;
                } else {
                    throw new ClientProtocolException("Response contains no content");
                }
            } else {
                // all other errors...
                String httpError = "HttpStatus is " + rcResponse + " for url " + url;
                throw new RESTException(rcResponse, httpError);
            }
        }
    });
    return streamConsumer.getResult();
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) InputStreamConsumer(com.ibm.streamsx.rest.internal.InputStreamConsumer) HttpEntity(org.apache.http.HttpEntity) InputStream(java.io.InputStream) Request(org.apache.http.client.fluent.Request) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) Response(org.apache.http.client.fluent.Response) StatusLine(org.apache.http.StatusLine)

Aggregations

RESTException (com.ibm.streamsx.rest.RESTException)1 InputStreamConsumer (com.ibm.streamsx.rest.internal.InputStreamConsumer)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 Request (org.apache.http.client.fluent.Request)1 Response (org.apache.http.client.fluent.Response)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1