use of org.apache.http.entity.BasicHttpEntity in project spring-cloud-netflix by spring-cloud.
the class HttpClientUtils method createEntity.
/**
* Creates an new {@link HttpEntity} by copying the {@link HttpEntity} from the {@link HttpResponse}.
* This method will close the response after copying the entity.
* @param response The response to create the {@link HttpEntity} from
* @return A new {@link HttpEntity}
* @throws IOException thrown if there is a problem closing the response.
*/
public static HttpEntity createEntity(HttpResponse response) throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream(EntityUtils.toByteArray(response.getEntity()));
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(is);
entity.setContentLength(response.getEntity().getContentLength());
if (CloseableHttpResponse.class.isInstance(response)) {
((CloseableHttpResponse) response).close();
}
return entity;
}
use of org.apache.http.entity.BasicHttpEntity in project spring-cloud-netflix by spring-cloud.
the class RibbonApacheHttpRequest method toRequest.
public HttpUriRequest toRequest(final RequestConfig requestConfig) {
final RequestBuilder builder = RequestBuilder.create(this.context.getMethod());
builder.setUri(this.uri);
for (final String name : this.context.getHeaders().keySet()) {
final List<String> values = this.context.getHeaders().get(name);
for (final String value : values) {
builder.addHeader(name, value);
}
}
for (final String name : this.context.getParams().keySet()) {
final List<String> values = this.context.getParams().get(name);
for (final String value : values) {
builder.addParameter(name, value);
}
}
if (this.context.getRequestEntity() != null) {
final BasicHttpEntity entity;
entity = new BasicHttpEntity();
entity.setContent(this.context.getRequestEntity());
// if the entity contentLength isn't set, transfer-encoding will be set
// to chunked in org.apache.http.protocol.RequestContent. See gh-1042
Long contentLength = this.context.getContentLength();
if ("GET".equals(this.context.getMethod()) && (contentLength == null || contentLength < 0)) {
entity.setContentLength(0);
} else if (contentLength != null) {
entity.setContentLength(contentLength);
}
builder.setEntity(entity);
}
customize(this.context.getRequestCustomizers(), builder);
builder.setConfig(requestConfig);
return builder.build();
}
use of org.apache.http.entity.BasicHttpEntity in project presto by prestodb.
the class AwsRequestSigner method process.
@Override
public void process(final HttpRequest request, final HttpContext context) throws IOException {
String method = request.getRequestLine().getMethod();
URI uri = URI.create(request.getRequestLine().getUri());
URIBuilder uriBuilder = new URIBuilder(uri);
Map<String, List<String>> parameters = new TreeMap<>(CASE_INSENSITIVE_ORDER);
for (NameValuePair parameter : uriBuilder.getQueryParams()) {
parameters.computeIfAbsent(parameter.getName(), key -> new ArrayList<>()).add(parameter.getValue());
}
Map<String, String> headers = Arrays.stream(request.getAllHeaders()).collect(toImmutableMap(Header::getName, Header::getValue));
InputStream content = null;
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request;
if (enclosingRequest.getEntity() != null) {
content = enclosingRequest.getEntity().getContent();
}
}
DefaultRequest<?> awsRequest = new DefaultRequest<>(SERVICE_NAME);
HttpHost host = (HttpHost) context.getAttribute(HTTP_TARGET_HOST);
if (host != null) {
awsRequest.setEndpoint(URI.create(host.toURI()));
}
awsRequest.setHttpMethod(HttpMethodName.fromValue(method));
awsRequest.setResourcePath(uri.getRawPath());
awsRequest.setContent(content);
awsRequest.setParameters(parameters);
awsRequest.setHeaders(headers);
signer.sign(awsRequest, credentialsProvider.getCredentials());
Header[] newHeaders = awsRequest.getHeaders().entrySet().stream().map(entry -> new BasicHeader(entry.getKey(), entry.getValue())).toArray(Header[]::new);
request.setHeaders(newHeaders);
InputStream newContent = awsRequest.getContent();
checkState(newContent == null || request instanceof HttpEntityEnclosingRequest);
if (newContent != null) {
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(newContent);
((HttpEntityEnclosingRequest) request).setEntity(entity);
}
}
use of org.apache.http.entity.BasicHttpEntity in project saga-android by AnandChowdhary.
the class HurlStack method entityFromConnection.
/**
* Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
* @param connection
* @return an HttpEntity populated with data from <code>connection</code>.
*/
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
BasicHttpEntity entity = new BasicHttpEntity();
InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (IOException ioe) {
inputStream = connection.getErrorStream();
}
entity.setContent(inputStream);
entity.setContentLength(connection.getContentLength());
entity.setContentEncoding(connection.getContentEncoding());
entity.setContentType(connection.getContentType());
return entity;
}
use of org.apache.http.entity.BasicHttpEntity in project k-9 by k9mail.
the class WebDavFolderTest method folder_can_fetch_sensible_body_data_and_notifies_listener.
@Test
public void folder_can_fetch_sensible_body_data_and_notifies_listener() throws MessagingException, IOException, URISyntaxException {
setupStoreForMessageFetching();
List<WebDavMessage> messages = setup25MessagesToFetch();
when(mockHttpClient.executeOverride(any(HttpUriRequest.class), nullable(HttpContext.class))).thenAnswer(new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
HttpResponse httpResponse = mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
BasicHttpEntity httpEntity = new BasicHttpEntity();
String body = "";
httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
when(httpResponse.getEntity()).thenReturn(httpEntity);
return httpResponse;
}
});
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.BODY_SANE);
folder.fetch(messages, profile, listener, MAX_DOWNLOAD_SIZE);
verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25));
verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25));
}
Aggregations