code: public static Node getNtResourceRecursively(Node node) throws ItemNotFoundException, RepositoryException
     {
  
        if (node.isNodeType("nt:resource"))
           return node;
  
        Item pi = node.getPrimaryItem();
        if (pi.isNode())
        {
           return getNtResourceRecursively((Node)pi);
        }
        throw new ItemNotFoundException("No nt:resource node found for " + node.getPath());
     }
docs: traverses incoming node trying to find primary nt:resource node
  
  @param node
  @return nt:resource node
  @throws ItemNotFoundException
  if no such node found
  @throws RepositoryException