You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jon Williams <wi...@gmail.com> on 2013/06/27 05:39:57 UTC

Problems with accessing my SSO from the service layer. request related NPE.

Hi,

For background info... I'm running 5.3.7 tapestry release.
I'm trying to get a handle to a SSO from inside a service class...
(warning...code failure ensues.)

I have an SSO "Visit"
here's the SSO's "injection" from my service class "UserServiceImpl"...
    private Visit getVisit() {
        Request request = this.request;
        assert(request!=null);
//        boolean visitExists = applicationStateManager.exists(Visit.class);
        Visit visit = applicationStateManager.getIfExists(Visit.class);
        return visit;
    }

This works fine in the context of runtime in a servlet container. My
problem is with my service layer Unit tests.
When I'm testing my UserService the services getVisit() method is being
exercised.
and then when invoking in that method...
this next line of code causes the request related NPE (see below)...

applicationStateManager.getIfExists(Visit.class);

...java.lang.NullPointerException: Unable to delegate method invocation to
property 'request' of <Proxy for
RequestGlobals(org.apache.tapestry5.services.RequestGlobals)>, because the
property is null.

As you can see the assert in my method proves the Request exits . it's the
SSO handling in ApplicationStateManager's getIfExists, exist, & get SSO
methods that is kicking off (included  below) exceptions in Unit test only.

It makes sense to me that with my testing app context for service layer
might (maybe should) not include a proper Request. But just prior to
failure i have an assert that checks the Request is not null. my assertion
seems to indicate the existence of a proper Request. ?? (or maybe it's just
a whacked-out  proxy?)

I've thought about it a bit, poked around and found no useful links
searching the interwebs.
Am I doing something I'm not supposed to do?

Any advice/insight appreciated. thanks
Jon



=========================================

java.lang.NullPointerException: Unable to delegate method invocation to
property 'request' of <Proxy for
RequestGlobals(org.apache.tapestry5.services.RequestGlobals)>, because the
property is null.
at $Request_130b64ff5f4b56f0.readProperty(Unknown Source)
at $Request_130b64ff5f4b56f0.getSession(Unknown Source)
at $Request_130b64ff5f4b56e1.getSession(Unknown Source)
at
org.apache.tapestry5.internal.services.SessionApplicationStatePersistenceStrategy.exists(SessionApplicationStatePersistenceStrategy.java:80)
at $ApplicationStatePersistenceStrategy_130b64ff5f4b56ef.exists(Unknown
Source)
at
org.apache.tapestry5.internal.services.ApplicationStateManagerImpl$ApplicationStateAdapter.exists(ApplicationStateManagerImpl.java:60)
at
org.apache.tapestry5.internal.services.ApplicationStateManagerImpl.getIfExists(ApplicationStateManagerImpl.java:140)
at $ApplicationStateManager_130b64ff5f4b56e2.getIfExists(Unknown Source)
at
info.ncapsuld.services.domain.UserServiceImpl.getVisit(UserServiceImpl.java:82)
at
info.ncapsuld.services.domain.UserServiceImpl.save(UserServiceImpl.java:46)
at $UserService_130b64ff5f4b56d5.save(Unknown Source)
at $UserService_130b64ff5f4b56d7.advised$save_130b64ff5f4b56dd(Unknown
Source)
at
$UserService_130b64ff5f4b56d7$Invocation_save_130b64ff5f4b56dc.proceedToAdvisedMethod(Unknown
Source)
at
org.apache.tapestry5.internal.plastic.AbstractMethodInvocation.proceed(AbstractMethodInvocation.java:84)
at
org.apache.tapestry5.ioc.internal.util.InternalUtils$21$1.proceed(InternalUtils.java:1386)
at
org.apache.tapestry5.internal.hibernate.HibernateTransactionAdvisorImpl$1.advise(HibernateTransactionAdvisorImpl.java:40)
at
org.apache.tapestry5.ioc.internal.util.InternalUtils$21.advise(InternalUtils.java:1455)
at
org.apache.tapestry5.internal.plastic.AbstractMethodInvocation.proceed(AbstractMethodInvocation.java:86)
at $UserService_130b64ff5f4b56d7.save(Unknown Source)
at $UserService_130b64ff5f4b56d3.save(Unknown Source)
at
info.ncapsuld.services.UserServiceTest.createInstance(UserServiceTest.java:34)
at
info.ncapsuld.services.UserServiceTest.testCreateSaveRetrieveUser(UserServiceTest.java:45)

