Deepak khetawat sling_models_sightly_jsp

29
Sling Models Using Sightly and JSP DEEPAK KHETAWAT

Transcript of Deepak khetawat sling_models_sightly_jsp

Page 1: Deepak khetawat sling_models_sightly_jsp

Sling Models Using Sightly and JSP

DEEPAK KHETAWAT

Page 2: Deepak khetawat sling_models_sightly_jsp

About the Speaker

Page 3: Deepak khetawat sling_models_sightly_jsp

What & Why Sling Models

- What are Sling Models

- Design Goals

- Why Sling Models ?

Sling Model Annotations

- Annotations Usage

Sling Model Injectors

- What are Injectors

- Injectors available in 1.0.x and 1.1.x

- Injector-specific Annotations

Agenda

Usage of Sling Models

- Prerequisite

- How to use Sling Models with JSP ?

- How to use Sling Models with Sightly?

Demo

- Functionality Demo

Appendix and Q&A

- Appendix

- Q&A

Page 4: Deepak khetawat sling_models_sightly_jsp

What & Why Sling Models

Page 5: Deepak khetawat sling_models_sightly_jsp

• Sling Models are POJO’s which are automatically

mapped from Sling objects, typically resources, request

objects. Sometimes these POJOs need OSGi services

as well

What are Sling Models?

Page 6: Deepak khetawat sling_models_sightly_jsp

• Entirely annotation driven. "Pure" POJOs

• Adapt multiple objects

• Work with existing Sling infrastructure

• Client doesn't know/care that these objects are different than any

other adapter factory

• Support both classes and interfaces

• Pluggable

Design Goals

Page 7: Deepak khetawat sling_models_sightly_jsp

Development :

• Saves from creating own adapters

• Situation Based Dependency Injection Framework

• Do More with less code making code readable and Removing

redundant (boilerplate) code

Testing :

• Highly Testable Code

- Sling Model Inject Annotations defines binding to

dependencies

- Mock dependencies with tools like Mockito using

@InjectMocks

Why Sling Models ?

Page 8: Deepak khetawat sling_models_sightly_jsp

Sling Model Annotations

Page 9: Deepak khetawat sling_models_sightly_jsp

Annotations Usage

Sling Model Annotation Code Snippet Description

@Model @Model(adaptables = Resource.class) Class is annotated with @Model and the adaptable class

@Inject

@Inject private String propertyName; (class )

@Inject String getPropertyName(); (interface )

A property named "propertyName" will be looked up from the

Resource (after first adapting it to a ValueMap) and it is injected ,

else will return null if property not found .

@Default @Inject @Default(values = "AEM") private String technology; A default value (for Strings , Arrays , primitives etc. )

@Optional @Inject @Optional private String otherName.

@Injected fields/methods are assumed to be required. To mark

them as optional, use @Optional i.e resource or adaptable may or

may not have property .

Page 10: Deepak khetawat sling_models_sightly_jsp

Annotations Usage

Sling Model Annotation Code Snippet Description

@Named @Inject @Named("title") private String pageTitle;Inject a property whose name does NOT match the Model field

name

@Via

@Model(adaptables=SlingHttpServletRequest.class)

public interface SlingModelDemo {

@Inject @Via("resource") String getPropertyName(); }

Use a JavaBean property of the adaptable as the source of the

injection

// Code Snippet will return

request.getResource().adaptTo(ValueMap.class).get("propertyNam

e", String.class)

@Source

@Model(adaptables=SlingHttpServletRequest.class)

@Inject @Source("script-bindings") Resource

getResource(); }

Explicitly tie an injected field or method to a particular injector (by

name). Can also be on other annotations

//Code snippet will ensure that "resource" is retrieved from the

bindings, not a request attribute

@PostConstruct @PostConstruct

protected void sayHello() { logger.info("hello"); }Methods to call upon model option creation (only for model classes)

Page 11: Deepak khetawat sling_models_sightly_jsp

Sling Model Injectors

Page 12: Deepak khetawat sling_models_sightly_jsp

• Injectors are OSGi services implementing the

org.apache.sling.models.spi.Injector interface

• Injectors are invoked in order of their service ranking, from lowest to

highest.

What are Injectors ?

Page 13: Deepak khetawat sling_models_sightly_jsp

• Script Bindings (script-bindings)

• Value Map (valuemap)

• Child Resources (child-resources)

• Request Attributes (request-attributes)

• OSGI Service References (osgi-services)

