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.couch4j.http.HttpConnectionManager.java

ServerResponse put(final String url, HttpEntity re) {
    HttpEntityEnclosingRequestBase m = new HttpPut(url);
    m.setEntity(re);
    return executeMethod(m);
}

From source file:org.couch4j.http.HttpConnectionManager.java

ServerResponse post(final String url, HttpEntity re) {
    HttpEntityEnclosingRequestBase m = new HttpPost(url);
    m.setEntity(re);
    return executeMethod(m);
}

From source file:org.aerogear.android.impl.core.HttpRestProvider.java

private void addBodyRequest(HttpEntityEnclosingRequestBase requestBase, String data) {
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(data.getBytes()));
    requestBase.setEntity(entity);
}

From source file:com.microsoft.services.odata.unittests.WireMockTestClient.java

private WireMockResponse requestWithBody(HttpEntityEnclosingRequestBase request, String body,
        String contentType, TestHttpHeader... headers) throws UnsupportedEncodingException {
    request.setEntity(new StringEntity(body, ContentType.create(contentType, "utf-8").toString()));
    return executeMethodAndCovertExceptions(request, headers);
}

From source file:it.restrung.rest.client.DefaultRestClientImpl.java

/**
 * Private helper to setup a multipart request body
 *///from  ww  w.  j a va2  s  .c o  m
private static void setupMultipartBodyWithFile(HttpEntityEnclosingRequestBase httpEntityEnclosingRequestBase,
        APIPostParams apiPostParams, String body, File file) throws UnsupportedEncodingException {
    //MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    CountingMultipartEntity mpEntity = new CountingMultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
            Charset.forName("UTF-8"), file != null ? body.length() + file.length() : body.length(),
            apiPostParams);
    mpEntity.addPart("body", new StringBody(body, Charset.forName("UTF-8")));
    if (file != null) {
        FileBody uploadFilePart = new FileBody(file, "application/octet-stream");
        mpEntity.addPart("file", uploadFilePart);
    }
    httpEntityEnclosingRequestBase.setHeader(mpEntity.getContentType());
    httpEntityEnclosingRequestBase.setEntity(mpEntity);
}

From source file:com.github.tomakehurst.wiremock.testsupport.WireMockTestClient.java

private WireMockResponse requestWithBody(HttpEntityEnclosingRequestBase request, String body,
        String contentType, TestHttpHeader... headers) {
    request.setEntity(new StringEntity(body, ContentType.create(contentType, "utf-8")));
    return executeMethodAndConvertExceptions(request, headers);
}

From source file:com.seleritycorp.common.base.http.client.HttpRequest.java

private org.apache.http.HttpResponse getHttpResponse() throws HttpException {
    final HttpRequestBase request;

    switch (method) {
    case HttpGet.METHOD_NAME:
        request = new HttpGet();
        break;/*from ww w .j a v a2  s .  c  o m*/
    case HttpPost.METHOD_NAME:
        request = new HttpPost();
        break;
    default:
        throw new HttpException("Unknown HTTP method '" + method + "'");
    }

    try {
        request.setURI(URI.create(uri));
    } catch (IllegalArgumentException e) {
        throw new HttpException("Failed to create URI '" + uri + "'", e);
    }

    if (userAgent != null) {
        request.setHeader(HTTP.USER_AGENT, userAgent);
    }

    if (readTimeoutMillis >= 0) {
        request.setConfig(RequestConfig.custom().setSocketTimeout(readTimeoutMillis)
                .setConnectTimeout(readTimeoutMillis).setConnectionRequestTimeout(readTimeoutMillis).build());
    }

    if (!data.isEmpty()) {
        if (request instanceof HttpEntityEnclosingRequestBase) {
            final HttpEntity entity;
            ContentType localContentType = contentType;
            if (localContentType == null) {
                localContentType = ContentType.create("text/plain", StandardCharsets.UTF_8);
            }
            entity = new StringEntity(data, localContentType);
            HttpEntityEnclosingRequestBase entityRequest = (HttpEntityEnclosingRequestBase) request;
            entityRequest.setEntity(entity);
        } else {
            throw new HttpException(
                    "Request " + request.getMethod() + " does not allow to send data " + "with the request");
        }
    }

    final HttpClient httpClient;
    if (uri.startsWith("file://")) {
        httpClient = this.fileHttpClient;
    } else {
        httpClient = this.netHttpClient;
    }
    final org.apache.http.HttpResponse response;
    try {
        response = httpClient.execute(request);
    } catch (IOException e) {
        throw new HttpException("Failed to execute request to '" + uri + "'", e);
    }
    return response;
}

