You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by Przemyslaw Bielicki <pb...@gmail.com> on 2014/06/10 16:27:33 UTC

How to get in/out message length in the interceptors?

Hi,

I need to know the input and output message size in my interceptors.

I assume that "Content-Length" is not set in the request i.e.
(Transfer-Encoding = chunked), so I cannot take the in length from the HTTP
header - it must be really "computed".

Additional difficulty is that I don't want to serialize incoming and
outgoing messages toString() as it will be a performance issue.

Is there any way to get these sizes from the Input/OutputStream? Is there a
way to inject my custom stream wrappers that will be counting number of
bytes read/written?

Many thanks,
Przemyslaw

Re: How to get in/out message length in the interceptors?

Posted by Przemyslaw Bielicki <pb...@gmail.com>.
one more thing - it might be a bug in CXF.

I noticed that POST_INVOKE (registered as InInterceptor) is executed *AFTER
*POST_STREAM (registered as OutInterceptor)

Is it normal?


On Wed, Jun 11, 2014 at 9:05 AM, Przemyslaw Bielicki <pb...@gmail.com>
wrote:

> Hi Daniel,
>
> thx a lot for the tip. In fact I discovered this solution five minutes
> after asking the question ;)
>
> FYI for the input stream I use RECEIVE phase to register the wrapper and
> INVOKE phase to read bytes read count.
> For the output stream I use PRE_STREAM phase to register the wrapper and
> POST_STREAM phase to read bytes written count.
>
> Cheers,
> Przemyslaw
>
>
> On Tue, Jun 10, 2014 at 9:30 PM, Daniel Kulp <dk...@apache.org> wrote:
>
>>
>> On Jun 10, 2014, at 10:27 AM, Przemyslaw Bielicki <pb...@gmail.com>
>> wrote:
>>
>> > I need to know the input and output message size in my interceptors.
>> >
>> > I assume that "Content-Length" is not set in the request i.e.
>> > (Transfer-Encoding = chunked), so I cannot take the in length from the
>> HTTP
>> > header - it must be really "computed".
>> >
>> > Additional difficulty is that I don't want to serialize incoming and
>> > outgoing messages toString() as it will be a performance issue.
>> >
>> > Is there any way to get these sizes from the Input/OutputStream? Is
>> there a
>> > way to inject my custom stream wrappers that will be counting number of
>> > bytes read/written?
>>
>> You would need to write an interceptor that would live fairly early in
>> the interceptor chain that would do something like:
>>
>> InputStream in = message.getContent(InputStream.class);
>> in = new MyLengthRecordingStream(in);
>> message.setContent(InputSteam.class, in);
>>
>> The rest of the processing would then use your input stream and at the
>> end of all the processing, you would have the length.
>>
>>
>> --
>> Daniel Kulp
>> dkulp@apache.org - http://dankulp.com/blog
>> Talend Community Coder - http://coders.talend.com
>>
>>
>

Re: How to get in/out message length in the interceptors?

Posted by Przemyslaw Bielicki <pb...@gmail.com>.
Hi Daniel,

thx a lot for the tip. In fact I discovered this solution five minutes
after asking the question ;)

FYI for the input stream I use RECEIVE phase to register the wrapper and
INVOKE phase to read bytes read count.
For the output stream I use PRE_STREAM phase to register the wrapper and
POST_STREAM phase to read bytes written count.

Cheers,
Przemyslaw


On Tue, Jun 10, 2014 at 9:30 PM, Daniel Kulp <dk...@apache.org> wrote:

>
> On Jun 10, 2014, at 10:27 AM, Przemyslaw Bielicki <pb...@gmail.com>
> wrote:
>
> > I need to know the input and output message size in my interceptors.
> >
> > I assume that "Content-Length" is not set in the request i.e.
> > (Transfer-Encoding = chunked), so I cannot take the in length from the
> HTTP
> > header - it must be really "computed".
> >
> > Additional difficulty is that I don't want to serialize incoming and
> > outgoing messages toString() as it will be a performance issue.
> >
> > Is there any way to get these sizes from the Input/OutputStream? Is
> there a
> > way to inject my custom stream wrappers that will be counting number of
> > bytes read/written?
>
> You would need to write an interceptor that would live fairly early in the
> interceptor chain that would do something like:
>
> InputStream in = message.getContent(InputStream.class);
> in = new MyLengthRecordingStream(in);
> message.setContent(InputSteam.class, in);
>
> The rest of the processing would then use your input stream and at the end
> of all the processing, you would have the length.
>
>
> --
> Daniel Kulp
> dkulp@apache.org - http://dankulp.com/blog
> Talend Community Coder - http://coders.talend.com
>
>

Re: How to get in/out message length in the interceptors?

Posted by Daniel Kulp <dk...@apache.org>.
On Jun 10, 2014, at 10:27 AM, Przemyslaw Bielicki <pb...@gmail.com> wrote:

> I need to know the input and output message size in my interceptors.
> 
> I assume that "Content-Length" is not set in the request i.e.
> (Transfer-Encoding = chunked), so I cannot take the in length from the HTTP
> header - it must be really "computed".
> 
> Additional difficulty is that I don't want to serialize incoming and
> outgoing messages toString() as it will be a performance issue.
> 
> Is there any way to get these sizes from the Input/OutputStream? Is there a
> way to inject my custom stream wrappers that will be counting number of
> bytes read/written?

You would need to write an interceptor that would live fairly early in the interceptor chain that would do something like:

InputStream in = message.getContent(InputStream.class);
in = new MyLengthRecordingStream(in);
message.setContent(InputSteam.class, in);

The rest of the processing would then use your input stream and at the end of all the processing, you would have the length.


-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com