apache sling as an osgi-powered rest middleware

30
@YourTwitterHandle #DV14 #YourTag @rombert #Devoxx #Sling Apache Sling as an OSGi-powered REST middleware Robert Munteanu, Adobe Systems Inc

Upload: robert-munteanu

Post on 14-Apr-2017

1.112 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Apache Sling as an OSGi-powered REST middleware

@YourTwitterHandle#DV14 #YourTag @rombert#Devoxx #Sling

Apache Sling as an OSGi-powered REST middleware

Robert Munteanu, Adobe Systems Inc

Page 2: Apache Sling as an OSGi-powered REST middleware

#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

Page 3: Apache Sling as an OSGi-powered REST middleware

#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?

Page 4: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Apache Sling - History

2007Incubation

2009TLP

2015Version 8

200xPre-Apache

Page 5: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

High-level View of the Code

Source: OpenHub

Page 6: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Level of activity

Source: OpenHub

Source: status.apache.org

Page 7: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Conceptual foundations

REST-based

Content-driven

OSGi-powered

Scripting InsideApache

Page 8: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

REST-based

/content/blog/

/content/blog/{0}.html

/

BlogViewController

BlogListController

HomeController

SlingMainServlet//content/content/blog/content/blog/hello-world

Page 9: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

REST-based

//content/content/blog/content/blog/hello-world

[sling/redirect][sling/redirect][ blog/welcome][ blog/page]

Page 10: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Barry – Senior Buzzword Deliverer

Page 11: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Demo time!

Page 12: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

How is everything mapped?

/ ( root )

/content/blog/comments

/content/blog/posts/content/blog/images

Page 13: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

How does Sling manage this?

ResourceProvider

JCR NoSQL FS

Couchbase MongoDB

ResourceProviderFactoryProduces

Page 14: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

SPI → API

ResourceProviderFactory

OSGi Service Registry

ResourceResolver

Registers Gets

Page 15: Apache Sling as an OSGi-powered REST middleware

#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(...)

Page 16: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Reading Resources

SELECT * FROM posts WHERE id = 83

rr.getResource(“/posts/hello_world”);

Page 17: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Reading Resources

SELECT * FROM posts

WHERE parent_id = 25

LIMIT 10

for (Resource post : resource.getChildren() ) {

// process first 10

}

Page 18: Apache Sling as an OSGi-powered REST middleware

#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”);

Page 19: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Writing Resources

rr.create(parent, “new-blog-post”, Map.of(“author”, “John”, “title”, “New Blog Post”));

rr.delete(resource);

rr.commit();

Page 20: Apache Sling as an OSGi-powered REST middleware

#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 */

}

}

Page 21: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Reading dataResource 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();

Page 22: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Why do we need adaptTo?

Adaptable

Resource ValueMap

Map<String,Object> Post

Page 23: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Writing dataResource 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();

Page 24: Apache Sling as an OSGi-powered REST middleware

#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>

Page 25: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Demo time!

Page 26: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Oh, but there's more

Page 27: Apache Sling as an OSGi-powered REST middleware

#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

Page 28: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Beyond the NoSQL datastores

Page 29: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Buzzword checklist✔ REST✔ Middleware✔ NoSQL✔ OSGi

Page 30: Apache Sling as an OSGi-powered REST middleware

#Devoxx #Sling @rombert

Resources● Apache Sling – http://sling.apache.org● Sling NoSQL providers -

http://sling.apache.org/documentation/bundles/nosql-resource-providers.html

● Apache Jackrabbit Oak - http://jackrabbit.apache.org/oak/