Search in sources :

Example 6 with InputStreamEntity

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpResponseParser(org.apache.http.impl.io.DefaultHttpResponseParser) DefaultHttpResponseFactory(org.apache.http.impl.DefaultHttpResponseFactory) BasicLineParser(org.apache.http.message.BasicLineParser) BasicHttpParams(org.apache.http.params.BasicHttpParams) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 7 with InputStreamEntity

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());
}
Also used : HttpEntity(org.apache.http.HttpEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) InputStreamEntity(org.apache.http.entity.InputStreamEntity) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) BasicRequestLine(org.apache.http.message.BasicRequestLine) RequestLine(org.apache.http.RequestLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicRequestLine(org.apache.http.message.BasicRequestLine) HttpHost(org.apache.http.HttpHost)

Example 8 with InputStreamEntity

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));
}
Also used : BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 9 with InputStreamEntity

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InputStreamEntity(org.apache.http.entity.InputStreamEntity) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 10 with InputStreamEntity

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));
}
Also used : BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Aggregations

InputStreamEntity (org.apache.http.entity.InputStreamEntity)41 ByteArrayInputStream (java.io.ByteArrayInputStream)15 HttpPost (org.apache.http.client.methods.HttpPost)14 HttpResponse (org.apache.http.HttpResponse)12 InputStream (java.io.InputStream)10 Test (org.junit.Test)10 HttpEntity (org.apache.http.HttpEntity)8 IOException (java.io.IOException)7 HttpPut (org.apache.http.client.methods.HttpPut)7 Map (java.util.Map)4 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)4 HttpClient (org.apache.http.client.HttpClient)4 StringEntity (org.apache.http.entity.StringEntity)4 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Header (org.apache.http.Header)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 HttpGet (org.apache.http.client.methods.HttpGet)3 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)3