Example usage for org.apache.http.entity BasicHttpEntity setChunked

List of usage examples for org.apache.http.entity BasicHttpEntity setChunked

Introduction

In this page you can find the example usage for org.apache.http.entity BasicHttpEntity setChunked.

Prototype

public void setChunked(boolean z) 

Source Link

Usage

From source file:com.aerofs.baseline.http.TestHttpRequestHandler.java

private static HttpPost newChunkedPost(String resource, byte[] bytes) {
    BasicHttpEntity basic = new BasicHttpEntity();
    basic.setChunked(true);
    basic.setContentLength(-1);// ww  w. ja v  a2s  .c  o m
    basic.setContent(new ByteArrayInputStream(bytes));

    HttpPost post = new HttpPost(ServiceConfiguration.SERVICE_URL + "/" + resource);
    post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
    post.setEntity(basic);
    return post;
}

From source file:org.apache.axis2.transport.http.server.AxisHttpResponseImpl.java

public void commit() throws IOException, HttpException {
    if (this.commited) {
        return;//from   w w w  .  j  ava2 s .c  o m
    }
    this.commited = true;

    this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, this.conn);
    this.context.setAttribute(ExecutionContext.HTTP_RESPONSE, this.response);

    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setChunked(true);
    entity.setContentType(this.contentType);

    this.response.setEntity(entity);

    this.httpproc.process(this.response, this.context);
    this.conn.sendResponse(this.response);
}

From source file:com.aerofs.baseline.http.TestHttpRequestHandler.java

@Test
public void shouldSuccessfullyPostAndReceiveResponseAfterMakingUnsuccessfulPost() throws Exception {
    // unsuccessful
    // how does this test work, you ask?
    // well, there's only one request processing
    // thread on the server so if that thread locks up waiting
    // for bytes that never come, even if the stream is closed
    // then the *second* request will time out.
    try {//from ww w .j  ava2  s.  c  om
        HttpPost post0 = new HttpPost(
                ServiceConfiguration.SERVICE_URL + "/" + Resources.BASIC_RESOURCE + "/data1");
        post0.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
        BasicHttpEntity basic = new BasicHttpEntity();
        basic.setChunked(true);
        basic.setContentLength(-1);
        basic.setContent(new InputStream() {

            private int counter = 0;

            @Override
            public int read() throws IOException {
                if (counter < (3 * 1024 * 1024)) {
                    counter++;
                    return 'a';
                } else {
                    throw new IOException("read failed");
                }
            }
        });
        post0.setEntity(basic);

        Future<HttpResponse> future0 = client.getClient().execute(post0, null);
        future0.get();
    } catch (Exception e) {
        // noop
    }

    // successful
    HttpPost post = new HttpPost(ServiceConfiguration.SERVICE_URL + "/" + Resources.BASIC_RESOURCE + "/data1");
    post.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
    post.setEntity(HttpUtils.writeStringToEntity("data2"));

    Future<HttpResponse> future = client.getClient().execute(post, null);
    HttpResponse response = future.get(10, TimeUnit.SECONDS);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_OK));
    assertThat(HttpUtils.readStreamToString(response.getEntity().getContent()), equalTo("data1-data2"));
}

From source file:org.jenkinsci.plugins.relution_publisher.net.requests.ZeroCopyFileRequestProducer.java

@Override
public HttpRequest generateRequest() throws IOException, HttpException {
    final BasicHttpEntity entity = new BasicHttpEntity();

    entity.setContentLength(this.getContentLength());
    entity.setContentType(this.getContentType());
    entity.setChunked(false);

    return this.createRequest(this.mRequest.getUri(), entity);
}

From source file:org.mycard.net.network.AndroidHttpClientConnection.java

/***
 * Return the next response entity./*from  ww w .ja  v  a2s  .  c o m*/
 * @param headers contains values for parsing entity
 * @see HttpClientConnection#receiveResponseEntity(HttpResponse response)
 */
public HttpEntity receiveResponseEntity(final Headers headers) {
    assertOpen();
    BasicHttpEntity entity = new BasicHttpEntity();

    long len = determineLength(headers);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, len));
    }

    String contentTypeHeader = headers.getContentType();
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    String contentEncodingHeader = headers.getContentEncoding();
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }

    return entity;
}

From source file:org.apache.synapse.transport.nhttp.Axis2HttpRequest.java

/**
 * Create and return a new HttpPost request to the destination EPR
 *
 * @return the HttpRequest to be sent out
 * @throws IOException in error retrieving the <code>HttpRequest</code>
 *///from   w  ww  .  ja  v  a2 s.  c  o m
