}
if (!RollerAtomHandler.canView(user, website)) {
throw new AtomNotAuthorizedException("Not authorized to access website");
}
Feed feed = new Feed();
feed.setId(atomURL
+"/"+website.getHandle() + "/resources/" + path + start);
feed.setTitle(website.getName());
Link link = new Link();
link.setHref(absUrl + "/" + website.getHandle());
link.setRel("alternate");
link.setType("text/html");
feed.setAlternateLinks(Collections.singletonList(link));
MediaFileManager fmgr = roller.getMediaFileManager();
MediaFileDirectory dir = null;
if (StringUtils.isNotEmpty(path)) {
log.debug("Fetching resource collection from weblog " + handle + " at path: " + path);
dir = fmgr.getMediaFileDirectoryByPath(website, path);
} else {
log.debug("Fetching root resource collection from weblog " + handle);
dir = fmgr.getMediaFileRootDirectory(website);
}
Set<MediaFile> files = dir.getMediaFiles();
SortedSet sortedSet = new TreeSet(new Comparator() {
public int compare(Object o1, Object o2) {
MediaFile f1 = (MediaFile)o1;
MediaFile f2 = (MediaFile)o2;
if (f1.getLastModified() < f2.getLastModified()) return 1;
else if (f1.getLastModified() == f2.getLastModified()) return 0;
else return -1;
}
});
if (files != null && start < files.size()) {
for (MediaFile mf : files) {
sortedSet.add(mf);
}
int count = 0;
MediaFile[] sortedResources =
(MediaFile[])sortedSet.toArray(new MediaFile[sortedSet.size()]);
List atomEntries = new ArrayList();
for (int i=start; i<(start + max) && i<(sortedResources.length); i++) {
Entry entry = createAtomResourceEntry(website, sortedResources[i]);
atomEntries.add(entry);
if (count == 0) {
// first entry is most recent
feed.setUpdated(entry.getUpdated());
}
count++;
}
List otherLinks = new ArrayList();
if (start + count < files.size()) { // add next link
int nextOffset = start + max;
String url = atomURL
+"/"+ website.getHandle() + "/resources/" + path + nextOffset;
Link nextLink = new Link();
nextLink.setRel("next");
nextLink.setHref(url);
otherLinks.add(nextLink);
}
if (start > 0) { // add previous link
int prevOffset = start > max ? start - max : 0;
String url = atomURL
+"/"+website.getHandle() + "/resources/" + path + prevOffset;
Link prevLink = new Link();
prevLink.setRel("previous");
prevLink.setHref(url);
otherLinks.add(prevLink);
}
feed.setOtherLinks(otherLinks);
feed.setEntries(atomEntries);
log.debug("Collection contains: " + count);
} else {
log.debug("Returning empty collection");