class GoogleStorageBucketListService implements ListService {

    @Override
    public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
        try {
            final AttributedList<Path> buckets = new AttributedList<Path>();
            Buckets response;
            String page = null;
            do {
                response = session.getClient().buckets().list(session.getHost().getCredentials().getUsername())
                    .setMaxResults(preferences.getLong("googlestorage.listing.chunksize"))
                    .setPageToken(page)
                    .execute();
                if(null != response.getItems()) {
                    for(Bucket item : response.getItems()) {
                        final Path bucket = new Path(PathNormalizer.normalize(item.getName()), EnumSet.of(Path.Type.volume, Path.Type.directory),
                            attributes.toAttributes(item)
                        );
                        buckets.add(bucket);
                        listener.chunk(directory, buckets);
                    }
                }
                page = response.getNextPageToken();
            }
            while(page != null);
            return buckets;
        }
        catch(IOException e) {
            throw new GoogleStorageExceptionMappingService().map("Listing directory {0} failed", e, directory);
        }
    }

    public  GoogleStorageBucketListService(final GoogleStorageSession session);

}