Class AfterContentTransformer
- All Implemented Interfaces:
AsyncMiddleManServlet.ContentTransformer
,Destroyable
A specialized transformer for AsyncMiddleManServlet
that performs
the transformation when the whole content has been received.
The content is buffered in memory up to a configurable maximum size
,
after which it is overflown to a file on disk. The overflow file is saved
in the overflow directory
as a
temporary file
with a name starting with the input prefix
and default suffix.
Application must implement the transformation method
to transform the content.
The transformed content is buffered in memory up to a configurable maximum size
after which it is overflown to a file on disk. The overflow file is saved
in the overflow directory
as a
temporary file
with a name starting with the getOutputFilePrefix()
output prefix}
and default suffix.
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
The target to where the transformed content is written after the transformation.class
The source from where the original content is read to be transformed. -
Field Summary
Fields inherited from interface org.eclipse.jetty.proxy.AsyncMiddleManServlet.ContentTransformer
IDENTITY
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
long
Returns the maximum input buffer size, after which the input is overflown to disk.long
Returns the maximum output buffer size, after which the output is overflown to disk.Returns the directory where input and output are overflown to temporary files if they exceed, respectively, themax input size
or themax output size
.void
setInputFilePrefix
(String inputFilePrefix) void
setMaxInputBufferSize
(long maxInputBufferSize) void
setMaxOutputBufferSize
(long maxOutputBufferSize) void
setOutputFilePrefix
(String outputFilePrefix) void
setOverflowDirectory
(Path overflowDirectory) final void
transform
(ByteBuffer input, boolean finished, List<ByteBuffer> output) Transforms the given input byte buffers into (possibly multiple) byte buffers.abstract boolean
Transforms the original content read from thesource
into transformed content written to thesink
.
-
Constructor Details
-
AfterContentTransformer
public AfterContentTransformer()
-
-
Method Details
-
getOverflowDirectory
Returns the directory where input and output are overflown to temporary files if they exceed, respectively, the
max input size
or themax output size
.Defaults to the directory pointed by the
java.io.tmpdir
system property.- Returns:
- the overflow directory path
- See Also:
-
setOverflowDirectory
- Parameters:
overflowDirectory
- the overflow directory path- See Also:
-
getInputFilePrefix
- Returns:
- the prefix of the input overflow temporary files
- See Also:
-
setInputFilePrefix
- Parameters:
inputFilePrefix
- the prefix of the input overflow temporary files- See Also:
-
getMaxInputBufferSize
public long getMaxInputBufferSize()Returns the maximum input buffer size, after which the input is overflown to disk.
Defaults to 1 MiB, i.e. 1048576 bytes.
- Returns:
- the max input buffer size
- See Also:
-
setMaxInputBufferSize
public void setMaxInputBufferSize(long maxInputBufferSize) - Parameters:
maxInputBufferSize
- the max input buffer size- See Also:
-
getOutputFilePrefix
- Returns:
- the prefix of the output overflow temporary files
- See Also:
-
setOutputFilePrefix
- Parameters:
outputFilePrefix
- the prefix of the output overflow temporary files- See Also:
-
getMaxOutputBufferSize
public long getMaxOutputBufferSize()Returns the maximum output buffer size, after which the output is overflown to disk.
Defaults to 1 MiB, i.e. 1048576 bytes.
- Returns:
- the max output buffer size
- See Also:
-
setMaxOutputBufferSize
public void setMaxOutputBufferSize(long maxOutputBufferSize) - Parameters:
maxOutputBufferSize
- the max output buffer size
-
transform
public final void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException Description copied from interface:AsyncMiddleManServlet.ContentTransformer
Transforms the given input byte buffers into (possibly multiple) byte buffers.
The transformation must happen synchronously in the context of a call to this method (it is not supported to perform the transformation in another thread spawned during the call to this method). The transformation may happen or not, depending on the transformer implementation. For example, a buffering transformer may buffer the input aside, and only perform the transformation when the whole input is provided (by looking at the
finished
flag).The input buffer will be cleared and reused after the call to this method. Implementations that want to buffer aside the input (or part of it) must copy the input bytes that they want to buffer.
Typical implementations:
// Identity transformation (no transformation, the input is copied to the output) public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) { output.add(input); } // Discard transformation (all input is discarded) public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) { // Empty } // Buffering identity transformation (all input is buffered aside until it is finished) public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) { ByteBuffer copy = ByteBuffer.allocate(input.remaining()); copy.put(input).flip(); store(copy); if (finished) { List<ByteBuffer> copies = retrieve(); output.addAll(copies); } }
- Specified by:
transform
in interfaceAsyncMiddleManServlet.ContentTransformer
- Parameters:
input
- the input content to transform (may be of length zero)finished
- whether the input content is finished or more will comeoutput
- where to put the transformed output content- Throws:
IOException
- in case of transformation failures
-
transform
public abstract boolean transform(AfterContentTransformer.Source source, AfterContentTransformer.Sink sink) throws IOException Transforms the original content read from the
source
into transformed content written to thesink
.The transformation must happen synchronously in the context of a call to this method (it is not supported to perform the transformation in another thread spawned during the call to this method).
Differently from
transform(ByteBuffer, boolean, List)
, this method is invoked only when the whole content is available, and offers a blocking API via the InputStream and OutputStream that can be obtained fromAfterContentTransformer.Source
andAfterContentTransformer.Sink
respectively.Implementations may read the source, inspect the input bytes and decide that no transformation is necessary, and therefore the source must be copied unchanged to the sink. In such case, the implementation must return false to indicate that it wishes to just pipe the bytes from the source to the sink.
Typical implementations:
// Identity transformation (no transformation, the input is copied to the output) public boolean transform(Source source, Sink sink) { org.eclipse.jetty.util.IO.copy(source.getInputStream(), sink.getOutputStream()); return true; }
- Parameters:
source
- where the original content is readsink
- where the transformed content is written- Returns:
- true if the transformation happened and the transformed bytes have been written to the sink, false if no transformation happened and the source must be copied to the sink.
- Throws:
IOException
- if the transformation fails
-
destroy
public void destroy()- Specified by:
destroy
in interfaceDestroyable
-