final XMLRepresentationParser xmlParser = new XMLRepresentationParser();
return new OutputRepresentation(MediaType.APPLICATION_ATOM) {
public void write(OutputStream os)
throws IOException
{
final ItemDestination dest = new WriterItemDestination(new OutputStreamWriter(os,"UTF-8"),"UTF-8");
final ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
try {
context.execute(new QueryContext.ResultListener() {
DocumentLoader loader = new SAXDocumentLoader();
public void onStart() throws QueryException {
try {
dest.send(constructor.createDocument());
dest.send(constructor.createElement(AtomResource.FEED_NAME));
dest.send(constructor.createCharacters("\n"));
link(constructor,dest,"self",getRequest().getResourceRef().toString());
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.TITLE_NAME,"Term Query");
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.ID_NAME,UUID.randomUUID().toString());
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.UPDATED_NAME,AtomResource.toXSDDate(new Date()));
} catch (XMLException ex) {
throw new QueryException("Exception during feed start.",ex);
}
}
public void onEnd() throws QueryException {
try {
dest.send(constructor.createCharacters("\n"));
dest.send(constructor.createElementEnd(AtomResource.FEED_NAME));
dest.send(constructor.createDocumentEnd());
} catch (XMLException ex) {
throw new QueryException("Exception during feed end.",ex);
}
}
public void onEntry(Entry entry) throws QueryException {
try {
Feed feed = entry.getFeed();
String feedPath = feed.getPath();
String feedBaseURI = resourceBase.toString()+feedPath;
dest.send(constructor.createCharacters("\n"));
// get the entry representation
Representation rep = app.getStorage().getEntry(feedBaseURI,feedPath,feed.getUUID(),entry.getUUID());
// avoid thread creation because reading an output representation requires a thread
StringWriter sw = new StringWriter();
rep.write(sw);
rep.release();
// TODO: optimize by giving item destination to storage
loader.generate(new StringReader(sw.toString()), new RemoveDocumentFilter(dest));
} catch (Exception ex) {
throw new QueryException("Exception during feed entry generation.",ex);
}
}
public void onFeed(Feed feed) throws QueryException {
try {
org.atomojo.app.client.Feed feedRep = new org.atomojo.app.client.Feed(xmlParser.load(app.getStorage().getFeedHead(feed.getPath(), feed.getUUID())));
dest.send(constructor.createCharacters("\n"));
dest.send(constructor.createElement(AtomResource.ENTRY_NAME));
text(constructor,dest,AtomResource.ID_NAME,feed.getUUID().toString());
text(constructor,dest,AtomResource.PUBLISHED_NAME,AtomResource.toXSDDate(feed.getCreated()));
text(constructor,dest,AtomResource.UPDATED_NAME,AtomResource.toXSDDate(feed.getEdited()));
text(constructor,dest,AtomResource.TITLE_NAME,feedRep.getTitle());
String summary = feedRep.getSummary();
text(constructor,dest,AtomResource.SUMMARY_NAME,summary);
term(constructor,dest,Categorization.FEED_TYPE_TERM,null);
Iterator<TermInstance<Feed>> terms = feed.getTerms();
while (terms.hasNext()) {
TermInstance<Feed> fterm = terms.next();
term(constructor,dest,fterm.getTerm().getURI(),fterm.getValue());
}
link(constructor,dest,"related",resourceBase+feed.getPath());
dest.send(constructor.createElementEnd(AtomResource.ENTRY_NAME));
} catch (Exception ex) {
throw new QueryException("Exception during feed entry generation.",ex);
}
}
} );