Oh you beat to it.:)  I had a similar approach for aggregating page specific 
JavaScript via a fake resource and was going to provide a skeleton of it. But I 
am glad you got it to work.

I want to add that Sling could probably benefit from Jersey-like ability to 
generate web services from simple POJOs. Maybe some day something like that can 
be implemented on top of Sling Models. :)
I think this is mainly what developers are looking for when they talk about 
such integration.

> On Jan 31, 2017, at 1:25 PM, lancedolan <lance.do...@gmail.com> wrote:
> 
> Aha! Solved!!!! Here's my solution for posterity.
> 
> While Jersey would have been a preferred, more feature-rich solution, I just
> couldn't get the OSGI-Jersey stuff working.
> 
> The solution: 
> 
> - ResourceProvider listens for all requests to a particular path, and
> returns a false "Resource" object, which doesn't actually exist in the JCR,
> but it does have a resourceType
> 
> - A Servlet registers to render that resourceType.
> 
> Between these two, you've essentially got a Servlet that listens to a all
> requests that fall under a particular Path :)
> 
> Registering a Servlet for a resourceType is pretty elementary, but for
> posterity looking to get this ResourceProvider working in Sling 8, here's
> how I did it. I expect there are better ways, but this is demonstrative:
> 
> 
> /**
> * Created by lancedolan on 1/27/17.
> */
> @Component
> @Service(value=ResourceProvider.class)
> @Properties({
>        @Property(name = ResourceProvider.ROOTS, value = "service/image"),
>        @Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
> })
> public class ImageResourceProvider implements ResourceProvider  {
> 
> //    @Override
>    public Resource getResource(ResourceResolver resourceResolver, String
> path) {
> 
>        AbstractResource abstractResource;
>        abstractResource = new AbstractResource() {
>            @Override
>            public String getResourceType() {
>                return ImageTypeServlet.RESOURCE_TYPE;
>            }
> 
>            @Override
>            public String getResourceSuperType() {
>                return null;
>            }
> 
>            @Override
>            public String getPath() {
>                return path;
>            }
> 
>            @Override
>            public ResourceResolver getResourceResolver() {
>                return resourceResolver;
>            }
> 
>            @Override
>            public ResourceMetadata getResourceMetadata() {
>                return new ResourceMetadata();
>            }
>        };
> 
>        return abstractResource;
>    }
> 
> //    @Override
>    public Resource getResource(ResourceResolver resourceResolver,
> HttpServletRequest httpServletRequest, String path) {
>        return getResource(resourceResolver , path);
>    }
> 
> //    @Override
>    public Iterator<Resource> listChildren(Resource resource) {
>        return null;
>    }
> }
> 
> And then you just create your own servlet, analogous to my ImageTypeServlet
> which contains a static final String RESOURCE_TYPE
> 
> 
> 
> --
> View this message in context: 
> http://apache-sling.73963.n3.nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-Sling-8-tp4069947p4070023.html
> Sent from the Sling - Users mailing list archive at Nabble.com.

Reply via email to