From: <ale...@us...> - 2006-06-29 18:57:23
|
Revision: 3758 Author: alexmilowski Date: 2006-06-29 11:39:05 -0700 (Thu, 29 Jun 2006) ViewCVS: http://svn.sourceforge.net/exist/?rev=3758&view=rev Log Message: ----------- Added introspection to the atom servlet Modified Paths: -------------- trunk/eXist-1.0/src/org/exist/atom/http/AtomServlet.java Added Paths: ----------- trunk/eXist-1.0/src/org/exist/atom/modules/Introspect.java trunk/eXist-1.0/src/org/exist/atom/modules/introspect.xq Property Changed: ---------------- trunk/eXist-1.0/src/org/exist/atom/http/ trunk/eXist-1.0/src/org/exist/atom/modules/ Property changes on: trunk/eXist-1.0/src/org/exist/atom/http ___________________________________________________________________ Name: svn:ignore + AtomServlet.java~ HttpResponseMessage.java~ HttpRequestMIMEMessage_1.java~ HttpRequestMessage.java~ HttpResponseMIMEMessage.java~ HttpRequestMIMEMessage.java~ EXistServlet.java~ Modified: trunk/eXist-1.0/src/org/exist/atom/http/AtomServlet.java =================================================================== --- trunk/eXist-1.0/src/org/exist/atom/http/AtomServlet.java 2006-06-29 17:45:59 UTC (rev 3757) +++ trunk/eXist-1.0/src/org/exist/atom/http/AtomServlet.java 2006-06-29 18:39:05 UTC (rev 3758) @@ -39,6 +39,7 @@ import org.exist.atom.AtomModule; import org.exist.atom.modules.AtomFeeds; import org.exist.atom.modules.AtomProtocol; +import org.exist.atom.modules.Introspect; import org.exist.atom.modules.Query; import org.exist.atom.modules.Topics; import org.exist.http.BadRequestException; @@ -198,6 +199,9 @@ Query query = new Query(); modules.put("query",query); query.init(new ModuleContext(config,"query")); + Introspect introspect = new Introspect(); + modules.put("introspect",introspect); + introspect.init(new ModuleContext(config,"introspect")); // XML lib checks.... Property changes on: trunk/eXist-1.0/src/org/exist/atom/modules ___________________________________________________________________ Name: svn:ignore + AtomProtocol_1.java~ AtomFeeds.java~ AtomProtocol.java~ AtomModuleBase.java~ Added: trunk/eXist-1.0/src/org/exist/atom/modules/Introspect.java =================================================================== --- trunk/eXist-1.0/src/org/exist/atom/modules/Introspect.java (rev 0) +++ trunk/eXist-1.0/src/org/exist/atom/modules/Introspect.java 2006-06-29 18:39:05 UTC (rev 3758) @@ -0,0 +1,126 @@ +/* + * AtomProtocol.java + * + * Created on June 16, 2006, 11:39 AM + * + * (C) R. Alexander Milowski al...@mi... + */ + +package org.exist.atom.modules; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.URL; +import java.util.Properties; +import org.apache.log4j.Logger; +import org.exist.EXistException; +import org.exist.atom.Atom; +import org.exist.atom.IncomingMessage; +import org.exist.atom.OutgoingMessage; +import org.exist.collections.Collection; +import org.exist.http.BadRequestException; +import org.exist.http.NotFoundException; +import org.exist.security.PermissionDeniedException; +import org.exist.security.xacml.AccessContext; +import org.exist.source.URLSource; +import org.exist.storage.DBBroker; +import org.exist.storage.serializers.Serializer; +import org.exist.util.serializer.SAXSerializer; +import org.exist.util.serializer.SerializerPool; +import org.exist.xmldb.XmldbURI; +import org.exist.xquery.CompiledXQuery; +import org.exist.xquery.XPathException; +import org.exist.xquery.XQuery; +import org.exist.xquery.XQueryContext; +import org.exist.xquery.value.Sequence; +import org.xml.sax.SAXException; + +/** + * + * @author R. Alexander Milowski + */ +public class Introspect extends AtomModuleBase implements Atom { + + protected final static Logger LOG = Logger.getLogger(Topics.class); + + URL getIntrospectQueryURL; + URLSource getIntrospectQuerySource; + + public Introspect() { + getIntrospectQueryURL = getClass().getResource("introspect.xq"); + getIntrospectQuerySource = new URLSource(getIntrospectQueryURL); + } + + public void doGet(DBBroker broker,IncomingMessage request,OutgoingMessage response) + throws BadRequestException,PermissionDeniedException,NotFoundException,EXistException + { + + Collection collection = broker.getCollection(XmldbURI.create(request.getPath())); + if (collection == null) { + throw new BadRequestException("Collection "+request.getPath()+" does not exist."); + } + + XQuery xquery = broker.getXQueryService(); + CompiledXQuery getIntrospectQuery = xquery.getXQueryPool().borrowCompiledXQuery(broker,getIntrospectQuerySource); + + XQueryContext context; + if (getIntrospectQuery==null) { + context = xquery.newContext(AccessContext.REST); + try { + getIntrospectQuery = xquery.compile(context, getIntrospectQuerySource); + } catch (XPathException ex) { + throw new EXistException("Cannot compile xquery "+getIntrospectQueryURL,ex); + } catch (IOException ex) { + throw new EXistException("I/O exception while compiling xquery "+getIntrospectQueryURL,ex); + } + } else { + context = getIntrospectQuery.getContext(); + } + try { + context.setStaticallyKnownDocuments(new XmldbURI[] { XmldbURI.create(request.getPath()).append(AtomProtocol.FEED_DOCUMENT_NAME) }); + context.declareVariable("path",request.getPath()); + } catch (XPathException ex) { + throw new EXistException("Error in setting up the context for "+getIntrospectQueryURL,ex); + } + + try { + Sequence resultSequence = xquery.execute(getIntrospectQuery, null); + if (resultSequence.isEmpty()) { + throw new BadRequestException("No topic was found."); + } + String charset = getContext().getDefaultCharset(); + response.setStatusCode(200); + response.setContentType("application/atomserv+xml; charset="+charset); + Serializer serializer = broker.getSerializer(); + serializer.reset(); + try { + Writer w = new OutputStreamWriter(response.getOutputStream(),charset); + SAXSerializer sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class); + Properties outputProperties = new Properties(); + sax.setOutput(w, outputProperties); + serializer.setProperties(outputProperties); + serializer.setSAXHandlers(sax, sax); + + serializer.toSAX(resultSequence, 1, 1, false); + + SerializerPool.getInstance().returnObject(sax); + w.flush(); + w.close(); + } catch (IOException ex) { + LOG.fatal("Cannot read resource "+request.getPath(),ex); + throw new EXistException("I/O error on read of resource "+request.getPath(),ex); + } catch (SAXException saxe) { + LOG.warn(saxe); + throw new BadRequestException("Error while serializing XML: " + saxe.getMessage()); + } + resultSequence.itemAt(0); + } catch (XPathException ex) { + throw new EXistException("Cannot execute xquery "+getIntrospectQueryURL,ex); + } finally { + xquery.getXQueryPool().returnCompiledXQuery(getIntrospectQuerySource, getIntrospectQuery); + } + + } + +} Added: trunk/eXist-1.0/src/org/exist/atom/modules/introspect.xq =================================================================== --- trunk/eXist-1.0/src/org/exist/atom/modules/introspect.xq (rev 0) +++ trunk/eXist-1.0/src/org/exist/atom/modules/introspect.xq 2006-06-29 18:39:05 UTC (rev 3758) @@ -0,0 +1,17 @@ +declare namespace atom="http://www.w3.org/2005/Atom"; +<service xmlns="http://purl.org/atom/app#"> + <workspace title="{/atom:feed/atom:title}"> + { + "
", + let $current := substring-before(base-uri(/atom:feed),'/.feed.atom'), + $current-path := substring-after($current,'/db') + for $i in (collection($current)/atom:feed) + let $path := substring-after(substring-before(base-uri($i),'/.feed.atom'),'/db') + return ( <collection title="{$i/atom:title}" href="/atom/edit{$path}"> + <accept>text/*,iamge/*,application/*</accept> + </collection>, + "
" + ) + } + </workspace> +</service> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |