Sling models - Resource decorator

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Sling models - Resource decorator

Roy Teeuwen
Hey all,

I am using sling models version 1.2.2, and I have the following logic:

- I find some resources with the resource resolver (resourceResolver.findResources)
- The returned resources are the correct resources which are decorated by my custom resource decorator
- I iterate the list to cast them to the sling model I want them to be, using a resource.adaptTo
- The sling model itself has a field Resource resource, to inject itself

What I notice now is that the injected resource is a JcrNodeResource and not my decorated resource class anymore. I guess this is a bug in the sling models implementation? Am I doing something wrong here or is this an actual bug in the sling models implementation? Is this maybe already fixed in any of the future versions?

Greets,
Roy

signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Sling models - Resource decorator

Jason Bailey
Hey Roy,

Couple of Assumptions:

For the Self resource you have it defined as:

@Self
public Resource resource;

and for the @Model you have something like
@Model(adaptables = RoysCustomDecorator.class)

Am I close?

-Jason

-----Original Message-----
From: Roy Teeuwen [mailto:[hidden email]]
Sent: Tuesday, June 06, 2017 10:08 AM
To: [hidden email]
Subject: Sling models - Resource decorator

Hey all,

I am using sling models version 1.2.2, and I have the following logic:

- I find some resources with the resource resolver (resourceResolver.findResources)
- The returned resources are the correct resources which are decorated by my custom resource decorator
- I iterate the list to cast them to the sling model I want them to be, using a resource.adaptTo
- The sling model itself has a field Resource resource, to inject itself

What I notice now is that the injected resource is a JcrNodeResource and not my decorated resource class anymore. I guess this is a bug in the sling models implementation? Am I doing something wrong here or is this an actual bug in the sling models implementation? Is this maybe already fixed in any of the future versions?

Greets,
Roy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

Roy Teeuwen
Hey Jason,

No to both assumptions

- I used @Inject public Resource resource. Just tried the @Self, same result
- I have @Model(adaptables = Resource.class). I don't want to bind my model to a custom Resource class, that would be the same as saying I have to do @Model(adaptables = JcrNodeResource.class), the decorator just decorates your resource implementation class, has nothing to do with your Resource interface you are coming from.

Greets,
Roy

> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
>
> Hey Roy,
>
> Couple of Assumptions:
>
> For the Self resource you have it defined as:
>
> @Self
> public Resource resource;
>
> and for the @Model you have something like
> @Model(adaptables = RoysCustomDecorator.class)
>
> Am I close?
>
> -Jason
>
> -----Original Message-----
> From: Roy Teeuwen [mailto:[hidden email]]
> Sent: Tuesday, June 06, 2017 10:08 AM
> To: [hidden email]
> Subject: Sling models - Resource decorator
>
> Hey all,
>
> I am using sling models version 1.2.2, and I have the following logic:
>
> - I find some resources with the resource resolver (resourceResolver.findResources)
> - The returned resources are the correct resources which are decorated by my custom resource decorator
> - I iterate the list to cast them to the sling model I want them to be, using a resource.adaptTo
> - The sling model itself has a field Resource resource, to inject itself
>
> What I notice now is that the injected resource is a JcrNodeResource and not my decorated resource class anymore. I guess this is a bug in the sling models implementation? Am I doing something wrong here or is this an actual bug in the sling models implementation? Is this maybe already fixed in any of the future versions?
>
> Greets,
> Roy


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

Roy Teeuwen
Hey Jason, Guys,

No one can give me an answer if this is a bug or something wanted? I can always register a Jira ticket for and put it on the dev list of course

Greets,
Roy

> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
>
> Hey Jason,
>
> No to both assumptions
>
> - I used @Inject public Resource resource. Just tried the @Self, same result
> - I have @Model(adaptables = Resource.class). I don't want to bind my model to a custom Resource class, that would be the same as saying I have to do @Model(adaptables = JcrNodeResource.class), the decorator just decorates your resource implementation class, has nothing to do with your Resource interface you are coming from.
>
> Greets,
> Roy
>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
>>
>> Hey Roy,
>>
>> Couple of Assumptions:
>>
>> For the Self resource you have it defined as:
>>
>> @Self
>> public Resource resource;
>>
>> and for the @Model you have something like
>> @Model(adaptables = RoysCustomDecorator.class)
>>
>> Am I close?
>>
>> -Jason
>>
>> -----Original Message-----
>> From: Roy Teeuwen [mailto:[hidden email]]
>> Sent: Tuesday, June 06, 2017 10:08 AM
>> To: [hidden email]
>> Subject: Sling models - Resource decorator
>>
>> Hey all,
>>
>> I am using sling models version 1.2.2, and I have the following logic:
>>
>> - I find some resources with the resource resolver (resourceResolver.findResources)
>> - The returned resources are the correct resources which are decorated by my custom resource decorator
>> - I iterate the list to cast them to the sling model I want them to be, using a resource.adaptTo
>> - The sling model itself has a field Resource resource, to inject itself
>>
>> What I notice now is that the injected resource is a JcrNodeResource and not my decorated resource class anymore. I guess this is a bug in the sling models implementation? Am I doing something wrong here or is this an actual bug in the sling models implementation? Is this maybe already fixed in any of the future versions?
>>
>> Greets,
>> Roy
>


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

justzzzz
Hi Roy,
This sounds like unexpected behavior. Please create a JIRA for this,
ideally with a test case, if you have not already.

Thanks,
Justin

On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:

> Hey Jason, Guys,
>
> No one can give me an answer if this is a bug or something wanted? I can
> always register a Jira ticket for and put it on the dev list of course
>
> Greets,
> Roy
> > On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
> >
> > Hey Jason,
> >
> > No to both assumptions
> >
> > - I used @Inject public Resource resource. Just tried the @Self, same
> result
> > - I have @Model(adaptables = Resource.class). I don't want to bind my
> model to a custom Resource class, that would be the same as saying I have
> to do @Model(adaptables = JcrNodeResource.class), the decorator just
> decorates your resource implementation class, has nothing to do with your
> Resource interface you are coming from.
> >
> > Greets,
> > Roy
> >> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
> >>
> >> Hey Roy,
> >>
> >> Couple of Assumptions:
> >>
> >> For the Self resource you have it defined as:
> >>
> >> @Self
> >> public Resource resource;
> >>
> >> and for the @Model you have something like
> >> @Model(adaptables = RoysCustomDecorator.class)
> >>
> >> Am I close?
> >>
> >> -Jason
> >>
> >> -----Original Message-----
> >> From: Roy Teeuwen [mailto:[hidden email]]
> >> Sent: Tuesday, June 06, 2017 10:08 AM
> >> To: [hidden email]
> >> Subject: Sling models - Resource decorator
> >>
> >> Hey all,
> >>
> >> I am using sling models version 1.2.2, and I have the following logic:
> >>
> >> - I find some resources with the resource resolver
> (resourceResolver.findResources)
> >> - The returned resources are the correct resources which are decorated
> by my custom resource decorator
> >> - I iterate the list to cast them to the sling model I want them to be,
> using a resource.adaptTo
> >> - The sling model itself has a field Resource resource, to inject itself
> >>
> >> What I notice now is that the injected resource is a JcrNodeResource
> and not my decorated resource class anymore. I guess this is a bug in the
> sling models implementation? Am I doing something wrong here or is this an
> actual bug in the sling models implementation? Is this maybe already fixed
> in any of the future versions?
> >>
> >> Greets,
> >> Roy
> >
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

justzzzz
FWIW, I committed an integration test at
http://svn.apache.org/viewvc?view=revision&revision=1798489 checking that
the wrapped resource is injected via the @Self annotation. Would be
interesting to see how this differs from what you are doing.

Regards,
Justin

On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <[hidden email]>
wrote:

> Hi Roy,
> This sounds like unexpected behavior. Please create a JIRA for this,
> ideally with a test case, if you have not already.
>
> Thanks,
> Justin
>
> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
>
>> Hey Jason, Guys,
>>
>> No one can give me an answer if this is a bug or something wanted? I can
>> always register a Jira ticket for and put it on the dev list of course
>>
>> Greets,
>> Roy
>> > On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
>> >
>> > Hey Jason,
>> >
>> > No to both assumptions
>> >
>> > - I used @Inject public Resource resource. Just tried the @Self, same
>> result
>> > - I have @Model(adaptables = Resource.class). I don't want to bind my
>> model to a custom Resource class, that would be the same as saying I have
>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
>> decorates your resource implementation class, has nothing to do with your
>> Resource interface you are coming from.
>> >
>> > Greets,
>> > Roy
>> >> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
>> >>
>> >> Hey Roy,
>> >>
>> >> Couple of Assumptions:
>> >>
>> >> For the Self resource you have it defined as:
>> >>
>> >> @Self
>> >> public Resource resource;
>> >>
>> >> and for the @Model you have something like
>> >> @Model(adaptables = RoysCustomDecorator.class)
>> >>
>> >> Am I close?
>> >>
>> >> -Jason
>> >>
>> >> -----Original Message-----
>> >> From: Roy Teeuwen [mailto:[hidden email]]
>> >> Sent: Tuesday, June 06, 2017 10:08 AM
>> >> To: [hidden email]
>> >> Subject: Sling models - Resource decorator
>> >>
>> >> Hey all,
>> >>
>> >> I am using sling models version 1.2.2, and I have the following logic:
>> >>
>> >> - I find some resources with the resource resolver
>> (resourceResolver.findResources)
>> >> - The returned resources are the correct resources which are decorated
>> by my custom resource decorator
>> >> - I iterate the list to cast them to the sling model I want them to
>> be, using a resource.adaptTo
>> >> - The sling model itself has a field Resource resource, to inject
>> itself
>> >>
>> >> What I notice now is that the injected resource is a JcrNodeResource
>> and not my decorated resource class anymore. I guess this is a bug in the
>> sling models implementation? Am I doing something wrong here or is this an
>> actual bug in the sling models implementation? Is this maybe already fixed
>> in any of the future versions?
>> >>
>> >> Greets,
>> >> Roy
>> >
>>
>>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

Roy Teeuwen
Hey Justin,

Cool, thanks for the integration test!
Just change the modelFactory.createModel to resource.adaptTo(SelfModel.class) and you will see that the test fails :)

Greets,
Roy

> On 12 Jun 2017, at 18:52, Justin Edelson <[hidden email]> wrote:
>
> FWIW, I committed an integration test at
> http://svn.apache.org/viewvc?view=revision&revision=1798489 checking that
> the wrapped resource is injected via the @Self annotation. Would be
> interesting to see how this differs from what you are doing.
>
> Regards,
> Justin
>
> On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <[hidden email]>
> wrote:
>
>> Hi Roy,
>> This sounds like unexpected behavior. Please create a JIRA for this,
>> ideally with a test case, if you have not already.
>>
>> Thanks,
>> Justin
>>
>> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
>>
>>> Hey Jason, Guys,
>>>
>>> No one can give me an answer if this is a bug or something wanted? I can
>>> always register a Jira ticket for and put it on the dev list of course
>>>
>>> Greets,
>>> Roy
>>>> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
>>>>
>>>> Hey Jason,
>>>>
>>>> No to both assumptions
>>>>
>>>> - I used @Inject public Resource resource. Just tried the @Self, same
>>> result
>>>> - I have @Model(adaptables = Resource.class). I don't want to bind my
>>> model to a custom Resource class, that would be the same as saying I have
>>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
>>> decorates your resource implementation class, has nothing to do with your
>>> Resource interface you are coming from.
>>>>
>>>> Greets,
>>>> Roy
>>>>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
>>>>>
>>>>> Hey Roy,
>>>>>
>>>>> Couple of Assumptions:
>>>>>
>>>>> For the Self resource you have it defined as:
>>>>>
>>>>> @Self
>>>>> public Resource resource;
>>>>>
>>>>> and for the @Model you have something like
>>>>> @Model(adaptables = RoysCustomDecorator.class)
>>>>>
>>>>> Am I close?
>>>>>
>>>>> -Jason
>>>>>
>>>>> -----Original Message-----
>>>>> From: Roy Teeuwen [mailto:[hidden email]]
>>>>> Sent: Tuesday, June 06, 2017 10:08 AM
>>>>> To: [hidden email]
>>>>> Subject: Sling models - Resource decorator
>>>>>
>>>>> Hey all,
>>>>>
>>>>> I am using sling models version 1.2.2, and I have the following logic:
>>>>>
>>>>> - I find some resources with the resource resolver
>>> (resourceResolver.findResources)
>>>>> - The returned resources are the correct resources which are decorated
>>> by my custom resource decorator
>>>>> - I iterate the list to cast them to the sling model I want them to
>>> be, using a resource.adaptTo
>>>>> - The sling model itself has a field Resource resource, to inject
>>> itself
>>>>>
>>>>> What I notice now is that the injected resource is a JcrNodeResource
>>> and not my decorated resource class anymore. I guess this is a bug in the
>>> sling models implementation? Am I doing something wrong here or is this an
>>> actual bug in the sling models implementation? Is this maybe already fixed
>>> in any of the future versions?
>>>>>
>>>>> Greets,
>>>>> Roy
>>>>
>>>
>>>


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

justzzzz
Hi Roy,
OK. That makes sense (and I'm sorry I missed that part in your original
email). The problem is that if you use WrappedResource, it delegates the
adaptTo() call to the wrapped resource, i.e. the wrapper is lost at that
point and the AdapterFactory (i.e. Sling Models) doesn't have access to it
anymore. I'm not sure how this can be best be handled. One approach would
be in your wrapper class to implement the adaptTo method. Something like:

class Wrapper extends ResourceWrapper {
        private Wrapper(Resource r) {
            super(r);
        }

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            AdapterType adapter = adapterManager.getAdapter(this, type);
            if (adapter != null) {
                return adapter;
            } else {
                return super.adaptTo(type);
            }
        }
    }

But this looks a bit odd to me, although I can't quite put my finger on why.

Regards,
Justin

On Mon, Jun 12, 2017 at 3:21 PM Roy Teeuwen <[hidden email]> wrote:

> Hey Justin,
>
> Cool, thanks for the integration test!
> Just change the modelFactory.createModel to
> resource.adaptTo(SelfModel.class) and you will see that the test fails :)
>
> Greets,
> Roy
> > On 12 Jun 2017, at 18:52, Justin Edelson <[hidden email]>
> wrote:
> >
> > FWIW, I committed an integration test at
> > http://svn.apache.org/viewvc?view=revision&revision=1798489 checking
> that
> > the wrapped resource is injected via the @Self annotation. Would be
> > interesting to see how this differs from what you are doing.
> >
> > Regards,
> > Justin
> >
> > On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <
> [hidden email]>
> > wrote:
> >
> >> Hi Roy,
> >> This sounds like unexpected behavior. Please create a JIRA for this,
> >> ideally with a test case, if you have not already.
> >>
> >> Thanks,
> >> Justin
> >>
> >> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
> >>
> >>> Hey Jason, Guys,
> >>>
> >>> No one can give me an answer if this is a bug or something wanted? I
> can
> >>> always register a Jira ticket for and put it on the dev list of course
> >>>
> >>> Greets,
> >>> Roy
> >>>> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
> >>>>
> >>>> Hey Jason,
> >>>>
> >>>> No to both assumptions
> >>>>
> >>>> - I used @Inject public Resource resource. Just tried the @Self, same
> >>> result
> >>>> - I have @Model(adaptables = Resource.class). I don't want to bind my
> >>> model to a custom Resource class, that would be the same as saying I
> have
> >>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
> >>> decorates your resource implementation class, has nothing to do with
> your
> >>> Resource interface you are coming from.
> >>>>
> >>>> Greets,
> >>>> Roy
> >>>>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
> >>>>>
> >>>>> Hey Roy,
> >>>>>
> >>>>> Couple of Assumptions:
> >>>>>
> >>>>> For the Self resource you have it defined as:
> >>>>>
> >>>>> @Self
> >>>>> public Resource resource;
> >>>>>
> >>>>> and for the @Model you have something like
> >>>>> @Model(adaptables = RoysCustomDecorator.class)
> >>>>>
> >>>>> Am I close?
> >>>>>
> >>>>> -Jason
> >>>>>
> >>>>> -----Original Message-----
> >>>>> From: Roy Teeuwen [mailto:[hidden email]]
> >>>>> Sent: Tuesday, June 06, 2017 10:08 AM
> >>>>> To: [hidden email]
> >>>>> Subject: Sling models - Resource decorator
> >>>>>
> >>>>> Hey all,
> >>>>>
> >>>>> I am using sling models version 1.2.2, and I have the following
> logic:
> >>>>>
> >>>>> - I find some resources with the resource resolver
> >>> (resourceResolver.findResources)
> >>>>> - The returned resources are the correct resources which are
> decorated
> >>> by my custom resource decorator
> >>>>> - I iterate the list to cast them to the sling model I want them to
> >>> be, using a resource.adaptTo
> >>>>> - The sling model itself has a field Resource resource, to inject
> >>> itself
> >>>>>
> >>>>> What I notice now is that the injected resource is a JcrNodeResource
> >>> and not my decorated resource class anymore. I guess this is a bug in
> the
> >>> sling models implementation? Am I doing something wrong here or is
> this an
> >>> actual bug in the sling models implementation? Is this maybe already
> fixed
> >>> in any of the future versions?
> >>>>>
> >>>>> Greets,
> >>>>> Roy
> >>>>
> >>>
> >>>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

Roy Teeuwen
Hey Justin,

The reason why it is odd to me is that it should be agnostic to being wrapped or not.

You use the resourceresolver to get the resource and then do an adapt, all not knowing anything about being wrapped or not, seeing as the resource resolver also does the wrapping internally, why wouldn't the adaptTo do it?

Resource resource = resourceResolver.getResource("/my/path");
SelfModel selfModel = resource.adaptTo(SelfModel.class);

Greets,
Roy

> On 12 Jun 2017, at 21:32, Justin Edelson <[hidden email]> wrote:
>
> Hi Roy,
> OK. That makes sense (and I'm sorry I missed that part in your original
> email). The problem is that if you use WrappedResource, it delegates the
> adaptTo() call to the wrapped resource, i.e. the wrapper is lost at that
> point and the AdapterFactory (i.e. Sling Models) doesn't have access to it
> anymore. I'm not sure how this can be best be handled. One approach would
> be in your wrapper class to implement the adaptTo method. Something like:
>
> class Wrapper extends ResourceWrapper {
>        private Wrapper(Resource r) {
>            super(r);
>        }
>
>        @Override
>        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
>            AdapterType adapter = adapterManager.getAdapter(this, type);
>            if (adapter != null) {
>                return adapter;
>            } else {
>                return super.adaptTo(type);
>            }
>        }
>    }
>
> But this looks a bit odd to me, although I can't quite put my finger on why.
>
> Regards,
> Justin
>
> On Mon, Jun 12, 2017 at 3:21 PM Roy Teeuwen <[hidden email]> wrote:
>
>> Hey Justin,
>>
>> Cool, thanks for the integration test!
>> Just change the modelFactory.createModel to
>> resource.adaptTo(SelfModel.class) and you will see that the test fails :)
>>
>> Greets,
>> Roy
>>> On 12 Jun 2017, at 18:52, Justin Edelson <[hidden email]>
>> wrote:
>>>
>>> FWIW, I committed an integration test at
>>> http://svn.apache.org/viewvc?view=revision&revision=1798489 checking
>> that
>>> the wrapped resource is injected via the @Self annotation. Would be
>>> interesting to see how this differs from what you are doing.
>>>
>>> Regards,
>>> Justin
>>>
>>> On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <
>> [hidden email]>
>>> wrote:
>>>
>>>> Hi Roy,
>>>> This sounds like unexpected behavior. Please create a JIRA for this,
>>>> ideally with a test case, if you have not already.
>>>>
>>>> Thanks,
>>>> Justin
>>>>
>>>> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
>>>>
>>>>> Hey Jason, Guys,
>>>>>
>>>>> No one can give me an answer if this is a bug or something wanted? I
>> can
>>>>> always register a Jira ticket for and put it on the dev list of course
>>>>>
>>>>> Greets,
>>>>> Roy
>>>>>> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
>>>>>>
>>>>>> Hey Jason,
>>>>>>
>>>>>> No to both assumptions
>>>>>>
>>>>>> - I used @Inject public Resource resource. Just tried the @Self, same
>>>>> result
>>>>>> - I have @Model(adaptables = Resource.class). I don't want to bind my
>>>>> model to a custom Resource class, that would be the same as saying I
>> have
>>>>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
>>>>> decorates your resource implementation class, has nothing to do with
>> your
>>>>> Resource interface you are coming from.
>>>>>>
>>>>>> Greets,
>>>>>> Roy
>>>>>>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]> wrote:
>>>>>>>
>>>>>>> Hey Roy,
>>>>>>>
>>>>>>> Couple of Assumptions:
>>>>>>>
>>>>>>> For the Self resource you have it defined as:
>>>>>>>
>>>>>>> @Self
>>>>>>> public Resource resource;
>>>>>>>
>>>>>>> and for the @Model you have something like
>>>>>>> @Model(adaptables = RoysCustomDecorator.class)
>>>>>>>
>>>>>>> Am I close?
>>>>>>>
>>>>>>> -Jason
>>>>>>>
>>>>>>> -----Original Message-----
>>>>>>> From: Roy Teeuwen [mailto:[hidden email]]
>>>>>>> Sent: Tuesday, June 06, 2017 10:08 AM
>>>>>>> To: [hidden email]
>>>>>>> Subject: Sling models - Resource decorator
>>>>>>>
>>>>>>> Hey all,
>>>>>>>
>>>>>>> I am using sling models version 1.2.2, and I have the following
>> logic:
>>>>>>>
>>>>>>> - I find some resources with the resource resolver
>>>>> (resourceResolver.findResources)
>>>>>>> - The returned resources are the correct resources which are
>> decorated
>>>>> by my custom resource decorator
>>>>>>> - I iterate the list to cast them to the sling model I want them to
>>>>> be, using a resource.adaptTo
>>>>>>> - The sling model itself has a field Resource resource, to inject
>>>>> itself
>>>>>>>
>>>>>>> What I notice now is that the injected resource is a JcrNodeResource
>>>>> and not my decorated resource class anymore. I guess this is a bug in
>> the
>>>>> sling models implementation? Am I doing something wrong here or is
>> this an
>>>>> actual bug in the sling models implementation? Is this maybe already
>> fixed
>>>>> in any of the future versions?
>>>>>>>
>>>>>>> Greets,
>>>>>>> Roy
>>>>>>
>>>>>
>>>>>
>>
>>


signature.asc (817 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

justzzzz
Hi Roy,
That's not the reason :) this is an implementation detail of the way the
wrapping is done, so it belongs in the wrapper.

Here's a bit more detail on the problem which I hope helps explain where
the issue lies.

When you call resource.adaptTo(Class) on a ResourceWrapper, it is actually
returning the result of adaptTo(Class) on the wrapped resource, i.e. when
you call

SelfModel selfModel = resource.adaptTo(SelfModel.class);

What is actually happening is

SelfModel selfModel = resource.getResource().adaptTo(SelfModel.class);

This is documented in the JavaDoc:
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java?view=markup#l174
<http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java?revision=1751431&view=markup#l174>

If your wrapper needs to behave differently, then it stands to reason it
would need to implement its own adaptTo() method. I've updated the test to
use this technique. See
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/services/TestResourceDecorator.java?view=markup

That said, I agree that this is a bit intrusive. One idea could be to
change the implementation in ResourceWrapper to do what I'm describing all
the time.

Regards,
Justin




On Mon, Jun 12, 2017 at 3:40 PM Roy Teeuwen <[hidden email]> wrote:

> Hey Justin,
>
> The reason why it is odd to me is that it should be agnostic to being
> wrapped or not.
>
> You use the resourceresolver to get the resource and then do an adapt, all
> not knowing anything about being wrapped or not, seeing as the resource
> resolver also does the wrapping internally, why wouldn't the adaptTo do it?
>
> Resource resource = resourceResolver.getResource("/my/path");
> SelfModel selfModel = resource.adaptTo(SelfModel.class);
>
> Greets,
> Roy
>
> > On 12 Jun 2017, at 21:32, Justin Edelson <[hidden email]>
> wrote:
> >
> > Hi Roy,
> > OK. That makes sense (and I'm sorry I missed that part in your original
> > email). The problem is that if you use WrappedResource, it delegates the
> > adaptTo() call to the wrapped resource, i.e. the wrapper is lost at that
> > point and the AdapterFactory (i.e. Sling Models) doesn't have access to
> it
> > anymore. I'm not sure how this can be best be handled. One approach would
> > be in your wrapper class to implement the adaptTo method. Something like:
> >
> > class Wrapper extends ResourceWrapper {
> >        private Wrapper(Resource r) {
> >            super(r);
> >        }
> >
> >        @Override
> >        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type)
> {
> >            AdapterType adapter = adapterManager.getAdapter(this, type);
> >            if (adapter != null) {
> >                return adapter;
> >            } else {
> >                return super.adaptTo(type);
> >            }
> >        }
> >    }
> >
> > But this looks a bit odd to me, although I can't quite put my finger on
> why.
> >
> > Regards,
> > Justin
> >
> > On Mon, Jun 12, 2017 at 3:21 PM Roy Teeuwen <[hidden email]> wrote:
> >
> >> Hey Justin,
> >>
> >> Cool, thanks for the integration test!
> >> Just change the modelFactory.createModel to
> >> resource.adaptTo(SelfModel.class) and you will see that the test fails
> :)
> >>
> >> Greets,
> >> Roy
> >>> On 12 Jun 2017, at 18:52, Justin Edelson <[hidden email]>
> >> wrote:
> >>>
> >>> FWIW, I committed an integration test at
> >>> http://svn.apache.org/viewvc?view=revision&revision=1798489 checking
> >> that
> >>> the wrapped resource is injected via the @Self annotation. Would be
> >>> interesting to see how this differs from what you are doing.
> >>>
> >>> Regards,
> >>> Justin
> >>>
> >>> On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <
> >> [hidden email]>
> >>> wrote:
> >>>
> >>>> Hi Roy,
> >>>> This sounds like unexpected behavior. Please create a JIRA for this,
> >>>> ideally with a test case, if you have not already.
> >>>>
> >>>> Thanks,
> >>>> Justin
> >>>>
> >>>> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
> >>>>
> >>>>> Hey Jason, Guys,
> >>>>>
> >>>>> No one can give me an answer if this is a bug or something wanted? I
> >> can
> >>>>> always register a Jira ticket for and put it on the dev list of
> course
> >>>>>
> >>>>> Greets,
> >>>>> Roy
> >>>>>> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
> >>>>>>
> >>>>>> Hey Jason,
> >>>>>>
> >>>>>> No to both assumptions
> >>>>>>
> >>>>>> - I used @Inject public Resource resource. Just tried the @Self,
> same
> >>>>> result
> >>>>>> - I have @Model(adaptables = Resource.class). I don't want to bind
> my
> >>>>> model to a custom Resource class, that would be the same as saying I
> >> have
> >>>>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
> >>>>> decorates your resource implementation class, has nothing to do with
> >> your
> >>>>> Resource interface you are coming from.
> >>>>>>
> >>>>>> Greets,
> >>>>>> Roy
> >>>>>>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]>
> wrote:
> >>>>>>>
> >>>>>>> Hey Roy,
> >>>>>>>
> >>>>>>> Couple of Assumptions:
> >>>>>>>
> >>>>>>> For the Self resource you have it defined as:
> >>>>>>>
> >>>>>>> @Self
> >>>>>>> public Resource resource;
> >>>>>>>
> >>>>>>> and for the @Model you have something like
> >>>>>>> @Model(adaptables = RoysCustomDecorator.class)
> >>>>>>>
> >>>>>>> Am I close?
> >>>>>>>
> >>>>>>> -Jason
> >>>>>>>
> >>>>>>> -----Original Message-----
> >>>>>>> From: Roy Teeuwen [mailto:[hidden email]]
> >>>>>>> Sent: Tuesday, June 06, 2017 10:08 AM
> >>>>>>> To: [hidden email]
> >>>>>>> Subject: Sling models - Resource decorator
> >>>>>>>
> >>>>>>> Hey all,
> >>>>>>>
> >>>>>>> I am using sling models version 1.2.2, and I have the following
> >> logic:
> >>>>>>>
> >>>>>>> - I find some resources with the resource resolver
> >>>>> (resourceResolver.findResources)
> >>>>>>> - The returned resources are the correct resources which are
> >> decorated
> >>>>> by my custom resource decorator
> >>>>>>> - I iterate the list to cast them to the sling model I want them to
> >>>>> be, using a resource.adaptTo
> >>>>>>> - The sling model itself has a field Resource resource, to inject
> >>>>> itself
> >>>>>>>
> >>>>>>> What I notice now is that the injected resource is a
> JcrNodeResource
> >>>>> and not my decorated resource class anymore. I guess this is a bug in
> >> the
> >>>>> sling models implementation? Am I doing something wrong here or is
> >> this an
> >>>>> actual bug in the sling models implementation? Is this maybe already
> >> fixed
> >>>>> in any of the future versions?
> >>>>>>>
> >>>>>>> Greets,
> >>>>>>> Roy
> >>>>>>
> >>>>>
> >>>>>
> >>
> >>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sling models - Resource decorator

Roy Teeuwen
Hey Justin,

Aha ok I get what you are saying. Then the problem indeed lies with the ResourceWrapper for not using the AdapterFactory. Thanks!

I guess I can change this in my own code to override the method in my resource wrapper, although it still feels like something I wouldn't expect, definitely because I'm using @Self.
Changing the ResourceWrapper implementation itself though would probably take it a lot further and have some side effects on dependent bundles :/.

Greets,
Roy

> On 12 Jun 2017, at 21:55, Justin Edelson <[hidden email]> wrote:
>
> Hi Roy,
> That's not the reason :) this is an implementation detail of the way the
> wrapping is done, so it belongs in the wrapper.
>
> Here's a bit more detail on the problem which I hope helps explain where
> the issue lies.
>
> When you call resource.adaptTo(Class) on a ResourceWrapper, it is actually
> returning the result of adaptTo(Class) on the wrapped resource, i.e. when
> you call
>
> SelfModel selfModel = resource.adaptTo(SelfModel.class);
>
> What is actually happening is
>
> SelfModel selfModel = resource.getResource().adaptTo(SelfModel.class);
>
> This is documented in the JavaDoc:
> http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java?view=markup#l174
> <http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceWrapper.java?revision=1751431&view=markup#l174>
>
> If your wrapper needs to behave differently, then it stands to reason it
> would need to implement its own adaptTo() method. I've updated the test to
> use this technique. See
> http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/services/TestResourceDecorator.java?view=markup
>
> That said, I agree that this is a bit intrusive. One idea could be to
> change the implementation in ResourceWrapper to do what I'm describing all
> the time.
>
> Regards,
> Justin
>
>
>
>
> On Mon, Jun 12, 2017 at 3:40 PM Roy Teeuwen <[hidden email]> wrote:
>
>> Hey Justin,
>>
>> The reason why it is odd to me is that it should be agnostic to being
>> wrapped or not.
>>
>> You use the resourceresolver to get the resource and then do an adapt, all
>> not knowing anything about being wrapped or not, seeing as the resource
>> resolver also does the wrapping internally, why wouldn't the adaptTo do it?
>>
>> Resource resource = resourceResolver.getResource("/my/path");
>> SelfModel selfModel = resource.adaptTo(SelfModel.class);
>>
>> Greets,
>> Roy
>>
>>> On 12 Jun 2017, at 21:32, Justin Edelson <[hidden email]>
>> wrote:
>>>
>>> Hi Roy,
>>> OK. That makes sense (and I'm sorry I missed that part in your original
>>> email). The problem is that if you use WrappedResource, it delegates the
>>> adaptTo() call to the wrapped resource, i.e. the wrapper is lost at that
>>> point and the AdapterFactory (i.e. Sling Models) doesn't have access to
>> it
>>> anymore. I'm not sure how this can be best be handled. One approach would
>>> be in your wrapper class to implement the adaptTo method. Something like:
>>>
>>> class Wrapper extends ResourceWrapper {
>>>       private Wrapper(Resource r) {
>>>           super(r);
>>>       }
>>>
>>>       @Override
>>>       public <AdapterType> AdapterType adaptTo(Class<AdapterType> type)
>> {
>>>           AdapterType adapter = adapterManager.getAdapter(this, type);
>>>           if (adapter != null) {
>>>               return adapter;
>>>           } else {
>>>               return super.adaptTo(type);
>>>           }
>>>       }
>>>   }
>>>
>>> But this looks a bit odd to me, although I can't quite put my finger on
>> why.
>>>
>>> Regards,
>>> Justin
>>>
>>> On Mon, Jun 12, 2017 at 3:21 PM Roy Teeuwen <[hidden email]> wrote:
>>>
>>>> Hey Justin,
>>>>
>>>> Cool, thanks for the integration test!
>>>> Just change the modelFactory.createModel to
>>>> resource.adaptTo(SelfModel.class) and you will see that the test fails
>> :)
>>>>
>>>> Greets,
>>>> Roy
>>>>> On 12 Jun 2017, at 18:52, Justin Edelson <[hidden email]>
>>>> wrote:
>>>>>
>>>>> FWIW, I committed an integration test at
>>>>> http://svn.apache.org/viewvc?view=revision&revision=1798489 checking
>>>> that
>>>>> the wrapped resource is injected via the @Self annotation. Would be
>>>>> interesting to see how this differs from what you are doing.
>>>>>
>>>>> Regards,
>>>>> Justin
>>>>>
>>>>> On Mon, Jun 12, 2017 at 12:23 PM Justin Edelson <
>>>> [hidden email]>
>>>>> wrote:
>>>>>
>>>>>> Hi Roy,
>>>>>> This sounds like unexpected behavior. Please create a JIRA for this,
>>>>>> ideally with a test case, if you have not already.
>>>>>>
>>>>>> Thanks,
>>>>>> Justin
>>>>>>
>>>>>> On Mon, Jun 12, 2017 at 4:38 AM Roy Teeuwen <[hidden email]> wrote:
>>>>>>
>>>>>>> Hey Jason, Guys,
>>>>>>>
>>>>>>> No one can give me an answer if this is a bug or something wanted? I
>>>> can
>>>>>>> always register a Jira ticket for and put it on the dev list of
>> course
>>>>>>>
>>>>>>> Greets,
>>>>>>> Roy
>>>>>>>> On 6 Jun 2017, at 21:50, Roy Teeuwen <[hidden email]> wrote:
>>>>>>>>
>>>>>>>> Hey Jason,
>>>>>>>>
>>>>>>>> No to both assumptions
>>>>>>>>
>>>>>>>> - I used @Inject public Resource resource. Just tried the @Self,
>> same
>>>>>>> result
>>>>>>>> - I have @Model(adaptables = Resource.class). I don't want to bind
>> my
>>>>>>> model to a custom Resource class, that would be the same as saying I
>>>> have
>>>>>>> to do @Model(adaptables = JcrNodeResource.class), the decorator just
>>>>>>> decorates your resource implementation class, has nothing to do with
>>>> your
>>>>>>> Resource interface you are coming from.
>>>>>>>>
>>>>>>>> Greets,
>>>>>>>> Roy
>>>>>>>>> On 6 Jun 2017, at 21:41, Jason Bailey <[hidden email]>
>> wrote:
>>>>>>>>>
>>>>>>>>> Hey Roy,
>>>>>>>>>
>>>>>>>>> Couple of Assumptions:
>>>>>>>>>
>>>>>>>>> For the Self resource you have it defined as:
>>>>>>>>>
>>>>>>>>> @Self
>>>>>>>>> public Resource resource;
>>>>>>>>>
>>>>>>>>> and for the @Model you have something like
>>>>>>>>> @Model(adaptables = RoysCustomDecorator.class)
>>>>>>>>>
>>>>>>>>> Am I close?
>>>>>>>>>
>>>>>>>>> -Jason
>>>>>>>>>
>>>>>>>>> -----Original Message-----
>>>>>>>>> From: Roy Teeuwen [mailto:[hidden email]]
>>>>>>>>> Sent: Tuesday, June 06, 2017 10:08 AM
>>>>>>>>> To: [hidden email]
>>>>>>>>> Subject: Sling models - Resource decorator
>>>>>>>>>
>>>>>>>>> Hey all,
>>>>>>>>>
>>>>>>>>> I am using sling models version 1.2.2, and I have the following
>>>> logic:
>>>>>>>>>
>>>>>>>>> - I find some resources with the resource resolver
>>>>>>> (resourceResolver.findResources)
>>>>>>>>> - The returned resources are the correct resources which are
>>>> decorated
>>>>>>> by my custom resource decorator
>>>>>>>>> - I iterate the list to cast them to the sling model I want them to
>>>>>>> be, using a resource.adaptTo
>>>>>>>>> - The sling model itself has a field Resource resource, to inject
>>>>>>> itself
>>>>>>>>>
>>>>>>>>> What I notice now is that the injected resource is a
>> JcrNodeResource
>>>>>>> and not my decorated resource class anymore. I guess this is a bug in
>>>> the
>>>>>>> sling models implementation? Am I doing something wrong here or is
>>>> this an
>>>>>>> actual bug in the sling models implementation? Is this maybe already
>>>> fixed
>>>>>>> in any of the future versions?
>>>>>>>>>
>>>>>>>>> Greets,
>>>>>>>>> Roy
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>
>>>>
>>
>>


signature.asc (817 bytes) Download Attachment
Loading...