code: private void traverseResources(Resource resource, int counter) throws XMLStreamException, RepositoryException,
        IllegalResourceTypeException, URISyntaxException, UnsupportedEncodingException
     {
  
        xmlStreamWriter.writeStartElement("DAV:", "response");
  
        xmlStreamWriter.writeStartElement("DAV:", "href");
        String href = resource.getIdentifier().toASCIIString();
        if (resource.isCollection())
        {
           xmlStreamWriter.writeCharacters(href + "/");
        }
        else
        {
           xmlStreamWriter.writeCharacters(href);
        }
        xmlStreamWriter.writeEndElement();
  
        PropstatGroupedRepresentation propstat =
           new PropstatGroupedRepresentation(resource, propertyNames, propertyNamesOnly, session);
  
        PropertyWriteUtil.writePropStats(xmlStreamWriter, propstat.getPropStats());
  
        xmlStreamWriter.writeEndElement();
  
        int d = depth;
  
        if (resource.isCollection())
        {
           if (counter < d)
           {
              CollectionResource collection = (CollectionResource)resource;
              for (Resource child : collection.getResources())
              {
                 traverseResources(child, counter + 1);
              }
           }
        }
  
     }
docs: Traverses resources and collects the vales of required properties.
  
  @param resource resource to traverse
  @param counter the depth
  @throws XMLStreamException {@link XMLStreamException}
  @throws RepositoryException {@link RepositoryException}
  @throws IllegalResourceTypeException {@link IllegalResourceTypeException}
  @throws URISyntaxException {@link URISyntaxException}
  @throws UnsupportedEncodingException