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);
}
}
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);
}
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);
}
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;
}
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} --> returned as-is.</li>
* <li>{@code byte[]}, {@link Byte}[] --> {@link ByteArrayEntity}</li>
* <li>java.io.{@link File} --> {@link FileEntity}</li>
* <li>java.io.{@link InputStream} --> {@link BufferedHttpEntity}</li>
* <li>{@link CharSequence} --> {@link StringEntity}</li>
* <li>java.io.{@link Serializable} --> {@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);
}
}
Aggregations