use of org.apache.http.entity.InputStreamEntity in project commoncrawl-examples by commoncrawl.
the class ArcRecord method getHttpResponse.
/**
* <p>Returns an HTTP response object parsed from the ARC record payload.<p>
* <p>Note: The payload is parsed on-demand, but is only parsed once. The
* parsed data is saved for subsequent calls.</p>
*
* @return The ARC record payload as an HTTP response object. See the Apache
* HttpComponents project.
*/
public HttpResponse getHttpResponse() throws IOException, HttpException {
if (this._httpResponse != null)
return this._httpResponse;
if (this._payload == null) {
LOG.error("Unable to parse HTTP response: Payload has not been set");
return null;
}
if (this._url != null && !this._url.startsWith("http://") && !this._url.startsWith("https://")) {
LOG.error("Unable to parse HTTP response: URL protocol is not HTTP");
return null;
}
this._httpResponse = null;
// Find where the HTTP headers stop
int end = this._searchForCRLFCRLF(this._payload);
if (end == -1) {
LOG.error("Unable to parse HTTP response: End of HTTP headers not found");
return null;
}
// Parse the HTTP status line and headers
DefaultHttpResponseParser parser = new DefaultHttpResponseParser(new ByteArraySessionInputBuffer(this._payload, 0, end), new BasicLineParser(), new DefaultHttpResponseFactory(), new BasicHttpParams());
this._httpResponse = parser.parse();
if (this._httpResponse == null) {
LOG.error("Unable to parse HTTP response");
return null;
}
// Set the reset of the payload as the HTTP entity. Use an InputStreamEntity
// to avoid a memory copy.
InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(this._payload, end, this._payload.length - end), this._payload.length - end);
entity.setContentType(this._httpResponse.getFirstHeader("Content-Type"));
entity.setContentEncoding(this._httpResponse.getFirstHeader("Content-Encoding"));
this._httpResponse.setEntity(entity);
return this._httpResponse;
}
use of org.apache.http.entity.InputStreamEntity in project elasticsearch by elastic.
the class ResponseExceptionTests method testResponseException.
public void testResponseException() throws IOException {
ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1);
StatusLine statusLine = new BasicStatusLine(protocolVersion, 500, "Internal Server Error");
HttpResponse httpResponse = new BasicHttpResponse(statusLine);
String responseBody = "{\"error\":{\"root_cause\": {}}}";
boolean hasBody = getRandom().nextBoolean();
if (hasBody) {
HttpEntity entity;
if (getRandom().nextBoolean()) {
entity = new StringEntity(responseBody, ContentType.APPLICATION_JSON);
} else {
//test a non repeatable entity
entity = new InputStreamEntity(new ByteArrayInputStream(responseBody.getBytes(StandardCharsets.UTF_8)), ContentType.APPLICATION_JSON);
}
httpResponse.setEntity(entity);
}
RequestLine requestLine = new BasicRequestLine("GET", "/", protocolVersion);
HttpHost httpHost = new HttpHost("localhost", 9200);
Response response = new Response(requestLine, httpHost, httpResponse);
ResponseException responseException = new ResponseException(response);
assertSame(response, responseException.getResponse());
if (hasBody) {
assertEquals(responseBody, EntityUtils.toString(responseException.getResponse().getEntity()));
} else {
assertNull(responseException.getResponse().getEntity());
}
String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri() + ": " + response.getStatusLine().toString();
if (hasBody) {
message += "\n" + responseBody;
}
assertEquals(message, responseException.getMessage());
}
use of org.apache.http.entity.InputStreamEntity in project android_frameworks_base by ParanoidAndroid.
the class Request method setBodyProvider.
/**
* Supply an InputStream that provides the body of a request. It's
* not great that the caller must also provide the length of the data
* returned by that InputStream, but the client needs to know up
* front, and I'm not sure how to get this out of the InputStream
* itself without a costly readthrough. I'm not sure skip() would
* do what we want. If you know a better way, please let me know.
*/
private void setBodyProvider(InputStream bodyProvider, int bodyLength) {
if (!bodyProvider.markSupported()) {
throw new IllegalArgumentException("bodyProvider must support mark()");
}
// Mark beginning of stream
bodyProvider.mark(Integer.MAX_VALUE);
((BasicHttpEntityEnclosingRequest) mHttpRequest).setEntity(new InputStreamEntity(bodyProvider, bodyLength));
}
use of org.apache.http.entity.InputStreamEntity in project YCSB by brianfrankcooper.
the class RestClient method httpExecute.
private int httpExecute(HttpEntityEnclosingRequestBase request, String data) throws IOException {
requestTimedout.setIsSatisfied(false);
Thread timer = new Thread(new Timer(execTimeout, requestTimedout));
timer.start();
int responseCode = 200;
for (int i = 0; i < headers.length; i = i + 2) {
request.setHeader(headers[i], headers[i + 1]);
}
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data.getBytes()), ContentType.APPLICATION_FORM_URLENCODED);
reqEntity.setChunked(true);
request.setEntity(reqEntity);
CloseableHttpResponse response = client.execute(request);
responseCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
// If null entity don't bother about connection release.
if (responseEntity != null) {
InputStream stream = responseEntity.getContent();
if (compressedResponse) {
stream = new GZIPInputStream(stream);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
StringBuffer responseContent = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
if (requestTimedout.isSatisfied()) {
// Must avoid memory leak.
reader.close();
stream.close();
EntityUtils.consumeQuietly(responseEntity);
response.close();
client.close();
throw new TimeoutException();
}
responseContent.append(line);
}
timer.interrupt();
// Closing the input stream will trigger connection release.
stream.close();
}
EntityUtils.consumeQuietly(responseEntity);
response.close();
client.close();
return responseCode;
}
use of org.apache.http.entity.InputStreamEntity in project XobotOS by xamarin.
the class Request method setBodyProvider.
/**
* Supply an InputStream that provides the body of a request. It's
* not great that the caller must also provide the length of the data
* returned by that InputStream, but the client needs to know up
* front, and I'm not sure how to get this out of the InputStream
* itself without a costly readthrough. I'm not sure skip() would
* do what we want. If you know a better way, please let me know.
*/
private void setBodyProvider(InputStream bodyProvider, int bodyLength) {
if (!bodyProvider.markSupported()) {
throw new IllegalArgumentException("bodyProvider must support mark()");
}
// Mark beginning of stream
bodyProvider.mark(Integer.MAX_VALUE);
((BasicHttpEntityEnclosingRequest) mHttpRequest).setEntity(new InputStreamEntity(bodyProvider, bodyLength));
}
Aggregations