Package org.apache.http.entity

Examples of org.apache.http.entity.ByteArrayEntity


      byte[] data = baos.toByteArray();

      log.debug("POSTing " + new String(data));

      // TODO: Stream body? We'd just need a custom ByteArrayEntity class
      request.setEntity(new ByteArrayEntity(data));

      response = httpClient.execute(request);

      StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() != 200) {
View Full Code Here


     * Posts given check-in request content and returns
     * {@link AndroidCheckinResponse}.
     */
    private AndroidCheckinResponse postCheckin(byte[] request) throws IOException {

  HttpEntity httpEntity = executePost(CHECKIN_URL, new ByteArrayEntity(request), new String[][] {
    { "User-Agent", "Android-Checkin/2.0 (generic JRO03E); gzip" }, { "Host", "android.clients.google.com" },
    { "Content-Type", "application/x-protobuffer" } });
  return AndroidCheckinResponse.parseFrom(httpEntity.getContent());
    }
View Full Code Here

     * Executes POST request and returns result as {@link ResponseWrapper}.
     * Content type can be specified for given byte array.
     */
    private ResponseWrapper executePOSTRequest(String url, byte[] datapost, String contentType) throws IOException {

  HttpEntity httpEntity = executePost(url, new ByteArrayEntity(datapost), getHeaderParameters(this.getToken(), contentType));
  return GooglePlay.ResponseWrapper.parseFrom(httpEntity.getContent());

    }
View Full Code Here

        }
       
        InputStream input = null;
       
        byte[] entityBytes = entityByteOut.toByteArray();
        HttpEntity entity = new ByteArrayEntity(entityBytes);
        post.setEntity(entity);
       
        try {
            if (config.getNtlmDomain() != null && !config.getNtlmDomain().equals("")) {
                // need to send a HEAD request to trigger NTLM authentication
View Full Code Here

    }

    private HttpEntity makeBody(int nbytes) {
        byte[] bytes = new byte[nbytes];
        (new Random()).nextBytes(bytes);
        return new ByteArrayEntity(bytes);
    }
View Full Code Here

    public void consumesBodyOf100ContinueResponseIfItArrives() throws Exception {
        HttpEntityEnclosingRequest req = new BasicHttpEntityEnclosingRequest("POST", "/", HttpVersion.HTTP_1_1);
        int nbytes = 128;
        req.setHeader("Content-Length","" + nbytes);
        req.setHeader("Content-Type", "application/octet-stream");
        HttpEntity postBody = new ByteArrayEntity(HttpTestUtils.getRandomBytes(nbytes));
        req.setEntity(postBody);
       
        HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_CONTINUE, "Continue");
        final Flag closed = new Flag();
        ByteArrayInputStream bais = makeTrackableBody(nbytes, closed);
View Full Code Here

                        // serialized java object
                        Serializable obj = in.getMandatoryBody(Serializable.class);
                        // write object to output stream
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        HttpHelper.writeObjectToStream(bos, obj);
                        ByteArrayEntity entity = new ByteArrayEntity(bos.toByteArray());
                        entity.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                        IOHelper.close(bos);
                        answer = entity;
                    } else if (data instanceof File || data instanceof GenericFile) {
                        // file based (could potentially also be a FTP file etc)
                        File file = in.getBody(File.class);
                        if (file != null) {
                            if (contentType != null) {
                                answer = new FileEntity(file, contentType);
                            } else {
                                answer = new FileEntity(file);
                            }
                        }
                    } else if (data instanceof String) {
                        // be a bit careful with String as any type can most likely be converted to String
                        // so we only do an instanceof check and accept String if the body is really a String
                        // do not fallback to use the default charset as it can influence the request
                        // (for example application/x-www-form-urlencoded forms being sent)
                        String charset = IOHelper.getCharsetName(exchange, false);
                        if (charset == null && contentType != null) {
                            // okay try to get the charset from the content-type
                            Charset cs = contentType.getCharset();
                            if (cs != null) {
                                charset = cs.name();
                            }
                        }
                        StringEntity entity = new StringEntity((String) data, charset);
                        if (contentType != null) {
                            entity.setContentType(contentType.toString());
                        }
                        answer = entity;
                    }

                    // fallback as input stream
                    if (answer == null) {
                        // force the body as an input stream since this is the fallback
                        InputStream is = in.getMandatoryBody(InputStream.class);
                        InputStreamEntity entity = new InputStreamEntity(is, -1);
                        if (contentType != null) {
                            entity.setContentType(contentType.toString());
                        }
                        answer = entity;
                    }
                }
            } catch (UnsupportedEncodingException e) {
View Full Code Here

        resp1.setHeader("ETag", "\"etag1\"");
        final byte[] bytes1 = new byte[128];
        for (int i = 0; i < bytes1.length; i++) {
            bytes1[i] = (byte) 1;
        }
        resp1.setEntity(new ByteArrayEntity(bytes1));

        final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
                new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
        req2.setHeader("Cache-Control", "no-cache");
        req2.setHeader("Range", "bytes=0-50");

        final Date inOneSecond = new Date(now.getTime() + 1000L);
        final HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT,
                "Partial Content");
        resp2.setHeader("Date", DateUtils.formatDate(inOneSecond));
        resp2.setHeader("Server", resp1.getFirstHeader("Server").getValue());
        resp2.setHeader("ETag", "\"etag2\"");
        resp2.setHeader("Content-Range", "bytes 0-50/128");
        final byte[] bytes2 = new byte[51];
        for (int i = 0; i < bytes2.length; i++) {
            bytes2[i] = (byte) 2;
        }
        resp2.setEntity(new ByteArrayEntity(bytes2));

        final Date inTwoSeconds = new Date(now.getTime() + 2000L);
        final HttpRequestWrapper req3 = HttpRequestWrapper.wrap(
                new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
        final HttpResponse resp3 = HttpTestUtils.make200Response();
        resp3.setHeader("Date", DateUtils.formatDate(inTwoSeconds));
        resp3.setHeader("Cache-Control", "max-age=3600");
        resp3.setHeader("ETag", "\"etag2\"");
        final byte[] bytes3 = new byte[128];
        for (int i = 0; i < bytes3.length; i++) {
            bytes3[i] = (byte) 2;
        }
        resp3.setEntity(new ByteArrayEntity(bytes3));

        EasyMock.expect(
                mockBackend.execute(
                        EasyMock.isA(HttpRoute.class),
                        EasyMock.isA(HttpRequestWrapper.class),
View Full Code Here

        resp1.setHeader("Last-Modified", DateUtils.formatDate(oneHourAgo));
        final byte[] bytes1 = new byte[128];
        for (int i = 0; i < bytes1.length; i++) {
            bytes1[i] = (byte) 1;
        }
        resp1.setEntity(new ByteArrayEntity(bytes1));

        final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
                new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
        req2.setHeader("Cache-Control", "no-cache");
        req2.setHeader("Range", "bytes=0-50");

        final Date inOneSecond = new Date(now.getTime() + 1000L);
        final HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT,
                "Partial Content");
        resp2.setHeader("Date", DateUtils.formatDate(inOneSecond));
        resp2.setHeader("Server", resp1.getFirstHeader("Server").getValue());
        resp2.setHeader("Last-Modified", DateUtils.formatDate(now));
        resp2.setHeader("Content-Range", "bytes 0-50/128");
        final byte[] bytes2 = new byte[51];
        for (int i = 0; i < bytes2.length; i++) {
            bytes2[i] = (byte) 2;
        }
        resp2.setEntity(new ByteArrayEntity(bytes2));

        final Date inTwoSeconds = new Date(now.getTime() + 2000L);
        final HttpRequestWrapper req3 = HttpRequestWrapper.wrap(
                new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
        final HttpResponse resp3 = HttpTestUtils.make200Response();
        resp3.setHeader("Date", DateUtils.formatDate(inTwoSeconds));
        resp3.setHeader("Cache-Control", "max-age=3600");
        resp3.setHeader("ETag", "\"etag2\"");
        final byte[] bytes3 = new byte[128];
        for (int i = 0; i < bytes3.length; i++) {
            bytes3[i] = (byte) 2;
        }
        resp3.setEntity(new ByteArrayEntity(bytes3));

        EasyMock.expect(
                mockBackend.execute(
                        EasyMock.isA(HttpRoute.class),
                        EasyMock.isA(HttpRequestWrapper.class),
View Full Code Here

                    "Partial Content"));
            originResponse.setHeader("Content-Range", "bytes 0-50/128");
            originResponse.setHeader("Cache-Control", "max-age=3600");
            final byte[] bytes = new byte[51];
            new Random().nextBytes(bytes);
            originResponse.setEntity(new ByteArrayEntity(bytes));

            EasyMock.expect(
                    mockBackend.execute(
                            EasyMock.isA(HttpRoute.class),
                            EasyMock.isA(HttpRequestWrapper.class),
View Full Code Here

TOP

Related Classes of org.apache.http.entity.ByteArrayEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.