Standard Injectors in 1.0.x

Injectors (with @Source names)

Page 14: Deepak khetawat sling_models_sightly_jsp

Injects adaptable itself or an object that can be adapted from

adaptable

@Self privateResource resource;

@Model(adaptables = SlingHttpServletRequest.class)

public class SlingModelDemo {

@SlingObject private Resource resource;

@SlingObject private SlingHttpServletRequest request;

}

Injects resource from given path , applicable to Resource or

SlingHttpServletRequest objects

@ResourcePath(path="/resource-path") private Resource resource;

• Self Injector (self)

• Sling Object Injector (sling-object)

• Resource Path Injector (resource-path)

Standard Injectors in 1.1.x

Injectors (with @Source names)

Page 15: Deepak khetawat sling_models_sightly_jsp

• Supported since Sling Models Impl 1.0.6

• Can use customized annotations with following advantages over

using the standard annotations:

- Less code to write (only one annotation is necessary in most of

the cases)

- More robust

• Injector Specific Annotation specifies source explicitly

• Example

@ValueMapValue String propertyName;

@ValueMapValue (name="jcr:title", optional=true)

String title;

Injector-specific Annotations

AnnotationSupported Optional

ElementsInjector

@ScriptVariable optional and name script-bindings

@ValueMapValue optional, name and via valuemap

@ChildResource optional, name and via child-resources

@RequestAttribute optional, name and via request-attributes

@ResourcePathoptional, path,

and name

resource-path

@OSGiService optional, filter osgi-services

@Self optional self

@SlingObject optional sling-object

Page 16: Deepak khetawat sling_models_sightly_jsp

Injectors depicted in Felix Console

Page 17: Deepak khetawat sling_models_sightly_jsp

Using Sling Models with Sightly and JSP

Page 18: Deepak khetawat sling_models_sightly_jsp

• Need org.apache.sling.models.api package

- OOTB supported in AEM 6 and above version , with

org.apache.sling.models.api package already present in AEM

instance

- For other versions download package from

http://sling.apache.org/downloads.cgi and install in AEM instance

• Maven dependency can be found at

http://<host>:<port>/system/console/depfinder , search for

org.apache.sling.models.annotations.Model

<dependency> <groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.models.api</artifactId>

<version>1.0.0</version> <scope>provided</scope>

</dependency>

Prerequisite

Page 19: Deepak khetawat sling_models_sightly_jsp

• Search for maven-bundle-plugin in pom.xml file , update it with

following

- <Sling-Model-Packages> com.slingmodels.core </Sling-Model-

Packages>

- It is because for Sling Model classes to be picked up this header

must be added to the bundle's manifest

Prerequisite

Page 20: Deepak khetawat sling_models_sightly_jsp

• Supported as of Sling Models 1.1.0

• Name of a constructor argument parameter cannot be detected

via the Java Reflection API

• @Named annotation is mandatory for injectors that require a

name for resolving the injection

• @Model(adaptables=Resource.class)

public class MyModel {

@Inject public MyModel(@Named("propertyName") String

propertyName)

{ // constructor code }

}

Constructor Injections

Page 21: Deepak khetawat sling_models_sightly_jsp

• Sling Model class object can be used in JSP by either

- <c:set var = "slingModel" value="<%=

resource.adaptTo(SlingModelDemo.class)%>" />

Or

- <sling:adaptTo adaptable="${resource}"

adaptTo="com.slingmodels.core.SlingModelDemo"

var=“slingModel"/>

Sling Models with JSP

Page 22: Deepak khetawat sling_models_sightly_jsp

• Sightly is a beautiful mark up language providing advantages like

separation of concerns , prevents xss vulnerabilities .

• Sling Model class object can be used in Sightly by :

<div data-sly-use.slingModel ="

com.slingmodels.core.SlingModelDemo">

Sling Models with Sightly

Page 23: Deepak khetawat sling_models_sightly_jsp

Demo

Page 24: Deepak khetawat sling_models_sightly_jsp

• Retrieving dialog values of a component

• Retrieving recent pages/articles under a content path

• Many more

Groovifying Demo UseCases :

Page 26: Deepak khetawat sling_models_sightly_jsp

Appendix and Q&A

Page 28: Deepak khetawat sling_models_sightly_jsp

Q&A

Page 29: Deepak khetawat sling_models_sightly_jsp

For more information contact:

DEEPAK KHETAWAT

M +91-9910941818

[email protected]

Thank you