Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Henry Saginor
Can’t you just create sling servlet registered to some resource type? 
You can then simply create JCR node(s) mapped to your resource type. 
You can also use a SynthaticResource via custom resource provider, which is the 
track you’re on and what Steven is suggesting. 
But I find that in most cases just creating a JCR node is more convenient and 
you can apply JCR ACLs to it control access if you need it. 

> On Jan 27, 2017, at 9:34 PM, Steven Walters  wrote:
> 
> On Sat, Jan 28, 2017 at 6:27 AM, lancedolan  wrote:
>> Hi friends,
>> 
>> I've tried routing questions through stackoverflow to cut down my mails to
>> this list. I'm losing lots of time on this one, though, and am stuck.
>> 
>> I need to create APIs which don't represent Sling Resources. Example:
>> /services/images/123123123
>> that image will exist somewhere else.
>> 
>> Bertrand suggests creating a ResourceProvider, as in the example here [1].
>> However, that uses the spi package which is not in version 2.9.0 of
>> org.apache.sling.api, and thus, not available to me in Sling 8.
>> 
>> I did find a ResourceProvider interface to implement though, and created
>> this code:
>> 
>> /**
>> * Created by lancedolan on 1/27/17.
>> */
>> @Component
>> @Service(value=ResourceProvider.class)
>> @Properties({
>>@Property(name = ResourceProvider.ROOTS, value = "things"),
>>@Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
>> })
>> public class ImageResourceProvider implements ResourceProvider {
>> 
>>/** If this provider required a context this would be more elaborate,
>> *  but for this simple example we don't need one.
>> */
>>public static class DoesNotNeedAContext {
>>};
>> 
>>@Override
>>public Resource getResource(ResourceResolver resourceResolver, String
>> path) {
>>Resource returnResource = new SyntheticResource(resourceResolver,
>> path, "edlio/microservice/image");
>>returnResource.getValueMap().put("myProp" , "myValue");
>>return returnResource;
>>}
>> 
>>@Override
>>public Resource getResource(ResourceResolver resourceResolver,
>> HttpServletRequest httpServletRequest, String path) {
>>return getResource(resourceResolver , path);
>>}
>> 
>>@Override
>>public Iterator listChildren(Resource resource) {
>>return null;
>>}
>> }
>> 
>> 
>> The result is that I get a 403 response. How do I control the authentication
>> for resources that don't actually exist? The fact that I'm not getting 404
>> means that my ResourceProvider is at least registering successfully.
>> 
>> Finally, I'd much prefer to use Jersey if possible... Anybody have success
>> getting Jersey to work in Sling 8? I dumped a bunch of time into it and gave
>> up after class not found errors for classes that should be found [2].
>> 
>> The ultimate goal is just to provide a restful API in Sling 8 and the
>> static-path-declaration of SlingSafeMethodsServlet just doesn't cut it.
>> 
>> Thanks a million guys...
> 
> The Sling paradigm for this is to create resources through the
> resource provider,
> and then create/register a servlet to respond to the resource type
> you're creating (not a static path).
> 
> So from your above code, you'd create an additional servlet registered
> to the resourceType "edlio/microservice/image" (instead of registered
> to a static path) and implement GET inside it.
> 
> Without the corresponding servlet, this is most likely being served by
> the default GET servlet, which will not particularly understand how to
> deal with these resources, thus the errors.



Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Steven Walters
On Sat, Jan 28, 2017 at 6:27 AM, lancedolan  wrote:
> Hi friends,
>
> I've tried routing questions through stackoverflow to cut down my mails to
> this list. I'm losing lots of time on this one, though, and am stuck.
>
> I need to create APIs which don't represent Sling Resources. Example:
> /services/images/123123123
> that image will exist somewhere else.
>
> Bertrand suggests creating a ResourceProvider, as in the example here [1].
> However, that uses the spi package which is not in version 2.9.0 of
> org.apache.sling.api, and thus, not available to me in Sling 8.
>
> I did find a ResourceProvider interface to implement though, and created
> this code:
>
> /**
>  * Created by lancedolan on 1/27/17.
>  */
> @Component
> @Service(value=ResourceProvider.class)
> @Properties({
> @Property(name = ResourceProvider.ROOTS, value = "things"),
> @Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
> })
> public class ImageResourceProvider implements ResourceProvider {
>
> /** If this provider required a context this would be more elaborate,
>  *  but for this simple example we don't need one.
>  */
> public static class DoesNotNeedAContext {
> };
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver, String
> path) {
> Resource returnResource = new SyntheticResource(resourceResolver,
> path, "edlio/microservice/image");
> returnResource.getValueMap().put("myProp" , "myValue");
> return returnResource;
> }
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver,
> HttpServletRequest httpServletRequest, String path) {
> return getResource(resourceResolver , path);
> }
>
> @Override
> public Iterator listChildren(Resource resource) {
> return null;
> }
> }
>
>
> The result is that I get a 403 response. How do I control the authentication
> for resources that don't actually exist? The fact that I'm not getting 404
> means that my ResourceProvider is at least registering successfully.
>
> Finally, I'd much prefer to use Jersey if possible... Anybody have success
> getting Jersey to work in Sling 8? I dumped a bunch of time into it and gave
> up after class not found errors for classes that should be found [2].
>
> The ultimate goal is just to provide a restful API in Sling 8 and the
> static-path-declaration of SlingSafeMethodsServlet just doesn't cut it.
>
> Thanks a million guys...

