Search in sources :

Example 76 with ContentType

use of org.apache.tapestry5.http.ContentType in project httpcomponents-jackson by ok2c.

the class JsonStreamConsumer method consumeMessage.

final void consumeMessage(H messageHead, EntityDetails entityDetails, FutureCallback<T> resultCallback) throws HttpException, IOException {
    if (messageConsumer != null) {
        messageConsumer.accept(messageHead);
    }
    if (entityDetails == null) {
        if (resultCallback != null) {
            resultCallback.completed(null);
        }
        return;
    }
    ContentType contentType = ContentType.parseLenient(entityDetails.getContentType());
    if (ContentType.APPLICATION_JSON.isSameMimeType(contentType)) {
        AsyncEntityConsumer<T> entityConsumer = entityConsumerSupplier.get();
        entityConsumerRef.set(entityConsumer);
    } else {
        entityConsumerRef.set(new NoopJsonEntityConsumer<>());
    }
    entityConsumerRef.get().streamStart(entityDetails, resultCallback);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType)

Example 77 with ContentType

use of org.apache.tapestry5.http.ContentType in project httpcomponents-jackson by ok2c.

the class JsonRequestObjectConsumer method createEntityConsumer.

@Override
protected AsyncEntityConsumer<T> createEntityConsumer(HttpRequest request, EntityDetails entityDetails) {
    ContentType contentType = ContentType.parseLenient(entityDetails.getContentType());
    AsyncEntityConsumer<T> entityConsumer;
    if (ContentType.APPLICATION_JSON.isSameMimeType(contentType)) {
        entityConsumer = consumerSupplier.get();
    } else {
        entityConsumer = new NoopJsonEntityConsumer<>();
    }
    return entityConsumer;
}
Also used : ContentType(org.apache.hc.core5.http.ContentType)

Example 78 with ContentType

use of org.apache.tapestry5.http.ContentType in project Artemis by mbizhani.

the class HttpRequest method send.

public void send(Consumer<HttpResponse> responseConsumer) {
    ALog.info("RQ: {} - {}{}", request.getMethod(), getUri(), builder.toString());
    final HttpClientContext context = HttpClientContext.create();
    final long start = System.currentTimeMillis();
    try (final CloseableHttpResponse rs = httpClient.execute(request, context)) {
        final long duration = System.currentTimeMillis() - start;
        final int code = rs.getCode();
        final String contentType = rs.getEntity().getContentType();
        final String body = getBody(rs);
        final String cookiesPart;
        final Map<String, String> cookiesMap;
        if (context.getCookieStore().getCookies().size() > 0) {
            final String cookies = context.getCookieStore().getCookies().stream().map(cookie -> String.format("%s=%s", cookie.getName(), cookie.getValue())).collect(Collectors.joining(","));
            cookiesPart = String.format("\n\tCookies: %s", cookies);
            cookiesMap = context.getCookieStore().getCookies().stream().collect(Collectors.toMap(Cookie::getName, Cookie::getValue));
        } else {
            cookiesMap = Collections.emptyMap();
            cookiesPart = "";
        }
        ContextHandler.get().setCookies(cookiesMap);
        if (!body.isEmpty()) {
            ALog.info("RS: {} ({}) - {} [{} ms]\n\tContentType: {}{}\n\t{}", request.getMethod(), code, request.getRequestUri(), duration, contentType, cookiesPart, body);
        } else {
            ALog.info("RS: {} ({}) - {} [{} ms]\n\t{} - (EMPTY BODY){}", request.getMethod(), code, request.getRequestUri(), duration, contentType != null && !contentType.trim().isEmpty() ? "ContentType: " + contentType : "(No ContentType)", cookiesPart);
        }
        StatisticsContext.add(rqGlobalId, request.getMethod(), request.getRequestUri(), code, duration);
        responseConsumer.accept(new HttpResponse(code, contentType, body, cookiesMap));
    } catch (HttpHostConnectException e) {
        throw new TestFailedException(rqId, String.format("Unknown Host (%s)", getUri()));
    } catch (IOException e) {
        throw new TestFailedException(rqId, String.format("%s (%s)", e.getMessage(), getUri()));
    }
}
Also used : HttpHostConnectException(org.apache.hc.client5.http.HttpHostConnectException) Cookie(org.apache.hc.client5.http.cookie.Cookie) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) URISyntaxException(java.net.URISyntaxException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TestFailedException(org.devocative.artemis.TestFailedException) StatisticsContext(org.devocative.artemis.StatisticsContext) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) Map(java.util.Map) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity) HttpUriRequestBase(org.apache.hc.client5.http.classic.methods.HttpUriRequestBase) IOException(java.io.IOException) ContextHandler(org.devocative.artemis.ContextHandler) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) List(java.util.List) ContentType(org.apache.hc.core5.http.ContentType) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) BufferedReader(java.io.BufferedReader) CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) Collections(java.util.Collections) ALog(org.devocative.artemis.ALog) Cookie(org.apache.hc.client5.http.cookie.Cookie) TestFailedException(org.devocative.artemis.TestFailedException) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpHostConnectException(org.apache.hc.client5.http.HttpHostConnectException) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) IOException(java.io.IOException)

Aggregations

ContentType (org.apache.hc.core5.http.ContentType)58 Charset (java.nio.charset.Charset)18 IOException (java.io.IOException)15 ContentType (org.apache.tapestry5.http.ContentType)15 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)13 HttpException (org.apache.hc.core5.http.HttpException)12 HttpResponse (org.apache.hc.core5.http.HttpResponse)12 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)10 HttpRequest (org.apache.hc.core5.http.HttpRequest)10 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)10 InputStream (java.io.InputStream)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 InetSocketAddress (java.net.InetSocketAddress)8 Header (org.apache.hc.core5.http.Header)8 Message (org.apache.hc.core5.http.Message)8 Test (org.junit.Test)8 Test (org.testng.annotations.Test)8 InterruptedIOException (java.io.InterruptedIOException)7 HttpEntity (org.apache.hc.core5.http.HttpEntity)7 StringTokenizer (java.util.StringTokenizer)6