SlideShare a Scribd company logo
1 of 19
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Ask the Experts – Using Sling and Adobe Experience Manager
Scott Macdonald | AEM Community Manager
Lokesh BS | Top AEM Community Member
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Agenda
 What is Sling and cheatsheet - http://docs.adobe.com/docs/en/cq/5-6-
1/developing/sling_cheatsheet.html (L)
 Sling Selectors within AEM
 Default Sling Post Servlet
 Sling APIs (S)
 Sling Models (L)
2
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling
 REST based web framework
 Content-driven, using a JCR content repository
 A resource is a central part of Sling and it assumes everything in the JCR is a resource
 Powered by OSGi
 Scripting inside, multiple languages (JSP, server-side javascript)
 Apache Open Source project
3
* With some exceptions
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Sling Architecture
 OSGi —Sling applications are built as OSGi bundles and makes heavy use of a number of OSGi
core and services.
 Sling API — The Sling API extends the Servlet API and provides more functionality to work on the
content.
 Request Processing — Sling takes a unique approach to handling requests in that a request URL
is first resolved to a resource, then based on the resource (and only the resource) it selects the
actual servlet or script to handle the request.
 Resources —Sling considers everything in the JCR as a Resource. Based on the resource, a
servlet or script is accessed to actually handle the request.
 Servlets and Scripts — Servlets and Scripts are handled uniformly in that they are represented
as resources themselves and are accessible by a resource path.
4
* With some exceptions
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
AEM and Sling
5
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Sling CheatSheet
6
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Using the SlingPostServlet
The SlingPostServlet is a front-end to JCR operations. To select a JCR operation to
execute, the :operation request parameter is used.
Out of the box, the SlingPostServlet supports the following operations:
 property not set or empty -- Create new content or modify existing content
 delete -- Remove existing content
 move -- Move existing content to a new location
 copy -- Copy existing content to a new location
 import -- Import content structures from JSON/XML/Zip
 nop -- Explicitly requests to do nothing and just sets the response status
 checkin - Check in a versionable node
 checkout - Check out a versionable node
7
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
SlingPostServlet Examples
 Create a node named mynode. Set (or overwrite) title and body properties.
<form action="/mynode" method="POST">
<input type="text" name="title">
<textarea name = "body"
</form>
 Create a node below /mynode and make it the first child
<form action="/mynode/" method="POST">
<input type="text" name="dummy">
<input type="hidden" name=":order">
<vaule = "first"
</form>
8
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
SlingPostServlet Examples (Continued)
 Delete a node.
<form action="/node/" method="POST">
<input name =":operation" type="hidden">
<vaule = "delete" >
</form>
 Create a node below /mynode and make it the first child
<form action="/old/node/" method="POST">
<input type="hidden“ name=":operation" value="move">
<input type="hidden" name=":dest" vaule = "/new/place">
</form>
9
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
SlingPostServlet Cheatsheet
10
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
SlingPostServlet DEMO
11
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
AEM Sling APIs
 The Sling API defines a presentation framework to build Web Applications.
 The Sling API is resource centric. That is, the request URL does not address a servlet or a portlet
but a resource represented by an instance of the
org.apache.sling.api.resource.Resource interface
 The Sling API uses the URL to select a resource to be delivered. You can retrieve content from
the AEM JCR using Sling APIs. You can use the Sling API from within an OSGi bundle to retrieve a
resource from within the AEM JCR.
 To use the Sling API from within an OSGi component, you inject an
org.apache.sling.api.resource.ResourceResolverFactory instance into the service
 When using the Sling API to query the AEM JCR, you have access to helper methods that are not
available when using the JCR API. For example, the adaptTo method converts a resource into an
appropriate object representing a certain aspect of this resource
 For example to translate a Resource object to the corresponding Node object, you invoke
the adaptTo method:
Node node = resource.adaptTo(Node.class);
12
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 13
Sling API “Live” Coding
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 14
Sling Models
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Design Goals
15
 Entirely annotation driven. "Pure" POJOs
 Use of standard annotations
 OOTB supports resource properties, SlingBindings, OSGi services
 Adapt multiple objects
 Support both classes and interfaces.
 Work with existing Sling infrastructure
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
First Model
16
 POJO Class
@Model(adaptables=Resource.class)
public class TestModel{
@Inject
private String propertyName;
}
 Interface
@Model(adaptables=Resource.class)
public interface TestModel{
@Inject
String getPropertyName();
}
 Required
In Manifest file:
<Sling-Model-Packages>
org.apache.sling.models.mymodels
</Sling-Model-Packages>
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Annotations
17
@Model
declares a model class or interface
@Inject
marks a field or method as injectable
@Named
declare a name for the injection (otherwise, defaults based on field or method name).
@Optional
marks a field or method injection as optional
@Source
explicitly tie an injected field or method to a particular injector (by name). Can also be on other annotations.
@Filter
an OSGi service filter
@PostConstruct
methods to call upon model option creation (only for model classes)
@Via
use a JavaBean property of the adaptable as the source of the injection
@Default
set default values for a field or method
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Client Code
18
Model Instantiation
 adaptTo()
