public class DeferredContentProvider extends java.lang.Object implements AsyncContentProvider, Callback, java.io.Closeable
ContentProvider that allows to add content after Request.send(Response.CompleteListener)
has been called, therefore providing the request content at a later time.
DeferredContentProvider can only be used in conjunction with
Request.send(Response.CompleteListener) (and not with its blocking counterpart Request.send())
because it provides content asynchronously.
The deferred content is provided once and then fully consumed.
Invocations to the iterator() method after the first will return an "empty" iterator
because the stream has been consumed on the first invocation.
However, it is possible for subclasses to override offer(ByteBuffer) and close() to copy
the content to another location (for example a file) and be able to support multiple invocations
of of iterator() returning the iterator provided by this
class on the first invocation, and an iterator on the bytes copied to the other location
for subsequent invocations.
Typical usage of DeferredContentProvider is in asynchronous proxies, where HTTP headers arrive
separately from HTTP content chunks.
The deferred content must be provided through offer(ByteBuffer), which can be invoked multiple
times, and when all content has been provided it must be signaled with a call to close().
Example usage:
HttpClient httpClient = ...;
// Use try-with-resources to autoclose DeferredContentProvider
try (DeferredContentProvider content = new DeferredContentProvider())
{
httpClient.newRequest("localhost", 8080)
.content(content)
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
// Your logic here
}
});
// At a later time...
content.offer(ByteBuffer.wrap("some content".getBytes()));
}
| Modifier and Type | Class and Description |
|---|---|
static class |
DeferredContentProvider.Chunk |
private class |
DeferredContentProvider.DeferredContentProviderIterator |
AsyncContentProvider.ListenerContentProvider.TypedCallback.Completable, Callback.NestedInvocable.InvocationType| Modifier and Type | Field and Description |
|---|---|
private java.util.Deque<DeferredContentProvider.Chunk> |
chunks |
private static DeferredContentProvider.Chunk |
CLOSE |
private java.util.concurrent.atomic.AtomicBoolean |
closed |
private java.lang.Throwable |
failure |
private DeferredContentProvider.DeferredContentProviderIterator |
iterator |
private long |
length |
private java.util.concurrent.atomic.AtomicReference<AsyncContentProvider.Listener> |
listener |
private java.lang.Object |
lock |
private int |
size |
__nonBlocking| Constructor and Description |
|---|
DeferredContentProvider(java.nio.ByteBuffer... buffers)
Creates a new
DeferredContentProvider with the given initial content |
| Modifier and Type | Method and Description |
|---|---|
private void |
clear() |
void |
close()
No more content will be added to this content provider
and notifies the listener that no more content is available.
|
void |
failed(java.lang.Throwable failure)
Callback invoked when the operation fails.
|
void |
flush() |
long |
getLength() |
boolean |
isClosed() |
java.util.Iterator<java.nio.ByteBuffer> |
iterator() |
private void |
notifyListener() |
boolean |
offer(java.nio.ByteBuffer buffer)
Adds the given content buffer to this content provider
and notifies the listener that content is available.
|
boolean |
offer(java.nio.ByteBuffer buffer,
Callback callback) |
private boolean |
offer(DeferredContentProvider.Chunk chunk) |
void |
setListener(AsyncContentProvider.Listener listener) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitisReproduciblegetInvocationType, getInvocationType, invokeNonBlocking, isNonBlockingInvocationprivate static final DeferredContentProvider.Chunk CLOSE
private final java.lang.Object lock
private final java.util.Deque<DeferredContentProvider.Chunk> chunks
private final java.util.concurrent.atomic.AtomicReference<AsyncContentProvider.Listener> listener
private final DeferredContentProvider.DeferredContentProviderIterator iterator
private final java.util.concurrent.atomic.AtomicBoolean closed
private long length
private int size
private java.lang.Throwable failure
public DeferredContentProvider(java.nio.ByteBuffer... buffers)
DeferredContentProvider with the given initial contentbuffers - the initial contentpublic void setListener(AsyncContentProvider.Listener listener)
setListener in interface AsyncContentProviderlistener - the listener to be notified of content availabilitypublic long getLength()
getLength in interface ContentProviderpublic boolean offer(java.nio.ByteBuffer buffer)
buffer - the content to addpublic boolean offer(java.nio.ByteBuffer buffer,
Callback callback)
private boolean offer(DeferredContentProvider.Chunk chunk)
private void clear()
public void flush()
throws java.io.IOException
java.io.IOExceptionpublic void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablepublic boolean isClosed()
public void failed(java.lang.Throwable failure)
CallbackCallback invoked when the operation fails.
private void notifyListener()
public java.util.Iterator<java.nio.ByteBuffer> iterator()
iterator in interface java.lang.Iterable<java.nio.ByteBuffer>