Search in sources :

Example 16 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequest method executeInternal.

@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
    return new HttpComponentsClientHttpResponse(httpResponse);
}
Also used : HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse)

Example 17 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project RoboZombie by sahan.

the class Entities method resolve.

/**
	  * <p>Discovers which implementation of {@link HttpEntity} is suitable for wrapping the given object. 
	  * This discovery proceeds in the following order by checking the runtime-type of the object:</p> 
	  *
	  * <ol>
	  * 	<li>org.apache.http.{@link HttpEntity} --&gt; returned as-is.</li> 
	  * 	<li>{@code byte[]}, {@link Byte}[] --&gt; {@link ByteArrayEntity}</li> 
	  *  	<li>java.io.{@link File} --&gt; {@link FileEntity}</li>
	  * 	<li>java.io.{@link InputStream} --&gt; {@link BufferedHttpEntity}</li>
	  * 	<li>{@link CharSequence} --&gt; {@link StringEntity}</li>
	  * 	<li>java.io.{@link Serializable} --&gt; {@link SerializableEntity} (with an internal buffer)</li>
	  * </ol>
	  *
	  * @param genericEntity
	  * 			a generic reference to an object whose concrete {@link HttpEntity} is to be resolved 
	  * <br><br>
	  * @return the resolved concrete {@link HttpEntity} implementation
	  * <br><br>
	  * @throws NullPointerException
	  * 			if the supplied generic type was {@code null}
	  * <br><br>
	  * @throws EntityResolutionFailedException
	  * 			if the given generic instance failed to be translated to an {@link HttpEntity} 
	  * <br><br>
	  * @since 1.3.0
	  */
public static final HttpEntity resolve(Object genericEntity) {
    assertNotNull(genericEntity);
    try {
        if (genericEntity instanceof HttpEntity) {
            return (HttpEntity) genericEntity;
        } else if (byte[].class.isAssignableFrom(genericEntity.getClass())) {
            return new ByteArrayEntity((byte[]) genericEntity);
        } else if (Byte[].class.isAssignableFrom(genericEntity.getClass())) {
            Byte[] wrapperBytes = (Byte[]) genericEntity;
            byte[] primitiveBytes = new byte[wrapperBytes.length];
            for (int i = 0; i < wrapperBytes.length; i++) {
                primitiveBytes[i] = wrapperBytes[i].byteValue();
            }
            return new ByteArrayEntity(primitiveBytes);
        } else if (genericEntity instanceof File) {
            return new FileEntity((File) genericEntity, null);
        } else if (genericEntity instanceof InputStream) {
            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent((InputStream) genericEntity);
            return new BufferedHttpEntity(basicHttpEntity);
        } else if (genericEntity instanceof CharSequence) {
            return new StringEntity(((CharSequence) genericEntity).toString());
        } else if (genericEntity instanceof Serializable) {
            return new SerializableEntity((Serializable) genericEntity, true);
        } else {
            throw new EntityResolutionFailedException(genericEntity);
        }
    } catch (Exception e) {
        throw (e instanceof EntityResolutionFailedException) ? (EntityResolutionFailedException) e : new EntityResolutionFailedException(genericEntity, e);
    }
}
Also used : FileEntity(org.apache.http.entity.FileEntity) Serializable(java.io.Serializable) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) SerializableEntity(org.apache.http.entity.SerializableEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) File(java.io.File)

Example 18 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project RoboZombie by sahan.

the class RequestParamEndpointTest method testWrapperByteArrayEntity.

/**
	 * <p>Test for a {@link Request} with a {@code Byte}[] entity.</p>
	 * 
	 * @since 1.3.0
	 */
@Test
public final void testWrapperByteArrayEntity() throws ParseException, IOException {
    Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
    String subpath = "/wrapperbytearrayentity";
    Byte[] bytes = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1 };
    ByteArrayEntity bae = new ByteArrayEntity(new byte[] { 1, 1, 1, 1, 1, 1, 1, 1 });
    stubFor(put(urlEqualTo(subpath)).willReturn(aResponse().withStatus(200)));
    requestEndpoint.wrapperByteArrayEntity(bytes);
    verify(putRequestedFor(urlEqualTo(subpath)).withRequestBody(equalTo(EntityUtils.toString(bae))));
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Test(org.junit.Test)

Example 19 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project spring-framework by spring-projects.

the class HttpComponentsHttpInvokerRequestExecutor method setRequestBody.

/**
	 * Set the given serialized remote invocation as request body.
	 * <p>The default implementation simply sets the serialized invocation as the
	 * HttpPost's request body. This can be overridden, for example, to write a
	 * specific encoding and to potentially set appropriate HTTP request headers.
	 * @param config the HTTP invoker configuration that specifies the target service
	 * @param httpPost the HttpPost to set the request body on
	 * @param baos the ByteArrayOutputStream that contains the serialized
	 * RemoteInvocation object
	 * @throws java.io.IOException if thrown by I/O methods
	 */
protected void setRequestBody(HttpInvokerClientConfiguration config, HttpPost httpPost, ByteArrayOutputStream baos) throws IOException {
    ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray());
    entity.setContentType(getContentType());
    httpPost.setEntity(entity);
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Example 20 with ByteArrayEntity

use of org.apache.http.entity.ByteArrayEntity in project baker-android by bakerframework.

the class AndroidHttpClient method getCompressedEntity.

/**
     * Compress data to send to server.
     * Creates a Http Entity holding the gzipped data.
     * The data will not be compressed if it is too short.
     * @param data The bytes to compress
     * @return Entity holding the data
     */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Aggregations

ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)74 HttpEntity (org.apache.http.HttpEntity)25 HttpPost (org.apache.http.client.methods.HttpPost)22 HttpResponse (org.apache.http.HttpResponse)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 JSONObject (org.json.JSONObject)10 Test (org.junit.Test)10 HttpClient (org.apache.http.client.HttpClient)8 JSONArray (org.json.JSONArray)7 InputStream (java.io.InputStream)6 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)5 BytesRef (org.apache.lucene.util.BytesRef)5 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)5 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)5 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)5 GetRequest (org.elasticsearch.action.get.GetRequest)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 WriteRequest (org.elasticsearch.action.support.WriteRequest)5 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)5