Well, my first thought is that for what you are trying to do, you are doing it 
in a very in-efficient manner.   :)

For the most part, you would be better off adding an interceptor to the 
beginning of the PRE_PROTOCOL phase.   At that point, you could do:


((SoapMessage)message).getHeaders()

To get the list of headers that have already been parsed.  You could modify the 
contents of the header objects at that point.  This would not break streaming 
of the body and would perform significantly better, particularly for large 
requests.  Your method involves parsing the entire message twice, potentially 
storing larger  messages on disk.   


Now, to answer the specific question2:   

> Query#1 :
> If CachedOutputStream  is used for creating a re-readable inputStream why the 
> name is so misleading ? (It doesn't even have input word in it) 

CachedOutputStream is not just used for that.    It’s not required to ever get 
an InputStream from it.    There is a writeCacheTo method that can be used to 
write everything to a different output stream.   There is a getBytes() call for 
getting everything as a byte[], etc….    Its primary use case is via the 
CachedOutputStreamCallback objects.   In some cases, we use the 
CachedOutputStream to start writing, and once a certain threshold is hit, the 
callback is triggered and the cache is flushed to a real output stream, and the 
CachedOutputStream is replaced with the real output stream.      Thus, if the 
amount of data never hits the threshold, we can optimize certain cases, but if 
it does, we go another route.   


> Query#2 :
> For the requirement of re-readable inputStream, is CachedOutputStream  the 
> best candidate, why not 
> DelegatingInputStream<https://cxf.apache.org/javadoc/latest/org/apache/cxf/io/DelegatingInputStream.html>,
>  if yes would you be kind enough to provide a working code snippet of same ?


CachedOutputStream is likely the best option at this point, but see above for 
your specific use case.

Dan





> On Mar 5, 2019, at 10:30 AM, Varun Singhal <[email protected]> wrote:
> 
> Hi guys,
> 
> Can anyone advise on the below queries, please ?
> 
> I really want to understand if my understanding is flawed ☹
> 
> Many thanks !
> 
> Warm Regards,
> Varun SINGHAL
> 
> ________________________________
> From: Varun Singhal
> Sent: Tuesday, March 5, 2019 12:09:24 AM
> To: [email protected]
> Subject: [Reading CXF message content] What is CachedOutputStream's primary 
> purpose and how its different than DelegatingInputStream ?
> 
> Hi all,
> 
> Greetings !
> 
> I have designed a interceptor that runs in RECEIVE phase and takes a decision 
> whether to modify the header of an incoming SOAP request OR not.
> It does so by reading the message's contents and parsing it into a XML 
> document instance.
> 
> Here is the code:
> 
>      //get message as stream
>      InputStream ipStream = message.getContent(InputStream.class);
>      CachedOutputStream cos = new CachedOutputStream();
>      //create a re-readable inputstream
>      IOUtils.copy(ipStream, cos);
>      ipStream.close();
>      cos.flush();
>      //set the new inputstream
>      message.setContent(InputStream.class, cos.getInputStream());
>     //XML document instance
>      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
>      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
>      doc = dBuilder.parse(cos.getInputStream());
>      cos.close();
>      if (doc != null) {
>          takeDecisionOnHeaderModification(doc);
>        }
>      }
> 
> The code works fine.
> As you can make out i needed a re-readable `inputStream` otherwise successive 
> application code/interceptors would have empty input stream.
> so i used CachedOutputStream because cos.getInputStream() is re-readable.
> 
> Here are my queries about this approach :
> 
> Query#1 :
> If CachedOutputStream  is used for creating a re-readable inputStream why the 
> name is so misleading ? (It doesn't even have input word in it)
> 
> Query#2 :
> For the requirement of re-readable inputStream, is CachedOutputStream  the 
> best candidate, why not 
> DelegatingInputStream<https://cxf.apache.org/javadoc/latest/org/apache/cxf/io/DelegatingInputStream.html>,
>  if yes would you be kind enough to provide a working code snippet of same ?
> 
> I will be very grateful if anyone could shed light on these queries ?
> 
> Many Thanks !
> 
> Warm Regards,
> 
> Varun SINGHAL
> 

-- 
Daniel Kulp
[email protected] <mailto:[email protected]> - http://dankulp.com/blog 
<http://dankulp.com/blog>
Talend Community Coder - http://talend.com <http://coders.talend.com/>

Reply via email to