Class AfterContentTransformer

java.lang.Object
org.eclipse.jetty.proxy.AfterContentTransformer
All Implemented Interfaces:
AsyncMiddleManServlet.ContentTransformer, Destroyable

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.

  • Constructor Details

    • AfterContentTransformer

      public AfterContentTransformer()
  • Method Details

    • getOverflowDirectory

      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.

      Returns:
      the overflow directory path
      See Also:
    • setOverflowDirectory

      public void setOverflowDirectory(Path overflowDirectory)
      Parameters:
      overflowDirectory - the overflow directory path
      See Also:
    • getInputFilePrefix

      public String getInputFilePrefix()
      Returns:
      the prefix of the input overflow temporary files
      See Also:
    • setInputFilePrefix

      public void setInputFilePrefix(String inputFilePrefix)
      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

      public String getOutputFilePrefix()
      Returns:
      the prefix of the output overflow temporary files
      See Also:
    • setOutputFilePrefix

      public void setOutputFilePrefix(String outputFilePrefix)
      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 interface AsyncMiddleManServlet.ContentTransformer
      Parameters:
      input - the input content to transform (may be of length zero)
      finished - whether the input content is finished or more will come
      output - 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 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;
       }
       
      Parameters:
      source - where the original content is read
      sink - 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 interface Destroyable