Search in sources :

Example 26 with ContentType

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

the class Request method bodyForm.

public Request bodyForm(final Iterable<? extends NameValuePair> formParams, final Charset charset) {
    final List<NameValuePair> paramList = new ArrayList<>();
    for (final NameValuePair param : formParams) {
        paramList.add(param);
    }
    final ContentType contentType = charset != null ? ContentType.APPLICATION_FORM_URLENCODED.withCharset(charset) : ContentType.APPLICATION_FORM_URLENCODED;
    final String s = WWWFormCodec.format(paramList, contentType.getCharset());
    return bodyString(s, contentType);
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) ContentType(org.apache.hc.core5.http.ContentType) ArrayList(java.util.ArrayList)

Example 27 with ContentType

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

the class EchoHandler method handle.

/**
 * Handles a request by echoing the incoming request entity.
 * If there is no request entity, an empty document is returned.
 *
 * @param request   the request
 * @param response  the response
 * @param context   the context
 *
 * @throws HttpException    in case of a problem
 * @throws IOException      in case of an IO problem
 */
@Override
public void handle(final ClassicHttpRequest request, final ClassicHttpResponse response, final HttpContext context) throws HttpException, IOException {
    final String method = request.getMethod().toUpperCase(Locale.ROOT);
    if (!"GET".equals(method) && !"HEAD".equals(method) && !"POST".equals(method) && !"PUT".equals(method)) {
        throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
    }
    HttpEntity entity = request.getEntity();
    // For some reason, just putting the incoming entity into
    // the response will not work. We have to buffer the message.
    final byte[] data;
    final ContentType contentType;
    if (entity == null) {
        data = new byte[0];
        contentType = null;
    } else {
        data = EntityUtils.toByteArray(entity);
        final String contentTypeStr = entity.getContentType();
        contentType = contentTypeStr == null ? null : ContentType.parse(contentTypeStr);
    }
    entity = new ByteArrayEntity(data, contentType);
    response.setCode(HttpStatus.SC_OK);
    response.setEntity(entity);
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) ContentType(org.apache.hc.core5.http.ContentType) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException)

Example 28 with ContentType

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

the class CachedHttpResponseGenerator method generateResponse.

/**
 * If it is legal to use cached content in response response to the {@link HttpRequest} then
 * generate an {@link HttpResponse} based on {@link HttpCacheEntry}.
 * @param request {@link HttpRequest} to generate the response for
 * @param entry {@link HttpCacheEntry} to transform into an {@link HttpResponse}
 * @return {@link SimpleHttpResponse} constructed response
 */
SimpleHttpResponse generateResponse(final HttpRequest request, final HttpCacheEntry entry) throws ResourceIOException {
    final Instant now = Instant.now();
    final SimpleHttpResponse response = new SimpleHttpResponse(entry.getStatus());
    response.setVersion(HttpVersion.DEFAULT);
    response.setHeaders(entry.getHeaders());
    if (responseShouldContainEntity(request, entry)) {
        final Resource resource = entry.getResource();
        final Header h = entry.getFirstHeader(HttpHeaders.CONTENT_TYPE);
        final ContentType contentType = h != null ? ContentType.parse(h.getValue()) : null;
        final byte[] content = resource.get();
        addMissingContentLengthHeader(response, content);
        response.setBody(content, contentType);
    }
    final TimeValue age = this.validityStrategy.getCurrentAge(entry, now);
    if (TimeValue.isPositive(age)) {
        if (age.compareTo(CacheValidityPolicy.MAX_AGE) >= 0) {
            response.setHeader(HeaderConstants.AGE, "" + CacheValidityPolicy.MAX_AGE.toSeconds());
        } else {
            response.setHeader(HeaderConstants.AGE, "" + age.toSeconds());
        }
    }
    return response;
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ContentType(org.apache.hc.core5.http.ContentType) Instant(java.time.Instant) Resource(org.apache.hc.client5.http.cache.Resource) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) TimeValue(org.apache.hc.core5.util.TimeValue)

Example 29 with ContentType

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

the class AbstractCharPushConsumer method consumePromise.

@Override
public final void consumePromise(final HttpRequest promise, final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException {
    if (entityDetails != null) {
        final ContentType contentType;
        try {
            contentType = ContentType.parse(entityDetails.getContentType());
        } catch (final UnsupportedCharsetException ex) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
        Charset charset = contentType != null ? contentType.getCharset() : null;
        if (charset == null) {
            charset = StandardCharsets.US_ASCII;
        }
        setCharset(charset);
        start(promise, response, contentType != null ? contentType : ContentType.DEFAULT_TEXT);
    } else {
        start(promise, response, null);
        completed();
    }
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Charset(java.nio.charset.Charset)

Example 30 with ContentType

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

the class AbstractCharResponseConsumer method consumeResponse.

@Override
public final void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext context, final FutureCallback<T> resultCallback) throws HttpException, IOException {
    this.resultCallback = resultCallback;
    if (entityDetails != null) {
        final ContentType contentType;
        try {
            contentType = ContentType.parse(entityDetails.getContentType());
        } catch (final UnsupportedCharsetException ex) {
            throw new UnsupportedEncodingException(ex.getMessage());
        }
        Charset charset = contentType != null ? contentType.getCharset() : null;
        if (charset == null) {
            charset = StandardCharsets.US_ASCII;
        }
        setCharset(charset);
        start(response, contentType != null ? contentType : ContentType.DEFAULT_TEXT);
    } else {
        start(response, null);
        completed();
    }
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Charset(java.nio.charset.Charset)

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