Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

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

Prototype

public ByteArrayEntity(byte[] bArr) 

Source Link

Usage

From source file:neal.http.impl.httpstack.HttpClientStack.java

private static void setEntityIfNonEmptyBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request)
        throws HttpErrorCollection.AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        HttpEntity entity = new ByteArrayEntity(body);
        httpRequest.setEntity(entity);//from   w  ww. j av  a  2  s .  c  om
    }
}

From source file:com.favalike.http.GAEClientConnection.java

@Override
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException {
    if (this.response == null) {
        throw new IOException("receiveResponseEntity() called on closed connection");
    }/*from  w  w w.j a v  a  2  s . c  o  m*/

    ByteArrayEntity bae = new ByteArrayEntity(this.response.getContent());
    bae.setContentType(response.getFirstHeader("Content-Type"));
    response.setEntity(bae);

    response = null;
}

From source file:com.android.providers.downloads.ui.network.ExtHttpClientStack.java

/**
 * Creates the appropriate subclass of HttpUriRequest for passed in request.
 *//*from   w w w  . ja  v  a2  s  .  c o m*/
@SuppressWarnings("deprecation")
/* protected */static HttpUriRequest createHttpRequest(Request<?> request,
        Map<String, String> additionalHeaders) throws AuthFailureError {
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST: {
        // This is the deprecated way that needs to be handled for
        // backwards compatibility.
        // If the request's post body is null, then the assumption is
        // that the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            HttpPost postRequest = new HttpPost(request.getUrl());
            postRequest.addHeader(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            HttpEntity entity;
            entity = new ByteArrayEntity(postBody);
            postRequest.setEntity(entity);
            return postRequest;
        } else {
            return new HttpGet(request.getUrl());
        }
    }
    case Method.GET:
        return new HttpGet(request.getUrl());
    case Method.DELETE:
        return new HttpDelete(request.getUrl());
    case Method.POST: {
        HttpPost postRequest = new HttpPost(request.getUrl());
        postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(postRequest, request);
        return postRequest;
    }
    case Method.PUT: {
        HttpPut putRequest = new HttpPut(request.getUrl());
        putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
        setEntityIfNonEmptyBody(putRequest, request);
        return putRequest;
    }
    default:
        throw new IllegalStateException("Unknown request method.");
    }
}

From source file:groovyx.net.http.thirdparty.GAEClientConnection.java

public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException {
    if (this.response == null) {
        throw new IOException("receiveResponseEntity() called on closed connection");
    }/*  w w w.j  av  a 2  s. c o m*/

    ByteArrayEntity bae = new ByteArrayEntity(this.response.getContent());
    bae.setContentType(response.getFirstHeader("Content-Type"));
    response.setEntity(bae);

    response = null;
}

From source file:org.n52.sos.service.it.SosV1IT.java

/**
 * Send a V1 GetCapabilities request via POST and verify the response
 * //from  w w w  . j  av a  2 s. c o m
 * @throws URISyntaxException
 * @throws IOException
 * @throws IllegalStateException
 * @throws XmlException
 * @throws SAXException
 */
@Ignore
@Test
public void getCapabilitiesV1Post()
        throws URISyntaxException, IOException, IllegalStateException, XmlException, SAXException {
    GetCapabilitiesDocument xb_getCapDoc = GetCapabilitiesDocument.Factory.newInstance();
    GetCapabilities xb_getCap = xb_getCapDoc.addNewGetCapabilities();
    xb_getCap.setService(SosConstants.SOS);
    xb_getCap.addNewAcceptVersions().addVersion(Sos1Constants.SERVICEVERSION);
    HttpPost httppost = new HttpPost(getSOSURI());
    byte[] xmlBytes = IOUtils.toByteArray(xb_getCapDoc.newInputStream());
    httppost.setEntity(new ByteArrayEntity(xmlBytes));
    verifyCapabilitiesV1(httppost);
}

From source file:com.github.wnameless.spring.bulkapi.test.BulkApiTest.java

@Test
public void testOverLimitationError() throws Exception {
    HttpEntity entity = new ByteArrayEntity(mapper.writeValueAsString(operationTimes(1001)).getBytes("UTF-8"));
    post.setEntity(entity);/* w w  w .j  av  a2 s  . c om*/
    HttpResponse response = client.execute(post);

    assertEquals(413, response.getStatusLine().getStatusCode());
}

From source file:org.apache.commons.jcs.auxiliary.remote.http.client.RemoteHttpCacheDispatcher.java

/**
 * Process single request//from  ww w. ja v a2s.c om
 *
 * @param requestAsByteArray request body
 * @param remoteCacheRequest the cache request
 * @param url target url
 *
 * @return byte[] - the response
 *
 * @throws IOException
 * @throws HttpException
 */
protected <K, V> byte[] processRequest(byte[] requestAsByteArray, RemoteCacheRequest<K, V> remoteCacheRequest,
        String url) throws IOException, HttpException {
    RequestBuilder builder = RequestBuilder.post(url).setCharset(DEFAULT_ENCODING);

    if (getRemoteHttpCacheAttributes().isIncludeCacheNameAsParameter()
            && remoteCacheRequest.getCacheName() != null) {
        builder.addParameter(PARAMETER_CACHE_NAME, remoteCacheRequest.getCacheName());
    }
    if (getRemoteHttpCacheAttributes().isIncludeKeysAndPatternsAsParameter()) {
        String keyValue = "";
        switch (remoteCacheRequest.getRequestType()) {
        case GET:
        case REMOVE:
        case GET_KEYSET:
            keyValue = remoteCacheRequest.getKey().toString();
            break;
        case GET_MATCHING:
            keyValue = remoteCacheRequest.getPattern();
            break;
        case GET_MULTIPLE:
            keyValue = remoteCacheRequest.getKeySet().toString();
            break;
        case UPDATE:
            keyValue = remoteCacheRequest.getCacheElement().getKey().toString();
            break;
        default:
            break;
        }
        builder.addParameter(PARAMETER_KEY, keyValue);
    }
    if (getRemoteHttpCacheAttributes().isIncludeRequestTypeasAsParameter()) {
        builder.addParameter(PARAMETER_REQUEST_TYPE, remoteCacheRequest.getRequestType().toString());
    }

    builder.setEntity(new ByteArrayEntity(requestAsByteArray));
    HttpResponse httpResponse = doWebserviceCall(builder);
    byte[] response = EntityUtils.toByteArray(httpResponse.getEntity());
    return response;
}

From source file:com.ninehcom.userinfo.util.HttpUtils.java

/**
 * Put stream//from   w w w . j  a  v  a  2s  .co  m
 * @param host
 * @param path
 * @param method
 * @param headers
 * @param querys
 * @param body
 * @return
 * @throws Exception
 */
public static HttpResponse doPut(String host, String path, String method, Map<String, String> headers,
        Map<String, String> querys, byte[] body) throws Exception {
    HttpClient httpClient = wrapClient(host);

    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
        request.addHeader(e.getKey(), e.getValue());
    }

    if (body != null) {
        request.setEntity(new ByteArrayEntity(body));
    }

    return httpClient.execute(request);
}