You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@abdera.apache.org by Glen Daniels <gl...@wso2.com> on 2008/05/14 05:39:18 UTC

Passing an Object from the servlet through to the provider?

Hey Abderites:

So here's my situation.  I've got an Abdera server (i.e. AbderaServlet 
in Tomcat) running that needs to share some information with another 
servlet.  In particular, the non-AbderaServlet (let's call it Servlet1) 
needs to create an Object and then in an ideal world pass that through 
to the AbderaServlet in such a way that the Provider instance that gets 
created has access to it.  Make sense?

The best solution for me would be to put something into the 
ServletContext, and then somehow get access to that later - but I can't 
seem to do that currently.

Since the initialization step doesn't really need to be done until the 
first actual request, and I've already got a solid connection between my 
Provider and my TargetResolver, I figured one way would be something 
like (pseudocode):

TargetResolver.resolve(Request request) {
   ServletRequestContext context = (ServletRequestContext)request;
   if (thing == null) {
     thing = context.getServletContext().getAttribute("thing");
   }
}

A patch that would just carry the ServletContext in addition to the 
HttpServletRequest in each ServletRequestContext would be very easy to 
do in Abdera, and would get me what I need.  Am happy to contribute same.

Is there a better way to do this?  I noticed the ManagedProvider stuff 
but it looks like you can only pass Strings in there, and only in very 
specific ways.  Perhaps some kind of more general initialization context 
that I'm missing?  The above patch would solve my problem later but not 
in v0.4.0.

The solution I've come up with for now is to subclass the AbderaServlet, 
and add the ServletContext to every HttpRequest as an attribute - that 
lets me do basically the same trick, but it seems... less than optimal.

Thoughts/comments?

Thanks,
--Glen

Re: Passing an Object from the servlet through to the provider?

Posted by Glen Daniels <gl...@wso2.com>.
Hi James:

James M Snell wrote:
> You can create a Filter.  The filters are invoked immediately before the 
> Provider.  I've used them to set properties on the RequestContext prior 
> to passing it off to the Provider for handling.

The problem then becomes that there's no way to get the right object to 
the Filter. :)

>> If AbderaServlet made the ServletContext available in the same way it 
>> makes the HttpServletRequest available, I wouldn't need the subclassed 
>> servlet or the per-request overhead.
> 
> True. Ok, I'll see what I can do... unless, of course, you want to 
> submit a patch :-)

Patch on the way.

Thanks,
--Glen

Re: Passing an Object from the servlet through to the provider?

Posted by James M Snell <ja...@gmail.com>.

Glen Daniels wrote:
> Hi James!
> 
> James M Snell wrote:
>> RequestContext.setAttribute(Scope scope, String name, Object value) 
>> ... where Scope == Scope.REQUEST or Scope.SESSION
>>
>> Does that work for what you need?
> 
> Nope, because that implies you have access to the RequestContext, which 
> I don't.  Even if I subclass AbderaServlet, there's no hook in service() 
> that lets me get at the ServletRequestContext in between creation and 
> passing it to the Provider/TargetResolver.
> 

You can create a Filter.  The filters are invoked immediately before the 
Provider.  I've used them to set properties on the RequestContext prior 
to passing it off to the Provider for handling.

> To be clear, here's my situation:
> 
> class Servlet1 {
>   init() {
>     Object thing = createObject(); // Here's my thing
>     servletContext.setAttribute("thing", thing);  // Store it somewhere
>   }
> }
> 
> class MyServlet extends AbderaServlet {
>   service(request, response) {
>     // I can't get at the ServletContext from within Abdera, so...
>     Object thing = servletContext.getAttribute("thing");
>     // ...put it somewhere I *can* get to
>     request.setAttribute("thing", thing);
>     super.service(request, response);
>   }
> }
> 
> If AbderaServlet made the ServletContext available in the same way it 
> makes the HttpServletRequest available, I wouldn't need the subclassed 
> servlet or the per-request overhead.

True. Ok, I'll see what I can do... unless, of course, you want to 
submit a patch :-)

- James

> 
> --Glen
> 
>> Glen Daniels wrote:
>>> Hey Abderites:
>>>
>>> So here's my situation.  I've got an Abdera server (i.e. 
>>> AbderaServlet in Tomcat) running that needs to share some information 
>>> with another servlet.  In particular, the non-AbderaServlet (let's 
>>> call it Servlet1) needs to create an Object and then in an ideal 
>>> world pass that through to the AbderaServlet in such a way that the 
>>> Provider instance that gets created has access to it.  Make sense?
>>>
>>> The best solution for me would be to put something into the 
>>> ServletContext, and then somehow get access to that later - but I 
>>> can't seem to do that currently.
>>>
>>> Since the initialization step doesn't really need to be done until 
>>> the first actual request, and I've already got a solid connection 
>>> between my Provider and my TargetResolver, I figured one way would be 
>>> something like (pseudocode):
>>>
>>> TargetResolver.resolve(Request request) {
>>>   ServletRequestContext context = (ServletRequestContext)request;
>>>   if (thing == null) {
>>>     thing = context.getServletContext().getAttribute("thing");
>>>   }
>>> }
>>>
>>> A patch that would just carry the ServletContext in addition to the 
>>> HttpServletRequest in each ServletRequestContext would be very easy 
>>> to do in Abdera, and would get me what I need.  Am happy to 
>>> contribute same.
>>>
>>> Is there a better way to do this?  I noticed the ManagedProvider 
>>> stuff but it looks like you can only pass Strings in there, and only 
>>> in very specific ways.  Perhaps some kind of more general 
>>> initialization context that I'm missing?  The above patch would solve 
>>> my problem later but not in v0.4.0.
>>>
>>> The solution I've come up with for now is to subclass the 
>>> AbderaServlet, and add the ServletContext to every HttpRequest as an 
>>> attribute - that lets me do basically the same trick, but it seems... 
>>> less than optimal.
>>>
>>> Thoughts/comments?
>>>
>>> Thanks,
>>> --Glen
>>>
>>
> 

