Search in sources :

Example 1 with BasicHttpEntity

use of org.apache.hc.core5.http.io.entity.BasicHttpEntity in project metis-framework by europeana.

the class StringHttpClientTest method createResult.

@Test
void createResult() throws URISyntaxException, IOException {
    List<Closeable> closeables = new ArrayList<>();
    HttpEntity responseEntity = new BasicHttpEntity(new ByteArrayInputStream("content".getBytes()), ContentType.TEXT_PLAIN);
    final ContentRetriever contentRetriever = ContentRetriever.forNonCloseableContent(responseEntity == null ? InputStream::nullInputStream : responseEntity::getContent, closeables::add);
    StringContent actualContent = stringHttpClient.createResult(new URI("/resource/provided"), new URI("/resource/actual"), "text/plain", 7L, contentRetriever);
    assertEquals("content", actualContent.getContent());
    assertEquals("text/plain", actualContent.getContentType());
}
Also used : BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) HttpEntity(org.apache.hc.core5.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) StringContent(eu.europeana.metis.network.StringHttpClient.StringContent) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) URI(java.net.URI) ContentRetriever(eu.europeana.metis.network.AbstractHttpClient.ContentRetriever) Test(org.junit.jupiter.api.Test)

Example 2 with BasicHttpEntity

use of org.apache.hc.core5.http.io.entity.BasicHttpEntity in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseContentEntityChunkedHTTP10.

@Test
public void testResponseContentEntityChunkedHTTP10() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    context.setProtocolVersion(HttpVersion.HTTP_1_0);
    final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null, true));
    final ResponseContent interceptor = new ResponseContent();
    interceptor.process(response, response.getEntity(), context);
    final Header h1 = response.getFirstHeader(HttpHeaders.TRANSFER_ENCODING);
    Assertions.assertNull(h1);
    final Header h2 = response.getFirstHeader(HttpHeaders.CONTENT_LENGTH);
    Assertions.assertNull(h2);
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with BasicHttpEntity

use of org.apache.hc.core5.http.io.entity.BasicHttpEntity in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseConnControlEntityUnknownContentLengthExplicitKeepAlive.

@Test
public void testResponseConnControlEntityUnknownContentLengthExplicitKeepAlive() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
    context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null));
    final ResponseConnControl interceptor = new ResponseConnControl();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("keep-alive", header.getValue());
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with BasicHttpEntity

use of org.apache.hc.core5.http.io.entity.BasicHttpEntity in project httpcomponents-core by apache.

the class TestStandardInterceptors method testResponseConnControlEntityUnknownContentLengthHTTP10.

@Test
public void testResponseConnControlEntityUnknownContentLengthHTTP10() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    context.setProtocolVersion(HttpVersion.HTTP_1_0);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.addHeader(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
    context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    final BasicClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK, "OK");
    response.setEntity(new BasicHttpEntity(EmptyInputStream.INSTANCE, null));
    final ResponseConnControl interceptor = new ResponseConnControl();
    interceptor.process(response, response.getEntity(), context);
    final Header header = response.getFirstHeader(HttpHeaders.CONNECTION);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("close", header.getValue());
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 5 with BasicHttpEntity

use of org.apache.hc.core5.http.io.entity.BasicHttpEntity in project JAuswertung by dennisfabri.

the class SimpleHttpClient method put.

public boolean put(String url, String resultsAsCsv) throws IOException {
    HttpPut putRequest = new HttpPut(url);
    putRequest.setEntity(new BasicHttpEntity(new ByteArrayInputStream(resultsAsCsv.getBytes(StandardCharsets.UTF_8)), ContentType.TEXT_PLAIN));
    try (CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = client.execute(putRequest)) {
        log.debug("Upload with response code {}: {}", response.getCode(), response.getReasonPhrase());
        return isValid(response.getCode());
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) BasicHttpEntity(org.apache.hc.core5.http.io.entity.BasicHttpEntity) HttpPut(org.apache.hc.client5.http.classic.methods.HttpPut)

Aggregations

BasicHttpEntity (org.apache.hc.core5.http.io.entity.BasicHttpEntity)15 Test (org.junit.jupiter.api.Test)14 Header (org.apache.hc.core5.http.Header)11 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)11 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)9 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)7 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)6 HttpRequestInterceptor (org.apache.hc.core5.http.HttpRequestInterceptor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ContentRetriever (eu.europeana.metis.network.AbstractHttpClient.ContentRetriever)1 StringContent (eu.europeana.metis.network.StringHttpClient.StringContent)1 Closeable (java.io.Closeable)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HttpPut (org.apache.hc.client5.http.classic.methods.HttpPut)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 HttpEntity (org.apache.hc.core5.http.HttpEntity)1