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);
}
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);
}
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;
}
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();
}
}
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();
}
}
Aggregations