Example usage for org.apache.http.client.methods HttpEntityEnclosingRequestBase setEntity

List of usage examples for org.apache.http.client.methods HttpEntityEnclosingRequestBase setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpEntityEnclosingRequestBase setEntity.

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:org.lightcouch.CouchDbClientBase.java

/**
 * Sets a JSON String as a request entity.
 * @param httpRequest The request to set entity.
 * @param json The JSON String to set.// w ww .  ja  v  a2s.  c o  m
 */
protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String json) {
    try {
        StringEntity entity = new StringEntity(json, "UTF-8");
        entity.setContentType("application/json");
        httpRequest.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
        log.error("Error setting request data. " + e.getMessage());
        throw new IllegalArgumentException(e);
    }
}

From source file:com.glodon.paas.document.api.AbstractDocumentAPITest.java

private void setHttpBody(HttpEntityEnclosingRequestBase requestBase, JSONObject body) {
    if (body != null) {
        try {//  w  w  w .  j av  a2 s .c  o  m
            StringEntity entity = new StringEntity(body.toString());
            entity.setContentType("application/json");
            entity.setContentEncoding("UTF-8");
            requestBase.setEntity(entity);
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
    }
}

From source file:com.LaunchKeyManager.http.AsyncHttpClient.java

/**
 * Perform a HTTP POST request and track the Android Context which initiated
 * the request. Set headers only for this request
 *
 * @param context         the Android Context which initiated the request.
 * @param url             the URL to send the request to.
 * @param headers         set headers only for this request
 * @param params          additional POST parameters to send with the request.
 * @param contentType     the content type of the payload you are sending, for
 *                        example application/json if sending a json payload.
 * @param responseHandler the response handler instance that should handle
 *                        the response./* www. ja  v a 2 s.  c o  m*/
 */
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType,
        AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = new HttpPost(url);
    if (params != null) {
        request.setEntity(paramsToEntity(params));
    }
    if (headers != null) {
        request.setHeaders(headers);
    }
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}

From source file:com.frentix.restapi.RestConnection.java

/**
 * Add an object (application/json)/*  w  ww .  j av  a 2s . c  o  m*/
 * 
 * @param put the PUT request
 * @param obj the JSON
 * @throws UnsupportedEncodingException
 */
