You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mu...@sectra.se on 2009/09/03 17:17:22 UTC
how to unwrap a Request from RequestFacade
Hi,
I am using tomcat 5.5 and just wanted to write a simple servlet to get
some information using StandardManager such as activeSessions,
expriedSessions, sessionCounter etc.. I am not that familiar with these
classes. I noticed when the request comes into my servlet it comes as
RequestFacade where the org.apache.catalina.connector.Request is wrapped
inside it. I haven't figured out how to unwrap the Request object so that
I can get the Context from which I can get the Manager (StandardManager)
to access the information I need. Any help with a snippet of code to get
the Request Object from RequestFacade will be appreciated.
Thanks,
------------------------------------------
Muthu
Re: how to unwrap a Request from RequestFacade
Posted by Mu...@sectra.se.
So are you saying that I can't get what I want using TC 5.5? Is upgrading
to TC 6 is the only option?
Thanks
------------------------------------------
Muthu
From:
"Bill Barker" <bi...@verizon.net>
To:
users@tomcat.apache.org
Date:
09/04/2009 11:44 PM
Subject:
Re: how to unwrap a Request from RequestFacade
Sent by:
news <ne...@ger.gmane.org>
<Mu...@sectra.se> wrote in message
news:OF85B6A7DA.49853BD1-ON85257627.006211EE-85257627.0065728A@sectra.se..
.
> Thanks Chris. Still not having any luck. Let me give you the snippet of
> the code so that you can understand the problem better:
>
> I have a servlet like this
>
> protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
> // some code
>
>
> RequestFacade rf = (RequestFacade) request; // this
> throws a class cast exception
> Request realRequest = new
> MyRequestFacade().getRequest(rf);
> Context ctx = _realRequest.getContext();
> StandardManager sessionMgr = (StandardManager)
> ctx.getManager();
> sessionMgr.getActiveSessions();
>
> }
>
>
> When I debug I see that the request object coming into doGet method of
the
> servlet is RequestFacade but I can't assign it directly to a
RequestFacade
> object or can't type cast it. Although when I type cast it I don't get
any
> compilation error but at runtime it throws as a ClassCastException.
>
> Am I doing something stupid here? :-( Looks simple it is so frustrating
as
> when I debug I can open the RequestFacade object and see the Request
> Object, Context, Manager and all the information I need but I don't know
> how to access it.
>
> Any thoughts from fresh pair eyes?
>
Yes, TC 5.5 still has the server ClassLoader, so the RequestFacade is
loaded
in a ClassLoader that isn't the parent of your ClassLoader. You need to
do
one or more of the following:
1) upgrade to TC 6.0 so that the Request and RequestFacade are in your
ClassLoader hierarchy (in the default settings)
2) declare your webapp 'privileged' in your context.xml (giving access to
the server ClassLoader).
3) I think all the info that you want is exposed by JMX, so use JMX to
retrieve it.
4) use introspection to grab the fields. On 5.5 this will be painful,
since
you have to use introspection everywhere (none of the classes will be in
your ClassLoader hierachy)
5) change your catalina.properties file to have the same hierachy as TC 6
(but will get over-written on upgrades).
> Thanks,
> ------------------------------------------
> Muthu Chandir
> Sectra North America, Inc.
> Phone: (800)307-4425 ext 222
> Email: mu-cha@sectra.com
>
>
>
> From:
> Christopher Schultz <ch...@christopherschultz.net>
> To:
> Tomcat Users List <us...@tomcat.apache.org>
> Date:
> 09/04/2009 10:28 AM
> Subject:
> Re: how to unwrap a Request from RequestFacade
>
>
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bill,
>
> On 9/3/2009 10:47 PM, Bill Barker wrote:
>> "Christopher Schultz" <ch...@christopherschultz.net> wrote in message
>> news:4AA03CA3.4010405@christopherschultz.net...
>> Muthu,
>>
>> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>>>>> I am using tomcat 5.5 and just wanted to write a simple servlet to
> get
>>>>> some information using StandardManager such as activeSessions,
>>>>> expriedSessions, sessionCounter etc.. I am not that familiar with
> these
>>>>> classes. I noticed when the request comes into my servlet it comes
as
>>>>> RequestFacade where the org.apache.catalina.connector.Request is
> wrapped
>>>>> inside it. I haven't figured out how to unwrap the Request object so
> that
>>>>> I can get the Context from which I can get the Manager
> (StandardManager)
>>>>> to access the information I need. Any help with a snippet of code to
> get
>>>>> the Request Object from RequestFacade will be appreciated.
>>
>> Maybe try something like this:
>>
>> public class SneekyRequestFacade
>> extends RequestFacade
>> {
>> public SneekyRequestFacade() { super(null); }
>>
>> public Request getRequest(RequestFacade rf)
>> {
>> return rf.request;
>> }
>> }
>>
>> Now:
>>
>> RequestFacade rf = ...; // get your requestfacade
>> Request req = new SneekyRequestFacade().getRequest(rf);
>>
>> I think that ought to do it. Isn't OO abuse great?
>>
>>
>>> It is great ;). But you save a couple of lines by just doing straight
>>> introspection.
>
> I had thought about that, but I thought the JVM prevented code from
> getting-around access privileges by using introspection. Or is that only
> when a SecurityManager is running?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqhImUACgkQ9CaO5/Lv0PDVkgCaA9UWhDHNXKdfN2mSPZN1lauK
> Z3oAnRphaMCX6vkmjvLH+t/tmUIxp52m
> =qxEb
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Bill Barker <bi...@verizon.net>.
<Mu...@sectra.se> wrote in message
news:OF85B6A7DA.49853BD1-ON85257627.006211EE-85257627.0065728A@sectra.se...
> Thanks Chris. Still not having any luck. Let me give you the snippet of
> the code so that you can understand the problem better:
>
> I have a servlet like this
>
> protected void doGet(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
> // some code
>
>
> RequestFacade rf = (RequestFacade) request; // this
> throws a class cast exception
> Request realRequest = new
> MyRequestFacade().getRequest(rf);
> Context ctx = _realRequest.getContext();
> StandardManager sessionMgr = (StandardManager)
> ctx.getManager();
> sessionMgr.getActiveSessions();
>
> }
>
>
> When I debug I see that the request object coming into doGet method of the
> servlet is RequestFacade but I can't assign it directly to a RequestFacade
> object or can't type cast it. Although when I type cast it I don't get any
> compilation error but at runtime it throws as a ClassCastException.
>
> Am I doing something stupid here? :-( Looks simple it is so frustrating as
> when I debug I can open the RequestFacade object and see the Request
> Object, Context, Manager and all the information I need but I don't know
> how to access it.
>
> Any thoughts from fresh pair eyes?
>
Yes, TC 5.5 still has the server ClassLoader, so the RequestFacade is loaded
in a ClassLoader that isn't the parent of your ClassLoader. You need to do
one or more of the following:
1) upgrade to TC 6.0 so that the Request and RequestFacade are in your
ClassLoader hierarchy (in the default settings)
2) declare your webapp 'privileged' in your context.xml (giving access to
the server ClassLoader).
3) I think all the info that you want is exposed by JMX, so use JMX to
retrieve it.
4) use introspection to grab the fields. On 5.5 this will be painful, since
you have to use introspection everywhere (none of the classes will be in
your ClassLoader hierachy)
5) change your catalina.properties file to have the same hierachy as TC 6
(but will get over-written on upgrades).
> Thanks,
> ------------------------------------------
> Muthu Chandir
> Sectra North America, Inc.
> Phone: (800)307-4425 ext 222
> Email: mu-cha@sectra.com
>
>
>
> From:
> Christopher Schultz <ch...@christopherschultz.net>
> To:
> Tomcat Users List <us...@tomcat.apache.org>
> Date:
> 09/04/2009 10:28 AM
> Subject:
> Re: how to unwrap a Request from RequestFacade
>
>
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bill,
>
> On 9/3/2009 10:47 PM, Bill Barker wrote:
>> "Christopher Schultz" <ch...@christopherschultz.net> wrote in message
>> news:4AA03CA3.4010405@christopherschultz.net...
>> Muthu,
>>
>> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>>>>> I am using tomcat 5.5 and just wanted to write a simple servlet to
> get
>>>>> some information using StandardManager such as activeSessions,
>>>>> expriedSessions, sessionCounter etc.. I am not that familiar with
> these
>>>>> classes. I noticed when the request comes into my servlet it comes as
>>>>> RequestFacade where the org.apache.catalina.connector.Request is
> wrapped
>>>>> inside it. I haven't figured out how to unwrap the Request object so
> that
>>>>> I can get the Context from which I can get the Manager
> (StandardManager)
>>>>> to access the information I need. Any help with a snippet of code to
> get
>>>>> the Request Object from RequestFacade will be appreciated.
>>
>> Maybe try something like this:
>>
>> public class SneekyRequestFacade
>> extends RequestFacade
>> {
>> public SneekyRequestFacade() { super(null); }
>>
>> public Request getRequest(RequestFacade rf)
>> {
>> return rf.request;
>> }
>> }
>>
>> Now:
>>
>> RequestFacade rf = ...; // get your requestfacade
>> Request req = new SneekyRequestFacade().getRequest(rf);
>>
>> I think that ought to do it. Isn't OO abuse great?
>>
>>
>>> It is great ;). But you save a couple of lines by just doing straight
>>> introspection.
>
> I had thought about that, but I thought the JVM prevented code from
> getting-around access privileges by using introspection. Or is that only
> when a SecurityManager is running?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqhImUACgkQ9CaO5/Lv0PDVkgCaA9UWhDHNXKdfN2mSPZN1lauK
> Z3oAnRphaMCX6vkmjvLH+t/tmUIxp52m
> =qxEb
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Mu...@sectra.se.
Thanks Chris. Still not having any luck. Let me give you the snippet of
the code so that you can understand the problem better:
I have a servlet like this
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// some code
RequestFacade rf = (RequestFacade) request; // this
throws a class cast exception
Request realRequest = new
MyRequestFacade().getRequest(rf);
Context ctx = _realRequest.getContext();
StandardManager sessionMgr = (StandardManager)
ctx.getManager();
sessionMgr.getActiveSessions();
}
When I debug I see that the request object coming into doGet method of the
servlet is RequestFacade but I can't assign it directly to a RequestFacade
object or can't type cast it. Although when I type cast it I don't get any
compilation error but at runtime it throws as a ClassCastException.
Am I doing something stupid here? :-( Looks simple it is so frustrating as
when I debug I can open the RequestFacade object and see the Request
Object, Context, Manager and all the information I need but I don't know
how to access it.
Any thoughts from fresh pair eyes?
Thanks,
------------------------------------------
Muthu Chandir
Sectra North America, Inc.
Phone: (800)307-4425 ext 222
Email: mu-cha@sectra.com
From:
Christopher Schultz <ch...@christopherschultz.net>
To:
Tomcat Users List <us...@tomcat.apache.org>
Date:
09/04/2009 10:28 AM
Subject:
Re: how to unwrap a Request from RequestFacade
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bill,
On 9/3/2009 10:47 PM, Bill Barker wrote:
> "Christopher Schultz" <ch...@christopherschultz.net> wrote in message
> news:4AA03CA3.4010405@christopherschultz.net...
> Muthu,
>
> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>>>> I am using tomcat 5.5 and just wanted to write a simple servlet to
get
>>>> some information using StandardManager such as activeSessions,
>>>> expriedSessions, sessionCounter etc.. I am not that familiar with
these
>>>> classes. I noticed when the request comes into my servlet it comes as
>>>> RequestFacade where the org.apache.catalina.connector.Request is
wrapped
>>>> inside it. I haven't figured out how to unwrap the Request object so
that
>>>> I can get the Context from which I can get the Manager
(StandardManager)
>>>> to access the information I need. Any help with a snippet of code to
get
>>>> the Request Object from RequestFacade will be appreciated.
>
> Maybe try something like this:
>
> public class SneekyRequestFacade
> extends RequestFacade
> {
> public SneekyRequestFacade() { super(null); }
>
> public Request getRequest(RequestFacade rf)
> {
> return rf.request;
> }
> }
>
> Now:
>
> RequestFacade rf = ...; // get your requestfacade
> Request req = new SneekyRequestFacade().getRequest(rf);
>
> I think that ought to do it. Isn't OO abuse great?
>
>
>> It is great ;). But you save a couple of lines by just doing straight
>> introspection.
I had thought about that, but I thought the JVM prevented code from
getting-around access privileges by using introspection. Or is that only
when a SecurityManager is running?
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkqhImUACgkQ9CaO5/Lv0PDVkgCaA9UWhDHNXKdfN2mSPZN1lauK
Z3oAnRphaMCX6vkmjvLH+t/tmUIxp52m
=qxEb
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Bill Barker <bi...@verizon.net>.
"Christopher Schultz" <ch...@christopherschultz.net> wrote in message
news:4AA12265.3020901@christopherschultz.net...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Bill,
>
> On 9/3/2009 10:47 PM, Bill Barker wrote:
>> "Christopher Schultz" <ch...@christopherschultz.net> wrote in message
>> news:4AA03CA3.4010405@christopherschultz.net...
>> Muthu,
>>
>> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>>>>> I am using tomcat 5.5 and just wanted to write a simple servlet to
>>>>> get
>>>>> some information using StandardManager such as activeSessions,
>>>>> expriedSessions, sessionCounter etc.. I am not that familiar with
>>>>> these
>>>>> classes. I noticed when the request comes into my servlet it comes as
>>>>> RequestFacade where the org.apache.catalina.connector.Request is
>>>>> wrapped
>>>>> inside it. I haven't figured out how to unwrap the Request object so
>>>>> that
>>>>> I can get the Context from which I can get the Manager
>>>>> (StandardManager)
>>>>> to access the information I need. Any help with a snippet of code to
>>>>> get
>>>>> the Request Object from RequestFacade will be appreciated.
>>
>> Maybe try something like this:
>>
>> public class SneekyRequestFacade
>> extends RequestFacade
>> {
>> public SneekyRequestFacade() { super(null); }
>>
>> public Request getRequest(RequestFacade rf)
>> {
>> return rf.request;
>> }
>> }
>>
>> Now:
>>
>> RequestFacade rf = ...; // get your requestfacade
>> Request req = new SneekyRequestFacade().getRequest(rf);
>>
>> I think that ought to do it. Isn't OO abuse great?
>>
>>
>>> It is great ;). But you save a couple of lines by just doing straight
>>> introspection.
>
> I had thought about that, but I thought the JVM prevented code from
> getting-around access privileges by using introspection. Or is that only
> when a SecurityManager is running?
>
No, without a SecurityManager, you can do pretty much anything with
introspection by at most calling field.setAccessible(true). This is how the
WebappClassLoader nulls out static fields when you undeploy a webapp. It's
also a big part of the reason that the server ClassLoader went away by
default, since it didn't really stop anyone from accessing Tomcat core
classes.
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqhImUACgkQ9CaO5/Lv0PDVkgCaA9UWhDHNXKdfN2mSPZN1lauK
> Z3oAnRphaMCX6vkmjvLH+t/tmUIxp52m
> =qxEb
> -----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bill,
On 9/3/2009 10:47 PM, Bill Barker wrote:
> "Christopher Schultz" <ch...@christopherschultz.net> wrote in message
> news:4AA03CA3.4010405@christopherschultz.net...
> Muthu,
>
> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>>>> I am using tomcat 5.5 and just wanted to write a simple servlet to get
>>>> some information using StandardManager such as activeSessions,
>>>> expriedSessions, sessionCounter etc.. I am not that familiar with these
>>>> classes. I noticed when the request comes into my servlet it comes as
>>>> RequestFacade where the org.apache.catalina.connector.Request is wrapped
>>>> inside it. I haven't figured out how to unwrap the Request object so that
>>>> I can get the Context from which I can get the Manager (StandardManager)
>>>> to access the information I need. Any help with a snippet of code to get
>>>> the Request Object from RequestFacade will be appreciated.
>
> Maybe try something like this:
>
> public class SneekyRequestFacade
> extends RequestFacade
> {
> public SneekyRequestFacade() { super(null); }
>
> public Request getRequest(RequestFacade rf)
> {
> return rf.request;
> }
> }
>
> Now:
>
> RequestFacade rf = ...; // get your requestfacade
> Request req = new SneekyRequestFacade().getRequest(rf);
>
> I think that ought to do it. Isn't OO abuse great?
>
>
>> It is great ;). But you save a couple of lines by just doing straight
>> introspection.
I had thought about that, but I thought the JVM prevented code from
getting-around access privileges by using introspection. Or is that only
when a SecurityManager is running?
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkqhImUACgkQ9CaO5/Lv0PDVkgCaA9UWhDHNXKdfN2mSPZN1lauK
Z3oAnRphaMCX6vkmjvLH+t/tmUIxp52m
=qxEb
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Bill Barker <bi...@verizon.net>.
"Christopher Schultz" <ch...@christopherschultz.net> wrote in message
news:4AA03CA3.4010405@christopherschultz.net...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Muthu,
>
> On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
>> I am using tomcat 5.5 and just wanted to write a simple servlet to get
>> some information using StandardManager such as activeSessions,
>> expriedSessions, sessionCounter etc.. I am not that familiar with these
>> classes. I noticed when the request comes into my servlet it comes as
>> RequestFacade where the org.apache.catalina.connector.Request is wrapped
>> inside it. I haven't figured out how to unwrap the Request object so that
>> I can get the Context from which I can get the Manager (StandardManager)
>> to access the information I need. Any help with a snippet of code to get
>> the Request Object from RequestFacade will be appreciated.
>
> Maybe try something like this:
>
> public class SneekyRequestFacade
> extends RequestFacade
> {
> public SneekyRequestFacade() { super(null); }
>
> public Request getRequest(RequestFacade rf)
> {
> return rf.request;
> }
> }
>
> Now:
>
> RequestFacade rf = ...; // get your requestfacade
> Request req = new SneekyRequestFacade().getRequest(rf);
>
> I think that ought to do it. Isn't OO abuse great?
>
It is great ;). But you save a couple of lines by just doing straight
introspection.
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqgPKMACgkQ9CaO5/Lv0PBmaACgvmnqSUh8xEhziGwDNE4SbVwQ
> ytQAnA2u2nUwb4XXtqLZqFa92oFDiBn2
> =PCPu
> -----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: how to unwrap a Request from RequestFacade
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Muthu,
On 9/3/2009 11:17 AM, Muthu.Chandir@sectra.se wrote:
> I am using tomcat 5.5 and just wanted to write a simple servlet to get
> some information using StandardManager such as activeSessions,
> expriedSessions, sessionCounter etc.. I am not that familiar with these
> classes. I noticed when the request comes into my servlet it comes as
> RequestFacade where the org.apache.catalina.connector.Request is wrapped
> inside it. I haven't figured out how to unwrap the Request object so that
> I can get the Context from which I can get the Manager (StandardManager)
> to access the information I need. Any help with a snippet of code to get
> the Request Object from RequestFacade will be appreciated.
Maybe try something like this:
public class SneekyRequestFacade
extends RequestFacade
{
public SneekyRequestFacade() { super(null); }
public Request getRequest(RequestFacade rf)
{
return rf.request;
}
}
Now:
RequestFacade rf = ...; // get your requestfacade
Request req = new SneekyRequestFacade().getRequest(rf);
I think that ought to do it. Isn't OO abuse great?
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAkqgPKMACgkQ9CaO5/Lv0PBmaACgvmnqSUh8xEhziGwDNE4SbVwQ
ytQAnA2u2nUwb4XXtqLZqFa92oFDiBn2
=PCPu
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org