===========================

Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Ilya Obshadko <il...@gmail.com>.
So I've ended up with the following implementation:

    @Contribute(HttpServletRequestHandler.class)

    public void contributeHttpServletRequestHandler (
OrderedConfiguration<HttpServletRequestFilter> configuration,

            @Inject @Symbol(ResteasySymbols.MAPPING_PREFIX) final String
restFilterPath,

            @Inject @Symbol(SymbolConstants.CHARSET) final String
applicationCharset,

            @Primary final SessionPersistedObjectAnalyzer<?> analyzer,

            final RequestGlobals requestGlobals ) {


        HttpServletRequestFilter storeRestSession =
newHttpServletRequestFilter () {

            public boolean service ( HttpServletRequest servletRequest,
HttpServletResponse servletResponse,

                    HttpServletRequestHandler handler ) throws IOException {



                if ( servletRequest.getServletPath ().startsWith (
restFilterPath + "/" ) ) {

                    Request request = new RequestImpl ( servletRequest,
applicationCharset, new TapestrySessionFactoryImpl ( false, analyzer,
servletRequest ) );

                    Response response = new ResponseImpl ( servletRequest,
servletResponse );

                    requestGlobals.storeRequestResponse ( request, response
);

                }



                return handler.service ( servletRequest, servletResponse );

            }

        };


        configuration.add("StoreRestSession", storeRestSession,
"before:ResteasyRequestFilter");

    }

Probably it's possible to achieve the same goal by overriding RestEasy
request filter.



On Fri, Jan 24, 2014 at 11:07 PM, Ilya Obshadko <il...@gmail.com>wrote:

> Just run into this thread with a similar problem. I'm implementing REST
> service (using tapestry-resteasy) for mobile clients.
>
> I've already found a solution:
>
>     public void contributeHttpServletRequestHandler (
> OrderedConfiguration<HttpServletRequestFilter> configuration,
>
>             @Inject @Symbol(SymbolConstants.CHARSET) final String
> applicationCharset,
>
>             @Primary final SessionPersistedObjectAnalyzer<?> analyzer,
>
>             final RequestGlobals requestGlobals ) {
>
>
>         HttpServletRequestFilter storeRequestResponse = newHttpServletRequestFilter () {
>
>             public boolean service ( HttpServletRequest servletRequest,
> HttpServletResponse servletResponse,
>
>                     HttpServletRequestHandler handler ) throwsIOException {
>
>
>
>                 Request request = new RequestImpl ( servletRequest,
> applicationCharset, new TapestrySessionFactoryImpl ( false, analyzer,
> servletRequest ) );
>
>                 Response response = new ResponseImpl ( servletRequest,
> servletResponse );
>
>                 requestGlobals.storeRequestResponse ( request, response );
>
>                 return handler.service ( servletRequest, servletResponse
> );
>
>             }
>
>         };
>
>
>         configuration.add("StoreRequestResponse", storeRequestResponse,
> "before:*");
>
>     }
>
> But it doesn't look very elegant. Is there any better way to expose
> ApplicationStateManager to RestEasy service?
>
>
>
> On Fri, Jun 28, 2013 at 12:04 PM, Thiago H de Paula Figueiredo <
> thiagohp@gmail.com> wrote:
>
>> On Thu, 27 Jun 2013 19:46:20 -0300, Jon Williams <
>> williams.jonathan@gmail.com> wrote:
>>
>>  Hi,
>>>
>>
>> Hi!
>>
>>  My problem has gone away.
>>> A short FYI on how I got past this problem...
>>>
>>> First I tried adding a new request Builder method in QaModule using the
>>> shadowbuilder service that would build my TestRequest. But that didn't
>>> work because of a Request service implementation collision with predeclared
>>> (in TapModule) service Request.class interface implementation. Only 1
>>> Request
>>> interface implementation is allowed, and TapModule beats me to the punch.
>>>
>>
>> You've already found a very good solution, but I'd like to mention you
>> could also use service overriding, so your Request implementation is used
>> and not Tapestry's.
>>
>>
>> --
>> Thiago H. de Paula Figueiredo
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
>
> --
> Ilya Obshadko
>
>