public void addJsonEntity(HttpEntityEnclosingRequestBase put, Object obj) throws UnsupportedEncodingException {
    if (obj == null)
        return;

    String objectStr = stringuified(obj);
    HttpEntity myEntity = new StringEntity(objectStr, MediaType.APPLICATION_JSON, "UTF-8");
    put.setEntity(myEntity);
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

/**
 * <p>Executes a PUT request and consumes the entity in the response. The content is cached and be retrieved by calling
 * {@code response.getContent()}</p>
 *
 * <p>Adds the passed entity and headers and checks the expected status</p>
 *
 * @param requestPath path relative to client url
 * @param entity the entity to be added to request
 * @param headers optional url parameters to be added
 * @param expectedStatus if passed, the response status will have to match one of them
 * @return the response with the entity consumed and the content cached
 * @throws ClientException if the request could not be executed
 *//*from w  w w .ja v  a2  s  .co m*/
public SlingHttpResponse doPut(String requestPath, HttpEntity entity, List<Header> headers,
        int... expectedStatus) throws ClientException {
    HttpEntityEnclosingRequestBase request = new HttpPut(getUrl(requestPath));
    if (entity != null) {
        request.setEntity(entity);
    }
    return doRequest(request, headers, expectedStatus);
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

/**
 * <p>Executes a POST request WITHOUT consuming the entity in the response. The caller is responsible to close the connection</p>
 *
 * <p><b>Use this with caution and only if necessary for streaming</b>, otherwise use the safe method
 * {@link #doPost(String, HttpEntity, List, int...)}</p>
 *
 * <p>Adds the headers and checks the response against expected status</p>
 * @param requestPath path relative to client url
 * @param entity http entity to be sent by POST
 * @param headers optional headers to be added
 * @param expectedStatus if passed, the response status will have to match one of them
 * @return the response with the entity not consumed
 * @throws ClientException if the request could not be executed
 *///w w  w . j  a  va 2s .  com
public SlingHttpResponse doStreamPost(String requestPath, HttpEntity entity, List<Header> headers,
        int... expectedStatus) throws ClientException {
    HttpEntityEnclosingRequestBase request = new HttpPost(getUrl(requestPath));
    if (entity != null) {
        request.setEntity(entity);
    }
    return doStreamRequest(request, headers, expectedStatus);
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

/**
 * <p>Executes a POST request and consumes the entity in the response. The content is cached and be retrieved by calling
 * {@code response.getContent()}</p>
 *
 * <p>Adds the passed entity and headers and checks the expected status</p>
 *
 * @param requestPath path relative to client url
 * @param entity the entity to be added to request
 * @param headers optional headers to be added
 * @param expectedStatus if passed, the response status will have to match one of them
 * @return the response with the entity consumed and the content cached
 * @throws ClientException if the request could not be executed
 *//*from  w w w . j a  v a2  s  .  c om*/
public SlingHttpResponse doPost(String requestPath, HttpEntity entity, List<Header> headers,
        int... expectedStatus) throws ClientException {
    HttpEntityEnclosingRequestBase request = new HttpPost(getUrl(requestPath));
    if (entity != null) {
        request.setEntity(entity);
    }
    return doRequest(request, headers, expectedStatus);
}

From source file:org.apache.sling.testing.clients.AbstractSlingClient.java

/**
 * <p>Executes a PATCH request and consumes the entity in the response. The content is cached and be retrieved by calling
 * {@code response.getContent()}</p>
 *
 * <p>Adds the passed entity and headers and checks the expected status</p>
 *
 * @param requestPath path relative to client url
 * @param entity the entity to be added to request
 * @param headers optional url parameters to be added
 * @param expectedStatus if passed, the response status will have to match one of them
 * @return the response with the entity consumed and the content cached
 * @throws ClientException if the request could not be executed
 *///from   w w w .  j  a  v  a  2  s  . co m
public SlingHttpResponse doPatch(String requestPath, HttpEntity entity, List<Header> headers,
        int... expectedStatus) throws ClientException {
    HttpEntityEnclosingRequestBase request = new HttpPatch(getUrl(requestPath));
    if (entity != null) {
        request.setEntity(entity);
    }
    return doRequest(request, headers, expectedStatus);
}

From source file:org.expath.httpclient.impl.ApacheHttpConnection.java

/**
 * Configure the request to get its entity body from the {@link HttpRequestBody}.
 *///from w ww . j  a v  a2s.c om
private void setRequestEntity(HttpRequestBody body) throws HttpClientException {
    if (body == null) {
        return;
    }
    // make the entity from a new producer
    HttpEntity entity;
    if (myVersion == HttpVersion.HTTP_1_1) {
        // Take advantage of HTTP 1.1 chunked encoding to stream the
        // payload directly to the request.
        ContentProducer producer = new RequestBodyProducer(body);
        EntityTemplate template = new EntityTemplate(producer);
        template.setContentType(body.getContentType());
        entity = template;
    } else {
        // With HTTP 1.0, chunked encoding is not supported, so first
        // serialize into memory and use the resulting byte array as the
        // entity payload.
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        body.serialize(buffer);
        entity = new ByteArrayEntity(buffer.toByteArray());
    }
    // cast the request
    HttpEntityEnclosingRequestBase req = null;
    if (!(myRequest instanceof HttpEntityEnclosingRequestBase)) {
        String msg = "Body not allowed on a " + myRequest.getMethod() + " request";
        throw new HttpClientException(msg);
    } else {
        req = (HttpEntityEnclosingRequestBase) myRequest;
    }
    // set the entity on the request
    req.setEntity(entity);
}