Class AfterContentTransformer
- java.lang.Object
-
- org.eclipse.jetty.proxy.AfterContentTransformer
-
- All Implemented Interfaces:
AsyncMiddleManServlet.ContentTransformer
,Destroyable
public abstract class AfterContentTransformer extends java.lang.Object implements 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 theoverflow directory
as atemporary file
with a name starting with theinput 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 theoverflow directory
as atemporary file
with a name starting with thegetOutputFilePrefix()
output prefix} and default suffix.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AfterContentTransformer.Sink
The target to where the transformed content is written after the transformation.class
AfterContentTransformer.Source
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
Constructors Constructor Description AfterContentTransformer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
destroy()
java.lang.String
getInputFilePrefix()
long
getMaxInputBufferSize()
Returns the maximum input buffer size, after which the input is overflown to disk.long
getMaxOutputBufferSize()
Returns the maximum output buffer size, after which the output is overflown to disk.java.lang.String
getOutputFilePrefix()
java.nio.file.Path
getOverflowDirectory()
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(java.lang.String inputFilePrefix)
void
setMaxInputBufferSize(long maxInputBufferSize)
void
setMaxOutputBufferSize(long maxOutputBufferSize)
void
setOutputFilePrefix(java.lang.String outputFilePrefix)
void
setOverflowDirectory(java.nio.file.Path overflowDirectory)
void
transform(java.nio.ByteBuffer input, boolean finished, java.util.List<java.nio.ByteBuffer> output)
Transforms the given input byte buffers into (possibly multiple) byte buffers.abstract boolean
transform(AfterContentTransformer.Source source, AfterContentTransformer.Sink sink)
Transforms the original content read from thesource
into transformed content written to thesink
.
-
-
-
Method Detail
-
getOverflowDirectory
public java.nio.file.Path 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(Path)
-
setOverflowDirectory
public void setOverflowDirectory(java.nio.file.Path overflowDirectory)
- Parameters:
overflowDirectory
- the overflow directory path- See Also:
getOverflowDirectory()
-
getInputFilePrefix
public java.lang.String getInputFilePrefix()
- Returns:
- the prefix of the input overflow temporary files
- See Also:
setInputFilePrefix(String)
-
setInputFilePrefix
public void setInputFilePrefix(java.lang.String inputFilePrefix)
- Parameters:
inputFilePrefix
- the prefix of the input overflow temporary files- See Also:
getInputFilePrefix()
-
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(long)
-
setMaxInputBufferSize
public void setMaxInputBufferSize(long maxInputBufferSize)
- Parameters:
maxInputBufferSize
- the max input buffer size- See Also:
getMaxInputBufferSize()
-
getOutputFilePrefix
public java.lang.String getOutputFilePrefix()
- Returns:
- the prefix of the output overflow temporary files
- See Also:
setOutputFilePrefix(String)
-
setOutputFilePrefix
public void setOutputFilePrefix(java.lang.String outputFilePrefix)
- Parameters:
outputFilePrefix
- the prefix of the output overflow temporary files- See Also:
getOutputFilePrefix()
-
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(long)
-
setMaxOutputBufferSize
public void setMaxOutputBufferSize(long maxOutputBufferSize)
- Parameters:
maxOutputBufferSize
- the max output buffer size
-
transform
public final void transform(java.nio.ByteBuffer input, boolean finished, java.util.List<java.nio.ByteBuffer> output) throws java.io.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:
java.io.IOException
- in case of transformation failures
-
transform
public abstract boolean transform(AfterContentTransformer.Source source, AfterContentTransformer.Sink sink) throws java.io.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:
java.io.IOException
- if the transformation fails
-
destroy
public void destroy()
- Specified by:
destroy
in interfaceDestroyable
-
-