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

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

Introduction

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

Prototype

public void setContentEncoding(String str) 

Source Link

Usage

From source file:org.envirocar.app.network.HTTPClient.java

public static HttpEntity createEntity(byte[] data) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < MIN_GZIP_SIZE) {
        entity = new ByteArrayEntity(data);
    } else {//from  ww w.  jav a2 s . com
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:fast.simple.download.http.DownloadHttpClient.java

/**
 * Compress data to send to server. Creates a Http Entity holding the
 * gzipped data. The data will not be compressed if it is too short.
 * /*w  ww .j a v a 2s  . co  m*/
 * @param data
 *            The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = zip(data);
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:br.com.anteros.android.synchronism.communication.AndroidHttpClient.java

/**
 * Compress data to send to server. Creates a Http Entity holding the
 * gzipped data. The data will not be compressed if it is too short.
 * /*from www .  j a  va  2  s.c om*/
 * @param data
 *            The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    ByteArrayOutputStream arr = new ByteArrayOutputStream();
    OutputStream zipper = new GZIPOutputStream(arr);
    zipper.write(data);
    zipper.close();
    entity = new ByteArrayEntity(arr.toByteArray());
    entity.setContentEncoding("gzip");
    return entity;
}

From source file:cn.salesuite.saf.download.AndroidHttpClient.java

/**
 * Compress data to send to server./*from   w w  w  . j a  v a  2 s. c  o m*/
 * Creates a Http Entity holding the gzipped data.
 * The data will not be compressed if it is too short.
 * @param data The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte data[], ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:com.appassit.http.AndroidHttpClient.java

