You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Thorsten Mauch <ma...@imkenberg.de> on 2008/11/01 21:47:47 UTC
Re: Can a Serializer acceess the Request Object ?
Thanks a lot. The second solution works for me ( cocoon 2.1.11) .
contextualize ist called only once, but is easy to store the context Object.
This save me a lot work :)
Thorsten
Andre Juffer schrieb:
> Hi,
>
> with reference to a previous discussion about accessing the
> HttpSession object in a possibly cocoon unrelated Java class (see [1]
> below), would the following work:
>
> public interface MyInterface {
> void someMethod(..);
> }
>
> public interface UserRepository {
> User getUser(Long id);
> }
>
> public class MyInterfaceImp implements MyInterface, Contextualizable {
>
> private UserRepository userRepository;
> private HttpServletRequest ||request;
>
> public MyInterfaceImp(UserRepository userRepository)
> {
> this.userRepository = userRepository;
> this.request = null;
> }
>
> public void contextualize(Context context) throws ContextException
> {
> this.request =
> org.apache.cocoon.components.ContextHelper.getRequest(context);
> }
>
> public void someMethod(..)
> {
> HttpSession session = this.request.getSession();
> Long id = session.getAttribute("userid");
> User user = this.userRepository.getUser(id);
> ....
> }
> }
>
> The MyInterface is completely unrelated to Cocoon (version 2.2).
> Should the MyInterfaceImpl would be defined as a cocoon component in
> the sitemap? Or in some Spring configuration file? The someMethod
> would be called by means of aspects, pointcuts and join points (see
> [1]) (this already works) so I also would need to inform the Spring
> AOP setup about it and this would only be aware of the MyInterface.
> The Session would store an identifier to recognize an user.
>
> It was actually not clear to me whether the "void
> contextualize(Context context)" is called for every new request. If
> not, the MyInterfaceImpl possibly could be implemented as follows.
>
> public class MyInterfaceImp implements MyInterface, Contextualizable {
>
> private Context context;
> private UserRepository userRepository;
>
> public MyInterfaceImp(UserRepository userRepository)
> {
> this.context = null;
> this.userRepository = userRepository;
> }
>
> public void contextualize(Context context) throws ContextException
> {
> this.context = context;
> }
>
> public void someMethod(..)
> {
> HttpServletRequest request =
> org.apache.cocoon.components.ContextHelper.getRequest(this.context);
> HttpSession session = this.request.getSession();
> Long id = session.getAttribute("userid");
> User user = this.userRepository.getUser(id);
> ....
> }
> }
>
> Thanks,
> Andre
>
> [1] http://www.mail-archive.com/users@cocoon.apache.org/msg43673.html
>
>
>
> Jason Johnston wrote:
>> On 10/27/2008 05:21 AM, Thorsten Mauch wrote:
>>> Hi
>>> I wonder if a Serializer can access the request object ? My problem is
>>> that i want to pass a java Object to the Serializer that is created
>>> somewhere else. Is this possible ?
>>
>>
>> Your serializer can implement the Contextualizable interface, and get
>> access to the org.apache.cocoon.environment.Request object from the
>> injected Context object:
>>
>> public void contextualize(Context context) throws ContextException {
>> Request req = org.apache.cocoon.components
>> .ContextHelper.getRequest(context);
>> }
>>
>> ---------------------------------------------------------------------
>> 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: Can a Serializer acceess the Request Object ?
Posted by Andre Juffer <aj...@sun3.oulu.fi>.
Hi Thorsten,
so you actually went for the second option! That's very interesting, I
did not actually tried this all myself, but it was just a thought of how
one could possibly do this. In any case, I will now try this also myself
with cocoon 2.2 and see that I get this to work as well.
Andre.
Thorsten Mauch wrote:
> Thanks a lot. The second solution works for me ( cocoon 2.1.11) .
> contextualize ist called only once, but is easy to store the context
> Object.
> This save me a lot work :)
>
> Thorsten
>
>
>
>
> Andre Juffer schrieb:
>> Hi,
>>
>> with reference to a previous discussion about accessing the
>> HttpSession object in a possibly cocoon unrelated Java class (see [1]
>> below), would the following work:
>>
>> public interface MyInterface {
>> void someMethod(..);
>> }
>>
>> public interface UserRepository {
>> User getUser(Long id);
>> }
>>
>> public class MyInterfaceImp implements MyInterface, Contextualizable {
>>
>> private UserRepository userRepository;
>> private HttpServletRequest ||request;
>>
>> public MyInterfaceImp(UserRepository userRepository)
>> {
>> this.userRepository = userRepository;
>> this.request = null;
>> }
>>
>> public void contextualize(Context context) throws ContextException
>> {
>> this.request =
>> org.apache.cocoon.components.ContextHelper.getRequest(context);
>> }
>>
>> public void someMethod(..)
>> {
>> HttpSession session = this.request.getSession();
>> Long id = session.getAttribute("userid");
>> User user = this.userRepository.getUser(id);
>> ....
>> }
>> }
>>
>> The MyInterface is completely unrelated to Cocoon (version 2.2).
>> Should the MyInterfaceImpl would be defined as a cocoon component in
>> the sitemap? Or in some Spring configuration file? The someMethod
>> would be called by means of aspects, pointcuts and join points (see
>> [1]) (this already works) so I also would need to inform the Spring
>> AOP setup about it and this would only be aware of the MyInterface.
>> The Session would store an identifier to recognize an user.
>>
>> It was actually not clear to me whether the "void
>> contextualize(Context context)" is called for every new request. If
>> not, the MyInterfaceImpl possibly could be implemented as follows.
>>
>> public class MyInterfaceImp implements MyInterface, Contextualizable {
>>
>> private Context context;
>> private UserRepository userRepository;
>>
>> public MyInterfaceImp(UserRepository userRepository)
>> {
>> this.context = null;
>> this.userRepository = userRepository;
>> }
>>
>> public void contextualize(Context context) throws ContextException
>> {
>> this.context = context;
>> }
>>
>> public void someMethod(..)
>> {
>> HttpServletRequest request =
>> org.apache.cocoon.components.ContextHelper.getRequest(this.context);
>> HttpSession session = this.request.getSession();
>> Long id = session.getAttribute("userid");
>> User user = this.userRepository.getUser(id);
>> ....
>> }
>> }
>>
>> Thanks,
>> Andre
>>
>> [1] http://www.mail-archive.com/users@cocoon.apache.org/msg43673.html
>>
>>
>>
>> Jason Johnston wrote:
>>> On 10/27/2008 05:21 AM, Thorsten Mauch wrote:
>>>> Hi
>>>> I wonder if a Serializer can access the request object ? My problem is
>>>> that i want to pass a java Object to the Serializer that is created
>>>> somewhere else. Is this possible ?
>>>
>>>
>>> Your serializer can implement the Contextualizable interface, and get
>>> access to the org.apache.cocoon.environment.Request object from the
>>> injected Context object:
>>>
>>> public void contextualize(Context context) throws ContextException {
>>> Request req = org.apache.cocoon.components
>>> .ContextHelper.getRequest(context);
>>> }
>>>
>>> ---------------------------------------------------------------------
>>> 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
>
--
Andre H. Juffer | Email: Andre.Juffer@oulu.fi
The Biocenter and | WWW: www.biochem.oulu.fi/Biocomputing/
the Dep. of Biochemistry | Fax: +358-8-553-1141
University of Oulu, Finland | Phone: +358-8-553 1161
Triacle Biocomputing | WWW: www.triacle-bc.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org