-- 
Ilya Obshadko

Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Ilya Obshadko <il...@gmail.com>.
Just run into this thread with a similar problem. I'm implementing REST
service (using tapestry-resteasy) for mobile clients.

I've already found a solution:

    public void contributeHttpServletRequestHandler (
OrderedConfiguration<HttpServletRequestFilter> configuration,

            @Inject @Symbol(SymbolConstants.CHARSET) final String
applicationCharset,

            @Primary final SessionPersistedObjectAnalyzer<?> analyzer,

            final RequestGlobals requestGlobals ) {


        HttpServletRequestFilter storeRequestResponse =
newHttpServletRequestFilter () {

            public boolean service ( HttpServletRequest servletRequest,
HttpServletResponse servletResponse,

                    HttpServletRequestHandler handler ) throws IOException {



                Request request = new RequestImpl ( servletRequest,
applicationCharset, new TapestrySessionFactoryImpl ( false, analyzer,
servletRequest ) );

                Response response = new ResponseImpl ( servletRequest,
servletResponse );

                requestGlobals.storeRequestResponse ( request, response );

                return handler.service ( servletRequest, servletResponse );

            }

        };


        configuration.add("StoreRequestResponse", storeRequestResponse,
"before:*");

    }

But it doesn't look very elegant. Is there any better way to expose
ApplicationStateManager to RestEasy service?



On Fri, Jun 28, 2013 at 12:04 PM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Thu, 27 Jun 2013 19:46:20 -0300, Jon Williams <
> williams.jonathan@gmail.com> wrote:
>
>  Hi,
>>
>
> Hi!
>
>  My problem has gone away.
>> A short FYI on how I got past this problem...
>>
>> First I tried adding a new request Builder method in QaModule using the
>> shadowbuilder service that would build my TestRequest. But that didn't
>> work because of a Request service implementation collision with predeclared
>> (in TapModule) service Request.class interface implementation. Only 1
>> Request
>> interface implementation is allowed, and TapModule beats me to the punch.
>>
>
> You've already found a very good solution, but I'd like to mention you
> could also use service overriding, so your Request implementation is used
> and not Tapestry's.
>
>
> --
> Thiago H. de Paula Figueiredo
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Ilya Obshadko

Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 27 Jun 2013 19:46:20 -0300, Jon Williams  
<wi...@gmail.com> wrote:

> Hi,

Hi!

> My problem has gone away.
> A short FYI on how I got past this problem...
>
> First I tried adding a new request Builder method in QaModule using the
> shadowbuilder service that would build my TestRequest. But that didn't  
> work because of a Request service implementation collision with  
> predeclared (in TapModule) service Request.class interface  
> implementation. Only 1 Request
> interface implementation is allowed, and TapModule beats me to the punch.

You've already found a very good solution, but I'd like to mention you  
could also use service overriding, so your Request implementation is used  
and not Tapestry's.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Jon Williams <wi...@gmail.com>.
Hi,
My problem has gone away.
A short FYI on how I got past this problem...

First I tried adding a new request Builder method in QaModule using the
shadowbuilder service that would build my TestRequest. But that didn't work
because of a Request service implementation collision with predeclared (in
TapModule) service Request.class interface implementation. Only 1 Request
interface implementation is allowed, and TapModule beats me to the punch. I
don't want to mess with TapModule at all, so I abandoned this approach.

I ended up doing a minor kludge to sidestep this issue.

My new service layer method works NPE-free now.

    private Visit getVisit() {
        Request request = this.request;
        assert(request!=null);
        assert(requestGlobals!=null);
        Request currentRequest = requestGlobals.getRequest();
        if (currentRequest==null) {
            //only during unit testing will currentRequest be null
            //for testing we use a Mock request.
            requestGlobals.storeRequestResponse(new TestRequest(), null);
        }
        currentRequest = requestGlobals.getRequest();
        assert(currentRequest!=null);
        boolean visitExists = applicationStateManager.exists(Visit.class);
        assert(visitExists);
        Visit visit = applicationStateManager.get(Visit.class);
        assert(visit!=null);
        return visit;
    }

Thanks for your help guys.
Jon


