users@jersey.java.net

RE: [Jersey] Problem with injected HttpServletRequest proxy

From: Ian Carr <Ian.Carr_at_focus-solutions.co.uk>
Date: Mon, 7 Dec 2009 18:03:48 +0000

Just added the following to a Filter elsewhere in the application:

        public static void debugLoginMethod(HttpServletRequest hreq) {
                try {
                        __log.info("Checking request object: " + hreq.getClass().getName());
                        Method meth = hreq.getClass().getMethod("login", String.class, String.class);
                        Class<?>[] ets = meth.getExceptionTypes();

                        for (Class<?> et : ets) {
                                __log.info("Exception class: " + et.getName());
                        }
                } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (NoSuchMethodException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

Calling it from the filter and from the Jersey object this is dumping out the expected result:

[#|2009-12-07T17:59:25.330+0000|INFO|glassfishv3.0|imc.test.util.PrincipalFilter|_ThreadID=26;_ThreadName=Thread-1;|Checking request object: org.apache.catalina.connector.RequestFacade|#]

[#|2009-12-07T17:59:25.331+0000|INFO|glassfishv3.0|imc.test.util.PrincipalFilter|_ThreadID=26;_ThreadName=Thread-1;|Exception class: javax.servlet.ServletException|#]

[#|2009-12-07T17:59:25.333+0000|INFO|glassfishv3.0|imc.test.resource.MainResource|_ThreadID=26;_ThreadName=Thread-1;|constructed|#]

[#|2009-12-07T17:59:25.339+0000|INFO|glassfishv3.0|imc.test.util.PrincipalFilter|_ThreadID=26;_ThreadName=Thread-1;|Checking request object: $Proxy165|#]

[#|2009-12-07T17:59:25.339+0000|INFO|glassfishv3.0|imc.test.util.PrincipalFilter|_ThreadID=26;_ThreadName=Thread-1;|Exception class: javax.servlet.ServletException|#]

So looks like the class and the proxy in the classpath is OK

It looks like the problem is more likely to be with the login code throwing a different implementation of ServletException from a duplicate jar??

I'll dig deeper

Ian

-----Original Message-----
From: Ian Carr [mailto:Ian.Carr_at_focus-solutions.co.uk]
Sent: 07 December 2009 17:46
To: users_at_jersey.dev.java.net
Subject: RE: [Jersey] Problem with injected HttpServletRequest proxy

Will do!

I have checked that I am not accidentally deploying anything I shouldn't and as far as I can tell, the WEB-INF lib of my project is clean.

I can add a reflection test elsewhere in the app (in a filter) to examine the login method signature and let you know the results.

I assume the proxy is created on the fly based on the implementation in the application classpath?

Ian

-----Original Message-----
From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: 07 December 2009 16:02
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] Problem with injected HttpServletRequest proxy

Hi Ian,

Could you log an issue?

I am not sure why the proxying is not working. The JavaDoc of
UndeclaredThrowableException [1] states:

   Thrown by a method invocation on a proxy instance if its invocation
handler's invoke method throws a checked exception (a
   Throwable that is not assignable to RuntimeException or Error) that
is not assignable to any of the exception types declared
   in the throws clause of the method that was invoked on the proxy
instance and dispatched to the invocation handler.

As i understand that can only happen if the HttpServletRequest class
that is proxied has a login method that does not declare that it
throws ServletException. So i am wondering how that can be the case. I
am guessing this is a new case because AFAICT previously Servlet did
not have any checked exceptions on HttpServletRequest methods.

As work around you can inject:

   @Context ThreadLocal<HttpServletRequest> req

Paul.

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/UndeclaredThrowableException.html

On Dec 7, 2009, at 4:11 PM, Ian Carr wrote:

> Hi, I am working with glassfish v3 build 74 and trying to experiment
> with the new Servlet 3.0 login and logout methods from jersey.
>
> I am injecting the HttpServletRequest into one of my Jersey methods
> and
> invoking the login method, if the user id and password map then all
> is OK. However if the login method throws it's ServletException as
> documented (bad username/password or other issue) I get a stack trace
> which seems to indicate that the, presumably generated, injected proxy
> ($Proxy153) is not expecting the ServletException
> (java.lang.reflect.UndeclaredThrowableException)
>
> Not sure if this is relevant but the class is also using the JCDI
> injection mechanism and is annotated as an @ManagedBean.
>
> Method:
>
> @POST
> @Path("login")
> @Consumes( { MediaType.APPLICATION_JSON })
> public void loginUser(@Context HttpServletRequest req, UIdent ident) {
> try {
> // does the login work?
> String uname = ident.getUname();
> String pword = ident.getPword();
> req.login(uname, pword);
> } catch (ServletException ex) {
> __log.info("Failed login attempt", ex);
> throw new WebApplicationException(Status.UNAUTHORIZED);
> }
> }
>
> Stack trace:
>
> [#|2009-12-07T14:58:37.252+0000|WARNING|glassfishv3.0|
> javax.enterprise.system.container.web.com.sun.enterprise.web|
> _ThreadID=24;_ThreadName=http-thread-pool-8080-(1);|
> StandardWrapperValve[default]:
> PWC1406: Servlet.service() for servlet default threw exception
> java.lang.reflect.UndeclaredThrowableException
> at $Proxy153.login(Unknown Source)
> at imc.test.resource.MainResource.loginUser(MainResource.java:121)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun
> .reflect
> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun
> .reflect
> .DelegatingMethodAccessorImpl
> .invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> com
> .sun
> .jersey
> .server
> .impl.model.method.dispatch.AbstractResourceMethodDispatchProvider
> $
> VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:
> 139)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .model
> .method
> .dispatch
> .ResourceJavaMethodDispatcher
> .dispatch(ResourceJavaMethodDispatcher.java:67)
> at
> com
> .sun
> .jersey
> .server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:208)
> at
> com
> .sun
> .jersey
> .server
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:115)
> at
> com
> .sun
> .jersey
> .server
> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:75)
> at
> com
> .sun
> .jersey
> .server
> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:115)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .uri
> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:67)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:775)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:740)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:731)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.service(WebComponent.java:
> 372)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:452)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.doFilter(ServletContainer.java:
> 780)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.doFilter(ServletContainer.java:
> 732)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 256)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
> at imc.test.util.AcceptFilter.doFilter(AcceptFilter.java:65)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 256)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 277)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 188)
> at
> org
> .apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:
> 641)
> at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
> at
> com
> .sun
> .enterprise
> .web
> .PESessionLockingStandardPipeline
> .invoke(PESessionLockingStandardPipeline.java:85)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
> at
> org
> .apache
> .catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 233)
> at
> com
> .sun
> .enterprise
> .v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
> at
> com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:
> 791)
> at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:
> 693)
> at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
> at
> com
> .sun
> .grizzly
> .DefaultProtocolChain
> .executeProtocolFilter(DefaultProtocolChain.java:135)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 102)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 88)
> at
> com
> .sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
> at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
> at
> com.sun.grizzly.util.AbstractThreadPool
> $Worker.doWork(AbstractThreadPool.java:330)
> at
> com.sun.grizzly.util.AbstractThreadPool
> $Worker.run(AbstractThreadPool.java:309)
> at java.lang.Thread.run(Thread.java:619)
> Caused by: java.lang.reflect.InvocationTargetException
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun
> .reflect
> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun
> .reflect
> .DelegatingMethodAccessorImpl
> .invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .container.servlet.ThreadLocalInvoker.invoke(ThreadLocalInvoker.java:
> 90)
> ... 48 more
> Caused by: javax.servlet.ServletException: Exception thrown while
> attempting to authenticate for user: test
> at org.apache.catalina.connector.Request.login(Request.java:1921)
> at
> org.apache.catalina.connector.RequestFacade.login(RequestFacade.java:
> 1145)
> ... 53 more
> Caused by: javax.servlet.ServletException: Failed login while
> attempting
> to authenticate user: test
> at org.apache.catalina.connector.Request.login(Request.java:1913)
> ... 54 more
>
> Any ideas?
>
> Many thanks
>
> Ian
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net