Download - RESTFul development with Apache sling
![Page 1: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/1.jpg)
RESTful developmentwith Apache Sling
Sergii Fesenko
![Page 2: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/2.jpg)
What is Apache Sling?● Content centric web application framework
● Built around RESTful principles
● Powered by OSGI and JCR
● Apache Open Source project
● Supports Scripting (JSR-233)
● Implemented in Java
![Page 3: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/3.jpg)
Sling history● Sling started as an internal project at Day Software, and entered the Apache
Incubator in September 2007.
● Apache a top level project since 2009
● Name refers the weapon David uses to slay the giant Goliath
(simplest device for delivering content very fast)
![Page 4: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/4.jpg)
Who is using Sling
Teaching and learning open source environment
CMS CMS
![Page 5: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/5.jpg)
Sites using AEM
aws.amazon.com chase.com telegraph.co.uk
cisco.comnike.comsalesforce.com intel.com
![Page 6: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/6.jpg)
Sling/REST concepts● Anything can be a resource
● Resources are independent from their representation
● A resource is identified by URL
● URLs have an implicit hierarchy
● Methods perform Operations on resources
● HTTP status codes are used to indicate result success
● Safe methods shouldn‘t change anything
● Idempotent methods shouldn‘t change anything beyond their first execution
![Page 7: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/7.jpg)
The difference (JEE/Spring MVC vs Sling)
@RestController
@Service
@Repository
DB Schema
DB data scripts
JEE stack
![Page 8: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/8.jpg)
The difference (JEE/Spring MVC vs Sling)
@RestController
@Service
@Repository
DB Schema
DB data scripts
Resources
JEE stack Apache Sling
@SlingServlet
Scripts
![Page 9: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/9.jpg)
The difference (JEE/Spring MVC vs Sling) JEE stack:
Use different URLs & controllers for different representation of the same resource
@RestController1
@RestController2
@RestController3
![Page 10: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/10.jpg)
Sling Architecture
Sling engine
JCR repository(apache jackrabbit/oak)
OSGi framework (Apache Felix)
HTTP ● Jetty process http requests
● Sling does URL decomposition, resource
and script resolution
● OSGi manage bundles and provide system
services to bundles (sling and jackrabbit
are bundles inside OSGi container)
● JCR stores content
Sling launchpad
![Page 11: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/11.jpg)
What is JCR● API to access content repositories in a uniform manner
● Everything is content
● Kind of NoSQL (tree of nodes and properties)
● Covered by JSR-170 (Version 1), and by JSR-283 (version 2)
● Major implementation is Apache Jackrabbit
![Page 12: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/12.jpg)
What is JCR: how it looks{ "jcr:primaryType": "sling:Folder", "jcr:createdBy": "admin", "jcr:created": "Sun Oct 09 2016 21:37:57 GMT+0300", "private": { "jcr:primaryType": "nt:unstructured", "confirmed": { "jcr:primaryType": "nt:unstructured", "jcr:title": "Confirmed orders", "sling:resourceType": "slingbucks/confirmed", "703bed1112ac94c2b446787a6c5096bb": { "jcr:primaryType": "nt:unstructured", "opt_cup": "plastic", "opt_coffeetype": "espresso", "orderConfirmed": "Confirm this order", "opt_size": "small", "customerName": "Anonymous Coffee Drinker", "sling:resourceType": "slingbucks/order", "opt_sugar": "white", "lastModified": "Sun Oct 09 2016 21:40:38 GMT+0300" }, } },....
![Page 13: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/13.jpg)
Sling request handling
URL decomposition Script resolution Script execution
Break down url into:● Resource path● Selectors● Extension● Suffix
● Use sling resourceType to locate script’s path
● Use selectors and extension to locate script name
Scripts that render resource as text (html/json/xml/etc) or create/modify resource
![Page 14: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/14.jpg)
Sling URL decomposition
/content/slingbucks/public/orders.tidy.json/last
Resource path● The substring of the request URL before
the first dot (.)● Mandatory
Selector● Substring between 1st dot, and the dot leading the extension● Used for alt. methods of rendering the content● Multiple selectors may be used (separated by . )● Optional
Extension● The string between the last dot after the resource path and
the next slash● Specifies the content format● Optional
Suffix● Path starting with the slash up to the end of
the request URL● At least a dot must be in the URL to let Sling
detect the suffix path● Can be used to provide additional information
for the processing script
![Page 15: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/15.jpg)
Sling script resolution: servlets@SlingServlet( resourceTypes = "slingbucks/order", extensions = {"txt"}, methods = "GET")public class SlingSampleServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { Resource r = request.getResource(); String value =r.adaptTo(ValueMap.class).get("customerName", String.class); response.getWriter().append(value); }}
![Page 16: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/16.jpg)
Sling: script name resolution/content/slingbucks/order
/content slingbacks order
slingResourcetype = slingbucks/order
/app slingbucks order order.jsp
txt.jsp ….
/libs ...
![Page 17: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/17.jpg)
Sling script name resolution
![Page 18: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/18.jpg)
Sling scripts● Sling supports JSR 223 (Scripting for the Java Platform), so any programming
languages may be supported
● Sling has build-in support for ECMAScript, JSP and Java (via servlets/OSGi
service)
![Page 19: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/19.jpg)
Sling scripts<html><head><title><%= currentNode["jcr:title"] %></title><% load("../common/head.esp"); %>
</head><body><h1><%= currentNode["jcr:title"] %></h1><%var childNodes = currentNode.getChildren(); for(i in childNodes) { sling.include(childNodes[i].path, "replaceSelectors=backoffice");} %></body></html>
![Page 20: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/20.jpg)
Sling concepts: adaptTo
Response = f (Request)
![Page 21: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/21.jpg)
Sling concepts: adaptTopublic interface Adaptable { /** * Adapts the adaptable to another type. * */ <AdapterType> AdapterType adaptTo(Class<AdapterType> type);}
@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { MyResponse myResponse = request.adaptTo(MyResponse.class); response.getWriter().append(myResponse.toJson());}
![Page 22: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/22.jpg)
Sling concepts: adaptTo@Component@Service(value=org.apache.sling.api.adapter.AdapterFactory.class)@Properties({ @Property(name = "adaptables", value = { "org.apache.sling.api.SlingHttpServletRequest" }), @Property(name = "adapters", value = { "sling.sample.MyResponse" })})public class MyResponseProvider implements AdapterFactory {
@Override public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) { return (AdapterType) (MyResponse) …..; }
}
![Page 23: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/23.jpg)
Summary Good
● Provide ability to “touch” resources
● Encourage to design resources
● Everything out of the box (more like platform
rather than framework)
Not good● Too little available information
● No async support
● Development is relatively slow
![Page 24: RESTFul development with Apache sling](https://reader034.vdocuments.site/reader034/viewer/2022042515/58719b1b1a28ab044e8b5a9f/html5/thumbnails/24.jpg)
The End
?