restful development with apache sling

Post on 08-Jan-2017

142 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

RESTful developmentwith Apache Sling

Sergii Fesenko

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

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)

Who is using Sling

Teaching and learning open source environment

CMS CMS

Sites using AEM

aws.amazon.com chase.com telegraph.co.uk

cisco.comnike.comsalesforce.com intel.com

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

The difference (JEE/Spring MVC vs Sling)

@RestController

@Service

@Repository

DB Schema

DB data scripts

JEE stack

The difference (JEE/Spring MVC vs Sling)

@RestController

@Service

@Repository

DB Schema

DB data scripts

Resources

JEE stack Apache Sling

@SlingServlet

Scripts

The difference (JEE/Spring MVC vs Sling) JEE stack:

Use different URLs & controllers for different representation of the same resource

@RestController1

@RestController2

@RestController3

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

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

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" }, } },....

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

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

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); }}

Sling: script name resolution/content/slingbucks/order

/content slingbacks order

slingResourcetype = slingbucks/order

/app slingbucks order order.jsp

txt.jsp ….

/libs ...

Sling script name resolution

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)

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>

Sling concepts: adaptTo

Response = f (Request)

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());}

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) …..; }

}

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

The End

?

top related