This action might not be possible to undo. Are you sure you want to continue?
You are here: Users > aklimets > Cq5AdaptToReference
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.
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.
getValueMap(Resource) (handles null case etc. if this is a JCR-node-based resource (or other resource supporting value maps). if this is a JCR-node-based resource (or other resource supporting value maps) returns a convenient-to-use map of the properties. under /system/userManager) InputStream URL File SlingScript Servlet Authorizable (jackrabbit) String Boolean Long Double Calendar Value String Boolean returns the value(s).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.) 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.jackrabbit. if this is a JCR-Propertybased resource (and the value fits) .apache. file URL if this is a file system resource) if this is a file system resource if this resource is a script (eg.usermanager. can be done simpler by using ResourceUtil. 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. 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.sling.
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. 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.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()). 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 . 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.
if this is a JCR-based resource resolver based on the 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. 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.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. if this is a JCR-based resource resolver .4) request's JCR session. if this is a JCR-based resource resolver. 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.
tripod I agree. fmeschbe .. 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. 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 .. even w/o request object (since 5...4) (no targets yet. Component Resource LabeledResource Resource Template LabeledResource Node Security Authorizable User Group (cq-security) DAM Resource Asset Node . I would consider this a bug.(wiki) Externalizer for externalizing absolute URLs. they should only be adapted from a ResourceResolver.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue listening from where you left off, or restart the preview.