Example usage for org.apache.http.entity ByteArrayEntity setContentEncoding

List of usage examples for org.apache.http.entity ByteArrayEntity setContentEncoding

Introduction

In this page you can find the example usage for org.apache.http.entity ByteArrayEntity setContentEncoding.

Prototype

public void setContentEncoding(Header header) 

Source Link

Usage

From source file:com.eviware.soapui.impl.wsdl.support.CompressionSupport.java

public static byte[] decompress(String alg, byte[] content) throws Exception {
    // Use the excellent content encoding handling that exists in HTTP Client
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new HttpVersion(1, 0), 0, null));
    ByteArrayEntity entity = new ByteArrayEntity(content);
    entity.setContentEncoding(alg);
    response.setEntity(entity);//from  w w  w .  ja  v a 2  s . c  o m
    new ResponseContentEncoding().process(response, null);
    return IOUtils.toByteArray(response.getEntity().getContent());
}

From source file:com.jkoolcloud.jesl.net.http.apache.HttpMessageUtils.java

/**
 * Set HTTP message content//from  w  w  w. j a  v a2s  .c o m
 *
 * @param message HTTP message
 * @param contentType contentType
 * @param content content bytes
 * @param contentEncoding content encoding
 * @throws IOException if error writing message content
 */
public static void setContent(HttpMessage message, String contentType, byte[] content, String contentEncoding)
        throws IOException {
    ByteArrayEntity httpContent = new ByteArrayEntity(content);
    if (!StringUtils.isEmpty(contentEncoding))
        httpContent.setContentEncoding(contentEncoding);
    message.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(httpContent.getContentLength()));
    if (!StringUtils.isEmpty(contentType))
        message.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
    setEntity(message, httpContent);
}

From source file:org.esigate.HttpErrorPage.java

private static HttpEntity toMemoryEntity(HttpEntity httpEntity) {
    if (httpEntity == null) {
        return null;
    }//from w ww.ja v  a 2 s  . c om
    HttpEntity memoryEntity;
    try {
        byte[] content = EntityUtils.toByteArray(httpEntity);
        ByteArrayEntity byteArrayEntity = new ByteArrayEntity(content, ContentType.get(httpEntity));
        Header contentEncoding = httpEntity.getContentEncoding();
        if (contentEncoding != null) {
            byteArrayEntity.setContentEncoding(contentEncoding);
        }
        memoryEntity = byteArrayEntity;
    } catch (IOException e) {
        StringBuilderWriter out = new StringBuilderWriter(Parameters.DEFAULT_BUFFER_SIZE);
        PrintWriter pw = new PrintWriter(out);
        e.printStackTrace(pw);
        pw.close();
        memoryEntity = new StringEntity(out.toString(), ContentType.getOrDefault(httpEntity));
    }
    return memoryEntity;
}

From source file:com.wbtech.dao.NetworkUitlity.java

