Search in sources :

Example 91 with HttpEntity

use of org.apache.http.HttpEntity in project robolectric by robolectric.

the class ParamsParser method parseParamsForRequestWithEntity.

private static Map<String, String> parseParamsForRequestWithEntity(HttpEntityEnclosingRequestBase request) {
    try {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        HttpEntity entity = request.getEntity();
        if (entity != null) {
            List<NameValuePair> pairs = URLEncodedUtils.parse(entity);
            for (NameValuePair pair : pairs) {
                map.put(pair.getName(), pair.getValue());
            }
        }
        return map;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

Example 92 with HttpEntity

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

the class HttpComponentsAsyncClientHttpRequest method executeInternal.

@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
    HttpComponentsClientHttpRequest.addHeaders(this.httpRequest, headers);
    if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
        HttpEntity requestEntity = new NByteArrayEntity(bufferedOutput);
        entityEnclosingRequest.setEntity(requestEntity);
    }
    HttpResponseFutureCallback callback = new HttpResponseFutureCallback(this.httpRequest);
    Future<HttpResponse> futureResponse = this.httpClient.execute(this.httpRequest, this.httpContext, callback);
    return new ClientHttpResponseFuture(futureResponse, callback);
}
Also used : HttpEntity(org.apache.http.HttpEntity) NByteArrayEntity(org.apache.http.nio.entity.NByteArrayEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse)

Example 93 with HttpEntity

use of org.apache.http.HttpEntity 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 94 with HttpEntity

use of org.apache.http.HttpEntity in project RoboZombie by sahan.

the class EntityProcessor method process.

/**
	 * <p>Accepts the {@link InvocationContext} along with the {@link HttpResponse} and retrieves the 
	 * {@link HttpEntity} form the response. This is then converted to an instance of the required request 
	 * return type by consulting the @{@link Deserialize} metadata on the endpoint definition.</p>
	 * 
	 * <p>If the desired return type is {@link HttpResponse} or {@link HttpEntity} the response or entity 
	 * is simply returned without any further processing.</p>
	 * 
	 * <p><b>Note</b> that this processor returns {@code null} for successful responses with the status 
	 * codes {@code 205} or {@code 205}.</p>
	 * 
	 * @param context
	 * 			the {@link InvocationContext} which is used to discover deserializer metadata 
	 * <br><br>
	 * @param response
	 * 			the {@link HttpResponse} whose response content is deserialized to the desired output type
	 * <br><br>
	 * @return the deserialized response content which conforms to the expected type
	 * <br><br> 
	 * @throws ResponseProcessorException
	 * 			if deserializer instantiation or execution failed for the response entity
	 * <br><br>
	 * @since 1.3.0
	 */
@Override
protected Object process(InvocationContext context, HttpResponse response, Object content) {
    if (response.getEntity() == null) {
        return content;
    }
    HttpEntity entity = response.getEntity();
    Method request = context.getRequest();
    Class<?> responseType = request.getReturnType();
    try {
        if (successful(response) && !status(response, 204, 205)) {
            if (HttpResponse.class.isAssignableFrom(responseType)) {
                return response;
            }
            if (HttpEntity.class.isAssignableFrom(responseType)) {
                return response.getEntity();
            }
            boolean responseExpected = !(responseType.equals(void.class) || responseType.equals(Void.class));
            boolean handleAsync = async(context);
            if (handleAsync || responseExpected) {
                Class<?> endpoint = context.getEndpoint();
                AbstractDeserializer<?> deserializer = null;
                Deserialize metadata = (metadata = request.getAnnotation(Deserialize.class)) == null ? endpoint.getAnnotation(Deserialize.class) : metadata;
                if (metadata != null & !isDetached(context, Deserialize.class)) {
                    deserializer = (metadata.value() == ContentType.UNDEFINED) ? Deserializers.resolve(metadata.type()) : Deserializers.resolve(metadata.value());
                } else if (handleAsync || CharSequence.class.isAssignableFrom(responseType)) {
                    deserializer = Deserializers.resolve(ContentType.PLAIN);
                } else {
                    throw new DeserializerUndefinedException(endpoint, request);
                }
                return deserializer.run(context, response);
            }
        }
    } catch (Exception e) {
        throw (e instanceof ResponseProcessorException) ? (ResponseProcessorException) e : new ResponseProcessorException(getClass(), context, e);
    } finally {
        if (!(HttpResponse.class.isAssignableFrom(responseType) || HttpEntity.class.isAssignableFrom(responseType))) {
            EntityUtils.consumeQuietly(entity);
        }
    }
    return content;
}
Also used : HttpEntity(org.apache.http.HttpEntity) Deserialize(com.lonepulse.robozombie.annotation.Deserialize) HttpResponse(org.apache.http.HttpResponse) Method(java.lang.reflect.Method)

Example 95 with HttpEntity

use of org.apache.http.HttpEntity 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)

Aggregations

HttpEntity (org.apache.http.HttpEntity)518 HttpResponse (org.apache.http.HttpResponse)185 HttpGet (org.apache.http.client.methods.HttpGet)152 IOException (java.io.IOException)144 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)105 Test (org.junit.Test)93 HttpPost (org.apache.http.client.methods.HttpPost)84 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)83 InputStream (java.io.InputStream)71 ArrayList (java.util.ArrayList)69 Header (org.apache.http.Header)64 StatusLine (org.apache.http.StatusLine)61 URI (java.net.URI)58 NameValuePair (org.apache.http.NameValuePair)58 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)58 HttpClient (org.apache.http.client.HttpClient)55 StringEntity (org.apache.http.entity.StringEntity)51 InputStreamReader (java.io.InputStreamReader)44 BufferedReader (java.io.BufferedReader)41