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.ginstr.android.service.opencellid.upload.data.MeasurementsUploaderService.java

/**
 * uploads measurements data as a batch file
 * @param dbIterator/*from w w w  . j  a v a2  s . c  o m*/
 * @param latLonFormat
 * @param count
 * @param max
 * @return number of uploaded measurements
 */
private int uploadMeasurementsBatch(MeasurementsDBIterator dbIterator, NumberFormat latLonFormat, int count,
        int max) {
    writeToLog("uploadMeasurementsBatch(" + count + ", " + max + ")");

    try {
        StringBuilder sb = new StringBuilder(
                "lat,lon,mcc,mnc,lac,cellid,signal,measured_at,rating,speed,direction,act\n");

        int thisBatchSize = 0;
        while (thisBatchSize < MEASUREMENTS_BATCH_SIZE && dbIterator.hasNext() && uploadThreadRunning) {
            Measurement meassurement = dbIterator.next();

            sb.append(latLonFormat.format(meassurement.getLat())).append(",");
            sb.append(latLonFormat.format(meassurement.getLon())).append(",");
            sb.append(meassurement.getMcc()).append(",");
            sb.append(meassurement.getMnc()).append(",");
            sb.append(meassurement.getLac()).append(",");
            sb.append(meassurement.getCellid()).append(",");
            sb.append(meassurement.getGsmSignalStrength()).append(",");
            sb.append(meassurement.getTimestamp()).append(",");
            sb.append((meassurement.getAccuracy() != null) ? meassurement.getAccuracy() : "").append(",");
            sb.append((int) meassurement.getSpeed()).append(",");
            sb.append((int) meassurement.getBearing()).append(",");
            sb.append((meassurement.getNetworkType() != null) ? meassurement.getNetworkType() : "");
            sb.append("\n");

            thisBatchSize++;
        }

        HttpResponse response = null;

        writeToLog("Upload request URL: " + httppost.getURI());

        if (uploadThreadRunning) {
            String csv = sb.toString();

            writeToLog("Upload data: " + csv);

            MultipartEntity mpEntity = new MultipartEntity();
            mpEntity.addPart("key", new StringBody(apiKey));
            mpEntity.addPart("appId", new StringBody(appId));
            mpEntity.addPart("datafile", new InputStreamBody(new ByteArrayInputStream(csv.getBytes()),
                    "text/csv", MULTIPART_FILENAME));

            ByteArrayOutputStream bArrOS = new ByteArrayOutputStream();
            // reqEntity is the MultipartEntity instance
            mpEntity.writeTo(bArrOS);
            bArrOS.flush();
            ByteArrayEntity bArrEntity = new ByteArrayEntity(bArrOS.toByteArray());
            bArrOS.close();

            bArrEntity.setChunked(false);
            bArrEntity.setContentEncoding(mpEntity.getContentEncoding());
            bArrEntity.setContentType(mpEntity.getContentType());

            httppost.setEntity(bArrEntity);

            response = httpclient.execute(httppost);
            if (response == null) {
                writeToLog("Upload: null HTTP-response");
                throw new IllegalStateException("no HTTP-response from server");
            }

            HttpEntity resEntity = response.getEntity();

            writeToLog(
                    "Upload: " + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine());

            if (resEntity != null) {
                resEntity.consumeContent();
            }

            if (response.getStatusLine() == null) {
                writeToLog(": " + "null HTTP-status-line");
                throw new IllegalStateException("no HTTP-status returned");
            }

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new IllegalStateException(
                        "HTTP-status code returned : " + response.getStatusLine().getStatusCode());
            }
        }

        return count + thisBatchSize;

    } catch (IOException e) {
        throw new IllegalStateException("IO-Error: " + e.getMessage());
    }
}

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

private void fillMessageHeadersAndContent(HttpMessage msg, InputStream is) throws IOException, HttpException {
    CharArrayBuffer lineBuf = null;/*w  ww.java2s  . c  o  m*/
    long cl = 0;
    Header ct = null;
    Header ce = null;
    while ((lineBuf = readLine(is)).length() > 0) {
        Header h = lineParser.parseHeader(lineBuf);
        String hn = h.getName();
        if (HDR_CONTENT_LENGTH.equalsIgnoreCase(hn)) {
            cl = Long.valueOf(h.getValue());
        } else if (HDR_CONTENT_TYPE.equalsIgnoreCase(hn)) {
            ct = h;
        } else if (HDR_CONTENT_ENCODING.equalsIgnoreCase(hn)) {
            ce = h;
        } else {
            msg.addHeader(h);
        }
    }
    if (cl > 0) {
        byte[] payload = IO.readAndClose(is);
        Check.runtime.assertTrue(cl == payload.length);
        ByteArrayEntity bae = new ByteArrayEntity(payload);
        if (ct != null) {
            bae.setContentType(ct);
        }
        if (ce != null) {
            bae.setContentEncoding(ce);
        }
        setEntity(msg, bae);
    }
}

