Search in sources :

Example 86 with BasicHttpEntity

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 87 with BasicHttpEntity

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();
}
Also used : RequestBuilder(org.apache.http.client.methods.RequestBuilder) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 88 with BasicHttpEntity

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);
    }
}
Also used : Arrays(java.util.Arrays) DefaultRequest(com.amazonaws.DefaultRequest) CASE_INSENSITIVE_ORDER(java.lang.String.CASE_INSENSITIVE_ORDER) HTTP_TARGET_HOST(org.apache.http.protocol.HttpCoreContext.HTTP_TARGET_HOST) Header(org.apache.http.Header) ArrayList(java.util.ArrayList) Map(java.util.Map) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) AWS4Signer(com.amazonaws.auth.AWS4Signer) URI(java.net.URI) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) URIBuilder(org.apache.http.client.utils.URIBuilder) IOException(java.io.IOException) HttpMethodName(com.amazonaws.http.HttpMethodName) HttpRequest(org.apache.http.HttpRequest) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) TreeMap(java.util.TreeMap) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpContext(org.apache.http.protocol.HttpContext) BasicHeader(org.apache.http.message.BasicHeader) NameValuePair(org.apache.http.NameValuePair) HttpHost(org.apache.http.HttpHost) InputStream(java.io.InputStream) NameValuePair(org.apache.http.NameValuePair) DefaultRequest(com.amazonaws.DefaultRequest) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) TreeMap(java.util.TreeMap) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ArrayList(java.util.ArrayList) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 89 with BasicHttpEntity

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;
}
Also used : InputStream(java.io.InputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException)

Example 90 with BasicHttpEntity

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));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) FetchProfile(com.fsck.k9.mail.FetchProfile) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)139 ByteArrayInputStream (java.io.ByteArrayInputStream)104 Test (org.junit.Test)89 InputStream (java.io.InputStream)60 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)15 StatusLine (org.apache.http.StatusLine)12 BasicStatusLine (org.apache.http.message.BasicStatusLine)11 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)9 HttpException (org.apache.http.HttpException)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpContext (org.apache.http.protocol.HttpContext)8 URISyntaxException (java.net.URISyntaxException)7 Map (java.util.Map)7 URI (java.net.URI)6 Header (org.apache.http.Header)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6