/**
 * Compress data to send to server. Creates a Http Entity holding the gzipped data. The data will not be compressed if it is too short.
 * //from  ww  w. j ava2s.  c  om
 * @param data
 *            The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte data[]) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize()) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:com.appassit.http.AndroidHttpClient.java

/**
 * Compress data to send to server. Creates a Http Entity holding the gzipped data. The data will not be compressed if it is too short.
 * //from  w  ww  .ja v  a2  s  .  c  o  m
 * @param data
 *            The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(InputStream in) throws IOException {
    AbstractHttpEntity entity;
    byte[] buffer = new byte[4096];
    int bytesRead = in.read(buffer);

    if (bytesRead < getMinGzipSize()) {
        byte[] data = new byte[bytesRead];
        System.arraycopy(buffer, 0, data, 0, bytesRead);
        entity = new ByteArrayEntity(data);
        in.close();

    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);

        do {
            zipper.write(buffer, 0, bytesRead);
        } while ((bytesRead = in.read(buffer)) != -1);

        in.close();
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}

From source file:eu.comvantage.dataintegration.SparulSimulationServlet.java

private void simulateEvent(String eventID, String endpoint) {
    //container for the final SPARUL update command including header information
    String body = "";
    //additional body information for the SPARUL update command

    //try to encode the SPARUL update command string with UTF-8
    String temp = "";
    Gson gson = new Gson();

    //simulation of a correct request
    if (eventID.equalsIgnoreCase("sparul1")) {
        temp = "{ " + "\"Template\" : 1, " + "\"Client\" : 1, "
                + "\"Params\" : [{\"name\":\"ticket\", \"value\":\"ex:Ticket0070071239swd\"}, "
                + "{\"name\":\"person\", \"value\":\"ex:nn00110011\"}]" + "}";
    }/*from w  w w.j  av a2s.co m*/
    //simulation of invalid template id for client
    else if (eventID.equalsIgnoreCase("sparul2")) {
        temp = "{ " + "\"Template\" : 8, " + "\"Client\" : 1, "
                + "\"Params\" : [{\"name\":\"ticket\", \"value\":\"ex:Ticket008008123swd\"}, "
                + "{\"name\":\"person\", \"value\":\"ex:nn1234567\"}]" + "}";
    }
    //simulation of invalid client id
    else if (eventID.equalsIgnoreCase("sparul3")) {
        temp = "{ " + "\"Template\" : 1, " + "\"Client\" : 3, "
                + "\"Params\" : [{\"name\":\"ticket\", \"value\":\"ex:Ticket000000000swd\"}, "
                + "{\"name\":\"person\", \"value\":\"ex:nn55555\"}]" + "}";
    }
    //simulation of invalid parameter for specified template
    else if (eventID.equalsIgnoreCase("sparul4")) {
        temp = "{ " + "\"Template\" : 1, " + "\"Client\" : 1, "
                + "\"Params\" : [{\"name\":\"bla\", \"value\":\"ex:Ticket98761234swd\"}, "
                + "{\"name\":\"person\", \"value\":\"ex:nn223344\"}]" + "}";
    }
    //simulation of invalid parameter for specified template
    else if (eventID.equalsIgnoreCase("sparul5")) {
        temp = "{ " + "\"Templates\" : 1, " + "\"Clients\" : 1, "
                + "\"Param\" : [{\"name\":\"bla\", \"value\":\"ex:Ticket98761234swd\"}, "
                + "{\"name\":\"person\", \"value\":\"ex:nn223344\"}]" + "}";
    }
    //malformed json
    else if (eventID.equalsIgnoreCase("sparul6")) {
        temp = "blabla";
    }
    //simulation of a correct request
    else if (eventID.equalsIgnoreCase("sparul7")) {
        temp = "{ " + "\"Template\" : 1, " + "\"Client\" : 1, "
                + "\"Params\" : [{\"name\":\"templateId\", \"value\":\"tee:Ticket0070071239swd\"}], " + "}";
        //test of the long statement parameters of file client1_test0
    } else if (eventID.equalsIgnoreCase("sparul8")) {
        temp = "{ " + "\"Template\" : 1, " + "\"Client\" : 1, "
                + "\"Params\" : [{\"name\":\"templateId\", \"value\":\"tee:test1\"}, "
                + "{\"name\":\"reportId\", \"value\":\"1\"},"
                + "{\"name\":\"device1\", \"value\":\"tee:test2\"},"
                + "{\"name\":\"device2\", \"value\":\"tee:test3\"},"
                + "{\"name\":\"device3\", \"value\":\"tee:test4\"}]" + "}";

    }
    //body = gson.toJson(temp);
    body = temp;

    //try to execute the SPARUL update command
    try {
        //insertion is done by a manual HTTP post
        HttpPost httpPost = new HttpPost(endpoint);

        //put SPARUL update command to output stream
        ByteArrayOutputStream b_out = new ByteArrayOutputStream();
        OutputStreamWriter wr = new OutputStreamWriter(b_out);
        wr.write(body);
        wr.flush();

        //transform output stream and modify header information for HTTP post             
        byte[] bytes = b_out.toByteArray();
        AbstractHttpEntity reqEntity = new ByteArrayEntity(bytes);
        reqEntity.setContentType("application/x-www-form-urlencoded");
        reqEntity.setContentEncoding(HTTP.UTF_8);
        httpPost.setEntity(reqEntity);
        httpPost.setHeader("role", "http://www.comvantage.eu/ontologies/ac-schema/cv_wp6_comau_employee");

        HttpClient httpclient = new DefaultHttpClient();

        //          //set proxy if defined
        //            if(System.getProperty("http.proxyHost") != null) {
        //               HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort")), "http");
        //                httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        //            }

        try {
            //execute the HTTP put
            System.out.println(
                    "SparulSimulationServlet: Event '" + eventID + "' simulated at endpoint " + endpoint);
            HttpResponse response = httpclient.execute(httpPost);

            //handle different server responses and failures
            int responseCode = response.getStatusLine().getStatusCode();
            String responseMessage = response.getStatusLine().getReasonPhrase();
            System.out.println("SparulSimulationServlet: Response = " + responseCode + ", " + responseMessage);
            //close the output stream
            wr.close();
        } catch (IOException ex) {
            throw new Exception(ex);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.nominanuda.web.http.HttpCoreHelper.java

public void setContentEncoding(HttpResponse resp, String contentEncoding) {
    HttpEntity e = resp.getEntity();/*from   w w  w .j  a va2 s .c  om*/
    if (e != null && e instanceof AbstractHttpEntity) {
        AbstractHttpEntity ae = (AbstractHttpEntity) e;
        ae.setContentEncoding(contentEncoding);
    } else {
        resp.setHeader(HDR_CONTENT_ENCODING, contentEncoding);
    }
}