The Sling paradigm for this is to create resources through the
resource provider,
and then create/register a servlet to respond to the resource type
you're creating (not a static path).

So from your above code, you'd create an additional servlet registered
to the resourceType "edlio/microservice/image" (instead of registered
to a static path) and implement GET inside it.

Without the corresponding servlet, this is most likely being served by
the default GET servlet, which will not particularly understand how to
deal with these resources, thus the errors.


Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Mike Nimer
Check out http://neba.io/ it should do what you need with Spring
instead of Jersey.

hope this helps
--mike

On Fri, Jan 27, 2017 at 3:27 PM, lancedolan  wrote:
> Hi friends,
>
> I've tried routing questions through stackoverflow to cut down my mails to
> this list. I'm losing lots of time on this one, though, and am stuck.
>
> I need to create APIs which don't represent Sling Resources. Example:
> /services/images/123123123
> that image will exist somewhere else.
>
> Bertrand suggests creating a ResourceProvider, as in the example here [1].
> However, that uses the spi package which is not in version 2.9.0 of
> org.apache.sling.api, and thus, not available to me in Sling 8.
>
> I did find a ResourceProvider interface to implement though, and created
> this code:
>
> /**
>  * Created by lancedolan on 1/27/17.
>  */
> @Component
> @Service(value=ResourceProvider.class)
> @Properties({
> @Property(name = ResourceProvider.ROOTS, value = "things"),
> @Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
> })
> public class ImageResourceProvider implements ResourceProvider {
>
> /** If this provider required a context this would be more elaborate,
>  *  but for this simple example we don't need one.
>  */
> public static class DoesNotNeedAContext {
> };
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver, String
> path) {
> Resource returnResource = new SyntheticResource(resourceResolver,
> path, "edlio/microservice/image");
> returnResource.getValueMap().put("myProp" , "myValue");
> return returnResource;
> }
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver,
> HttpServletRequest httpServletRequest, String path) {
> return getResource(resourceResolver , path);
> }
>
> @Override
> public Iterator listChildren(Resource resource) {
> return null;
> }
> }
>
>
> The result is that I get a 403 response. How do I control the authentication
> for resources that don't actually exist? The fact that I'm not getting 404
> means that my ResourceProvider is at least registering successfully.
>
> Finally, I'd much prefer to use Jersey if possible... Anybody have success
> getting Jersey to work in Sling 8? I dumped a bunch of time into it and gave
> up after class not found errors for classes that should be found [2].
>
> The ultimate goal is just to provide a restful API in Sling 8 and the
> static-path-declaration of SlingSafeMethodsServlet just doesn't cut it.
>
> Thanks a million guys...
>
>
>
> [1]
> https://github.com/apache/sling/blob/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java
>
> [2] http://stackoverflow.com/questions/41901337/how-to-use-jersey-in-sling
>
>
>
>
>
> --
> View this message in context: 
> http://apache-sling.73963.n3.nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-Sling-8-tp4069947.html
> Sent from the Sling - Users mailing list archive at Nabble.com.


Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Andreas Schaefer Sr.
I never develop for Sling but a few years doing backend on AEM.

So I am wondering if you used the Sling IDE Tooling (Eclipse or our IntelliJ 
Plugin)
and debugged your code there.

Did you try to use a Servlet that is bound to a Type instead of a Path? This
might do what you want.

Cheers - Andy

> On Jan 27, 2017, at 2:29 PM, lancedolan  wrote:
> 
> These APIs I'm looking to build are likely to use other OSGI services I've
> written, and perhaps even write to the JCR for other reasons. Imagine doing
> a GET /api/images/my-new-image.jpg and the image lives in Amazon S3, but you
> want to write some audit data into the JCR.
> 
> I'm still thinking through the overall solution, and have considered you're
> suggestion, but I don't want to give up on building APIs inside of
> OSGI/Sling unless I really need to.
> 
> 
> 
> --
> View this message in context: 
> http://apache-sling.73963.n3.nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-Sling-8-tp4069947p4069949.html
> Sent from the Sling - Users mailing list archive at Nabble.com.



Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Roy Teeuwen
Hey Lance,

If you really want to use jax-rs, have a look at the following:
https://github.com/apache/aries-jax-rs-whiteboard 


Greets,
Roy

