Você está na página 1de 5

CQ5 adaptTo() Reference

You are here: Users > aklimets > Cq5AdaptToReference

About adaptTo()
The interface Adaptable provides a method adaptTo() that allows to conveniently get from one object to another: Node node = resource.adaptTo(Node.class); The method is a generics method, and will be given the type of the object that should be returned, ie. its static class member. There are various use cases: get implementation-specific objects for example, a JCR-based implementation of the generic Resource interface provides access to the underlying JCR Node shortcut creation of objects that require internal context objects to be passed example: the JCR-based ResourceResolver holds a reference to the request's JCR Session, which in turn is needed for many objects that will work based on that request session, such as the PageManager or UserManager shortcut to services (rare case, sling.getService() is simple as well)

Null return value


adaptTo() can return null. There are various reasons for that: implementation doesn't support the target type, adapter factory handling this case is not active (eg. due to missing service references), internal condition failed, service is not available, etc. It is important to handle the null case gracefully! (For jsp renderings it might be ok to have the jsp fail, if that will end up with an empty piece of content.)

How it works
There are two ways Adaptable.adaptTo() can be implemented: by the object itself, implementing the method itself and mapping to certain objects by an AdapterFactory, which can map arbitrary objects (the objects must still implement the Adaptable interface and must extend from SlingAdaptable, which passes the adaptTo call to a central adapter manager); this allows to hook into the adaptTo mechanism for existing classes, such as Resource (or a combination of both) For the first case, the javadocs can state what adaptTo-targets are possible, however, for specific subclasses such as the JCR-based Resource, this is mostly not possible. The latter case, AdapterFactories, are typically part of the private classes of a bundle and thus not exposed as client API and in javadocs at all. One could theoretically get all AdapterFactory implementations from the OSGi service runtime and look at their "adaptables" (sources) and "adapters" (targets) configurations, but not each map to each other. In the end, this depends on the actual logic implemented inside, which must be documented. Hence this reference.

Reference
Note about versions: this starts with CQ 5.3. If new adaptations are added, they should be noted with "since 5.4" like in javadocs.

Source Sling

Targets

Notes / Conditions

Node Property Item

if this is a JCR-node-based resource or a JCR property referencing a node if this is a JCR-property-based resource if this is a JCR-based resource (node or property) returns a map of the properties, if this is a JCR-node-based resource (or other resource supporting value maps) returns a convenient-to-use map of the properties, if this is a JCR-node-based resource (or other resource supporting value maps); can be done simpler by using ResourceUtil.getValueMap(Resource) (handles null case etc.)

Map

ValueMap

