Effective Web Application Development with Apache Sling

36
Effective Web Application Development with Apache Sling Robert Munteanu ( @rombert ) , Adobe Systems Romania

description

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 talk shows how Sling integrates various standard-based technologies, like OSGi and the Content Repository API for Java to create a coherent framework for web application development. We will walk through the development of a simple application with minimal effort and demonstrate how to productize the resulting application. We will pay special attention to some approaches which are not yet part of mainstream development, such as using OSGi for dependecy injection and JCR for persistence.

Transcript of Effective Web Application Development with Apache Sling

Page 1: Effective Web Application Development with Apache Sling

Effective Web Application Development with Apache Sling

Robert Munteanu ( @rombert ) , Adobe Systems Romania

Page 2: Effective Web Application Development with Apache Sling

About the Speaker

11/18/14 2

● Apache Sling PMC member

● Fanboy of the Sling/JCR/OSGi stack

● Enthusiastic Open-Source contributor

● Senior Computer Scientist at Adobe in Romania

Speaker.currentSpeaker().interrupt();

Page 3: Effective Web Application Development with Apache Sling

Presentation Flow

11/18/14 3

● Quick facts and figures

● Conceptual foundations

● Building blocks

● Building Sling applications

Page 4: Effective Web Application Development with Apache Sling

Quick facts and figures

Page 5: Effective Web Application Development with Apache Sling

Apache Sling - History

11/18/14 5

2007incubation

2009TLP

2014Version 7

200xPre-Apache

Page 6: Effective Web Application Development with Apache Sling

Apache Sling – High-level View of the Code

11/18/14 6

Source: OpenHub

Page 7: Effective Web Application Development with Apache Sling

Apache Sling - Activity Level

11/18/14 7

Source: OpenHub

Source: status.apache.org

Page 8: Effective Web Application Development with Apache Sling

Conceptual Foundations

Page 9: Effective Web Application Development with Apache Sling

Apache Sling – Conceptual Foundations

11/18/14 9

REST-basedContent-driven

OSGi-powered

Scripting insideApache

Page 10: Effective Web Application Development with Apache Sling

Apache Sling – Apache Open Source

11/18/14 10

4Aries

1ServiceMix

7Commons

17Felix

3Geronimo

6Jackrabbit

1Derby

2Tika

Page 11: Effective Web Application Development with Apache Sling

Apache Sling – REST-based

11/18/14 11

/blog/

/blog/{0}.html

/

BlogViewController

BlogListController

HomeController

//blog/blog/hello-world

SlingMainServlet

Page 12: Effective Web Application Development with Apache Sling

Apache Sling – Content-Driven

11/18/14 12

blog

hello-world

images

jcr:content

some-cat.jpg

other-cat.jpg

Page 13: Effective Web Application Development with Apache Sling

Apache Sling – Content-Driven

11/18/14 13

- jcr:primaryType = app:asset- jcr:title = Some Cat- jcr:description = A longer description of this picture of a cat- jcr:created = 2014-06-03T00:00:00.000+02:00- jcr:lastUpdated = 2014-06-03T11:00:00.000+02:00- tags = [Animal, Cat, Color]- width = 400- height = 600

some-cat.jpg

Page 14: Effective Web Application Development with Apache Sling

Apache Sling – Scripting Inside

11/18/14 14

JSP

Page 15: Effective Web Application Development with Apache Sling

Apache Sling – Scripting Inside

11/18/14 15

libs

blogapp

welcome.jsp

welcome

json.html

Page 16: Effective Web Application Development with Apache Sling

Apache Sling – OSGi-powered

● Provision and deploy bundles

● Configure, register and lookup services

● Eventing

● Web Console

11/18/14 16

Page 17: Effective Web Application Development with Apache Sling

Building blocks

Page 18: Effective Web Application Development with Apache Sling

Apache Sling – Serving a request

GET /blog/welcome.html

11/18/14 18

/blog/welcome myblog/blog.groovy???

Page 19: Effective Web Application Development with Apache Sling

Apache Sling – Resource Types

11/18/14 19

blog [blogapp/listing]

hello-world

images

jcr:content [blogapp/blog/content]

some-cat.jpg

other-cat.jpg