From source file:com.ginstr.android.service.opencellid.upload.data.MeasurementsUploaderService.java

/**
 * Used to upload entries from networks table to OCID servers.
 *//*from w  w w.  j a  v  a 2  s.  c om*/
private void uploadNetworks() {
    writeToLog("uploadNetworks()");

    String existingFileName = "uploadNetworks.csv";
    String data = null;

    NetworkDBIterator dbIterator = mDatabase.getNonUploadedNetworks();
    try {
        if (dbIterator.getCount() > 0) {
            //timestamp, mcc, mnc, net (network type), nen (network name)
            StringBuilder sb = new StringBuilder("timestamp,mcc,mnc,net,nen" + ((char) 0xA));

            while (dbIterator.hasNext() && uploadThreadRunning) {
                Network network = dbIterator.next();
                sb.append(network.getTimestamp()).append(",");
                sb.append(network.getMcc()).append(",");
                sb.append(network.getMnc()).append(",");
                sb.append(network.getType()).append(",");
                sb.append(network.getName());
                sb.append(((char) 0xA));
            }

            data = sb.toString();

        } else {
            writeToLog("No networks for upload.");
            return;
        }
    } finally {
        dbIterator.close();
    }

    writeToLog("uploadNetworks(): " + data);

    if (uploadThreadRunning) {

        try {
            httppost = new HttpPost(networksUrl);

            HttpResponse response = null;

            writeToLog("Upload request URL: " + httppost.getURI());

            if (uploadThreadRunning) {
                MultipartEntity mpEntity = new MultipartEntity();
                mpEntity.addPart("apikey", new StringBody(apiKey));
                mpEntity.addPart("datafile", new InputStreamBody(new ByteArrayInputStream(data.getBytes()),
                        "text/csv", existingFileName));

                ByteArrayOutputStream bArrOS = new ByteArrayOutputStream();
                // reqEntity is the MultipartEntity instance
                mpEntity.writeTo(bArrOS);
                bArrOS.flush();
                ByteArrayEntity bArrEntity = new ByteArrayEntity(bArrOS.toByteArray());
                bArrOS.close();

                bArrEntity.setChunked(false);
                bArrEntity.setContentEncoding(mpEntity.getContentEncoding());
                bArrEntity.setContentType(mpEntity.getContentType());

                httppost.setEntity(bArrEntity);

                response = httpclient.execute(httppost);
                if (response == null) {
                    writeToLog("Upload: null HTTP-response");
                    throw new IllegalStateException("no HTTP-response from server");
                }

                HttpEntity resEntity = response.getEntity();

                writeToLog("Response: " + response.getStatusLine().getStatusCode() + " - "
                        + response.getStatusLine());

                if (resEntity != null) {
                    writeToLog("Response content: " + EntityUtils.toString(resEntity));
                    resEntity.consumeContent();
                }
            }

            if (uploadThreadRunning) {
                if (response == null) {
                    writeToLog(": " + "null response");

                    throw new IllegalStateException("no response");
                }
                if (response.getStatusLine() == null) {
                    writeToLog(": " + "null HTTP-status-line");

                    throw new IllegalStateException("no HTTP-status returned");
                }
                if (response.getStatusLine().getStatusCode() == 200) {
                    mDatabase.setAllNetworksUploaded();
                } else if (response.getStatusLine().getStatusCode() != 200) {
                    throw new IllegalStateException(
                            response.getStatusLine().getStatusCode() + " HTTP-status returned");
                }
            }

        } catch (Exception e) {
            // httppost cancellation throws exceptions
            if (uploadThreadRunning) {
                writeExceptionToLog(e);
            }
        }
    }
}

From source file:org.esigate.http.HttpClientRequestExecutorTest.java

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();//w w w  .  j av  a 2s . c o m
    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;
}

From source file:org.esigate.DriverTest.java

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();// w w  w.j ava 2 s .c  o m
    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));
}

From source file:org.coronastreet.gpxconverter.StravaForm.java

