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();
}
Aggregations