You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Laurent Perez <ha...@gmail.com> on 2005/09/20 18:07:47 UTC

Where do you access HttpResponse from ?

Hello

I would like to access the
org.apache.cocoon.environment.http.HttpResponse object from within
Cocoon, but I don't understand where or how to do that (sitemap,
internal component, flow)

I can get cocoon.response from the flow, but this interface doesn't
have the methods that I'd like to access : for example,
getBufferSize() is missing.

Can I access this within Cocoon itself ?
Thanks

Laurent

-- 
<a href="http://in-pocket.blogspot.com">http://in-pocket.blogspot.com
- Mobile world, technology and more</a>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Laurent Perez <ha...@gmail.com>.
> > The easiest that comes to mind is to use processPipelineTo in
> > flowscript (see http://cocoon.apache.org/2.1/userdocs/flow/api.html)
> > to save the generated output.

Well, I was playing around with the Source interface obtained from a
SourceResolver, which has a getContentLength() method :

var res = cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.ROLE);
var src = res.resolveURI("cocoon://foo.xml");
log.debug("src length is " + src.getContentLength());

However, it always returns "-1", don't really know why since foo.xml
renders a non null XML document. Plus, isavailable() always returns
"true", even if I try resolveURI() on nonexistent patterns.

Anyway, I'll try processPipelineTo(), thanks :)

Laurent

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Ralph Goers <Ra...@dslextreme.com>.
That sounds like a much better solution than what I proposed.

Ralph

Bertrand Delacretaz wrote:

> Le 21 sept. 05, à 23:52, Laurent Perez a écrit :
>
>>> ...so you want to execute biz logic depending on the total size of 
>>> the http
>>> response ...
>>
>>
>> yep, exactly : I'm delivering content to poor browsers which, for
>> example, will not accept more than 50KB from the http response, the
>> biz logic is nothing too fancy, it only logs warnings about response
>> being too rich...
>
>
> The easiest that comes to mind is to use processPipelineTo in 
> flowscript (see http://cocoon.apache.org/2.1/userdocs/flow/api.html) 
> to save the generated output.
>
> Then, you can easily measure its size and decide what to do with it.
>
> -Bertrand


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 21 sept. 05, à 23:52, Laurent Perez a écrit :

>> ...so you want to execute biz logic depending on the total size of 
>> the http
>> response ...
>
> yep, exactly : I'm delivering content to poor browsers which, for
> example, will not accept more than 50KB from the http response, the
> biz logic is nothing too fancy, it only logs warnings about response
> being too rich...

The easiest that comes to mind is to use processPipelineTo in 
flowscript (see http://cocoon.apache.org/2.1/userdocs/flow/api.html) to 
save the generated output.

Then, you can easily measure its size and decide what to do with it.

-Bertrand

Re: Where do you access HttpResponse from ?

Posted by Ralph Goers <Ra...@dslextreme.com>.
If you use the RequestListener interface you have access to the 
Environment. If you look at SampleRequestListener you will see it obtain 
the Request. A similar method can be used to get the response (i.e. - 
ObjectModelHelper.getResponse()).

Having said that, HttpResponse simply wraps 
javax.servlet.http.HttpServletResponse. The getBufferSize() method in 
HttpResponse consists of

    public int getBufferSize() {
        return this.res.getBufferSize();
    }

this.res is the HttpServletResponse.

Now getBufferSize() is defined in javax.servlet.ServletResponse as

Returns the actual buffer size used for the response. If no buffering is 
used, this method returns 0.

This leads me to believe that it is only the size of the buffer, not the 
size of the response data. 

Unfortunately, what you may have to do is to extend Cocoon's 
HttpResponse with your own and replace the HttpResponse in the 
Environment with yours in the RequestListener's onRequestStart() method 
and then put the real one back in the onRequestEnd() method after 
appropriately updating it.

Ralph




Laurent Perez wrote:

>>so you want to execute biz logic depending on the total size of the http
>>response ...
>>    
>>
>
>yep, exactly : I'm delivering content to poor browsers which, for
>example, will not accept more than 50KB from the http response, the
>biz logic is nothing too fancy, it only logs warnings about response
>being too rich.
>
>  
>
>>I don't think it is possible to retrieve response headers using the
>>servlet API - however check the api docs to be sure.
>>    
>>
>
>well, there is org.apache.cocoon.environment.http.HttpResponse which
>implements the Response interface, and offers int getBufferSize() and
>ServletOutputStream getOutputStream(). getBufferSize() sounds good,
>however it's not documented in 2.1.7 javadocs :/
>
>Or I could tell the Cocoon servlet itself to write into a
>java.io.ByteArrayOutputStream, then do a size() to get the content
>length, process my logic, then use
>writeTo(HttpResponse.getOutputStream) to deliver the stream to the
>client, however this sounds overkill.
>
>what I miss is : can I access
>org.apache.cocoon.environment.http.HttpResponse from a "proper" place,
>or should I have to hack its source directly ? Because this class uses
>setContentLength(), I could probably fit my logic call in this method.
>
>laurent
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>  
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Laurent Perez <ha...@gmail.com>.
> so you want to execute biz logic depending on the total size of the http
> response ...

yep, exactly : I'm delivering content to poor browsers which, for
example, will not accept more than 50KB from the http response, the
biz logic is nothing too fancy, it only logs warnings about response
being too rich.

> I don't think it is possible to retrieve response headers using the
> servlet API - however check the api docs to be sure.

well, there is org.apache.cocoon.environment.http.HttpResponse which
implements the Response interface, and offers int getBufferSize() and
ServletOutputStream getOutputStream(). getBufferSize() sounds good,
however it's not documented in 2.1.7 javadocs :/

Or I could tell the Cocoon servlet itself to write into a
java.io.ByteArrayOutputStream, then do a size() to get the content
length, process my logic, then use
writeTo(HttpResponse.getOutputStream) to deliver the stream to the
client, however this sounds overkill.

what I miss is : can I access
org.apache.cocoon.environment.http.HttpResponse from a "proper" place,
or should I have to hack its source directly ? Because this class uses
setContentLength(), I could probably fit my logic call in this method.

laurent

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Jorg Heymans <jh...@domek.be>.
Laurent Perez wrote:

> I am trying to access the Content-length header from the http response
> directly within my Cocoon app, and if its value is greater than X
> bytes, then trigger some already written business logic (with a
> <map:call function="bizlogic">).
> 
so you want to execute biz logic depending on the total size of the http
response ...

> I had a look at the RequestListener interface, which can - I assume -
> give back the Response from an Environment, using ObjectModelHelper.
> However I don't know how to obtain the http Content-length header from
> the Response interface.
> 

I don't think it is possible to retrieve response headers using the
servlet API - however check the api docs to be sure.


Jorg


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Laurent Perez <ha...@gmail.com>.
> What exactly are you hoping to achieve with this object? There is
> usually more than one way to do things in cocoon, so if you explain a
> bit more we might be able to help more.

Hi Jorg

I am trying to access the Content-length header from the http response
directly within my Cocoon app, and if its value is greater than X
bytes, then trigger some already written business logic (with a
<map:call function="bizlogic">).

I had a look at the RequestListener interface, which can - I assume -
give back the Response from an Environment, using ObjectModelHelper.
However I don't know how to obtain the http Content-length header from
the Response interface.

Hope this clears up a little bit

Laurent

> 
> Regards
> Jorg
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
> 
> 


-- 
<a href="http://in-pocket.blogspot.com">http://in-pocket.blogspot.com
- Mobile world, technology and more</a>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: Where do you access HttpResponse from ?

Posted by Jorg Heymans <jh...@domek.be>.
Laurent Perez wrote:

> I would like to access the
> org.apache.cocoon.environment.http.HttpResponse object from within
> Cocoon, but I don't understand where or how to do that (sitemap,
> internal component, flow)
> 
> I can get cocoon.response from the flow, but this interface doesn't
> have the methods that I'd like to access : for example,
> getBufferSize() is missing.
> 
> Can I access this within Cocoon itself ?

What exactly are you hoping to achieve with this object? There is
usually more than one way to do things in cocoon, so if you explain a
bit more we might be able to help more.

Regards
Jorg


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org