public void upload() {
    //httpClient = new DefaultHttpClient();
    httpClient = HttpClientBuilder.create().build();
    localContext = new BasicHttpContext();
    cookieStore = new BasicCookieStore();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    //httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

    if (doLogin()) {
        //log("Ok....logged in...");
        try {/*  w  ww.  j a  va 2s  . c o m*/
            // Have to fetch the form to get the CSRF Token
            HttpGet get = new HttpGet(uploadFormURL);
            HttpResponse formResponse = httpClient.execute(get, localContext);
            //log("Fetched the upload form...: " + formResponse.getStatusLine());
            org.jsoup.nodes.Document doc = Jsoup.parse(EntityUtils.toString(formResponse.getEntity()));
            String csrftoken, csrfparam;
            Elements metalinksParam = doc.select("meta[name=csrf-param]");
            if (!metalinksParam.isEmpty()) {
                csrfparam = metalinksParam.first().attr("content");
            } else {
                csrfparam = null;
                log("Missing csrf-param?");
            }
            Elements metalinksToken = doc.select("meta[name=csrf-token]");
            if (!metalinksToken.isEmpty()) {
                csrftoken = metalinksToken.first().attr("content");
            } else {
                csrftoken = null;
                log("Missing csrf-token?");
            }

            HttpPost request = new HttpPost(uploadURL);
            request.setHeader("X-CSRF-Token", csrftoken);

            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            entity.addPart("method", new StringBody("post"));
            entity.addPart("new_uploader", new StringBody("1"));
            entity.addPart(csrfparam, new StringBody(csrftoken));
            entity.addPart("files[]",
                    new InputStreamBody(document2InputStream(outDoc), "application/octet-stream", "temp.tcx"));

            // Need to do this bit because without it you can't disable chunked encoding, and Strava doesn't support chunked.
            ByteArrayOutputStream bArrOS = new ByteArrayOutputStream();
            entity.writeTo(bArrOS);
            bArrOS.flush();
            ByteArrayEntity bArrEntity = new ByteArrayEntity(bArrOS.toByteArray());
            bArrOS.close();

            bArrEntity.setChunked(false);
            bArrEntity.setContentEncoding(entity.getContentEncoding());
            bArrEntity.setContentType(entity.getContentType());

            request.setEntity(bArrEntity);

            HttpResponse response = httpClient.execute(request, localContext);

            if (response.getStatusLine().getStatusCode() != 200) {
                log("Failed to Upload");
                HttpEntity en = response.getEntity();
                if (en != null) {
                    String output = EntityUtils.toString(en);
                    log(output);
                }
            } else {
                HttpEntity ent = response.getEntity();
                if (ent != null) {
                    String output = EntityUtils.toString(ent);
                    //log(output);
                    JSONObject userInfo = new JSONArray(output).getJSONObject(0);
                    //log("Object: " + userInfo.toString());

                    if (userInfo.get("workflow").equals("Error")) {
                        log("Upload Error: " + userInfo.get("error"));
                    } else {
                        log("Successful Uploaded. ID is " + userInfo.get("id"));
                    }
                }
            }
            httpClient.close();
        } catch (Exception ex) {
            log("Exception? " + ex.getMessage());
            ex.printStackTrace();
            // handle exception here
        }
    } else {
        log("Failed to upload!");
    }
}

From source file:org.ohmage.OhmageApi.java

private HttpResponse doHttpPost(String url, HttpEntity requestEntity, boolean gzip) {

    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
    HttpConnectionParams.setSoTimeout(params, 20 * 1000);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpClientParams.setRedirecting(params, false);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

    HttpClient httpClient = new DefaultHttpClient(manager, params);
    HttpPost httpPost = new HttpPost(url);

    if (gzip) {// w w w. j  av a  2  s.  c o  m
        try {
            //InputStream is = requestEntity.getContent();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //BufferedOutputStream zipper = new BufferedOutputStream(new GZIPOutputStream(baos));
            GZIPOutputStream zipper = new GZIPOutputStream(baos);

            requestEntity.writeTo(zipper);

            /* byte [] data2 = new byte[(int)formEntity.getContentLength()]; 
             is.read(data2);
                     
             String testing = new String(data2);
             Log.i("api", testing);
             zipper.write(data2);*/

            /*int bufferSize = 1024;
            byte [] buffer = new byte [bufferSize];
            int len = 0;
            String fullString = "";
            while ((len = is.read(buffer, 0, bufferSize)) != -1) {
            zipper.write(buffer, 0, len);
            //fullString += new String(buffer, 0, len);
            Log.i("api", new String(buffer, 0, len));
            }
            Log.i("api", fullString);
                    
            is.close();*/
            //zipper.flush();
            //zipper.finish();
            zipper.close();

            ByteArrayEntity byteEntity = new ByteArrayEntity(baos.toByteArray());
            //baos.close();
            byteEntity.setContentEncoding("gzip");

            httpPost.setEntity(byteEntity);
        } catch (IOException e) {
            Log.e(TAG, "Unable to gzip entity, using unzipped entity", e);
            httpPost.setEntity(requestEntity);
        }

    } else {
        httpPost.setEntity(requestEntity);
    }

    try {
        return httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException while executing httpPost", e);
        return null;
    } catch (IOException e) {
        Log.e(TAG, "IOException while executing httpPost", e);
        return null;
    }
}