On Thu, Jun 27, 2013 at 6:31 AM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Thu, 27 Jun 2013 09:07:24 -0300, Dmitry Gusev <dm...@gmail.com>
> wrote:
>
>  See also:
>>
>> http://tapestry.apache.org/**current/apidocs/org/apache/**
>> tapestry5/services/**RequestGlobals.html#**storeRequestResponse(org.**
>> apache.tapestry5.services.**Request,%20org.apache.**
>> tapestry5.services.Response)<http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/RequestGlobals.html#storeRequestResponse(org.apache.tapestry5.services.Request,%20org.apache.tapestry5.services.Response)>
>>
>
> Good one, Dmitry! And also check http://tapestry.apache.org/**
> shadowbuilder-service.html<http://tapestry.apache.org/shadowbuilder-service.html>,
> which explains what a shadow service is (Request is one such service) and
> the example is . . . Request. :D
>
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 27 Jun 2013 09:07:24 -0300, Dmitry Gusev <dm...@gmail.com>  
wrote:

> See also:
>
> http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/RequestGlobals.html#storeRequestResponse(org.apache.tapestry5.services.Request,%20org.apache.tapestry5.services.Response)

Good one, Dmitry! And also check  
http://tapestry.apache.org/shadowbuilder-service.html, which explains what  
a shadow service is (Request is one such service) and the example is . . .  
Request. :D

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Dmitry Gusev <dm...@gmail.com>.
See also:

http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/RequestGlobals.html#storeRequestResponse(org.apache.tapestry5.services.Request,%20org.apache.tapestry5.services.Response)


On Thu, Jun 27, 2013 at 4:04 PM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Thu, 27 Jun 2013 00:39:57 -0300, Jon Williams <
> williams.jonathan@gmail.com> wrote:
>
>  Hi,
>>
>
> Hi!
>
>
>  applicationStateManager.**getIfExists(Visit.class);
>>
>> ...java.lang.**NullPointerException: Unable to delegate method
>> invocation to property 'request' of <Proxy for
>> RequestGlobals(org.apache.**tapestry5.services.**RequestGlobals)>,
>> because the property is null.
>>
>
> This is expected. Unless the thread is a web request, it will fail.
>
>
>  As you can see the assert in my method proves the Request exits .
>>
>
> You're not quite right here. The Request, as a service, it does exist, but
> as a proxy. It (the Request service) is defined as invoking
> RequestGlobals.getRequest(). If the thread is not a web request,
> RequestGlobals won't have its request set, so the exception will occur.
>
>
>  It makes sense to me that with my testing app context for service layer
>> might (maybe should) not include a proper Request.
>>
>
> Request is a service and you can mock it usin EasyMock or some other mock
> framework.
>
>
>  failure i have an assert that checks the Request is not null. my assertion
>> seems to indicate the existence of a proper Request. ?? (or maybe it's
>> just a whacked-out  proxy?)
>>
>
> It's not a real Request and it's not a whacked-out proxy, as explained
> above.
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: Problems with accessing my SSO from the service layer. request related NPE.

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 27 Jun 2013 00:39:57 -0300, Jon Williams  
<wi...@gmail.com> wrote:

> Hi,

Hi!

> applicationStateManager.getIfExists(Visit.class);
>
> ...java.lang.NullPointerException: Unable to delegate method invocation  
> to property 'request' of <Proxy for
> RequestGlobals(org.apache.tapestry5.services.RequestGlobals)>, because  
> the property is null.

This is expected. Unless the thread is a web request, it will fail.

> As you can see the assert in my method proves the Request exits .

You're not quite right here. The Request, as a service, it does exist, but  
as a proxy. It (the Request service) is defined as invoking  
RequestGlobals.getRequest(). If the thread is not a web request,  
RequestGlobals won't have its request set, so the exception will occur.

> It makes sense to me that with my testing app context for service layer
> might (maybe should) not include a proper Request.

Request is a service and you can mock it usin EasyMock or some other mock  
framework.

> failure i have an assert that checks the Request is not null. my  
> assertion
> seems to indicate the existence of a proper Request. ?? (or maybe it's  
> just a whacked-out  proxy?)

It's not a real Request and it's not a whacked-out proxy, as explained  
above.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org