From source file:com.meltmedia.cadmium.blackbox.test.ApiRequest.java

private void addPostBody(HttpUriRequest request) throws Exception {
    String postContent = null;//  w  w  w  .  j  a  va  2  s  . co m
    if (postBody != null) {
        if (postBody instanceof String) {
            postContent = (String) postBody;
        } else if (postBody instanceof Map
                && (postContentType == null || !postContentType.equals("application/json"))) {
            Map<?, ?> postBodyMap = (Map<?, ?>) postBody;
            if (!postBodyMap.isEmpty()) {
                postContent = "";
                for (Object key : postBodyMap.keySet()) {
                    Object value = postBodyMap.get(key);
                    if (value instanceof Collection) {
                        Collection<?> values = (Collection<?>) value;
                        for (Object val : values) {
                            if (postContent.length() > 0) {
                                postContent += "&";
                            }
                            postContent += URLEncoder.encode(key.toString(), "UTF-8");
                            postContent += "=" + URLEncoder.encode(val.toString(), "UTF-8");
                        }
                    } else {
                        if (postContent.length() > 0) {
                            postContent += "&";
                        }
                        postContent += URLEncoder.encode(key.toString(), "UTF-8");
                        postContent += "=" + URLEncoder.encode(value.toString(), "UTF-8");
                    }
                }
            }
            if (postContentType == null) {
                postContentType = "application/x-www-form-urlencoded";
            }
        } else {
            postContent = new Gson().toJson(postBody);
        }
        if (postContent != null && request instanceof HttpEntityEnclosingRequestBase) {
            System.out.println("Posting body: " + postContent);
            HttpEntityEnclosingRequestBase entityBasedRequest = (HttpEntityEnclosingRequestBase) request;
            StringEntity entity = new StringEntity(postContent);
            entityBasedRequest.setEntity(entity);
            if (postContentType != null) {
                entity.setContentType(postContentType);
            }
        }
    }
}

From source file:org.apache.olingo.client.core.communication.request.AsyncRequestWrapperImpl.java

protected AsyncRequestWrapperImpl(final ODataClient odataClient, final ODataRequest odataRequest) {
    this.odataRequest = odataRequest;
    this.odataRequest.setAccept(this.odataRequest.getAccept());
    this.odataRequest.setContentType(this.odataRequest.getContentType());

    extendHeader(HttpHeader.PREFER, new ODataPreferences().respondAsync());

    this.odataClient = odataClient;
    final HttpMethod method = odataRequest.getMethod();

    // target uri
    this.uri = odataRequest.getURI();

    HttpClient _httpClient = odataClient.getConfiguration().getHttpClientFactory().create(method, this.uri);
    if (odataClient.getConfiguration().isGzipCompression()) {
        _httpClient = new DecompressingHttpClient(_httpClient);
    }/*from w  ww  .  j ava2s . c  om*/
    this.httpClient = _httpClient;

    this.request = odataClient.getConfiguration().getHttpUriRequestFactory().create(method, this.uri);

    if (request instanceof HttpEntityEnclosingRequestBase) {
        if (odataRequest instanceof AbstractODataBasicRequest) {
            AbstractODataBasicRequest<?> br = (AbstractODataBasicRequest<?>) odataRequest;
            HttpEntityEnclosingRequestBase httpRequest = ((HttpEntityEnclosingRequestBase) request);
            httpRequest.setEntity(new InputStreamEntity(br.getPayload(), -1));
        }
    }
}

From source file:com.github.segator.scaleway.api.ScalewayClient.java

private void setResponseObject(HttpEntityEnclosingRequestBase request, Object responseObject)
        throws ScalewayException {
    try {/*from   w  w w .  jav a 2  s. com*/
        request.setEntity(new StringEntity(Utils.formatJson(responseObject), "UTF-8"));
    } catch (JsonProcessingException ex) {
        throw new ScalewayException(ex);
    }
}