Search in sources :

Example 11 with ContentType

use of org.apache.tapestry5.http.ContentType in project mercury by yellow013.

the class AsyncClientHttpExchangeStreaming method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).build();
    client.start();
    final HttpHost target = new HttpHost("httpbin.org");
    final String[] requestUris = new String[] { "/", "/ip", "/user-agent", "/headers" };
    for (final String requestUri : requestUris) {
        final BasicHttpRequest request = BasicRequestBuilder.get().setHttpHost(target).setPath(requestUri).build();
        System.out.println("Executing request " + request);
        final Future<Void> future = client.execute(new BasicRequestProducer(request, null), new AbstractCharResponseConsumer<Void>() {

            @Override
            protected void start(final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
                System.out.println(request + "->" + new StatusLine(response));
            }

            @Override
            protected int capacityIncrement() {
                return Integer.MAX_VALUE;
            }

            @Override
            protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
                while (data.hasRemaining()) {
                    System.out.print(data.get());
                }
                if (endOfStream) {
                    System.out.println();
                }
            }

            @Override
            protected Void buildResult() throws IOException {
                return null;
            }

            @Override
            public void failed(final Exception cause) {
                System.out.println(request + "->" + cause);
            }

            @Override
            public void releaseResources() {
            }
        }, null);
        future.get();
    }
    System.out.println("Shutting down");
    client.close(CloseMode.GRACEFUL);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) CharBuffer(java.nio.CharBuffer) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) HttpHost(org.apache.hc.core5.http.HttpHost) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpException(org.apache.hc.core5.http.HttpException)

Example 12 with ContentType

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

the class JsonResponseObjectConsumer method createEntityConsumer.

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

Example 13 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class ResponseRendererImpl method findContentType.

public ContentType findContentType(Object component) {
    Component c = (Component) component;
    String pageName = c.getComponentResources().getPageName();
    Page page = pageCache.get(pageName);
    return pageContentAnalyzer.findContentType(page);
}
Also used : Page(org.apache.tapestry5.internal.structure.Page) Component(org.apache.tapestry5.runtime.Component)

Example 14 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class MarkupWriterFactoryImpl method constructMarkupWriter.

private MarkupWriter constructMarkupWriter(ContentType contentType, boolean partial, boolean HTML5) {
    final String mimeType = contentType.getMimeType();
    boolean isHTML = mimeType.equalsIgnoreCase("text/html");
    MarkupModel model;
    if (isHTML)
        model = HTML5 ? (partial ? html5PartialModel : html5Model) : (partial ? htmlPartialModel : htmlModel);
    else
        model = partial ? xmlPartialModel : xmlModel;
    return new MarkupWriterImpl(model, contentType.getCharset(), mimeType);
}
Also used : Html5MarkupModel(org.apache.tapestry5.dom.Html5MarkupModel) DefaultMarkupModel(org.apache.tapestry5.dom.DefaultMarkupModel) XMLMarkupModel(org.apache.tapestry5.dom.XMLMarkupModel) MarkupModel(org.apache.tapestry5.dom.MarkupModel)

Example 15 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class MarkupWriterFactoryImpl method newMarkupWriter.

public MarkupWriter newMarkupWriter(Page page) {
    boolean isHTML5 = hasHTML5Doctype(page);
    ContentType contentType = pageContentTypeAnalyzer.findContentType(page);
    return constructMarkupWriter(contentType, false, isHTML5);
}
Also used : ContentType(org.apache.tapestry5.http.ContentType)

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