cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thorsten Mauch <ma...@imkenberg.de>
Subject Re: Can a Serializer acceess the Request Object ?
Date Sat, 01 Nov 2008 20:47:47 GMT
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


Mime
View raw message