if this is a JCR-node-based resource and the user has permissions to modify properties on PersistableValueMap that node (Note: multiple persistable maps don't share their values) returns the binary content of a "file" resource (if this is a JCR-node-based resource and the node type is nt:file or nt:resource; if this is a bundle resource; file content if this is a file system resource) or the data of a binary JCR property resource returns an URL to the resource (repository URL of this node if this is a JCR-node-based resource; jar bundle URL if this is a bundle resource; file URL if this is a file system resource) if this is a file system resource if this resource is a script (eg. jsp file) for which a script engine is registered with sling if this resource is a script (eg. jsp file) for which a script engine is registered with sling or if this is a servlet resource if this is a an authorizable resource (from the AuthorizableResourceProvider in org.apache.sling.jackrabbit.usermanager, under /system/userManager)

InputStream

URL

File SlingScript

Servlet

Authorizable (jackrabbit) String Boolean Long Double Calendar Value String[] Boolean[]

returns the value(s), if this is a JCR-Propertybased resource (and the value fits)

Long[] Calendar[] Value[] LabeledResource Page Component Resource Design Template Blueprint Asset Tag Preferences Profile UserManager 1 Authorizable (cqsecurity) User (cq-security) PrivilegeManager 1 SimpleSearch WorkflowStatus ReplicationStatus Page (contentbus) searches below the resource (or use setSearchIn()), if this is a JCR-based resource workflow status for the given page/workflow payload node replication status for the given resource or its jcr:content subnode (checked first) an adapter for a legacy cq4 page node in the jcr repository returns an adapted connector resource for certain types, if this is a JCR-node-based resource returns the blog at the given JCR-node-based resource or one of its ancestors returns the blog entry at the given JCR-nodebased resource or one of its ancestors if this is a JCR-node-based resource (typically with mixin cq:Calendar, but not required) if this is a cq:CalendarEvent node resource if this is a calendar component node resource (collab/calendar/components/calendar) if this is a JCR-node-based resource if this is a JCR-node-based resource and the node is a cq:Page (or cq:PseudoPage) if this is a cq:Component node resource if this is a design node (cq:Page, typically under /etc/designs) if this is a cq:Template node resource if this is a cq:Page node resource (more specific checks possible in the future) if this is a dam:Asset node resource if this is a cq:Tag node resource if this is a cq:Preferences node resource for a valid user/group if this is the profile below a user/group node (eg. cq/security/components/profile) based on the JCR session if this is a JCR-based resource and if the user has permissions to access the UserManager if this is a authorizable home node if this is a user home node

ConnectorResource

Blog BlogEntry CqCalendar Event CalendarComponent

Comment

if this is a cq:Comment node resource below the user generated content tree if this is a comments component (collab/commons/components/comments) or a cq:Comment node resource below the user generated content tree if this is a JCR-based node resource (for cq:Rating nodes) if this is a ratings component (collab/commons/components/ratings) or a cq:Rating node resource below the user generated content tree if this is a wiki:Topic node resource if this is a cq:ContentSyncConfig node resource (since 5.4) if this is below a cq:ContentSyncConfig node resource (since 5.4) request's JCR session, if this is a JCR-based resource resolver (default)

CommentSystem

Rating

RatingSystem

WikiTopic Config ConfigEntry Session PageManager ComponentManager Designer AssetManager TagManager

based on the JCR session, if this is a JCR-based resource resolver based on the JCR session, if this is a JCR-based resource resolver based on the JCR session, if this is a JCR-based resource resolver, and if the user has permissions to access the UserManager the current user

UserManager Authorizable (cqsecurity) User (cq-security) PrivilegeManager ResourceResolver Preferences PreferencesService PinManager QueryBuilder Ticket (contentbus) BlogManager CalendarManager TopicManager (wiki) JcrTopicManager

preferences of the current user (based on JCR session if this is a JCR-based resource resolver)

adapts the underlying JCR session to a legacy cq4 ticket based on the JCR session, if this is a JCR-based resource resolver based on the JCR session, if this is a JCR-based resource resolver

(wiki) Externalizer for externalizing absolute URLs, even w/o request object (since 5.4) (no targets yet, but implements Adaptable and could be used as source in a custom AdapterFactory) ContentHandler (XML) if this is a sling rewriter response

SlingHttpServletRequest

SlingHttpServletResponse WCM

Resource LabeledResource Page Node ... Component Resource LabeledResource Resource Template LabeledResource Node Security Authorizable User Group (cq-security) DAM Resource Asset Node ... Tagging Tag Other Resource Node Node ReplicationStatus

resource of the page labeled resource (== this) node of the page everything that the page's Resource can be adapted to resource of the component labeled resource (== this) resource of the template labeled resource (== this) node of this template

returns the user/group home node returns the replicatoin status for the user/group home node

resource of the asset node of the asset everything that the asset's Resource can be adapted to

resource of the tag node of the tag

Furthermore sling/jcr/ocm also provides an AdapterFactory for custom OCM objects


1 IMO it's not right that a resource can be adapted to a UserManager or PrivilegeManager, they

should only be adapted from a ResourceResolver. tripod I agree; I would consider this a bug. fmeschbe

Você também pode gostar