public HttpRequest getRequest() throws IOException, HttpException {
    String httpMethod = (String) msgContext.getProperty(Constants.Configuration.HTTP_METHOD);
    if (httpMethod == null) {
        httpMethod = "POST";
    }
    endpointURLPrefix = (String) msgContext.getProperty(NhttpConstants.ENDPOINT_PREFIX);

    boolean forceHTTP10 = msgContext.isPropertyTrue(NhttpConstants.FORCE_HTTP_1_0);
    HttpVersion httpver = forceHTTP10 ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;

    HttpRequest httpRequest;

    try {
        if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {

            URI uri = rewriteRequestURI(new URI(epr.getAddress()));
            BasicHttpEntityEnclosingRequest requestWithEntity = new BasicHttpEntityEnclosingRequest(httpMethod,
                    uri.toASCIIString(), httpver);

            BasicHttpEntity entity = new BasicHttpEntity();

            if (forceHTTP10) {
                setStreamAsTempData(entity);
            } else {
                entity.setChunked(chunked);
                if (!chunked) {
                    setStreamAsTempData(entity);
                }
            }
            requestWithEntity.setEntity(entity);
            requestWithEntity.setHeader(HTTP.CONTENT_TYPE,
                    messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction()));
            httpRequest = requestWithEntity;

        } else if ("GET".equals(httpMethod) || "DELETE".equals(httpMethod)) {

            URL url = messageFormatter.getTargetAddress(msgContext, format, new URL(epr.getAddress()));

            URI uri = rewriteRequestURI(url.toURI());
            httpRequest = new BasicHttpRequest(httpMethod, uri.toASCIIString(), httpver);
            /*GETs and DELETEs do not need Content-Type headers because they do not have payloads*/
            //httpRequest.setHeader(HTTP.CONTENT_TYPE, messageFormatter.getContentType(
            //        msgContext, format, msgContext.getSoapAction()));

        } else {
            URI uri = rewriteRequestURI(new URI(epr.getAddress()));
            httpRequest = new BasicHttpRequest(httpMethod, uri.toASCIIString(), httpver);
        }

    } catch (URISyntaxException ex) {
        throw new HttpException(ex.getMessage(), ex);
    }

    // set any transport headers
    Object o = msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
    if (o != null && o instanceof Map) {
        Map headers = (Map) o;
        for (Object header : headers.keySet()) {
            Object value = headers.get(header);
            if (header instanceof String && value != null && value instanceof String) {
                if (!HTTPConstants.HEADER_HOST.equalsIgnoreCase((String) header)) {
                    httpRequest.setHeader((String) header, (String) value);

                    String excessProp = NhttpConstants.EXCESS_TRANSPORT_HEADERS;

                    Map map = (Map) msgContext.getProperty(excessProp);
                    if (map != null && map.get(header) != null) {
                        log.debug("Number of excess values for " + header + " header is : "
                                + ((Collection) (map.get(header))).size());

                        for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
                            String key = (String) iterator.next();

                            for (String excessVal : (Collection<String>) map.get(key)) {
                                httpRequest.addHeader((String) header, (String) excessVal);
                            }

                        }
                    }

                } else {
                    if (msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER) != null) {
                        httpRequest.setHeader((String) header,
                                (String) msgContext.getProperty(NhttpConstants.REQUEST_HOST_HEADER));
                    }
                }

            }
        }
    }

    // if the message is SOAP 11 (for which a SOAPAction is *required*), and
    // the msg context has a SOAPAction or a WSA-Action (give pref to SOAPAction)
    // use that over any transport header that may be available
    String soapAction = msgContext.getSoapAction();
    if (soapAction == null) {
        soapAction = msgContext.getWSAAction();
    }
    if (soapAction == null) {
        msgContext.getAxisOperation().getInputAction();
    }

    if (msgContext.isSOAP11() && soapAction != null && soapAction.length() >= 0) {
        Header existingHeader = httpRequest.getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION);
        if (existingHeader != null) {
            httpRequest.removeHeader(existingHeader);
        }
        httpRequest.setHeader(HTTPConstants.HEADER_SOAP_ACTION,
                messageFormatter.formatSOAPAction(msgContext, null, soapAction));
    }

    if (NHttpConfiguration.getInstance().isKeepAliveDisabled()
            || msgContext.isPropertyTrue(NhttpConstants.NO_KEEPALIVE)) {
        httpRequest.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
    }

    return httpRequest;
}

From source file:org.restcomm.camelgateway.slee.CamelGatewaySbb.java

private void pushContent(HttpUriRequest request, String contentType, String contentEncoding, byte[] content) {

    // TODO: check other preconditions?
    if (contentType != null && content != null && request instanceof HttpEntityEnclosingRequest) {
        BasicHttpEntity entity = new BasicHttpEntity();
        entity.setContent(new ByteArrayInputStream(content));
        entity.setContentLength(content.length);
        entity.setChunked(false);
        if (contentEncoding != null)
            entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
        HttpEntityEnclosingRequest rr = (HttpEntityEnclosingRequest) request;
        rr.setEntity(entity);/*from  ww w  .j a v a2s .  c om*/
    }

}