0
votes

I am using magnolia for one of my project and in that I have created a REST Endpoint. The rest endpoint will read the nodes from my custom workspace and return the result to client. It is very simple.

If I run the below query in magnolia JCR Utils query app it is working fine and returning the correct results.

SELECT * FROM [mgnl:contentIndex]

Below is a screenshot

http://s1.postimg.org/gl59arw2n/correct_result.png

If I run the below code in my REST ENDPOINT Handler it is returning empty result set.

    // Get JCR session for "dinnacoDriven" workspace
    try {
        Session session = MgnlContext.getJCRSession("dinnacoDriven");
        StringBuilder queryBuilder = new StringBuilder("SELECT * FROM [mgnl:contentIndex]");
        System.out.println(queryBuilder.toString());
        Query query = session.getWorkspace().getQueryManager().createQuery(queryBuilder.toString(), Query.JCR_SQL2);
        List<String> suggestions = new ArrayList<String>();
        NodeIterator iterator = query.execute().getNodes();
        while(iterator.hasNext()) {
            System.out.println("next node");
            Node node = iterator.nextNode();
            suggestions.add(node.getProperty("title").getString());
        }
        return suggestions;
    } catch(RepositoryException ex) {
        throw new ServiceException("Internal Server Error", ex);
    }

I can see in the logs that System.out.println(queryString.toString()); will print the exact same query that I am running above but next node will never get printed. It is returning empty results.

Its been 12 hours since I am trying to figure out what is wrong with this code, but I can't find anything.

What is problem with my code ? Why it is returning empty result set ?

EDIT: Adding some additional information. When I am accessing from code, I am not logged in. I am accessing anonymously.

The contentIndex nodes are stored in dinnacoDriven workspace under root node contentIndex.

Some more information, I exported the dinnacoDriven workspace into xml, this is the xml file

https://gist.github.com/riteshsangwan/efe93ee4c5077236c0c0

1

1 Answers

1
votes

Not enough data, so just wild guess: When executing query in admin central, you are logged in and has access to the nodes. When running against REST endpoint, you are not logged in and anonymous user doesn't have assigned access to dinnacoDriven workspace.

HTH,
Jan