Re: Passing an Object from the servlet through to the provider?

Posted by Glen Daniels <gl...@wso2.com>.
Hi James!

James M Snell wrote:
> RequestContext.setAttribute(Scope scope, String name, Object value) ... 
> where Scope == Scope.REQUEST or Scope.SESSION
> 
> Does that work for what you need?

Nope, because that implies you have access to the RequestContext, which 
I don't.  Even if I subclass AbderaServlet, there's no hook in service() 
that lets me get at the ServletRequestContext in between creation and 
passing it to the Provider/TargetResolver.

To be clear, here's my situation:

class Servlet1 {
   init() {
     Object thing = createObject(); // Here's my thing
     servletContext.setAttribute("thing", thing);  // Store it somewhere
   }
}

class MyServlet extends AbderaServlet {
   service(request, response) {
     // I can't get at the ServletContext from within Abdera, so...
     Object thing = servletContext.getAttribute("thing");
     // ...put it somewhere I *can* get to
     request.setAttribute("thing", thing);
     super.service(request, response);
   }
}

If AbderaServlet made the ServletContext available in the same way it 
makes the HttpServletRequest available, I wouldn't need the subclassed 
servlet or the per-request overhead.

--Glen

> Glen Daniels wrote:
>> Hey Abderites:
>>
>> So here's my situation.  I've got an Abdera server (i.e. AbderaServlet 
>> in Tomcat) running that needs to share some information with another 
>> servlet.  In particular, the non-AbderaServlet (let's call it 
>> Servlet1) needs to create an Object and then in an ideal world pass 
>> that through to the AbderaServlet in such a way that the Provider 
>> instance that gets created has access to it.  Make sense?
>>
>> The best solution for me would be to put something into the 
>> ServletContext, and then somehow get access to that later - but I 
>> can't seem to do that currently.
>>
>> Since the initialization step doesn't really need to be done until the 
>> first actual request, and I've already got a solid connection between 
>> my Provider and my TargetResolver, I figured one way would be 
>> something like (pseudocode):
>>
>> TargetResolver.resolve(Request request) {
>>   ServletRequestContext context = (ServletRequestContext)request;
>>   if (thing == null) {
>>     thing = context.getServletContext().getAttribute("thing");
>>   }
>> }
>>
>> A patch that would just carry the ServletContext in addition to the 
>> HttpServletRequest in each ServletRequestContext would be very easy to 
>> do in Abdera, and would get me what I need.  Am happy to contribute same.
>>
>> Is there a better way to do this?  I noticed the ManagedProvider stuff 
>> but it looks like you can only pass Strings in there, and only in very 
>> specific ways.  Perhaps some kind of more general initialization 
>> context that I'm missing?  The above patch would solve my problem 
>> later but not in v0.4.0.
>>
>> The solution I've come up with for now is to subclass the 
>> AbderaServlet, and add the ServletContext to every HttpRequest as an 
>> attribute - that lets me do basically the same trick, but it seems... 
>> less than optimal.
>>
>> Thoughts/comments?
>>
>> Thanks,
>> --Glen
>>
> 

Re: Passing an Object from the servlet through to the provider?

Posted by James M Snell <ja...@gmail.com>.
RequestContext.setAttribute(Scope scope, String name, Object value) ... 
where Scope == Scope.REQUEST or Scope.SESSION

Does that work for what you need?

- James

Glen Daniels wrote:
> Hey Abderites:
> 
> So here's my situation.  I've got an Abdera server (i.e. AbderaServlet 
> in Tomcat) running that needs to share some information with another 
> servlet.  In particular, the non-AbderaServlet (let's call it Servlet1) 
> needs to create an Object and then in an ideal world pass that through 
> to the AbderaServlet in such a way that the Provider instance that gets 
> created has access to it.  Make sense?
> 
> The best solution for me would be to put something into the 
> ServletContext, and then somehow get access to that later - but I can't 
> seem to do that currently.
> 
> Since the initialization step doesn't really need to be done until the 
> first actual request, and I've already got a solid connection between my 
> Provider and my TargetResolver, I figured one way would be something 
> like (pseudocode):
> 
> TargetResolver.resolve(Request request) {
>   ServletRequestContext context = (ServletRequestContext)request;
>   if (thing == null) {
>     thing = context.getServletContext().getAttribute("thing");
>   }
> }
> 
> A patch that would just carry the ServletContext in addition to the 
> HttpServletRequest in each ServletRequestContext would be very easy to 
> do in Abdera, and would get me what I need.  Am happy to contribute same.
> 
> Is there a better way to do this?  I noticed the ManagedProvider stuff 
> but it looks like you can only pass Strings in there, and only in very 
> specific ways.  Perhaps some kind of more general initialization context 
> that I'm missing?  The above patch would solve my problem later but not 
> in v0.4.0.
> 
> The solution I've come up with for now is to subclass the AbderaServlet, 
> and add the ServletContext to every HttpRequest as an attribute - that 
> lets me do basically the same trick, but it seems... less than optimal.
> 
> Thoughts/comments?
> 
> Thanks,
> --Glen
>