public abstract class HttpContentConsumer extends Object
HttpServiceHandler
can return an instance of this class to consume the request body in
small chunks, to avoid running out of memory for requests with a large body.
Example:
public class MyHttpHandler extends AbstractHttpServiceHandler {
@POST
@Path("/digest")
public HttpContentConsumer computeDigest(HttpServiceRequest request,
HttpServiceResponder responder,
@HeaderParam String digestType) throws Exception {
if (digestType == null) {
responder.sendError(400, "No message digest type is provided");
return null;
}
final MessageDigest messageDigest = MessageDigest.getInstance(digestType);
return new HttpContentConsumer() {
@Override
public void onReceived(ByteBuffer chunk) throws Exception {
messageDigest.update(chunk);
}
@Override
public void onFinish(HttpServiceResponder responder) throws Exception {
responder.sendString(Bytes.toHexString(messageDigest.digest()));
}
@Override
public void onError(HttpServiceResponder responder, Throwable failureCause) {
responder.sendError(500, failureCause.getMessage());
}
}
}
}
Constructor and Description |
---|
HttpContentConsumer() |
Modifier and Type | Method and Description |
---|---|
abstract void |
onError(HttpServiceResponder responder,
Throwable failureCause)
This method is invoked when there is an error while processing the request body chunks.
|
abstract void |
onFinish(HttpServiceResponder responder)
This method is invoked when the end of the request body is reached.
|
abstract void |
onReceived(ByteBuffer chunk,
Transactional transactional)
This method is invoked when a new chunk of the request body is available to be consumed.
|
public abstract void onReceived(ByteBuffer chunk, Transactional transactional) throws Exception
Access to transactional Datasets
must be through the Transactional.execute(TxRunnable)
method.
chunk
- a ByteBuffer
containing a chunk of the request bodytransactional
- for executing a TxRunnable
in a single transaction.Exception
- if there is any error when processing the received chunkpublic abstract void onFinish(HttpServiceResponder responder) throws Exception
HttpServiceResponder
to send the response in order to complete the HTTP call. This
method is always executed inside a single transaction unless annotated with TransactionPolicy(TransactionControl
.responder
- a HttpServiceResponder
for sending responseException
- if there is any errorpublic abstract void onError(HttpServiceResponder responder, Throwable failureCause)
HttpServiceResponder
to send the response in order to complete the HTTP
call.
Any issues related to network as well as any Exceptions
raised from either
onReceived(ByteBuffer, Transactional)
or onFinish(HttpServiceResponder)
methods will have this method invoked.
This method is always executed inside a single transaction unless annotated with TransactionPolicy(TransactionControl
.
responder
- a HttpServiceResponder
for sending responsefailureCause
- the reason of the failureCopyright © 2024 Cask Data, Inc. Licensed under the Apache License, Version 2.0.