Expand my Community achievements bar.

SOLVED

Reading File Names from JCR repository

Avatar

Level 2

I am trying to read file names from a path in JCR repository.

My component jsp is as follows:

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="java.util.ArrayList" %>
<h1><%= properties.get("title", currentPage.getTitle()) %></h1>
<%
    com.cts.gto.dex.ListFragments FragmentsList = sling.getService(com.cts.gto.dex.ListFragments.class);
    ArrayList<String> myList = FragmentsList.getList("//apps//DexScraper//fragments") ; 

    String fragname = myList.get(0);
    System.out.println("Fragment Name is::"+fragname);
%>

 

But am getting error when trying to retrieve the list(highlighted in bold). I have attached the exception being generated.

I guess am not able to access this JCR path. Please suggest what changes are needed.

 

Many Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10
5 Replies

Avatar

Level 10

Hi Naveen,

  Based on your log, there is NPE in 

com.cts.gto.dex.impl.CreateFragmentList.getList(CreateFragmentList.java:27)

We are not sure what is the logic in your service class. If you debug you should be able to see where is it throwing null pointer exception

Avatar

Level 2

Hi Naveed,

You can also use WCM Page api to read pages from CRX Repository. Your jsp would go like this:
Resource res = resourceResolver.getResource(resourcePath);
//Once you have a Resource instance, you can call the adaptTo method that converts the resource to another type(here, Page). 
Page page = res.adaptTo(Page.class);
Iterator<Page> childPagesIterator = page.listChildren();
 //Iterate through the pages to retrieve the file names(see, http://docs.adobe.com/docs/en/cq/current/javadoc/com/day/cq/wcm/api/Page.html)

Here, you deal with business object (a CQ page object), which offers a better level of abstraction and you don't need to worry about Repository exceptions.

For more details, you can refer:
http://cqdump.wordpress.com/2012/11/06/cq5-coding-patterns-sling-vs-jcr-part-1/

Avatar

Level 2

Thanks for your response. I understood that service written is not able to read files from the JCR path. 

In my service there is simple java code of reading files from the folder in this manner:

File folder = new File(" PATH TO THE JCR");
        File[] listOfFiles = folder.listFiles();
        System.out.println("Folder files :: "+listOfFiles);
        ArrayList<String> fragmentList = new ArrayList<String>();
            for (int i = 0; i < listOfFiles.length; i++) {

            if (listOfFiles[i].isFile()) {
                String fileName =  listOfFiles[i].getName();

I presume using this way osgi service wont be able to traverse the path so its not able to read the files and am getting the exception.

As am new to CQ could you tell me what is the right way of reading files from JCR.

 

Many Thanks,

Nav.

Avatar

Correct answer by
Level 10

Naveen,

Please refer [1] to see how to read nodes using JCR APIs

[1] http://helpx.adobe.com/experience-manager/using/programmatically-accessing-cq-content-using.html

Avatar

Employee Advisor

Hi Nav,

although a JCR path looks like it is file, it isn't. So you cannot use java.util.File, but you have to use the JCR API to access nodes and properties in the JCR repository.

kind regards,
Jörg