public static AbstractHttpEntity initEntity(byte[] paramArrayOfByte) {
    ByteArrayEntity localByteArrayEntity = null;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream localGZIPOutputStream;
    if (paramArrayOfByte.length < paramleng) {
        localByteArrayEntity = new ByteArrayEntity(paramArrayOfByte);
    } else {//  w  ww  .  ja  v a2 s.c om

        try {
            localGZIPOutputStream = new GZIPOutputStream(localByteArrayOutputStream);
            localGZIPOutputStream.write(paramArrayOfByte);
            localGZIPOutputStream.close();
            localByteArrayEntity = new ByteArrayEntity(localByteArrayOutputStream.toByteArray());
            localByteArrayEntity.setContentEncoding("gzip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return localByteArrayEntity;
}

From source file:csic.ceab.movelab.beepath.Util.java

public static boolean uploadJSONArray(Context context, JSONArray jsonArray, String uploadurl) {

    String response = "";

    try {/*from www  .j a  va 2s .co  m*/

        // Create a new HttpClient and Post Header
        HttpPatch httppatch = new HttpPatch(uploadurl);

        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 60000);
        HttpConnectionParams.setTcpNoDelay(myParams, true);

        httppatch.setHeader("Content-type", "application/json");

        String auth = "-u john:1234";

        httppatch.setHeader("Authorization", auth);

        HttpClient httpclient = new DefaultHttpClient();

        ByteArrayEntity bae = new ByteArrayEntity(jsonArray.toString().getBytes("UTF8"));

        // StringEntity se = new StringEntity(jsonArray.toString(),
        // HTTP.UTF_8);
        // se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
        // "application/json"));
        bae.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppatch.setEntity(bae);

        // Execute HTTP Post Request

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        response = httpclient.execute(httppatch, responseHandler);

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }

    if (response.contains("SUCCESS")) {
        Log.i("SERVER RESPONSE", response);
        return true;
    } else {
        Log.i("SERVER RESPONSE", response);
        return false;
    }

}

From source file:com.subgraph.vega.internal.model.requests.LazyEntityLoader.java

private HttpEntity createEntityCopy(RequestLogEntity entity) {
    final ByteArrayEntity copy = new ByteArrayEntity(entity.getContentArray());
    copy.setContentType(entity.getContentType());
    copy.setContentEncoding(copy.getContentEncoding());
    return copy;//from   w ww  . jav  a 2 s .  co m
}

From source file:org.dataconservancy.ui.it.support.CreateProjectApiAddRequest.java

public HttpPost asHttpPost() {
    HttpPost post = null;/*from ww  w .j a  v  a  2  s  . co m*/

    try {
        post = new HttpPost(uiUrlConfig.getProjectApiUrl().toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    builder.buildProject(project, sink);
    ByteArrayEntity projectEntity = new ByteArrayEntity(sink.toByteArray());

    projectEntity.setContentEncoding("UTF-8");
    post.addHeader("Content-Type", "text/xml");

    post.setEntity(projectEntity);

    return post;
}

From source file:com.lenovo.h2000.services.LicenseServiceImpl.java

@Override
public String upload(byte[] fileBytes, Map<String, String> headers) throws Exception {
    Map<String, String> params = new HashMap<String, String>();

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(
            HttpConfig.parseBaseUrl(headers) + "license/import" + "?" + TypeUtil.mapToString(params));

    String responseBody = "";
    DataInputStream in = null;//  ww w  .j  a va 2 s  . c  om
    try {

        ByteArrayEntity requestEntity = new ByteArrayEntity(fileBytes);
        requestEntity.setContentEncoding("UTF-8");
        requestEntity.setContentType("application/octet-stream");
        httpPost.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(httpPost);
        response.setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
        HttpEntity responseEntity = response.getEntity();
        responseBody = EntityUtils.toString(responseEntity);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        _logger.error("throwing HttpClientUtil.doPost ClientProtocolException with " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        _logger.error("throwing HttpClientUtil.doPost IOException with " + e.getMessage());
    } finally {
        httpClient.close();
        if (in != null) {
            in.close();
        }
    }
    return responseBody;
}

From source file:com.lenovo.h2000.services.LicenseServiceImpl.java

@Override
public String upload(String filePath, Map<String, String> headers) throws Exception {
    _logger.info("calling LicenseServiceImpl.upload function");
    Map<String, String> params = new HashMap<String, String>();

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(
            HttpConfig.parseBaseUrl(headers) + "license/import" + "?" + TypeUtil.mapToString(params));// new HttpPost("http://localhost:3002/license/import");

    String responseBody = "";
    DataInputStream in = null;//from  w ww .  j a  v  a  2s. com
    try {
        _logger.info("filePath" + filePath);
        File file = new File(filePath);
        in = new DataInputStream(new FileInputStream(filePath));
        byte[] bufferOut = new byte[(int) file.length()];

        int bytes = 0;
        int i = 0;
        int len = (int) (1024 > file.length() ? file.length() : 1024);
        while ((bytes = in.read(bufferOut, i, len)) > 0) {
            if (bytes < 1024)
                break;
            else {
                len = (int) (file.length() - bytes);
                i += bytes;
            }
        }

        ByteArrayEntity requestEntity = new ByteArrayEntity(bufferOut);
        requestEntity.setContentEncoding("UTF-8");
        requestEntity.setContentType("application/octet-stream");
        httpPost.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(httpPost);
        response.setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8");
        HttpEntity responseEntity = response.getEntity();
        responseBody = EntityUtils.toString(responseEntity);

        if (file.isFile() && file.exists()) {
            file.delete();
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
        _logger.error("throwing HttpClientUtil.doPost ClientProtocolException with " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        _logger.error("throwing HttpClientUtil.doPost IOException with " + e.getMessage());
    } finally {
        httpClient.close();
        if (in != null) {
            in.close();
        }
    }
    return responseBody;
}

From source file:com.nextdoor.bender.ipc.http.HttpTransport.java

protected HttpResponse sendBatchCompressed(HttpPost httpPost, byte[] raw) throws TransportException {
    /*/*from w ww.j a  va 2 s.  c  o  m*/
     * 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;
}