Example usage for org.apache.http.entity BasicHttpEntity setContentLength

List of usage examples for org.apache.http.entity BasicHttpEntity setContentLength

Introduction

In this page you can find the example usage for org.apache.http.entity BasicHttpEntity setContentLength.

Prototype

public void setContentLength(long j) 

Source Link

Usage

From source file:cn.garymb.wechatmoments.common.OkHttpStack.java

@SuppressWarnings("deprecation")
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;//from  ww  w .ja  va2 s.c  o  m
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:ai.eve.volley.stack.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @return an HttpEntity populated with data from <code>connection</code>.
 *//*w  w w .j  ava  2 s .  co  m*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.kubeiwu.commontool.khttp.toolbox.HurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * /*from  w  w  w  .  j  av a2s . c  o m*/
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {

    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:com.nxt.zyl.data.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection/*from w  w  w . j av  a 2s .co  m*/
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}

From source file:com.hackerati.android.user_sdk.volley.HHurlStack.java

/**
 * Initializes an {@link HttpEntity} from the given
 * {@link HttpURLConnection}./*  w ww  . j ava2 s .c  o  m*/
 * 
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(final HttpURLConnection connection) {
    final BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (final IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:common.net.volley.toolbox.HurlStack.java

/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection/* ww w  .  j  av a  2 s  .  com*/
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream rawStream = null;
    try {
        rawStream = connection.getInputStream();

        rawStream = stethoManager.interpretResponseStream(rawStream);
        InputStream decompressedStream = applyDecompressionIfApplicable(connection, rawStream);
        if (decompressedStream != null) {
            copy(decompressedStream, out, new byte[1024]);
        }
        entity.setContent(new ByteArrayInputStream(out.toByteArray()));

    } catch (IOException ioe) {
        rawStream = connection.getErrorStream();
        entity.setContent(rawStream);

    } finally {
        //            if(rawStream != null) {
        //                rawStream.close();
        //            }
    }

    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}

From source file:org.springframework.cloud.netflix.ribbon.apache.HttpClientStatusCodeExceptionTest.java

@Test
public void getResponse() throws Exception {
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    doReturn(new Locale("en")).when(response).getLocale();
    Header foo = new BasicHeader("foo", "bar");
    Header[] headers = new Header[] { foo };
    doReturn(headers).when(response).getAllHeaders();
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), 200, "Success");
    doReturn(statusLine).when(response).getStatusLine();
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream("foo".getBytes()));
    entity.setContentLength(3);
    doReturn(entity).when(response).getEntity();
    HttpEntity copiedEntity = HttpClientUtils.createEntity(response);
    HttpClientStatusCodeException ex = new HttpClientStatusCodeException("service", response, copiedEntity,
            new URI("http://service.com"));
    assertEquals("en", ex.getResponse().getLocale().toString());
    assertArrayEquals(headers, ex.getResponse().getAllHeaders());
    assertEquals("Success", ex.getResponse().getStatusLine().getReasonPhrase());
    assertEquals(200, ex.getResponse().getStatusLine().getStatusCode());
    assertEquals("http", ex.getResponse().getStatusLine().getProtocolVersion().getProtocol());
    assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMajor());
    assertEquals(1, ex.getResponse().getStatusLine().getProtocolVersion().getMinor());
    assertEquals("foo", EntityUtils.toString(ex.getResponse().getEntity()));
    verify(response, times(1)).close();/*from   w  w  w .j ava  2 s .c o  m*/
}

From source file:com.android.volley.toolbox.BaseHttpStack.java

/**
 * @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated
 * Apache HTTP library. Nothing in Volley's own source calls this method. However, since
 * {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation in
 * case legacy client apps are dependent on that field. This method may be removed in a future
 * release of Volley./*  w  ww  .  j  av  a 2 s  .c o  m*/
 */
@Deprecated
@Override
public final org.apache.http.HttpResponse performRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    HttpResponse response = executeRequest(request, additionalHeaders);

    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    StatusLine statusLine = new BasicStatusLine(protocolVersion, response.getStatusCode(),
            "" /* reasonPhrase */);
    BasicHttpResponse apacheResponse = new BasicHttpResponse(statusLine);

    List<org.apache.http.Header> headers = new ArrayList<>();
    for (Header header : response.getHeaders()) {
        headers.add(new BasicHeader(header.getName(), header.getValue()));
    }
    apacheResponse.setHeaders(headers.toArray(new org.apache.http.Header[headers.size()]));

    InputStream responseStream = response.getContent();
    if (responseStream != null) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(responseStream);
        entity.setContentLength(response.getContentLength());
        apacheResponse.setEntity(entity);
    }

    return apacheResponse;
}

From source file:org.syncope.console.commons.HttpResourceStream.java

private HttpResponse buildFakeResponse(final String errorMessage) {
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(bais);/*from  ww  w .ja va 2s  . c  o  m*/
    entity.setContentLength(0);
    entity.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

    BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 400,
            "Exception: " + errorMessage);
    response.setEntity(entity);

    response.addHeader("Content-Disposition", "attachment; filename=error");

    return response;
}

From source file:es.tid.fiware.rss.oauth.test.ResponseHandlerTest.java

/**
 * //from   w w  w. j  a  v  a2 s.  c  o  m
 */
@Test
public void handleResponseTest() throws Exception {
    ResponseHandler handler = new ResponseHandler();
    HttpResponseFactory factory = new DefaultHttpResponseFactory();
    HttpResponse responseSent = factory
            .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "reason"), null);
    HttpResponse response = handler.handleResponse(responseSent);
    Assert.assertEquals(handler.getStatus(), HttpStatus.SC_OK);
    Assert.assertFalse(handler.hasContent());
    // response with content.
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = new ByteArrayInputStream("new content".getBytes());
    entity.setContent(inputStream);
    entity.setContentLength("new content".length()); // sets the length
    response.setEntity(entity);
    response = handler.handleResponse(responseSent);
    Assert.assertEquals("new content", handler.getResponseContent());
}