> On 27 Jan 2017, at 23:29, lancedolan  wrote:
> 
> These APIs I'm looking to build are likely to use other OSGI services I've
> written, and perhaps even write to the JCR for other reasons. Imagine doing
> a GET /api/images/my-new-image.jpg and the image lives in Amazon S3, but you
> want to write some audit data into the JCR.
> 
> I'm still thinking through the overall solution, and have considered you're
> suggestion, but I don't want to give up on building APIs inside of
> OSGI/Sling unless I really need to.
> 
> 
> 
> --
> View this message in context: 
> http://apache-sling.73963.n3.nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-Sling-8-tp4069947p4069949.html
> Sent from the Sling - Users mailing list archive at Nabble.com.



signature.asc
Description: Message signed with OpenPGP


Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread lancedolan
These APIs I'm looking to build are likely to use other OSGI services I've
written, and perhaps even write to the JCR for other reasons. Imagine doing
a GET /api/images/my-new-image.jpg and the image lives in Amazon S3, but you
want to write some audit data into the JCR.

I'm still thinking through the overall solution, and have considered you're
suggestion, but I don't want to give up on building APIs inside of
OSGI/Sling unless I really need to.



--
View this message in context: 
http://apache-sling.73963.n3.nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-Sling-8-tp4069947p4069949.html
Sent from the Sling - Users mailing list archive at Nabble.com.


Re: How to create Rest APIs for non-JCR data in Sling 8??

2017-01-27 Thread Ben Fortuna
Hi Lance,

I would personally recommend that you find a different solution for serving
REST APIs. I also thought it might be good to service an API from Sling, as
it does make JSON a "first class citizen", but then I realised that Sling
is geared towards serving resources from JCR and ultimately you'll be
fighting with the platform to serve an API without all the JCR metadata.

At very least you could try to run Jersey using plain Servlets, but I think
Sling should be left to do what it is meant for.

Other solutions such as Spring Boot or Ratpack are specifically designed
for serving REST APIs, and with Docker it is trivial to support both a
Sling platform for web content and an API back-end on the same server.

One thing about REST APIs I do think Sling is good for is stubbing. You can
easily create example API responses to test your web UI without needing to
run with a back-end (good for UI testing).

regards,
ben


On 28 January 2017 at 08:27, lancedolan  wrote:

> Hi friends,
>
> I've tried routing questions through stackoverflow to cut down my mails to
> this list. I'm losing lots of time on this one, though, and am stuck.
>
> I need to create APIs which don't represent Sling Resources. Example:
> /services/images/123123123
> that image will exist somewhere else.
>
> Bertrand suggests creating a ResourceProvider, as in the example here [1].
> However, that uses the spi package which is not in version 2.9.0 of
> org.apache.sling.api, and thus, not available to me in Sling 8.
>
> I did find a ResourceProvider interface to implement though, and created
> this code:
>
> /**
>  * Created by lancedolan on 1/27/17.
>  */
> @Component
> @Service(value=ResourceProvider.class)
> @Properties({
> @Property(name = ResourceProvider.ROOTS, value = "things"),
> @Property(name = ResourceProvider.OWNS_ROOTS, value = "true")
> })
> public class ImageResourceProvider implements ResourceProvider {
>
> /** If this provider required a context this would be more elaborate,
>  *  but for this simple example we don't need one.
>  */
> public static class DoesNotNeedAContext {
> };
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver, String
> path) {
> Resource returnResource = new SyntheticResource(resourceResolver,
> path, "edlio/microservice/image");
> returnResource.getValueMap().put("myProp" , "myValue");
> return returnResource;
> }
>
> @Override
> public Resource getResource(ResourceResolver resourceResolver,
> HttpServletRequest httpServletRequest, String path) {
> return getResource(resourceResolver , path);
> }
>
> @Override
> public Iterator listChildren(Resource resource) {
> return null;
> }
> }
>
>
> The result is that I get a 403 response. How do I control the
> authentication
> for resources that don't actually exist? The fact that I'm not getting 404
> means that my ResourceProvider is at least registering successfully.
>
> Finally, I'd much prefer to use Jersey if possible... Anybody have success
> getting Jersey to work in Sling 8? I dumped a bunch of time into it and
> gave
> up after class not found errors for classes that should be found [2].
>
> The ultimate goal is just to provide a restful API in Sling 8 and the
> static-path-declaration of SlingSafeMethodsServlet just doesn't cut it.
>
> Thanks a million guys...
>
>
>
> [1]
> https://github.com/apache/sling/blob/trunk/launchpad/
> test-services/src/main/java/org/apache/sling/launchpad/
> testservices/resourceprovider/PlanetsResourceProvider.java
>
> [2] http://stackoverflow.com/questions/41901337/how-to-use-jersey-in-sling
>
>
>
>
>
> --
> View this message in context: http://apache-sling.73963.n3.
> nabble.com/How-to-create-Rest-APIs-for-non-JCR-data-in-
> Sling-8-tp4069947.html
> Sent from the Sling - Users mailing list archive at Nabble.com.
>