Content-Centric Web Development with
Apache Sling
1
07/09/2017
Koen Van Eeghem
Who am I?
2
Copyright © Capgemini 2017. All Rights Reserved
Koen Van Eeghem
DCX Consultant @ Capgemini - Atlas Copco Project since 09/2016
cown_ cown_ koenvaneeghem koen.van-eeghem@capgemini.com
3
Copyright © Capgemini 2017. All Rights Reserved
Planning
• What is Apache Sling?
• What makes Sling unique?
• Demo: Blog
4
What is Apache Sling?
5
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• REST based web framework
6
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• Content-driven, using a JCR1
content repository
• Implementation of the JSR 170 / 283 standard
• “best of both worlds”
• Hierarchically structured storage (node structure)
• Queryable
• != Content Management System
7
1 More on JCR: http://jackrabbit.apache.org/jcr/jcr-api.html
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• Content-driven, using a JCR1
content repository
• Implementation of the JSR 170 / 283 standard
• “best of both worlds”
• Hierarchically structured storage (node structure)
• Queryable
• != Content Management System
7
1 More on JCR: http://jackrabbit.apache.org/jcr/jcr-api.html
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• Powered by OSGi2
8
2 More on OSGi: https://www.osgi.org/developer/architecture
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• Scripting inside, multiple languages (JSP, ESP, Scala, etc.)
9
Copyright © Capgemini 2017. All Rights Reserved
What is Apache Sling?
• Apache Open Source project (since 2007)
• Originally developed by “Day Software”
10
What makes Sling unique?
11
Copyright © Capgemini 2017. All Rights Reserved
What makes Sling unique?
• “normal” frameworks resolve a servlet from the URL then try to load data
• localhost:8080/hello?name=Koen
12
Copyright © Capgemini 2017. All Rights Reserved
What makes Sling unique?
• Sling resolves a resource from the URL and renders it based on parameters
13
Copyright © Capgemini 2017. All Rights Reserved
What makes Sling unique?
• Sling resolves a resource from the URL and renders it based on parameters
13
Copyright © Capgemini 2017. All Rights Reserved
What makes Sling unique?
• Sling resolves a resource from the URL and renders it based on parameters
13
Copyright © Capgemini 2017. All Rights Reserved
What makes Sling unique?
• Sling models map a JCR node to a POJO
• Servlets can be defined on a certain resource or on a certain path
• Useful info can be provided by selectors
• Templates can be defined in multiple languages: HTL, JSP, ESP, …
• Html Templating Language (HTL1): easy binding between Java and front-end
14
1 More on HTL: https://sling.apache.org/documentation/bundles/scripting/scripting-htl.html
Demo: Blog
15
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog
• Basic blog with blogposts
• Sling Model for rendering blog posts
• Sling Servlet for returning a JSON list of blog posts without content
• All code can be found on GitHub
16
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 1; setting up your environment
• Download the Sling launchpad
• Start up the launchpad:
java -jar org.apache.sling.launchpad-9.jar
• localhost:8080/system/console/bundles
• Default credentials: admin / admin
17
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 1; Setting up your environment
• Generate a maven project: sling-bundle-archetype
• Delete whatever Java classes that were automatically created
• Create a run configuration to deploy your bundle:
18
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 2; Developing your bundle
• Create a folder for your models and a folder for your services
• Update your POM
• Specify the models’ location: <Sling-Model-Packages>
• Add the dependencies to use Sling Models:
• org.apache.sling.bnd.models
• org.apache.sling.models.api
19
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 2; Developing your bundle
• BlogPostModel.java
• @Model(adaptables = Resource.class): shows we’re mapping a resource
• @Inject: injected at runtime from the JCR resource
• @Optional: is not required for a “successful” instantiation
• @Named: has another name than the Java variable
20
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 3; Developing your HTL Template
21
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 4; Create some sample content
• 3 ways of administering content on your server:
1. WebDav client: good overview, limited options
2. HTTP methods: no feedback, but you can view/add
3. Deploy files: good overview and you can view/add
22
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 5; Extending your bundle with a servlet
• GetAllPostsServlet.java
• @SlingServlet(resourceTypes = "capgemini/blog", methods =
"GET", extensions = “json")
• extends SlingSafeMethodsServlet
• uses BlogPostService.java for getting projects
• uses Gson to return JSON (excludeFieldsWithoutExposeAnnotation())
23
/content/blog.json
Copyright © Capgemini 2017. All Rights Reserved
Demo: Blog - Part 6; Some final challenges
• Some articles were originally published somewhere else
→ add this in the model and template
• A customer wants webpages with “inspirational quotes” in a specific color
→ create a model and template for this
• A customer wants to be able to get the 3 most recent articles, with or without
details
→ adjust the JSON servlet to enable this with selectors
24
Thanks for your attention!
Questions?
25Don’t want to ask your question for the group? Just e-mail it to koen.van-eeghem@capgemini.com and i’ll be happy to reply!

Content-Centric Web Development with Apache Sling

  • 1.
    Content-Centric Web Developmentwith Apache Sling 1 07/09/2017 Koen Van Eeghem
  • 2.
  • 3.
    Copyright © Capgemini2017. All Rights Reserved Koen Van Eeghem DCX Consultant @ Capgemini - Atlas Copco Project since 09/2016 cown_ cown_ koenvaneeghem koen.van-eeghem@capgemini.com 3
  • 4.
    Copyright © Capgemini2017. All Rights Reserved Planning • What is Apache Sling? • What makes Sling unique? • Demo: Blog 4
  • 5.
  • 6.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • REST based web framework 6
  • 7.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • Content-driven, using a JCR1 content repository • Implementation of the JSR 170 / 283 standard • “best of both worlds” • Hierarchically structured storage (node structure) • Queryable • != Content Management System 7 1 More on JCR: http://jackrabbit.apache.org/jcr/jcr-api.html
  • 8.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • Content-driven, using a JCR1 content repository • Implementation of the JSR 170 / 283 standard • “best of both worlds” • Hierarchically structured storage (node structure) • Queryable • != Content Management System 7 1 More on JCR: http://jackrabbit.apache.org/jcr/jcr-api.html
  • 9.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • Powered by OSGi2 8 2 More on OSGi: https://www.osgi.org/developer/architecture
  • 10.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • Scripting inside, multiple languages (JSP, ESP, Scala, etc.) 9
  • 11.
    Copyright © Capgemini2017. All Rights Reserved What is Apache Sling? • Apache Open Source project (since 2007) • Originally developed by “Day Software” 10
  • 12.
    What makes Slingunique? 11
  • 13.
    Copyright © Capgemini2017. All Rights Reserved What makes Sling unique? • “normal” frameworks resolve a servlet from the URL then try to load data • localhost:8080/hello?name=Koen 12
  • 14.
    Copyright © Capgemini2017. All Rights Reserved What makes Sling unique? • Sling resolves a resource from the URL and renders it based on parameters 13
  • 15.
    Copyright © Capgemini2017. All Rights Reserved What makes Sling unique? • Sling resolves a resource from the URL and renders it based on parameters 13
  • 16.
    Copyright © Capgemini2017. All Rights Reserved What makes Sling unique? • Sling resolves a resource from the URL and renders it based on parameters 13
  • 17.
    Copyright © Capgemini2017. All Rights Reserved What makes Sling unique? • Sling models map a JCR node to a POJO • Servlets can be defined on a certain resource or on a certain path • Useful info can be provided by selectors • Templates can be defined in multiple languages: HTL, JSP, ESP, … • Html Templating Language (HTL1): easy binding between Java and front-end 14 1 More on HTL: https://sling.apache.org/documentation/bundles/scripting/scripting-htl.html
  • 18.
  • 19.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog • Basic blog with blogposts • Sling Model for rendering blog posts • Sling Servlet for returning a JSON list of blog posts without content • All code can be found on GitHub 16
  • 20.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 1; setting up your environment • Download the Sling launchpad • Start up the launchpad: java -jar org.apache.sling.launchpad-9.jar • localhost:8080/system/console/bundles • Default credentials: admin / admin 17
  • 21.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 1; Setting up your environment • Generate a maven project: sling-bundle-archetype • Delete whatever Java classes that were automatically created • Create a run configuration to deploy your bundle: 18
  • 22.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 2; Developing your bundle • Create a folder for your models and a folder for your services • Update your POM • Specify the models’ location: <Sling-Model-Packages> • Add the dependencies to use Sling Models: • org.apache.sling.bnd.models • org.apache.sling.models.api 19
  • 23.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 2; Developing your bundle • BlogPostModel.java • @Model(adaptables = Resource.class): shows we’re mapping a resource • @Inject: injected at runtime from the JCR resource • @Optional: is not required for a “successful” instantiation • @Named: has another name than the Java variable 20
  • 24.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 3; Developing your HTL Template 21
  • 25.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 4; Create some sample content • 3 ways of administering content on your server: 1. WebDav client: good overview, limited options 2. HTTP methods: no feedback, but you can view/add 3. Deploy files: good overview and you can view/add 22
  • 26.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 5; Extending your bundle with a servlet • GetAllPostsServlet.java • @SlingServlet(resourceTypes = "capgemini/blog", methods = "GET", extensions = “json") • extends SlingSafeMethodsServlet • uses BlogPostService.java for getting projects • uses Gson to return JSON (excludeFieldsWithoutExposeAnnotation()) 23 /content/blog.json
  • 27.
    Copyright © Capgemini2017. All Rights Reserved Demo: Blog - Part 6; Some final challenges • Some articles were originally published somewhere else → add this in the model and template • A customer wants webpages with “inspirational quotes” in a specific color → create a model and template for this • A customer wants to be able to get the 3 most recent articles, with or without details → adjust the JSON servlet to enable this with selectors 24
  • 28.
    Thanks for yourattention! Questions? 25Don’t want to ask your question for the group? Just e-mail it to koen.van-eeghem@capgemini.com and i’ll be happy to reply!