TestModel model = resource.adaptTo(TestModel.class)
 ModelFactory
try {
TestModel model = modelFactory.createModel(resource,
TestModel.class);
} catch (Exception e) {
//exception
}
© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

More Related Content

What's hot

Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
DEEPAK KHETAWAT
 
Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6
DEEPAK KHETAWAT
 

What's hot (20)

AEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep DiveAEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep Dive
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
 
AEM - Client Libraries
AEM - Client LibrariesAEM - Client Libraries
AEM - Client Libraries
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
Osgi
OsgiOsgi
Osgi
 
slingmodels
slingmodelsslingmodels
slingmodels
 
Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component Development
 
Dynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEMDynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEM
 
SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager Sites
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser Caching
 
Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak Khetawat
 
AEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene IndexesAEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene Indexes
 
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQDynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core Components
 
Adobe AEM core components
Adobe AEM core componentsAdobe AEM core components
Adobe AEM core components
 
Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricks
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101
 
Angular 9
Angular 9 Angular 9
Angular 9
 

Similar to AEM and Sling

Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
mfrancis
 

Similar to AEM and Sling (20)

Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
Dockerizing apps for the Deployment Platform of the Month with OSGi - David B...
Dockerizing apps for the Deployment Platform of the Month with OSGi - David B...Dockerizing apps for the Deployment Platform of the Month with OSGi - David B...
Dockerizing apps for the Deployment Platform of the Month with OSGi - David B...
 
Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
Creating an all-purpose REST API for Cloud services using OSGi and Sling - C ...
 
Sst hackathon express
Sst hackathon expressSst hackathon express
Sst hackathon express
 
IMMERSE'16 Introduction to adobe experience manager back end
IMMERSE'16 Introduction to adobe experience manager back endIMMERSE'16 Introduction to adobe experience manager back end
IMMERSE'16 Introduction to adobe experience manager back end
 
Open Architecture in the Adobe Marketing Cloud - Summit 2014
Open Architecture in the Adobe Marketing Cloud - Summit 2014Open Architecture in the Adobe Marketing Cloud - Summit 2014
Open Architecture in the Adobe Marketing Cloud - Summit 2014
 
Marcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL WorkbenchMarcin Szałowicz - MySQL Workbench
Marcin Szałowicz - MySQL Workbench
 
RESTful web apps with Apache Sling - 2013 version
RESTful web apps with Apache Sling - 2013 versionRESTful web apps with Apache Sling - 2013 version
RESTful web apps with Apache Sling - 2013 version
 
Ite express labs
Ite express labsIte express labs
Ite express labs
 
Apic dc api deep dive
Apic dc api deep dive Apic dc api deep dive
Apic dc api deep dive
 
Oracle mcs overview 1029
Oracle mcs overview 1029Oracle mcs overview 1029
Oracle mcs overview 1029
 
Modern Web Applications with Sightly
Modern Web Applications with SightlyModern Web Applications with Sightly
Modern Web Applications with Sightly
 
EVOLVE'15 | Enhance | Bob O'Conner & Kevin Nenning | Capturing Existing Cont...
EVOLVE'15 | Enhance |  Bob O'Conner & Kevin Nenning | Capturing Existing Cont...EVOLVE'15 | Enhance |  Bob O'Conner & Kevin Nenning | Capturing Existing Cont...
EVOLVE'15 | Enhance | Bob O'Conner & Kevin Nenning | Capturing Existing Cont...
 
AEM Evernote Sync
AEM Evernote SyncAEM Evernote Sync
AEM Evernote Sync
 
Integrating ONE Automation with Business Systems with the API
Integrating ONE Automation with Business Systems with the APIIntegrating ONE Automation with Business Systems with the API
Integrating ONE Automation with Business Systems with the API
 
Notes
NotesNotes
Notes
 
Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014Integrating with Adobe Marketing Cloud - Summit 2014
Integrating with Adobe Marketing Cloud - Summit 2014
 
Workshop: AWS DevOps Essentials: An Introductory Workshop on CI/CD Best Pract...
Workshop: AWS DevOps Essentials: An Introductory Workshop on CI/CD Best Pract...Workshop: AWS DevOps Essentials: An Introductory Workshop on CI/CD Best Pract...
Workshop: AWS DevOps Essentials: An Introductory Workshop on CI/CD Best Pract...
 
CDI Integration in OSGi - Emily Jiang
CDI Integration in OSGi - Emily JiangCDI Integration in OSGi - Emily Jiang
CDI Integration in OSGi - Emily Jiang
 
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