Page 20: Effective Web Application Development with Apache Sling

Apache Sling – Script Resolution

GET /blog.html

11/18/14 20

Type: blogapp/listing

Extension: html

Method: GET

/libs/blogapp/listing/html.jsp

@SlingServlet(resourceTypes=”blogapp/listing”,...)

/libs/blogapp/listing.jsp

Page 21: Effective Web Application Development with Apache Sling

Apache Sling – Request Selectors

GET /blog.rss.xml

11/18/14 21

Type: blogapp/listing

Extension: xml

Selector: rss

Method: GET

/libs/blogapp/listing/rss.html

Page 22: Effective Web Application Development with Apache Sling

Apache Sling – Resource Providers

11/18/14 22

JCR MongoDB

FS Cassandra

//content//content/comments/logs

Page 23: Effective Web Application Development with Apache Sling

Apache Sling – JCR

11/18/14 23

Page 24: Effective Web Application Development with Apache Sling

Apache Sling – JCR implementations

11/18/14 24

Apache Jackrabbit Oak

Page 25: Effective Web Application Development with Apache Sling

Building Sling Applications

Page 26: Effective Web Application Development with Apache Sling

Apache Sling – JCR modeling

11/18/14 26

images

File uploadsome-cat.jpg

renditions

small.jpg

ripple.jpg

Observation

annotations

initial-review

ACLs

Page 27: Effective Web Application Development with Apache Sling

Apache Sling - JCR

11/18/14 27

etc

rendition

ripple

- orientation = /etc/rendition/ripple/options ↵/orientation/vertical- antialiasing = true- edges = /etc/rendition/ripple/options/↵edges/wrap- wave type = /etc/rendition/ripple/options/ ↵wave_type/simple- period = 20- amplitude = 5

Page 28: Effective Web Application Development with Apache Sling

Apache Sling – Everything is a Resource

11/18/14 28

Everything is a Resource

Page 29: Effective Web Application Development with Apache Sling

Apache Sling – Reading from the Repository

11/18/14 29

@SlingServlet(resourceTypes = "blogapp/listing", extensions = "xml", methods = "GET")

public class RSSFeedServlet extends SlingSafeMethodsServlet {

@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

// actual implementation

}

}

Page 30: Effective Web Application Development with Apache Sling

Apache Sling – Reading from the Repository

11/18/14 30

Resource res = request.getResource();

ValueMap properties = ↵ res.adaptTo(ValueMap.class);

String title = properties.get(“jcr:title”,↵ “Missing”);

Post post = res.adaptTo(Post.class);

title = post.getTitle();

Page 31: Effective Web Application Development with Apache Sling

Apache Sling - Extensions

● Thread Pools and Scheduled Tasks

● I18n

● Caching

● Models

● Health Checks

● Eventing

11/18/14 31

Page 32: Effective Web Application Development with Apache Sling

Apache Sling - SlingQuery

Get the closest Folder parent

$(resource).parents("sling:Folder").last();

Get the second child of each resource

$(resource1, resource2).children(":eq(1)");

Find children named en or de

$(resource).children("#en, #de")

11/18/14 32

Page 33: Effective Web Application Development with Apache Sling

Apache Sling - Deployment

● Single executable Jar or War file – the Sling launchpad

● Configuration defined in multiple text files, defining bundles, configuration, variables, bootstrap commands, etc

11/18/14 33

Page 34: Effective Web Application Development with Apache Sling

Apache Sling – Provisioning Model

[feature name=main]

[variables] io.version=1.4

[configurations]org.apache.jackrabbit.....SegmentNodeStoreService

name="Default\ NodeStore" repository.home="sling/oak/repository"

[artifacts startLevel=5] commons-io/commons-io/${io.version}/jar commons-fileupload/commons-fileupload/1.3.1/jar

11/18/14 34

Page 35: Effective Web Application Development with Apache Sling

Apache Sling - Tooling

● Maven Plugins

● Bundle deployment

● Launchpad creation

● Maven Archetypes

● IDE Tooling

● Eclipse

● Netbeans (external)

11/18/14 35

Page 36: Effective Web Application Development with Apache Sling

Resources

● http://sling.apache.org/

● http://jackrabbit.apache.org/

11/18/14 36