Search in sources :

Example 6 with ContentType

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

the class EntityUtils method parse.

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
 * The encoding is taken from the entity's Content-Encoding header.
 * <p>
 * This is typically used while parsing an HTTP POST.
 * </p>
 *
 * @param entity
 *            The entity to parse
 * @param maxStreamLength
 *            The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
 * @return a list of {@link NameValuePair} as built from the URI's query portion.
 * @throws IOException
 *             If there was an exception getting the entity's data.
 */
public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws IOException {
    Args.notNull(entity, "HttpEntity");
    final int contentLength = toContentLength((int) Args.checkContentLength(entity));
    final ContentType contentType = ContentType.parse(entity.getContentType());
    if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
        return Collections.emptyList();
    }
    final Charset charset = contentType.getCharset(DEFAULT_CHARSET);
    final CharArrayBuffer buf;
    try (final InputStream inStream = entity.getContent()) {
        if (inStream == null) {
            return Collections.emptyList();
        }
        buf = toCharArrayBuffer(inStream, contentLength, charset, maxStreamLength);
    }
    if (buf.isEmpty()) {
        return Collections.emptyList();
    }
    return WWWFormCodec.parse(buf, charset);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) InputStream(java.io.InputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Charset(java.nio.charset.Charset)

Example 7 with ContentType

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

the class EntityUtils method toString.

/**
 * Gets the entity content as a String, using the provided default character set
 * if none is found in the entity.
 * If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity must not be null
 * @param defaultCharset character set to be applied if none found in the entity,
 * or if the entity provided charset is invalid or not available.
 * @param maxResultLength
 *            The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
 * @return the entity content as a String. May be null if
 *   {@link HttpEntity#getContent()} is null.
 * @throws ParseException if header elements cannot be parsed
 * @throws IllegalArgumentException if entity is null or if content length &gt; Integer.MAX_VALUE
 * @throws IOException if an error occurs reading the input stream
 * @throws java.nio.charset.UnsupportedCharsetException Thrown when the named entity's charset is not available in
 * this instance of the Java virtual machine and no defaultCharset is provided.
 */
public static String toString(final HttpEntity entity, final Charset defaultCharset, final int maxResultLength) throws IOException, ParseException {
    Args.notNull(entity, "HttpEntity");
    ContentType contentType = null;
    try {
        contentType = ContentType.parse(entity.getContentType());
    } catch (final UnsupportedCharsetException ex) {
        if (defaultCharset == null) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
    }
    if (contentType != null) {
        if (contentType.getCharset() == null) {
            contentType = contentType.withCharset(defaultCharset);
        }
    } else {
        contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
    }
    return toString(entity, contentType, maxResultLength);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with ContentType

use of org.apache.tapestry5.http.ContentType in project datagear by datageartech.

the class HttpDataSet method setHttpEntity.

protected void setHttpEntity(ClassicHttpRequest request, String requestContent) throws Throwable {
    if (REQUEST_CONTENT_TYPE_FORM_URLENCODED.equals(this.requestContentType)) {
        List<NameValuePair> params = toNameValuePairs(requestContent);
        if (params == NOT_NAME_VALUE_PAIR_OBJ_ARRAY_JSON)
            throw new RequestContentNotNameValueObjArrayJsonException(requestContent);
        request.setEntity(new UrlEncodedFormEntity(params, Charset.forName(this.requestContentCharset)));
    } else if (REQUEST_CONTENT_TYPE_JSON.equals(this.requestContentType)) {
        ContentType contentType = ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), Charset.forName(this.requestContentCharset));
        StringEntity entity = new StringEntity(requestContent, contentType);
        request.setEntity(entity);
    } else
        throw new DataSetException("Request content type [" + this.requestContentType + "] is not supported");
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.NameValuePair) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) ContentType(org.apache.hc.core5.http.ContentType) DataSetException(org.datagear.analysis.DataSetException) UrlEncodedFormEntity(org.apache.hc.client5.http.entity.UrlEncodedFormEntity)

Example 9 with ContentType

use of org.apache.tapestry5.http.ContentType in project datagear by datageartech.

the class HttpDataSetTest method getRequestStringContent.

protected static String getRequestStringContent(ClassicHttpRequest request) throws IOException {
    HttpEntity entity = request.getEntity();
    String contentTypeStr = entity.getContentType();
    ContentType contentType = ContentType.parse(contentTypeStr);
    Reader reader = new InputStreamReader(entity.getContent(), contentType.getCharset());
    String content = IOUtil.readString(reader, false);
    return content;
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) ContentType(org.apache.hc.core5.http.ContentType) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 10 with ContentType

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

the class AsyncClientH2ServerPush method main.

public static void main(final String[] args) throws Exception {
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
    final H2Config h2Config = H2Config.custom().setPushEnabled(true).build();
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2).setH2Config(h2Config).build();
    client.start();
    client.register("*", new Supplier<AsyncPushConsumer>() {

        @Override
        public AsyncPushConsumer get() {
            return new AbstractBinPushConsumer() {

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

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

                @Override
                protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
                }

                @Override
                protected void completed() {
                }

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

                @Override
                public void releaseResources() {
                }
            };
        }
    });
    final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").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 {
        }

        @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 : AsyncPushConsumer(org.apache.hc.core5.http.nio.AsyncPushConsumer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) AbstractBinPushConsumer(org.apache.hc.client5.http.async.methods.AbstractBinPushConsumer) 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) ByteBuffer(java.nio.ByteBuffer) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) StatusLine(org.apache.hc.core5.http.message.StatusLine) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) HttpException(org.apache.hc.core5.http.HttpException) H2Config(org.apache.hc.core5.http2.config.H2Config)

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