public abstract class AfterContentTransformer extends 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 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.
Modifier and Type | Class and 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.
|
IDENTITY
Constructor and Description |
---|
AfterContentTransformer() |
Modifier and Type | Method and Description |
---|---|
void |
destroy() |
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.
|
String |
getOutputFilePrefix() |
Path |
getOverflowDirectory()
Returns the directory where input and output are overflown to
temporary files if they exceed, respectively, the
max input size or the
max output size . |
void |
setInputFilePrefix(String inputFilePrefix) |
void |
setMaxInputBufferSize(long maxInputBufferSize) |
void |
setMaxOutputBufferSize(long maxOutputBufferSize) |
void |
setOutputFilePrefix(String outputFilePrefix) |
void |
setOverflowDirectory(Path overflowDirectory) |
abstract boolean |
transform(AfterContentTransformer.Source source,
AfterContentTransformer.Sink sink)
Transforms the original content read from the
source into
transformed content written to the sink . |
void |
transform(ByteBuffer input,
boolean finished,
List<ByteBuffer> output)
Transforms the given input byte buffers into (possibly multiple) byte buffers.
|
public Path getOverflowDirectory()
Returns the directory where input and output are overflown to
temporary files if they exceed, respectively, the
max input size
or the
max output size
.
Defaults to the directory pointed by the java.io.tmpdir
system property.
setOverflowDirectory(Path)
public void setOverflowDirectory(Path overflowDirectory)
overflowDirectory
- the overflow directory pathgetOverflowDirectory()
public String getInputFilePrefix()
setInputFilePrefix(String)
public void setInputFilePrefix(String inputFilePrefix)
inputFilePrefix
- the prefix of the input overflow temporary filesgetInputFilePrefix()
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.
setMaxInputBufferSize(long)
public void setMaxInputBufferSize(long maxInputBufferSize)
maxInputBufferSize
- the max input buffer sizegetMaxInputBufferSize()
public String getOutputFilePrefix()
setOutputFilePrefix(String)
public void setOutputFilePrefix(String outputFilePrefix)
outputFilePrefix
- the prefix of the output overflow temporary filesgetOutputFilePrefix()
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.
setMaxOutputBufferSize(long)
public void setMaxOutputBufferSize(long maxOutputBufferSize)
maxOutputBufferSize
- the max output buffer sizepublic final void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException
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); } }
transform
in interface AsyncMiddleManServlet.ContentTransformer
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 contentIOException
- in case of transformation failurespublic abstract boolean transform(AfterContentTransformer.Source source, AfterContentTransformer.Sink sink) throws IOException
Transforms the original content read from the source
into
transformed content written to the sink
.
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
from AfterContentTransformer.Source
and AfterContentTransformer.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; }
source
- where the original content is readsink
- where the transformed content is writtenIOException
- if the transformation failspublic void destroy()
destroy
in interface Destroyable
Copyright © 1995-2015 Webtide. All Rights Reserved.