javacmis

find out object type with its path


I have to find out the type (folder/file) of an object with it's directory path.

e.g. I have to find out that the object with this path is a file.

/home/user/test.docx

I found a solution that works but it isn't really a good one.

try {
    final Folder parentFolder = (Folder) session.getObjectByPath(path); 
    //throws exception when path points to a file
    //do sth when it's a folder
} catch (final Exception e) {
    //do sth when it's a document/file
}

I can't use 'instance of' here because i can't get the object (with session.getObjectByPath) wihout knowing the type of the output.

Is there a better way to find out the object type with it's path?


Solution

  • Instead of type casting this to Folder directly you can use the instanceof like this:

    CmisObject cmisObject = session.getObjectByPath(path);
    
    if (cmisObject instanceof Document) {
        Document document = (Document) cmisObject;
    } else if (cmisObject instanceof Folder) {
        Folder folder = (Folder) cmisDocument;
    }