RESTful web apps with Apache Sling - 2013 version

  • 1,683 views
Uploaded on

Quick overview of Apache Sling and its Slingbucks sample

Quick overview of Apache Sling and its Slingbucks sample

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,683
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
2
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Bertrand DelacrétazSenior Developer, CQ5 R&D team, Adobe BaselApache Software Foundation Member and (current) Director@bdelacretaz - http://grep.codeconsult.chLausanne, May 2013slides revision: 2013-05-301RESTfulwebappswithApacheSling
  • 2. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Apache Sling: OSGi-based applications layer for JCR2contenteverything is
  • 3. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Everything is content!3Any app that requires more thancurl as a minimal client shouldhave a good reason.JavacontentrepositoryJCR APIApache SlingHTTPLet’sbuildanintelligentWebserver.
  • 4. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Content?4designeverythingwebsitecode
  • 5. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Website Content5/content/geometrixx/en/company/jcr:content/par/titlesling:resourceTypedrives rendering
  • 6. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 6Apache Sling«any» scriptinglanguagescript == servletOSGi-basedRESTful default servletssling.apache.org
  • 7. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Sling Architecture Overview7OSGi framework(Apache Felix)WebDAVserverFelix OSGiconsoleJCRcontentrepositoryJCR APIECMAscriptJSPRubyVelocityJSR 223scriptingresourceresolutionservlet/scriptstandardservletscustomservletsSling Core
  • 8. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Sling is based on OSGiExtreme modularityFosters better structured codeDynamic plugins - for everything!In-JVM Service-based architecture8OSGi bundlePrivate packagesPublic packagesMetadataMatchless picture: Alvimann on morguefile.com
  • 9. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Clean URLs with Sling: «Reclaiming the Web»Sling/cars/audi/s4.details.htmlContentRepositoryPathSelectorsandextension9sling:resourceType
  • 10. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Sling Sample ApplicationSlingbucksa RESTful coffee shop10
  • 11. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks Application FlowCustomers,public contentStaff,private content11
  • 12. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks use cases: Order CoffeeDisplay order form with configurable options.Customer submits order.Redisplay order for confirmation with hard to guess ID.1Content:12POST
  • 13. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks use cases: Confirm OrderRedisplay order form.Customer either modifies andrecalculates price, or confirms order.12Content:13POSTGET
  • 14. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks Process Order use case and contentConfirmed order moves under /private, for Slingbucks staffApp displays a list of confirmed orders to staff.Staff delivers order and deletes it from list.3 Orders really move!14
  • 15. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks Resources DesignSecurity/ACLsMeaningfulURLs1:1JCRpathmappingObject-oriented design?No: everything is content: we want RESTful resources.15New order form:http://slingbucks.com/public/orders.htmlOrder editing and confirmation (example):http://slingbucks.com/public/orders/544da609.htmlPrice of an order:Same but ending with .price.html selectorList of confirmed orders:http://slingbucks.com/private/confirmed.html
  • 16. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Sling gets the Web!16allyouneediscurlor any other HTTP client
  • 17. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.The SlingPostServlet$ curl -D --F "customerName=Bob The Geek"-F sling:resourceType=slingbucks/order-F lastModified=""-F opt_coffeetype=capuccino-F opt_size=medium-F opt_sugar=raw-F opt_cup=rosewoodhttp://u:pwd@slingserver/content/slingbucks/public/orders/HTTP/1.1 201 CreatedLocation: /slingbucks/public/orders/117936075d4de452cbba5b46817SlingPostServletJCRcontentrepositoryJCR APIHTTP POST
  • 18. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Default JSON GET servlet$ curl http://localhost:8080/content/slingbucks/public/orders/fad01d62ca54209a1405e3b.tidy.json{"opt_size": "small","customerName": "Bob the Geek","opt_coffeetype": "espresso","opt_sugar": "none","sling:resourceType": "slingbucks/order","opt_cup": "plastic","lastModified": "Nov 01 2010 18:31:01","jcr:primaryType": "nt:unstructured"}18DefaultGetServletJCRcontentrepositoryJCR APIHTTP GETJSON
  • 19. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks Sample AppcodeJust two OSGi servicesand a few presentation scripts19Writinglotsofcode?Maybeyoushouldn’t
  • 20. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Code: OSGi plugin for hard to guess node names20/** OSGi SCR instantiates this and registers as Service */@Component@Servicepublic class HexNodeNameGeneratorimplements org.apache.sling..NodeNameGenerator {@Override/** Sling calls this when creating a node */public String getNodeName(SlingHttpServletRequest request,String parentPath, ...){...if path is in slingbucks tree:return computeHardToGuessName();...else return nullmaven-scr-pluginprocesses
  • 21. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Code: watch repository to move confirmed orders/** OSGi SCR instantiates this */@Componentpublic class ConfirmedOrdersObserverimplements EventListener, Runnable {/** OSGi SCR provides this */@Referenceprivate SlingRepository repository;/** OSGi SCR calls this at startup */protected void activate(ComponentContext ctx){// Register with repository// to callback our onEvent method// on changes under /content/slingbucks/orders...public void onEvent(EventIterator it) {while (it.hasNext()) {if(path.endsWith(«orderConfirmed») {... if orderConfirmed property is true:... move node under private/confirmed... usingsession.getWorkspace().move(....21Orders really move!TwoJavamethodsinaComponent
  • 22. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Coffee options? defined by content -"fields" : {"coffeetype" : {"jcr:title" : "Coffee type","espresso" : {"jcr:title" : "Espresso","jcr:description" : "The Italian job","priceOffset" : 2.20},"capuccino" : {"jcr:title" : "Capuccino","jcr:description" : "The one with cream on top","priceOffset" : 3.40}},"size" : {"jcr:title" : "Size","small" : {"jcr:title" : "Small","jcr:description" : "1dl","priceFactor" : 1},"large" : {"jcr:title" : "Large","jcr:description" : "5dl","priceFactor" : 2}}HTTP GET with .jsonextension.POST to set values.(out of the box)22Reconfigurein-flightwithcurl!
  • 23. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Slingbucks code sizeJava code: 250 lines3 java filesHTML representation scripts: 250 lines7 esp filesInitial repository content: 85 lines3 json filesStyle and client-side javascript: 43 lines1 css and 1 js fileScripts and designcontent:23Writinglotsofcode?Maybeyoushouldn’t
  • 24. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Did I mention this already?Sling design promotes RESTful applications.Powerful out of the box HTTP services.OSGi plugins for (most) everything.24JavacontentrepositoryJCR APIApache SlingWritinglotsofcode?Maybeyoushouldn’teverythingiscontent!
  • 25. © 2011 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.Where next?25Code at http://s.apache.org/slingbucksCode and community at sling.apache.orgOSGi info at www.osgi.orgSlides at www.slideshare.net/bdelacretaz