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