|
Restlet
A Description of the eXist/Restlet Integration
1. IntroductionThe eXist-restlet subproject provides an integration with the Restlet project. It provides a simple way to integrate an embedded version of eXist into a web application. The handling of services, web interfaces, etc. is all handled via regular Restlet code and there is a special "exist" protocol for talking to the internal database as if it is its own service via the Client restlet API. 2. Examples2.1 Starting the Database XMLDB xmldb = new XMLDB(new File("conf.xml"));
xmldb.start();2.2 A Web Component for eXist Requestspublic class WebComponent extends Component {
public WebComponent(String hostname,String ipAddress, int port) {
// ------------------
// Add the connectors
// ------------------
getServers().add(Protocol.HTTP, ipAddress, port);
getClients().add(Protocol.FILE);
getClients().add(XMLDBResource.EXIST);
// ---------------
// Add the virtual host
// ---------------
VirtualHost host = new VirtualHost(getContext());
host.setHostDomain(hostname);
host.setHostPort("80|" + Integer.toString(port));
host.attach(new XMLDBApplication(getContext()));
getHosts().add(host);
}
}2.3 Getting a Document Client client = new Client(Protocol.valueOf("exist"));
Response response = client.get("exist:///foo.xml");2.4 Storing a Document Client client = new Client(Protocol.valueOf("exist"));
Response response =client.put("exist:///foo.xml",new StringRepresentation("<foo/>",MediaType.APPLICATION_XML));2.5 Querying Client client = new Client(Protocol.valueOf("exist"));
Response response = client.post("exist:///",new StringRepresentation("//foo",MediaType.valueOf("application/xquery"))3. BuildingThe restlet integration relies upon the embedded version of eXist. You can build the embedded version by the following instructions
Once you have the embedded version of eXist, you can build the restlet project by:
All of these projects (eXist, embedded eXist, and this project) are also Netbeans projects. 4. APIThe Restlet integration makes using eXist as an embedded database very simple. The minimum you need to do is start the database and then access eXist with the "exist" protocol via the Client class. You can only start the database engine per process which is accomplished by: XMLDB xmldb = new XMLDB(new File("conf.xml"));
xmldb.start();The file passed into the constructor is the path to the eXist configuration file (an XML document). This is a regular eXist configuration. All the features of eXist with the exception of the HTTP services are available via this configuration. Once you have the database started, you can access exist via REST methods:
The embedded eXist database is treated like an HTTP protocol server in that requests are made to resources. Internally, these request are handled locally by interactions with the database. Externally, the Restlet Client class is used to interact with the embedded database. This allows the programmer to treat eXist as any other REST-oriented server even tough the database runs locally. 4.1 PUTA PUT request stores a document. Any media type that is considered XML (i.e. text/xml, application/xml, and those with the "+xml" suffix) are stored as XML documents. Otherwise, the document is stored as a binary resource. The media type used to store the document is associated with the resource in eXist. This media type is used for future GET requests against the resource. If the collection reference in the resource path does not exist, that collection path is created. For example: Client client = new Client(Protocol.valueOf("exist"));
Response response = client.put("exist:///foo.xml",StringRepresentation("<foo/>",MediaType.APPLICATION_XML));
if (response.getStatus().isSuccess()) {
...
}4.2 GETA GET request retrieves a resource from eXist. If it exists, a Response is formulated with a representation of the resource (often a output stream from the database). Currently, a GET request on a collection is not supported. For example, to get an XML resource: Client client = new Client(Protocol.valueOf("exist"));
Response response = client.get("exist:///foo.xml");
if (response.getStatus().isSuccess()) {
...
}4.3 POSTAll POST requests are interpreted as queries. If the media type is "application/xquery", the entity of the post is interpreted as an XQuery. If the media type is an XML media type, then the document is interpreted via the following rules:
same semantics as eXist's REST interface are applied. XUpdate process is performed with the same semantics as eXist's REST interface. The results are serialized and returned as an XML document with the application/xml media type. For example: Client client = new Client(Protocol.valueOf("exist"));
Response response = client.post("exist:///",new StringRepresentation("<docs>{/foo}</docs>,MediaType.valueOf("application/xquery")));
if (response.getStatus().isSuccess()) {
...
}4.4 DELETEA DELETE request on a collection or document resource deletes the resource. For example: Client client = new Client(Protocol.valueOf("exist"));
Response response = client.delete("exist:///foo.xml");
if (response.getStatus().isSuccess()) {
...
}4.5 HEADA HEAD request works very similar to a GET request exception that no entity body is returned. The last-modified header is set as appropriate and the media type is also returned. 4.6 Other Class
|
Sign in to add a comment
With Restlet 1.1, you will be able to write directly:
That will internally translate to:
Best, Jerome