Expand my Community achievements bar.

SOLVED

Unable to access properties of another node

Avatar

Level 2

Hi there

I've got some trouble while accessing another node to read 2 properties out of it. What I'm trying to do is create a page which should set a cookie, if our SSO-Proxy sends the user-id of the user. In order to set the cookie, I have to read the name of the cookie that should be created, and the value it should contain from another node.

I tried using the following code, but cookieValue and cookieName always equal to null, although the properties exist on the given node:

<%@include file="/libs/foundation/global.jsp"%> <%@page import="org.apache.sling.api.request.RequestParameter, javax.servlet.http.Cookie, com.day.cq.wcm.api.WCMMode, org.apache.sling.api.resource.ResourceUtil"%><% boolean isEdit = WCMMode.fromRequest(request) == WCMMode.EDIT; boolean isDesign = WCMMode.fromRequest(request) == WCMMode.DESIGN; //String configUrl = currentNode.getProperty("configUrl").getValue().getString(); String configUrl = "/content/path/to/another/node"; Resource configResource = resourceResolver.getResource(configUrl); ValueMap configProperties = ResourceUtil.getValueMap(configResource); String cookieName = configProperties.get("cookieName", (String) null); String cookieValue = configProperties.get("cookieValue", (String) null); RequestParameter languageParameter = slingRequest.getRequestParameter("language"); String languageString = languageParameter.getString(); String userId = request.getHeader("USERID"); if(userId != null && !userId.isEmpty()) { Cookie passwordCookie = new Cookie(cookieName, cookieValue); passwordCookie.setPath("/"); passwordCookie.setDomain(".company.com"); response.addCookie(passwordCookie); if(!isEdit && !isDesign) response.sendRedirect("/redirect/to/url/" + languageString + "/target.html"); return; } %>

Interestingly enough, the configResource object seems to find the node, because when I simply write it to the output (<%= configResource %>), writes the following:

JcrNodeResource, type=cq:Page, superType=null, path=/content/path/to/another/node

Also the properties seem to be valid  (if I change it to a node-path which doesn't exist, it will return some output containing ValueMapDecorator):

org.apache.sling.jcr.resource.JcrPropertyMap@35a739a8

I've used the same piece of code on another page, to access page properties of a node in the same directory. In this case my page tries to access the properties of a page in a completly different directory (the page I'm trying to access is in /content/website-A/corp/de/ while the page trying to get the information is in /content/website-B/application) - does this matter?

Any tips or help is greatly appreciated!

Ahatius

1 Accepted Solution

Avatar

Correct answer by
Level 2

Ok, I got it :O

Forgot to append /jcr:content to the url. After adding that, the values are read correctly.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

Ok, I got it :O

Forgot to append /jcr:content to the url. After adding that, the values are read correctly.