AEM and Sling

  • 1. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.© 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Ask the Experts – Using Sling and Adobe Experience Manager Scott Macdonald | AEM Community Manager Lokesh BS | Top AEM Community Member
  • 2. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Agenda  What is Sling and cheatsheet - http://docs.adobe.com/docs/en/cq/5-6- 1/developing/sling_cheatsheet.html (L)  Sling Selectors within AEM  Default Sling Post Servlet  Sling APIs (S)  Sling Models (L) 2
  • 3. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling  REST based web framework  Content-driven, using a JCR content repository  A resource is a central part of Sling and it assumes everything in the JCR is a resource  Powered by OSGi  Scripting inside, multiple languages (JSP, server-side javascript)  Apache Open Source project 3 * With some exceptions
  • 4. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Sling Architecture  OSGi —Sling applications are built as OSGi bundles and makes heavy use of a number of OSGi core and services.  Sling API — The Sling API extends the Servlet API and provides more functionality to work on the content.  Request Processing — Sling takes a unique approach to handling requests in that a request URL is first resolved to a resource, then based on the resource (and only the resource) it selects the actual servlet or script to handle the request.  Resources —Sling considers everything in the JCR as a Resource. Based on the resource, a servlet or script is accessed to actually handle the request.  Servlets and Scripts — Servlets and Scripts are handled uniformly in that they are represented as resources themselves and are accessible by a resource path. 4 * With some exceptions
  • 5. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. AEM and Sling 5
  • 6. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Sling CheatSheet 6
  • 7. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Using the SlingPostServlet The SlingPostServlet is a front-end to JCR operations. To select a JCR operation to execute, the :operation request parameter is used. Out of the box, the SlingPostServlet supports the following operations:  property not set or empty -- Create new content or modify existing content  delete -- Remove existing content  move -- Move existing content to a new location  copy -- Copy existing content to a new location  import -- Import content structures from JSON/XML/Zip  nop -- Explicitly requests to do nothing and just sets the response status  checkin - Check in a versionable node  checkout - Check out a versionable node 7
  • 8. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. SlingPostServlet Examples  Create a node named mynode. Set (or overwrite) title and body properties. <form action="/mynode" method="POST"> <input type="text" name="title"> <textarea name = "body" </form>  Create a node below /mynode and make it the first child <form action="/mynode/" method="POST"> <input type="text" name="dummy"> <input type="hidden" name=":order"> <vaule = "first" </form> 8
  • 9. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. SlingPostServlet Examples (Continued)  Delete a node. <form action="/node/" method="POST"> <input name =":operation" type="hidden"> <vaule = "delete" > </form>  Create a node below /mynode and make it the first child <form action="/old/node/" method="POST"> <input type="hidden“ name=":operation" value="move"> <input type="hidden" name=":dest" vaule = "/new/place"> </form> 9
  • 10. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. SlingPostServlet Cheatsheet 10
  • 11. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. SlingPostServlet DEMO 11
  • 12. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. AEM Sling APIs  The Sling API defines a presentation framework to build Web Applications.  The Sling API is resource centric. That is, the request URL does not address a servlet or a portlet but a resource represented by an instance of the org.apache.sling.api.resource.Resource interface  The Sling API uses the URL to select a resource to be delivered. You can retrieve content from the AEM JCR using Sling APIs. You can use the Sling API from within an OSGi bundle to retrieve a resource from within the AEM JCR.  To use the Sling API from within an OSGi component, you inject an org.apache.sling.api.resource.ResourceResolverFactory instance into the service  When using the Sling API to query the AEM JCR, you have access to helper methods that are not available when using the JCR API. For example, the adaptTo method converts a resource into an appropriate object representing a certain aspect of this resource  For example to translate a Resource object to the corresponding Node object, you invoke the adaptTo method: Node node = resource.adaptTo(Node.class); 12
  • 13. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 13 Sling API “Live” Coding
  • 14. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 14 Sling Models
  • 15. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Design Goals 15  Entirely annotation driven. "Pure" POJOs  Use of standard annotations  OOTB supports resource properties, SlingBindings, OSGi services  Adapt multiple objects  Support both classes and interfaces.  Work with existing Sling infrastructure
  • 16. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. First Model 16  POJO Class @Model(adaptables=Resource.class) public class TestModel{ @Inject private String propertyName; }  Interface @Model(adaptables=Resource.class) public interface TestModel{ @Inject String getPropertyName(); }  Required In Manifest file: <Sling-Model-Packages> org.apache.sling.models.mymodels </Sling-Model-Packages>
  • 17. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Annotations 17 @Model declares a model class or interface @Inject marks a field or method as injectable @Named declare a name for the injection (otherwise, defaults based on field or method name). @Optional marks a field or method injection as optional @Source explicitly tie an injected field or method to a particular injector (by name). Can also be on other annotations. @Filter an OSGi service filter @PostConstruct methods to call upon model option creation (only for model classes) @Via use a JavaBean property of the adaptable as the source of the injection @Default set default values for a field or method
  • 18. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Client Code 18 Model Instantiation  adaptTo() TestModel model = resource.adaptTo(TestModel.class)  ModelFactory try { TestModel model = modelFactory.createModel(resource, TestModel.class); } catch (Exception e) { //exception }
  • 19. © 2015 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.