Class DeferredContentProvider
- java.lang.Object
-
- org.eclipse.jetty.client.util.DeferredContentProvider
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Iterable<java.nio.ByteBuffer>
,ContentProvider
,AsyncContentProvider
,Callback
,Invocable
public class DeferredContentProvider extends java.lang.Object implements AsyncContentProvider, Callback, java.io.Closeable
AContentProvider
that allows to add content afterRequest.send(Response.CompleteListener)
has been called, therefore providing the request content at a later time.DeferredContentProvider
can only be used in conjunction withRequest.send(Response.CompleteListener)
(and not with its blocking counterpartRequest.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 overrideoffer(ByteBuffer)
andclose()
to copy the content to another location (for example a file) and be able to support multiple invocations of ofiterator()
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 toclose()
.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())); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DeferredContentProvider.Chunk
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.AsyncContentProvider
AsyncContentProvider.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.Callback
Callback.Completable, Callback.Completing, Callback.Nested
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.api.ContentProvider
ContentProvider.Typed
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.InvocationType
-
-
Field Summary
-
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking
-
-
Constructor Summary
Constructors Constructor Description DeferredContentProvider(java.nio.ByteBuffer... buffers)
Creates a newDeferredContentProvider
with the given initial content
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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()
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)
void
setListener(AsyncContentProvider.Listener listener)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.client.api.ContentProvider
isReproducible
-
Methods inherited from interface org.eclipse.jetty.util.thread.Invocable
getInvocationType
-
-
-
-
Constructor Detail
-
DeferredContentProvider
public DeferredContentProvider(java.nio.ByteBuffer... buffers)
Creates a newDeferredContentProvider
with the given initial content- Parameters:
buffers
- the initial content
-
-
Method Detail
-
setListener
public void setListener(AsyncContentProvider.Listener listener)
- Specified by:
setListener
in interfaceAsyncContentProvider
- Parameters:
listener
- the listener to be notified of content availability
-
getLength
public long getLength()
- Specified by:
getLength
in interfaceContentProvider
- Returns:
- the content length, if known, or -1 if the content length is unknown
-
offer
public boolean offer(java.nio.ByteBuffer buffer)
Adds the given content buffer to this content provider and notifies the listener that content is available.- Parameters:
buffer
- the content to add- Returns:
- true if the content was added, false otherwise
-
offer
public boolean offer(java.nio.ByteBuffer buffer, Callback callback)
-
flush
public void flush() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close()
No more content will be added to this content provider and notifies the listener that no more content is available.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
isClosed
public boolean isClosed()
-
failed
public void failed(java.lang.Throwable failure)
Description copied from interface:Callback
Callback invoked when the operation fails.
-
iterator
public java.util.Iterator<java.nio.ByteBuffer> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.nio.ByteBuffer>
-
-