Paging Through Folder Children Items

The paging support is available in IT Hit WebDAV Server Library for Java v4.0.2590-beta and later versions.

The WebDAV Server Library for Java provides support for paging through the children items and sorting results. The client application can requesting a specified range of items with a specified sort conditions.

Detecting Paging Support

To determine server features the client application submits OPTIONS request to folder item. The WebDAV Server Library v4.0.2590-beta and later will always add 'paging' token to the DAV header to indicate paging support:

DAV: 1,2,3,resumable-upload,paging

Implementing Paging

The Folder.getChildren() method provides parameters for specifying number of items to skip before returning the the rest of items, number of items to return and sort columns list. Below you can see a sample getChildren() method implementation for file system storage:

public PageResults getChildren(List<Property> propNames, Long offset, Long nResults, List<OrderProperty> orderProps) throws ServerException {
        String decodedPath = HierarchyItemImpl.decodeAndConvertToPath(getPath());
        Path fullFolderPath = Paths.get(getRootFolder() + decodedPath);
        List<HierarchyItemImpl> children = new ArrayList<>();
        Long total = null;
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(fullFolderPath)) {
            List<Path> paths = StreamSupport.stream(ds.spliterator(), false).collect(Collectors.toList());
            // Apply sorting
            paths = sortChildren(paths, orderProps);
            for (Path p : paths) {
                String childPath = getPath() + encode(p.getFileName().toString());
                HierarchyItemImpl item = (HierarchyItemImpl) getEngine().getHierarchyItem(childPath);
                children.add(item);
            }
            total = (long) paths.size();
            if (offset != null && nResults != null)
            {
                // Apply paging
                children = children.stream().skip(offset).limit(nResults).collect(Collectors.toList());
            }
        } catch (IOException e) {
            getEngine().getLogger().logError(e.getMessage(), e);
        }
        return new PageResults(children, total);
}

 Here is the description of each parameter :

  • offset - number of items to skip before returning the rest of the items.
  • nResults - number of items to return (page size). Note that on the last page the number of items may be smaller than specified in this parameter.
  • orderProps - list of sort order columns. You will sort your result set according to these rules before applying paging.

The getChildren() function returns the PageResults class instance that contains list of items that corresponds to the requested page as well as a total number of items in the folder. Typically you will use the total number of items on the client side to calculate the number of pages available and to render pages links/buttons UI.

The total number of items is optional and can be null, as soon as in some systems determining a total number of items could consume resources or is not required.

In the Paging Through Children Items article you can find how to submit a paging request and implement paging using WebDAV Ajax Library.

Note that paging is an IT Hit WebDAV Server Engine specific feature and is not supported by most WebDAV Clients such as Windows Explorer/Mini re-director and Mac OS X Finder. They always request all items in the folder and offset, nResults and orderProps parameters will always be null when used with these clients.

Next Article:

Creating WebDAV Server With Search Support (DASL), Java