You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by connuser1 connuser1 <co...@gmail.com> on 2015/01/07 06:54:45 UTC

Fwd: Resource Type for custom resource provider

Hi

I am writing a custom resource provider by implementing
the ResourceProviderFactory interface. Currently I have sets its 'Resource
Type' to a path which maps to my custom servlet for rendering this
resource. This is working perfectly but I am not really sure if this is the
right way to do it.

Should I be writing my own servlet for rendering the resource or should I
use the sling default GET servlet?

I tried setting the resource type to sling/servlet/default but when I make
a GET request to the resource using the json extension, I just get the
{"sling:resourceType":"sling/servlet/default"} in response and nothing
else. I was expecting to see the resource metadata that I have set on this
resource. Please guide.

Thanks

Re: Resource Type for custom resource provider

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Mon, Jan 12, 2015 at 10:56 AM, connuser1 connuser1
<co...@gmail.com> wrote:
> ...Eventually I figured out the problem by comparing it with planets. The
> problem was that my resource provider was providing a resource for the
> complete path including the extension and hence the complete path was being
> interpreted as resource path....

ah ok makes sense, I didn't think of that option earlier. Happy that
our trivial example helped!

-Bertrand

Re: Resource Type for custom resource provider

Posted by connuser1 connuser1 <co...@gmail.com>.
Thanks Bertrand!
Yes my requirement is similar to planets.
Eventually I figured out the problem by comparing it with planets. The
problem was that my resource provider was providing a resource for the
complete path including the extension and hence the complete path was being
interpreted as resource path. I modified it to return the resource on the
correct path and now its working fine.

On Fri, Jan 9, 2015 at 4:33 PM, Bertrand Delacretaz <bd...@apache.org>
wrote:

> Hi,
>
> On Fri, Jan 9, 2015 at 11:51 AM, connuser1 connuser1
> <co...@gmail.com> wrote:
> > ...I have set the RESOURCE_TYPE of my resource to
> > 'sling/servlet/default'...
>
> Setting a custom resource type is much better - the default servlets
> will still be used if you don't supply more specific ones, and you can
> later refine renderings without changes to your resource provider.
>
> > ...I am facing is that the DefaultGetServlet expects a
> > 'selector' to choose the correct rendering servlet...,
>
> I don't think so, that works correctly without selectors with the
> planets resource provider.
>
> After starting with "mvn clean launchpad:run" in
> sling/launchpad/testing I see this:
>
> $ curl http://localhost:8888/planets.tidy.2.json
> {
>   "sling:resourceType": "sling/test-services/planet",
>   "saturn": {
>     "distance": 10759,
>     "name": "Saturn"
>     },
> ...
>   "earth": {
>     "distance": 149600,
>     "name": "Earth",
>     "comment": "Resources can have different sets of properties",
>     "moon": {
>       "distance": 384,
>       "name": "Moon"
>       }
>     }
>   }
>
> And, with just an extension, no selector:
>
> $ curl http://localhost:8888/planets/earth.json
> {"distance":149600,"name":"Earth","comment":"Resources can have
> different sets of properties"}
>
> Isn't that what you want?
> If yes I suggest comparing what you do with our trivial planets
> ResourceProvider.
> -Bertrand
>

Re: Resource Type for custom resource provider

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Fri, Jan 9, 2015 at 11:51 AM, connuser1 connuser1
<co...@gmail.com> wrote:
> ...I have set the RESOURCE_TYPE of my resource to
> 'sling/servlet/default'...

Setting a custom resource type is much better - the default servlets
will still be used if you don't supply more specific ones, and you can
later refine renderings without changes to your resource provider.

> ...I am facing is that the DefaultGetServlet expects a
> 'selector' to choose the correct rendering servlet...,

I don't think so, that works correctly without selectors with the
planets resource provider.

After starting with "mvn clean launchpad:run" in
sling/launchpad/testing I see this:

$ curl http://localhost:8888/planets.tidy.2.json
{
  "sling:resourceType": "sling/test-services/planet",
  "saturn": {
    "distance": 10759,
    "name": "Saturn"
    },
...
  "earth": {
    "distance": 149600,
    "name": "Earth",
    "comment": "Resources can have different sets of properties",
    "moon": {
      "distance": 384,
      "name": "Moon"
      }
    }
  }

And, with just an extension, no selector:

$ curl http://localhost:8888/planets/earth.json
{"distance":149600,"name":"Earth","comment":"Resources can have
different sets of properties"}

Isn't that what you want?
If yes I suggest comparing what you do with our trivial planets
ResourceProvider.
-Bertrand

Re: Resource Type for custom resource provider

Posted by connuser1 connuser1 <co...@gmail.com>.
Hi Bertrand

Thanks for pointing to the Planet resource provider as it clarifies quite a
few things. I am almost there but still missing a few pieces.

I have setup properties on my custom resource using the value map. And I am
trying to rely on the default JSON renderer servlet to render my custom
resource. So for this, I have set the RESOURCE_TYPE of my resource to
'sling/servlet/default' . This setting is working fine as the
ServletResolver is resolving the DefaultGetServlet for rendering this
resource.

But the problem that I am facing is that the DefaultGetServlet expects a
'selector' to choose the correct rendering servlet. I am accessing my
resource as /path/to/my/resource.json but this is not setting the selector
to 'json' . I tried to debug this a bit and found that selectors are
figured out in SlingRequestPathInfo which uses the resource's
'resolutionPathInfo'  property to find out the selector. For my custom
resource, resolutionPathInfo is always an empty sting and hence no selector
is chosen for it. I tried setting resolutionPathInfo on my custom resource
through the metadata.setResolutionPathInfo.  But its not getting carried
over all the way through to the DefaultGetServlet. On debugging, I found
that its overwritten in ResourceResolver.resolveInternal as below:

        // SLING-627: set the part cut off from the uriPath as
        // sling.resolutionPathInfo property such that
        // uriPath = curPath + sling.resolutionPathInfo
        if (resource != null) {

            final String rpi = absPath.substring(curPath.length());

resource.getResourceMetadata().setResolutionPath(absPath.substring(0,
curPath.length()));
            resource.getResourceMetadata().setResolutionPathInfo(rpi);

            logger.debug("resolveInternal: Found resource {} with path info
{} for {}", new Object[] { resource, rpi, absPath });

        }

Hopefully I am able to explain the problem to you correctly. I am not sure
what am I missing here. Thanks for your patience. Please guide.

On Thu, Jan 8, 2015 at 2:15 PM, Bertrand Delacretaz <bd...@apache.org>
wrote:

> Hi,
>
> On Thu, Jan 8, 2015 at 8:19 AM, connuser1 connuser1 <co...@gmail.com>
> wrote:
> > ...I am creating a SyntheticResource. How do I add properties to it?...
>
> The Planets resource provider example at [1] might help, I think its
> json representation is what you expect. Note that it uses a
> SyntheticResource only for its root, the planets themselves are custom
> resources.
>
> -Bertrand
>
> [1]
> http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/
>

Re: Resource Type for custom resource provider

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Thu, Jan 8, 2015 at 8:19 AM, connuser1 connuser1 <co...@gmail.com> wrote:
> ...I am creating a SyntheticResource. How do I add properties to it?...

The Planets resource provider example at [1] might help, I think its
json representation is what you expect. Note that it uses a
SyntheticResource only for its root, the planets themselves are custom
resources.

-Bertrand

[1] http://svn.apache.org/repos/asf/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/

Re: Resource Type for custom resource provider

Posted by connuser1 connuser1 <co...@gmail.com>.
Thanks Bertrand!

>>what do you mean by "resource metadata"?
I meant org.apache.sling.api.resource.ResourceMetadata as used in here:
http://www.lucamasini.net/Home/sling-and-cq5/accessing-relational-data-as-sling-restful-urls
I am creating a SyntheticResource. How do I add properties to it?

On Wed, Jan 7, 2015 at 4:26 PM, Bertrand Delacretaz <bd...@apache.org>
wrote:

> Hi,
>
> On Wed, Jan 7, 2015 at 6:54 AM, connuser1 connuser1 <co...@gmail.com>
> wrote:
> > I am writing a custom resource provider by implementing
> > the ResourceProviderFactory interface. Currently I have sets its
> 'Resource
> > Type' to a path which maps to my custom servlet for rendering this
> > resource. This is working perfectly but I am not really sure if this is
> the
> > right way to do it...
>
> That's what I would recommend, with a partial path as the resource
> type like myapp/mything.
>
> >...Should I be writing my own servlet for rendering the resource or
> should I
> > use the sling default GET servlet?...
>
> If the default rendering works that's fine, and you can always add
> more specific servlets or scripts later, for specific request
> extensions, selectors etc.
>
> > ....I tried setting the resource type to sling/servlet/default but when
> I make
> > a GET request to the resource using the json extension, I just get the
> > {"sling:resourceType":"sling/servlet/default"} in response and nothing
> > else. I was expecting to see the resource metadata that I have set on
> this
> > resource....
>
> The default json servlet outputs all resource properties, but what do
> you mean by "resource metadata"?
>
> Note that even with a custom resource type like myapp/mything, the
> default json servlet will kick in unless you provide a more specific
> one.
>
> -Bertrand
>

Re: Resource Type for custom resource provider

Posted by Bertrand Delacretaz <bd...@apache.org>.
Hi,

On Wed, Jan 7, 2015 at 6:54 AM, connuser1 connuser1 <co...@gmail.com> wrote:
> I am writing a custom resource provider by implementing
> the ResourceProviderFactory interface. Currently I have sets its 'Resource
> Type' to a path which maps to my custom servlet for rendering this
> resource. This is working perfectly but I am not really sure if this is the
> right way to do it...

That's what I would recommend, with a partial path as the resource
type like myapp/mything.

>...Should I be writing my own servlet for rendering the resource or should I
> use the sling default GET servlet?...

If the default rendering works that's fine, and you can always add
more specific servlets or scripts later, for specific request
extensions, selectors etc.

> ....I tried setting the resource type to sling/servlet/default but when I make
> a GET request to the resource using the json extension, I just get the
> {"sling:resourceType":"sling/servlet/default"} in response and nothing
> else. I was expecting to see the resource metadata that I have set on this
> resource....

The default json servlet outputs all resource properties, but what do
you mean by "resource metadata"?

Note that even with a custom resource type like myapp/mything, the
default json servlet will kick in unless you provide a more specific
one.

-Bertrand