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/[email protected]/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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
Andre H. Juffer | Phone: +358-8-553 1161
The Biocenter and | Fax: +358-8-553-1141
the Dep. of Biochemistry | Email: [EMAIL PROTECTED]
University of Oulu, Finland | WWW: www.biochem.oulu.fi/Biocomputing/
NordProt | WWW: www.nordprot.org
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]