use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class TerminalAsyncServerFilter method handle.
@Override
public AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
if (exchangeHandler != null) {
exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {
@Override
public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.sendInformation(response);
}
@Override
public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {
@Override
public void failed(final Exception cause) {
exchangeHandler.failed(cause);
}
@Override
public boolean isRepeatable() {
return false;
}
@Override
public long getContentLength() {
return entityDetails.getContentLength();
}
@Override
public String getContentType() {
return entityDetails.getContentType();
}
@Override
public String getContentEncoding() {
return entityDetails.getContentEncoding();
}
@Override
public boolean isChunked() {
return entityDetails.isChunked();
}
@Override
public Set<String> getTrailerNames() {
return entityDetails.getTrailerNames();
}
@Override
public int available() {
return exchangeHandler.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
exchangeHandler.produce(channel);
}
@Override
public void releaseResources() {
exchangeHandler.releaseResources();
}
} : null);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
responseTrigger.pushPromise(promise, pushProducer);
}
}, context);
return exchangeHandler;
}
responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
return null;
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class AsyncRequestBuilder method build.
@Override
public AsyncRequestProducer build() {
String path = getPath();
if (TextUtils.isEmpty(path)) {
path = "/";
}
AsyncEntityProducer entityProducerCopy = entityProducer;
final String method = getMethod();
final List<NameValuePair> parameters = getParameters();
if (parameters != null && !parameters.isEmpty()) {
final Charset charset = getCharset();
if (entityProducerCopy == null && (Method.POST.isSame(method) || Method.PUT.isSame(method))) {
final String content = WWWFormCodec.format(parameters, charset != null ? charset : ContentType.APPLICATION_FORM_URLENCODED.getCharset());
entityProducerCopy = new StringAsyncEntityProducer(content, ContentType.APPLICATION_FORM_URLENCODED);
} else {
try {
final URI uri = new URIBuilder(path).setCharset(charset).addParameters(parameters).build();
path = uri.toASCIIString();
} catch (final URISyntaxException ex) {
// should never happen
}
}
}
if (entityProducerCopy != null && Method.TRACE.isSame(method)) {
throw new IllegalStateException(Method.TRACE + " requests may not include an entity");
}
final BasicHttpRequest request = new BasicHttpRequest(method, getScheme(), getAuthority(), path);
request.setVersion(getVersion());
request.setHeaders(getHeaders());
request.setAbsoluteRequestUri(isAbsoluteRequestUri());
return new BasicRequestProducer(request, entityProducerCopy);
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class AsyncServerExpectationFilter method handle.
@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
if (entityDetails != null) {
final Header h = request.getFirstHeader(HttpHeaders.EXPECT);
if (h != null && HeaderElements.CONTINUE.equalsIgnoreCase(h.getValue())) {
final boolean verified = verify(request, context);
if (verified) {
responseTrigger.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE));
} else {
final HttpResponse expectationFailed = new BasicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
final AsyncEntityProducer responseContentProducer = generateResponseContent(expectationFailed);
responseTrigger.submitResponse(expectationFailed, responseContentProducer);
return null;
}
}
}
return chain.proceed(request, entityDetails, context, responseTrigger);
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class AsyncServerFilterChainExchangeHandlerFactory method create.
@Override
public AsyncServerExchangeHandler create(final HttpRequest request, final HttpContext context) throws HttpException {
return new AsyncServerExchangeHandler() {
private final AtomicReference<AsyncDataConsumer> dataConsumerRef = new AtomicReference<>();
private final AtomicReference<AsyncResponseProducer> responseProducerRef = new AtomicReference<>();
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
dataConsumerRef.set(filterChain.handle(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
@Override
public void sendInformation(final HttpResponse response) throws HttpException, IOException {
responseChannel.sendInformation(response, context);
}
@Override
public void submitResponse(final HttpResponse response, final AsyncEntityProducer entityProducer) throws HttpException, IOException {
final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, entityProducer);
responseProducerRef.set(responseProducer);
responseProducer.sendResponse(responseChannel, context);
}
@Override
public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
responseChannel.pushPromise(promise, responseProducer, context);
}
}));
}
@Override
public void failed(final Exception cause) {
if (exceptionCallback != null) {
exceptionCallback.execute(cause);
}
final AsyncResponseProducer handler = responseProducerRef.get();
if (handler != null) {
handler.failed(cause);
}
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.updateCapacity(capacityChannel);
} else {
capacityChannel.update(Integer.MAX_VALUE);
}
}
@Override
public void consume(final ByteBuffer src) throws IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.consume(src);
}
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
if (dataConsumer != null) {
dataConsumer.streamEnd(trailers);
}
}
@Override
public int available() {
final AsyncResponseProducer responseProducer = responseProducerRef.get();
Asserts.notNull(responseProducer, "Response producer");
return responseProducer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
final AsyncResponseProducer responseProducer = responseProducerRef.get();
Asserts.notNull(responseProducer, "Response producer");
responseProducer.produce(channel);
}
@Override
public void releaseResources() {
final AsyncDataConsumer dataConsumer = dataConsumerRef.getAndSet(null);
if (dataConsumer != null) {
dataConsumer.releaseResources();
}
final AsyncResponseProducer responseProducer = responseProducerRef.getAndSet(null);
if (responseProducer != null) {
responseProducer.releaseResources();
}
}
};
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project mercury by yellow013.
the class AsyncClientMessageTrailers method main.
public static final void main(final String[] args) throws Exception {
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
@Override
public void execute(final HttpRequest request, final AsyncEntityProducer entityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
// Send MD5 hash in a trailer by decorating the original entity producer
chain.proceed(request, entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null, scope, asyncExecCallback);
}
}).build();
client.start();
final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post").setBody("some stuff", ContentType.TEXT_PLAIN).build();
System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future = client.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}
@Override
public void failed(final Exception ex) {
System.out.println(request + "->" + ex);
}
@Override
public void cancelled() {
System.out.println(request + " cancelled");
}
});
future.get();
System.out.println("Shutting down");
client.close(CloseMode.GRACEFUL);
}
Aggregations