Class AfterContentTransformer

    • Constructor Detail

      • AfterContentTransformer

        public AfterContentTransformer()
    • 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 the max 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 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:
        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 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:
        java.io.IOException - if the transformation fails