Java Code Examples for org.apache.http.entity.ByteArrayEntity#setContentEncoding()

The following examples show how to use org.apache.http.entity.ByteArrayEntity#setContentEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpTransport.java    From bender with Apache License 2.0 6 votes vote down vote up
protected HttpResponse sendBatchCompressed(HttpPost httpPost, byte[] raw)
    throws TransportException {
  /*
   * Write gzip data to Entity and set content encoding to gzip
   */
  ByteArrayEntity entity = new ByteArrayEntity(raw, ContentType.DEFAULT_BINARY);
  entity.setContentEncoding("gzip");

  httpPost.addHeader(new BasicHeader("Accept-Encoding", "gzip"));
  httpPost.setEntity(entity);

  /*
   * Make call
   */
  HttpResponse resp = null;
  try {
    resp = this.client.execute(httpPost);
  } catch (IOException e) {
    throw new TransportException("failed to make call", e);
  }

  return resp;
}
 
Example 2
Source File: RemoteResponse.java    From logbook with MIT License 6 votes vote down vote up
@Override
public State buffer(final HttpResponse response) throws IOException {
    @Nullable final HttpEntity entity = response.getEntity();

    if (entity == null) {
        return new Passing();
    } else {
        final byte[] body = toByteArray(entity);

        final ByteArrayEntity copy = new ByteArrayEntity(body);
        copy.setChunked(entity.isChunked());
        copy.setContentEncoding(entity.getContentEncoding());
        copy.setContentType(entity.getContentType());

        response.setEntity(copy);

        return new Buffering(body);
    }
}
 
Example 3
Source File: HttpClientRequestExecutorTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
private HttpResponse createMockGzippedResponse(String content) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    byte[] uncompressedBytes = content.getBytes();
    gzos.write(uncompressedBytes, 0, uncompressedBytes.length);
    gzos.close();
    byte[] compressedBytes = baos.toByteArray();
    ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes);
    httpEntity.setContentType("text/html; charset=ISO-8859-1");
    httpEntity.setContentEncoding("gzip");
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("p", 1, 2), HttpStatus.SC_OK, "OK");
    BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine);
    httpResponse.addHeader("Content-type", "text/html; charset=ISO-8859-1");
    httpResponse.addHeader("Content-encoding", "gzip");
    httpResponse.setEntity(httpEntity);
    return httpResponse;
}
 
Example 4
Source File: HttpHandlerIntegrationTest.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompressedRequests() throws Exception{

    URIBuilder builder = getMetricsURIBuilder()
            .setPath("/v2.0/acTEST/ingest");

    HttpPost post = new HttpPost( builder.build() );
    String content = generateJSONMetricsData();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(content.length());
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    gzipOut.write(content.getBytes());
    gzipOut.close();
    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    //Setting the content encoding to gzip
    entity.setContentEncoding("gzip");
    baos.close();
    post.setEntity(entity);
    HttpResponse response = client.execute(post);

    try {
        assertEquals( 200, response.getStatusLine().getStatusCode() );
    }
    finally {
        EntityUtils.consume( response.getEntity() ); // Releases connection apparently
    }
}
 
Example 5
Source File: HttpMethodClient.java    From nem.core with MIT License 5 votes vote down vote up
private static HttpPost createPostRequest(final URI uri, final HttpPostRequest request) {
	final HttpPost post = new HttpPost(uri);
	final ByteArrayEntity entity = new ByteArrayEntity(request.getPayload());
	entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, request.getContentType()));
	post.setEntity(entity);
	return post;
}
 
Example 6
Source File: DriverTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testGzipErrorPage() throws Exception {
    Properties properties = new Properties();
    properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://localhost");
    HttpResponse response =
            new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_INTERNAL_SERVER_ERROR,
                    "Internal Server Error");
    response.addHeader("Content-type", "Text/html;Charset=UTF-8");
    response.addHeader("Content-encoding", "gzip");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    byte[] uncompressedBytes = "é".getBytes("UTF-8");
    gzos.write(uncompressedBytes, 0, uncompressedBytes.length);
    gzos.close();
    byte[] compressedBytes = baos.toByteArray();
    ByteArrayEntity httpEntity = new ByteArrayEntity(compressedBytes);
    httpEntity.setContentType("Text/html;Charset=UTF-8");
    httpEntity.setContentEncoding("gzip");
    response.setEntity(httpEntity);
    mockConnectionManager.setResponse(response);
    Driver driver = createMockDriver(properties, mockConnectionManager);
    CloseableHttpResponse driverResponse;
    try {
        driverResponse = driver.proxy("/", request.build());
        fail("We should get an HttpErrorPage");
    } catch (HttpErrorPage e) {
        driverResponse = e.getHttpResponse();
    }
    assertEquals("é", HttpResponseUtils.toString(driverResponse));
}
 
Example 7
Source File: ApacheHTTPResponse.java    From jbosh with Apache License 2.0 4 votes vote down vote up
/**
 * Create and send a new request to the upstream connection manager,
 * providing deferred access to the results to be returned.
 *
 * @param client client instance to use when sending the request
 * @param cfg client configuration
 * @param params connection manager parameters from the session creation
 *  response, or {@code null} if the session has not yet been established
 * @param request body of the client request
 */
ApacheHTTPResponse(
        final HttpClient client,
        final BOSHClientConfig cfg,
        final CMSessionParams params,
        final AbstractBody request) {
    super();
    this.client = client;
    this.context = new BasicHttpContext();
    this.post = new HttpPost(cfg.getURI().toString());
    this.sent = false;

    try {
        String xml = request.toXML();
        byte[] data = xml.getBytes(CHARSET);

        String encoding = null;
        if (cfg.isCompressionEnabled() && params != null) {
            AttrAccept accept = params.getAccept();
            if (accept != null) {
                if (accept.isAccepted(ZLIBCodec.getID())) {
                    encoding = ZLIBCodec.getID();
                    data = ZLIBCodec.encode(data);
                } else if (accept.isAccepted(GZIPCodec.getID())) {
                    encoding = GZIPCodec.getID();
                    data = GZIPCodec.encode(data);
                }
            }
        }

        ByteArrayEntity entity = new ByteArrayEntity(data);
        entity.setContentType(CONTENT_TYPE);
        if (encoding != null) {
            entity.setContentEncoding(encoding);
        }
        post.setEntity(entity);
        if (cfg.isCompressionEnabled()) {
            post.setHeader(ACCEPT_ENCODING, ACCEPT_ENCODING_VAL);
        }
        for(Map.Entry<String, String> header: cfg.getHttpHeaders().entrySet()) {
            post.addHeader(new BasicHeader(header.getKey(), header.getValue()));
        }
    } catch (Exception e) {
        toThrow = new BOSHException("Could not generate request", e);
    }
}