Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Apache Sling as an OSGi-powered REST middleware

1,387 views

Published on

Apache Sling is an innovative web framework built on top of the Java Content Repository (JCR), that uses OSGi for its component model and fosters RESTful application design.

This presentation will showcase how the Apache Sling framework can be used to build a unified REST-based middleware for multiple data sources, such as MongoDB, Apache Cassandra, Apache Jackrabbit Oak or classical relational databases. In doing so it will present the benefits that come from a single Resource API over multiple data stores, both at the library level and at the HTTP boundary.

Published in: Software
  • Be the first to comment

Apache Sling as an OSGi-powered REST middleware

  1. 1. @YourTwitterHandle#DV14 #YourTag @rombert#Devoxx #Sling Apache Sling as an OSGi-powered REST middleware Robert Munteanu, Adobe Systems Inc
  2. 2. #Devoxx #Sling @rombert Getting to know each other $DAYJOB  Adobe Experience Manager  Apache Sling  Apache Jackrabbit  Apache Felix FOSS  Apache Sling  MantisBT  Mylyn Connector for MantisBT  Mylyn Connector for Review Board
  3. 3. #Devoxx #Sling @rombert Agenda ● What is Apache Sling? ● Meet Barry ● Demo 1 ● Why use Sling as a RESTful middleware? ● Demo 2 ● What else is there to Sling?
  4. 4. #Devoxx #Sling @rombert Apache Sling - History 2007 Incubation 2009 TLP 2015 Version 8 200x Pre-Apache
  5. 5. #Devoxx #Sling @rombert High-level View of the Code Source: OpenHub
  6. 6. #Devoxx #Sling @rombert Level of activity Source: OpenHub Source: status.apache.org
  7. 7. #Devoxx #Sling @rombert Conceptual foundations REST-based Content-driven OSGi-powered Scripting InsideApache
  8. 8. #Devoxx #Sling @rombert REST-based /content/blog/ /content/blog/{0}.html / BlogViewController BlogListController HomeController SlingMainServlet / /content /content/blog /content/blog/hello-world
  9. 9. #Devoxx #Sling @rombert REST-based / /content /content/blog /content/blog/hello-world [sling/redirect] [sling/redirect] [ blog/welcome] [ blog/page]
  10. 10. #Devoxx #Sling @rombert Barry – Senior Buzzword Deliverer
  11. 11. #Devoxx #Sling @rombert Demo time!
  12. 12. #Devoxx #Sling @rombert How is everything mapped? / ( root ) /content/blog/comments /content/blog/posts /content/blog/images
  13. 13. #Devoxx #Sling @rombert How does Sling manage this? ResourceProvider JCR NoSQL FS Couchbase MongoDB ResourceProviderFactory Produces
  14. 14. #Devoxx #Sling @rombert SPI → API ResourceProviderFactory OSGi Service Registry ResourceResolver Registers Gets
  15. 15. #Devoxx #Sling @rombert How does my code get invoked? 1 GET /content/blog/posts/hello_world.json 2 ResourceResolver.resolve(...) 3 ServletResolver.resolveServlet(...) 4 servlet.doGet(...)
  16. 16. #Devoxx #Sling @rombert Reading Resources SELECT * FROM posts WHERE id = 83 rr.getResource(“/posts/hello_world”);
  17. 17. #Devoxx #Sling @rombert Reading Resources SELECT * FROM posts WHERE parent_id = 25 LIMIT 10 for (Resource post : resource.getChildren() ) { // process first 10 }
  18. 18. #Devoxx #Sling @rombert Reading Resources SELECT * FROM posts WHERE author = “john” ORDER BY published DESC rr.findResources(“//element(*, blog:Posts)[@author='john'] ORDER BY @published DESC”, “xpath”);
  19. 19. #Devoxx #Sling @rombert Writing Resources rr.create(parent, “new-blog-post”, Map.of(“author”, “John”, “title”, “New Blog Post”)); rr.delete(resource); rr.commit();
  20. 20. #Devoxx #Sling @rombert How do I handle requests? @SlingServlet(methods="POST", extensions="comments", resourceTypes=SlingshotConstants.RESOURCETYPE_ITEM) public class CommentPostServlet extends SlingAllMethodsServlet { protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) { /* implementation */ } }
  21. 21. #Devoxx #Sling @rombert Reading data Resource res = request.getResource(); // 1. reading ... ValueMap properties = ↵ res.getValueMap(); String title = properties.get(“jcr:title”,↵ “Missing”); Post post = res.adaptTo(Post.class); title = post.getTitle();
  22. 22. #Devoxx #Sling @rombert Why do we need adaptTo? Adaptable Resource ValueMap Map<String,Object> Post
  23. 23. #Devoxx #Sling @rombert Writing data Resource res = request.getResource(); // 2. writing ... ValueMap properties = ↵ res.adaptTo(ValueMap.class); String title = properties.put(“jcr:title”,↵ “Hello, world”); Post post = res.adaptTo(Post.class); post.setTitle(“Hello, world”); res.getResourceResolver().commit();
  24. 24. #Devoxx #Sling @rombert A Sightly Template <h1>${resource.valueMap['jcr:title'] || 'Blog'}</h1> <div data-sly-resource="${'/content/blog/stats' @resourceType='redis/stats'}"/> <div data-sly-list.child="${resource.listChildren}"> <h2>${child.valueMap['jcr:title']}</h2> <div data-sly-use.helper="${'welcome.js' @text=child.valueMap['jcr:description'] }"> ${helper.excerpt @ context='html'} <a href="${child.path}.html">Read more</a> </div> </div>
  25. 25. #Devoxx #Sling @rombert Demo time!
  26. 26. #Devoxx #Sling @rombert Oh, but there's more
  27. 27. #Devoxx #Sling @rombert More features ● Eventing, Thread Pooling, Job Management, Caching ● Scripting: Groovy, Scala, JSP, Sightly, Java, Ruby, Thymeleaf ● Flexible resource rendering with resource types ● Very extensible due to being internally powered by OSGi – most extension points available to clients
  28. 28. #Devoxx #Sling @rombert Beyond the NoSQL datastores
  29. 29. #Devoxx #Sling @rombert Buzzword checklist ✔ REST ✔ Middleware ✔ NoSQL ✔ OSGi
  30. 30. #Devoxx #Sling @rombert Resources ● Apache Sling – http://sling.apache.org ● Sling NoSQL providers - http://sling.apache.org/documentation/bundles/nosql-res ource-providers.html ● Apache Jackrabbit Oak - http://jackrabbit.apache.org/oak/

×