Using multiple writers to process multipart responses

12 views
Skip to first unread message

Martin Hermes

unread,
Mar 23, 2015, 4:54:01 AM3/23/15
to streamfly...@googlegroups.com
Hi,
I want to process a multipart response in a reverse proxy with the ModifyingWriter. Some of the parts shall be written with the ModifyingWriter, other parts shall be left untouched. To keep the order of the bytes written with the two different Writer I flush the Writer after a part has been written. Here is some test code:
 

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        OutputStreamWriter originalWriter = new OutputStreamWriter(outputStream);

        ModifyingWriter modifyingWriter =
            new ModifyingWriterFactory().createRegexModifyingWriter(originalWriter, "abcd", "1234");

        modifyingWriter.write("mod ");
        modifyingWriter.flush();
        originalWriter.write("orig ");
        originalWriter.flush();
        modifyingWriter.write("mod ");
        modifyingWriter.flush();

        modifyingWriter.close();
        originalWriter.close();

I would expect the String "mod orig mod " as a result, but the result is "morig od mod". Any help is apreciated.
 
Best Regards,
Martin

 

 

rw...@gmx.de

unread,
Mar 23, 2015, 7:12:16 AM3/23/15
to streamfly...@googlegroups.com
Hi Martin,


Additionally, I added a new method to the ModifyingWriter: close(boolean closeUnderlyingWriter). This method does not close the underlying writer.

By using this new method one could rewrite the test:

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        OutputStreamWriter originalWriter = new OutputStreamWriter(outputStream, "UTF-8");

        originalWriter.flush(); // make sure everything is written

        // write part 1
        ModifyingWriter modifyingWriterPart1 =
                new ModifyingWriterFactory().createRegexModifyingWriter(originalWriter, "abcd", "1234");
        modifyingWriterPart1.write("mod ");
        modifyingWriterPart1.close(false);

        // write to the underlying writer
        originalWriter.write("orig ");
        originalWriter.flush();

        // write part 2
        ModifyingWriter modifyingWriterPart2 =
                new ModifyingWriterFactory().createRegexModifyingWriter(originalWriter, "abcd", "1234");
        modifyingWriterPart2.write("mod ");
        modifyingWriterPart2.close(false);

        originalWriter.close();
        
        String result = new String(outputStream.toByteArray(), "UTF-8");
        assertEquals("mod orig mod ", result);

The test can be run successfully with the latest source code revision.

Does this help you?

Cheers
Rod

Martin Hermes

unread,
Mar 23, 2015, 7:39:05 AM3/23/15
to streamfly...@googlegroups.com
Hi Rod,
This works perfectly fine for me. Thanks a lot for your fast response! Are there any plans for a new release?
 
Best Regards,
Martin

rw...@gmx.de

unread,
Mar 24, 2015, 12:14:36 PM3/24/15
to streamfly...@googlegroups.com
Streamflyer could not be released during the last two days due to an issue caused by Sonatype.

But today I could trigger a fresh release. The artifacts should be available in Maven Central in a couple of hours.

Be aware of the new groupId:

<groupId>com.github.rwitzel.streamflyer</groupId>
<artifactId>streamflyer-core</artifactId>
<version>1.2.0</version>

Streamflyer had to migrate to GitHub because the Google Code platform is closing.

Due to the changed groupId the package names for all classes had to be changed as well so that classpath issues are avoided.

Regards
Rodrigo

Martin Hermes

unread,
Mar 26, 2015, 12:39:57 PM3/26/15
to streamfly...@googlegroups.com
Thanks again for your fast response. I like streamflyer :-)
Reply all
Reply to author
Forward
0 new messages