Sling & REST
via Apache Sling : JCR, OSGi, Scripting and REST 👍
& Effective Web Application Development with Apache Sling
面向对象,Stateless 就是指每次 Request 携带所有相关信息,绝不在服务器端存储。
URI 直接对应到 JCR 的每个 Node,即 REST 中的 Resource,每个 Resource 有直接的 Properties 及其 Value:
Scripting 用于处理请求并解析出 Resource,然后交由 Script 进行处理,最终生成 rendered 的页面。
From URL to Content and Scripts
principles:
- the mapping uses the content path extracted from the request to locate the resource
- when the appropriate resource is located, the sling resource type is extracted, and used to locate the script to be used for rendering the content
终于联系起来了:With Sling, you specify which script renders a certain entity (by setting the sling:resourceType property in the JCR node). This mechanism offers more freedom than one in which the script accesses the data entities (as an SQL statement in a PHP script would do) as a resource can have several renditions.
对应到一个 JCR 节点之后,其中含有 Sling 相关的 properties,从而找到 resourceType 进而指定特别的 Script 去做渲染工作。
All Sling scripts are stored in subfolders of either /apps or /libs, which will be searched by order, & in this order (see Customizing Components and Other Elements).
- Mapping requests to resources:具体找法就是根据 URI 定位到对应的 content node,若没有找到则抛弃 .html 继续找,再没找到就 404
Locating the script:找到 content node 获取到 sling:resourceType 属性之后就load 相应的 Script:
when the Method (GET, POST) is required, it will be specified in uppercase as according to the HTTP specification e.g. jobs.POST.esp (see below)
- various script engines are supported:
- .esp, .ecma: ECMAScript (JavaScript) Pages (server-side execution)
- .jsp: Java Server Pages (server-side execution)
- .java: Java Servlet Compiler (server-side execution)
- .jst: JavaScript templates (client-side execution)
结合这张图一起看:
基于 OSGi
而 OSGi 的作用在于 Runtime,之所以为动态模块加载,用于管理复杂度,并可以动态扩展其依赖。(类 JavaScript 的 Require.js ?)
最佳实现,Apache Felix - SCR Annotations
这哥们用注解的方式实现 OSGi 标准 bundle。
@Property(name = "sample",
options = {
@PropertyOption(name = "option1", value = "&option.label.1"),
@PropertyOption(name = "option2", value = "&option.label.2")
}
)
参考项目:nateyolles/publick-sling-blog: Blog engine using Apache Sling and Sightly
This architecture allows you to extend Sling with application specific modules.
This enables you to perform the following actions on any of the packages within your installation:
- install
- start
- stop
- update
- uninstall
- see the current status
- access more detailed information (e.g. symbolic name, version, location, etc) about the specific bundles
总结就是 Sling 背后靠着 CRX 吃饭:
对比传统 Controller,直接对应 JCR 节点(文件结构?),包含相关信息。
不是直接对应文件目录,但是也完全可以这样。
WHAT IS CRX?
CRX is short for Content Repository eXtreme, Day's JCR-compliant repository. CRX allows you to store, manage, and access data using a standardized Java interface.
好像不关 Jackrabbit 什么事儿了? CRX 基于 Jackrabbit
CRX is drop-in and source-code compatible with Apache Jackrabbit. If you have an existing Jackrabbit repository, you can migrate it to CRX with minimal effort. via Jackrabbit Compatibility - docs.adobe.com
via How/When is CRX and Apache Jackrabbit used in CQ5/AEM? - Stack Overflow:
Adobe CRX is the commercial content repository component used in the AEM, which uses some elements of Jackrabbit (e.g. some of the security APIs). CRX provides additional features such as development tools & clustering capabilities and has its own storage mechanism which differs from the Jackrabbit implementation.