Search code examples
restlucenealfrescocmis

Alfresco REST Core API find node by path


I am developing an Alfresco client based on REST APIs.

But I haven't found a method that allowsto retrieve a node with its path.

[ base url: /alfresco/api/-default-/public/alfresco/versions/1 , api version: 1 ] 

Could you tell me how i can do it ?


Solution

  • The below code will return the noderef as you are expecting from the folder/file path.

    public static void main(String[] args) throws IOException {
            client = new AlfrescoClient.Builder().connect("http://localhost:8080/alfresco", "admin", "admin").build();
            NodeRepresentation node = findNodeByPath("Sites/testsite/documentLibrary/TestFolder/UploadTest.txt");
            System.out.println(node == null ? "null" : node.getName());
        }
    
    private static NodeRepresentation findNodeByPath(String path) throws IOException {
        if (path.startsWith("/"))
            path = path.substring(1);
        if (path.endsWith("/"))
            path = path.substring(0, path.length() - 1);
        return findNodeByPath("-root-", path);
    }
    
    private static NodeRepresentation findNodeByPath(String parentNodeId, String path) throws IOException {
        String[] pathParts = path.split("/");
        String name = pathParts[0];
        String remaining = pathParts.length == 1 ? "" : path.substring(name.length() + 1, path.length());
        List<NodeRepresentation> children = client.getNodesAPI().listNodeChildrenCall(parentNodeId).execute().body()
                .getList();
        for (NodeRepresentation child : children) {
            if (child.getName().equals(name)) {
                return pathParts.length == 1 ? child : findNodeByPath(child.getId(), remaining);
            }
        }
        return null;
    }