}
int h = path.indexOf('#');
if (h>=0) {
path = path.substring(0,h);
}
final DB atomDB = (DB)getContext().getAttributes().get(AtomApplication.DB_ATTR);
Feed feed = null;
if (path.length()>0) {
String [] segments = path.split("/");
feed = atomDB.findFeedByPath(segments);
if (feed==null) {
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return new StringRepresentation("No feed found at path: "+path);
}
}
Form form = getRequest().getResourceRef().getQueryAsForm();
String sinceS = form.getFirstValue("since");
Date since = null;
if (sinceS!=null && sinceS.length()>0) {
try {
since = AtomResource.fromXSDDate(sinceS);
} catch (ParseException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Bad since value '"+sinceS+"': "+ex.getMessage());
}
}
String limitS = form.getFirstValue("limit");
int limit = 100;
if (limitS!=null && limitS.length()>0) {
try {
limit = Integer.parseInt(limitS);
} catch (NumberFormatException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Bad limit value '"+limitS+"': "+ex.getMessage());
}
}
String startS = form.getFirstValue("start");
int start = 1;
if (startS!=null && startS.length()>0) {
try {
start = Integer.parseInt(startS);
} catch (NumberFormatException ex) {
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
return new StringRepresentation("Bad start value '"+startS+"': "+ex.getMessage());
}
}
final int startRow = start;
final int limitCount = limit;
final Date sinceMarker = since==null ? new Date() : since;
final Feed target = feed;
return new OutputRepresentation(MediaType.APPLICATION_ATOM) {
public void write(OutputStream os)
throws IOException
{
setCharacterSet(CharacterSet.UTF_8);
try {
String prevRef = null;
if (startRow>1) {
prevRef = getRequest().getResourceRef().toString();
int q = prevRef.indexOf('?');
prevRef = prevRef.substring(0,q);
prevRef += "?";
if (sinceMarker!=null) {
prevRef += "since=";
prevRef += AtomResource.toXSDDate(sinceMarker);
prevRef += "&";
}
prevRef += "start=";
int prevStart = startRow-limitCount;
int prevLimit = limitCount;
if (prevStart<1) {
prevStart = 1;
prevLimit = startRow-1;
}
prevRef += prevStart;
prevRef += "&limit=";
prevRef += prevLimit;
}
String nextRef = getRequest().getResourceRef().toString();
int q = nextRef.indexOf('?');
if (q>=0) {
nextRef = nextRef.substring(0,q);
}
nextRef += "?";
if (sinceMarker!=null) {
nextRef += "since=";
nextRef += AtomResource.toXSDDate(sinceMarker);
nextRef += "&";
}
nextRef += "start=";
nextRef += (startRow+limitCount);
nextRef += "&limit=";
nextRef += limitCount;
DBIterator<Entry> entries = atomDB.getEntriesModifiedBefore(target,sinceMarker,startRow,limitCount);
Form form = getRequest().getResourceRef().getQueryAsForm();
generate(new WriterItemDestination(new OutputStreamWriter(os,"UTF-8"),"UTF-8"),entries,prevRef,nextRef);
entries.finished();
} catch (XMLException ex) {
getContext().getLogger().log(Level.SEVERE,"Cannot serialize metadata feed.",ex);