<?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>ModeShape JCR RESTful Interface</display-name>
ModeShape provides a pair of ways to connect from remote clients: a WebDAV interface and a RESTful interface. This chapter details the capabilities of both as well as the configuration required to use each.
Although the WebDAV and REST servers are treated separately here, many of the configuration parameters are the same. This is because both share a fair amount of common code and have been designed to be able to be deployed simultaneously on the same server or even within the same web archive.
The WebDAV and REST servers described here exist for easy use, though they may need to be customized and WAR files reassembled to fit your particular application server and configuration. ModeShape's JBoss AS kit is one such customization, with a number of additional components built specifically for the JBoss Application Server environment.
ModeShape provides a WebDAV server interface to its JCR implementation to ease integration with client applications. The WebDAV server maps some of the content nodes (by default, nodes with a primary type of nt:file) to WebDAV resources and the other nodes to WebDAV folders. This allows any WebDAV client to navigate through the content repository to store files in a given location, as well as to create or delete nodes in the repository. The remainder of this section describes how to configure and deploy the WebDAV server.
The ModeShape WebDAV server is deployed as a WAR and configured mostly through its web configuration file (web.xml). Here is an example web configuration that is used for integration testing of the ModeShape WebDAV server along with an explanation of its parts.
<?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>ModeShape JCR RESTful Interface</display-name>
This first section is largely boilerplate and should look familiar to anyone who has deployed a servlet-based application before. The display-name can be customized, of course.
The next stanza configures the repository provider.
<!-- This parameter provides the fully-qualified name of a class that implements the o.m.web.jcr.spi.RepositoryProvider interface. It is required by the ModeShapeJcrDeployer that controls the lifecycle for the ModeShape WebDAV server. --> <context-param> <param-name>org.modeshape.web.jcr.REPOSITORY_PROVIDER</param-name> <param-value>org.modeshape.web.jcr.spi.FactoryRepositoryProvider</param-value> </context-param>
As noted above, this parameter informs the ModeShapeJcrDeployer of the specific repository provider in use. Unless you are using the ModeShape WebDAV server to connect to a different JCR implementation, this should never change. The ModeShape REST server also uses the ModeShapeJcrDeployer to get access to the JCR repository, so the two servlets can be deployed in the same WAR.
Next we configure the ModeShape JcrEngine itself.
<!-- This parameter, specific to the FactoryRepositoryProvider implementation, specifies the name of the configuration file to initialize the repository or repositories. This configuration file must be on the classpath and is given as a classpath-relative directory. --> <context-param> <param-name>org.modeshape.web.jcr.JCR_URL</param-name> <param-value>file:/configRepository.xml</param-value> </context-param>
If you are not familiar with the file format for a JcrEngine configuration file, you can build one programatically with the JcrConfiguration class and call save(...) instead of build() to output the configuration file that equates to the configuration.
The ContentMapper implementation can also be configured, but this is optional.
<!-- This parameter provides the fully-qualified name of a class that implements the o.m.w.jcr.webdav.ContentMapper interface. If no value is provided for this parameter, o.m.w.jcr.webdav.DefaultContentMapper will be used. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.CONTENT_MAPPER_CLASS_NAME</param-name> <param-value>org.modeshape.web.jcr.webdav.DefaultContentMapper</param-value> </context-param>
This class is used to prepare WebDAV responses from content nodes. The DefaultContentMapper implementation creates nodes with type nt:folder and nt:file for WebDAV requests to create WebDAV folders and files, respectively. Users can provide their own implementation that maps WebDAV content to other node content or structures.
This is followed by some additional WebDAV configuration that controls the mapping between JCR node types and WebDAV files and resources. These parameters are all specific to the DefaultContentMapper implementation. You can omit this section entirely to use the default values or if a custom ContentMapper is used.
<!-- Nodes with any of the primary node types in this comma-delimited list will be treated by the WebDAV implementation as content nodes. The value below is the default value for this parameter. That is, if this init parameter is omitted, the value below will be used by default. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.CONTENT_PRIMARY_TYPE_NAMES</param-name> <param-value>nt:resource, mode:resource</param-value> </context-param> <!-- Nodes with any of the primary node types in this comma-delimited list will be treated by the WebDAV implementation as resource (file) nodes. The value below is the default value for this parameter. That is, if this init parameter is omitted, the value below will be used by default. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.RESOURCE_PRIMARY_TYPE_NAMES</param-name> <param-value>nt:file</param-value> </context-param> <!-- Each folder created through the WebDAV servlet will be created as a node with the primary node type below. The value below is the default value for this parameter. That is, if this init parameter is omitted, the value below will be used by default. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.NEW_FOLDER_PRIMARY_TYPE_NAME</param-name> <param-value>nt:folder</param-value> </context-param> <!-- Each resource (file created through the WebDAV servlet will be created as a node with the primary node type below. The value below is the default value for this parameter. That is, if this init parameter is omitted, the value below will be used by default. --> <context-param> <param-name> org.modeshape.web.jcr.webdav.NEW_RESOURCE_PRIMARY_TYPE_NAME </param-name> <param-value>nt:file</param-value> </context-param> <!-- Content created through the WebDAV servlet will be created as a node with the primary node type below. The value below is the default value for this parameter. That is, if this init parameter is omitted, the value below will be used by default. --> <context-param> <param-name> org.modeshape.web.jcr.webdav.NEW_CONTENT_PRIMARY_TYPE_NAME </param-name> <param-value>nt:resource</param-value> </context-param>
In general, this part of the web configuration file should not be modified.
Next, the RequestResolver must be configured. The RequestResolver converts the incoming URI into a repository name, workspace name, and path within the repository. ModeShape provides several implementations:
MultiRepositoryRequestResolver- supports multiple repositories and workspaces, by using a URI format with repository name and workspace name as the first two levels of the URI. This was added in ModeShape 2.3.0.Final, and is now the resolver that is configured by default.
SingleRepositoryRequestResolver- maps URIs onto a single repository and workspace that are configured in the web.xml. This is useful if you want to limit which repository and workspace is exposed via WebDAV.
DefaultRequestResolver- maps URIs onto a single repository and workspace that are configured in the web.xml. This used to be the default resolver, and is identical to SingleRepositoryRequestResolver. However, it is now deprecated and will be removed in a future version.
If none of these fit your needs, it is easy to develop a custom implementation of this interface.
To specify the resolver, set the org.modeshape.web.jcr.webdav.REQUEST_RESOLVER_CLASS_NAME property to the name of the implementation class. For example, here is how the MultiRepositoryRequestResolver class is specified:
<!-- This optional parameter provides the name of the o.m.w.j.webdav.RequestResolver implementation class. The provided value must be the name of a class that implements the RequestResolver interface and has a public, no-arg constructor. If no value is provided, o.m.w.j.webdav.MultiRepositoryRequestResolver will be used. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.REQUEST_RESOLVER_CLASS_NAME</param-name> <param-value>org.modeshape.web.jcr.webdav.MultiRepositoryRequestResolver</param-value> </context-param>
Alternatively, if the SingleRepositoryRequestResolver class is to be used, then two additional properties must define the repository name and workspace name:
<!-- This optional parameter provides the name of the o.m.w.j.webdav.RequestResolver implementation class. The provided value must be the name of a class that implements the RequestResolver interface and has a public, no-arg constructor. --> <context-param> <param-name>org.modeshape.web.jcr.webdav.REQUEST_RESOLVER_CLASS_NAME</param-name> <param-value>org.modeshape.web.jcr.webdav.SingleRepositoryRequestResolver</param-value> </context-param> <!-- This parameter is required if (and only if) the SingleRequestResolver is used. It provides the name of the JCR repository that will be accessed. An exception will be thrown if no value is provided for this parameter. --> <context-param> <param-name> org.modeshape.web.jcr.webdav.SINGLE_REPOSITORY_RESOLVER_REPOSITORY_NAME </param-name> <param-value>repository</param-value> </context-param> <!-- This parameter is required if (and only if) the SingleRequestResolver is used. It provides the name of the JCR workspace that will be accessed. An exception will be thrown if no value is provided for this parameter. --> <context-param> <param-name> org.modeshape.web.jcr.webdav.SINGLE_REPOSITORY_RESOLVER_WORKSPACE_NAME </param-name> <param-value>default</param-value> </context-param>
ModeShape also provides the older DefaultRequestResolver class is to be used, which is now deprecated. Please switch use the SingleRepositoryRequestResolver or MultiRepositoryRequestResolver classes. This class is provided for backward compatibility.
Once the RequestResolver has been specified, then more brief boilerplate ensues defines additional configuration information:
<!-- Required parameter for ModeShape WebDAV - should not be modified --> <listener> <listener-class>org.modeshape.web.jcr.ModeShapeJcrDeployer</listener-class> </listener> <!-- Required WebDAV servlet - should not be modified --> <servlet> <servlet-name>WebDAV</servlet-name> <servlet-class>org.modeshape.web.jcr.webdav.ModeShapeWebdavServlet</servlet-class> <!-- The webdav library requires this parameter to be present, but does not use it. --> <init-param> <param-name>rootpath</param-name> <param-value>.</param-value> </init-param> </servlet> <!-- Required parameter for ModeShape WebDAV - should not be modified --> <servlet-mapping> <servlet-name>WebDAV</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Finally, security must be configured for the WebDAV server.
<!-- The ModeShape WebDAV implementation leverages the HTTP credentials to for authentication and authorization within the JCR repository. Unless the repository provides for anonymous access, it makes no sense to try to log into the JCR repository without credentials, so this constraint helps lock down the repository. This should generally not be modified. --> <security-constraint> <display-name>ModeShape WebDAV</display-name> <web-resource-collection> <web-resource-name>WebDAV</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <!-- A user must be assigned this role to connect to any JCR repository, in addition to needing the READONLY or READWRITE roles to actually read or modify the data. This is not used internally, so another role could be substituted here. --> <role-name>connect</role-name> </auth-constraint> </security-constraint> <!-- Any auth-method will work for ModeShape. BASIC is used this example for simplicity. --> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- This must match the role-name in the auth-constraint above. --> <security-role> <role-name>connect</role-name> </security-role> </web-app>
As noted above, the WebDAV server will not function properly unless security is configured. All authorization methods supported by the Servlet specification are supported by ModeShape and can be used interchangeable, as long as authenticated users have the connect role listed above.
Deploying the ModeShape WebDAV server only requires three steps: preparing the web configuration, configuring the users and their roles in your web container (outside the scope of this document), and assembling the WAR. This section describes the requirements for assembling the WAR.
If you are using Maven to build your projects, the WAR can be built from a POM. Here is a portion of the POM used to build the ModeShape WebDAV Server integration subproject.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>modeshape</artifactId> <groupId>org.modeshape</groupId> <version>2.0</version> <relativePath>../..</relativePath> </parent> <artifactId>modeshape-web-jcr-webdav-war</artifactId> <packaging>war</packaging> <name>ModeShape JCR WebDAV Servlet</name> <description>ModeShape servlet that provides WebDAV access to JCR items</description> <url>http://www.modeshape.org</url> <dependencies> <dependency> <groupId>org.modeshape</groupId> <artifactId>modeshape-web-jcr-webdav</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
If you use this approach, make sure that web configuration file is in the /src/main/webapp/WEB-INF directory.
Of course, the JBoss WebDAV Server WAR can still be built if you are not using Maven. Simply construct a WAR with the following contents:
+ /WEB-INF + /classes | + configRepository.xml | + log4j.properties (Optional) + /lib | + aperture-1.1.0.Beta1.jar | + hamcrest-core-1.1.jar | + jakarta-regexp-1.4.jar | + jcr-2.0.jar | + joda-time-1.6.jar | + junit-dep-4.4.jar | + lucene-analyzers-3.0.2.jar | + lucene-core-3.0.2.jar | + lucene-regex-3.0.2.jar | + lucene-snowball-3.0.2.jar | + lucene-misc-3.0.2.jar | + modeshape-cnd-2.7.0.Final.jar | + modeshape-common-2.7.0.Final.jar | + modeshape-graph-2.7.0.Final.jar | + modeshape-jcr-2.7.0.Final.jar | + modeshape-jcr-api-2.7.0.Final.jar | + modeshape-mimetype-detector-aperture-2.7.0.Final.jar | + modeshape-repository-2.7.0.Final.jar | + modeshape-search-lucene-2.7.0.Final.jar | + modeshape-web-jcr-2.7.0.Final.jar | + modeshape-web-jcr-webdav-2.7.0.Final.jar | + rdf2go.api-4.6.2.jar | + slf4j-api-1.6.1.jar | + slf4j-log4j12-1.6.1.jar | + stax-api-1.0-2.jar | + webdav-servlet-2.0.1.jar + web.xml
Your servlet container may already provide a logging system, and you may need to remove the "slf4j-log4j12-1.5.8.jar" and replace with the appropriate SLF4J binding jar. Or, if your servlet container already uses SLF4J globally, you may want to remove all of the "slf4j*.jar" files.
This WAR can be deployed into your servlet container.
ModeShape provides a RESTful interface to its JCR implementation that allows HTTP-based access and updating of content. Although the initial version of this REST server only supports the ModeShape JCR implementation, it has been designed to make integration with other JCR implementors easy. This section describes how to configure and deploy the REST server.
The REST Server currently supports the URIs and HTTP methods described below. The URI patterns assume that the REST server is deployed at its conventional location of "/resources". These URI patterns would change if the REST server were deployed under a different web context and URI patterns below would change accordingly.
The JBoss AS kit by default will deploy the RESTful service at the "/modeshape-rest" location, which is more descriptive and better fits with the other deployed applications and services. To use these examples against this RESTful service, simply replace "/resources" with "/modeshape-rest" in each of the URLs.
Currently, only JSON-encoded responses are provided.
URI Pattern |
HTTP Method(s) |
HTTP Description |
/resources |
Returns a list of accessible repositories |
GET |
/resources/{repositoryName} |
Returns a list of accessible workspaces within that repository |
GET |
/resources/{repositoryName}/{workspaceName} |
Returns a list of available operations within the workspace |
GET |
/resources/{repositoryName}/{workspaceName}/item/{path} |
Accesses the item (node or property) at the path |
GET, POST, PUT, DELETE |
/resources/{repositoryName}/{workspaceName}/query |
Executes the query in the request body |
POST |
Supported URIs for the ModeShape REST Server
Note that this approach supports dynamic discovery of the available repositories on the server. A typical conversation might start with a request to the server to check the available repositories.
GET http://www.example.com/resources Content-Type: application/json
The "Content-Type HTTP header should be supplied as shown above, or it can be left out. However, other values for the "Content-Type" are not allowed and will result in an error. This header is not shown in the remaining examples for simplicity.
This request would generate a response that mapped the names of the available repositories to metadata information about the repositories like so:
{ "modeshape%3arepository" : { "repository" : { "name" : "modeshape%3arepository", "resources" : { "workspaces":"/resources/modeshape%3arepository" } "metadata" : { "jcr.specification.name" : "Content Repository for Java Technology API", "jcr.specification.version" : "2.0", "jcr.repository.name" : "ModeShape JCR Repository", "jcr.repository.vendor.url" : "http://www.modeshape.org", "jcr.repository.version" : "2.6.0.FINAL", "option.versioning.supported" : "true", ... etc. ... } } } } }
Also, the "metadata" section is included only in responses from RESTful services starting with the version 2.5.0.Final release, and contains the JCR descriptors keys and values, where each value will either be a string or, if there are multiple values for the descriptor, an array of strings. Note not all the descriptors are shown in the above example.
The only thing that you can do with a repository through the REST interface at this time is to get a list of its workspaces. A request to do so can be built up from the previous response like this:
GET http://www.example.com/resources/modeshape%3arepository
{ "default" : { "workspace" : { "name" : "default", "resources" : { "items":"/resources/modeshape%3arepository/default/items", "query":"/resources/modeshape%3arepository/default/query" }, } } }
Now a request can be built to retrieve the root item of the repository.
GET http://www.example.com/resources/modeshape%3arepository/default/items
{ "properties": { "jcr:primaryType": "mode:root", "jcr:uuid": "97d7e2ef-996e-4d99-8ec2-dc623e6c2239" }, "children": ["jcr:system"]
The items resource also contains an option query parameter: mode:depth. This parameter, which defaults to 1, controls how deep the hierarchy of returned nodes should be. Had the request had the parameter:
GET http://www.example.com/resources/modeshape%3arepository/default/items?mode:depth=2
{ "properties": { "jcr:primaryType": "mode:root", "jcr:uuid": "163bc5e5-3b57-4e63-b2ae-ededf43d3445" }, "children": { "jcr:system": { "properties": {"jcr:primaryType": "mode:system"}, "children": ["mode:namespaces"] } } }
It is also possible to use the RESTful API to add, modify and remove repository content. Removes are simple - a DELETE request with no body returns a response with no body.
DELETE http://www.example.com/resources/modeshape%3arepository/default/items/path/to/deletedNode
Adding content simply requires a POST to the name of the relative root node of the content that you wish to add and a request body in the same format as the response from a GET. Adding multiple nodes at once is supported, as shown below.
POST http://www.example.com/resources/modeshape%3arepository/default/items/newNode { "properties": { "jcr:primaryType": "nt:unstructured", "jcr:mixinTypes": "mix:referenceable", "someProperty": "foo" }, "children": { "newChildNode": { "properties": {"jcr:primaryType": "nt:unstructured"} } } }
If you do not need this information, add mode:includeNode=false as a query parameter to your URL.
POST http://www.example.com/resources/modeshape%3arepository/default/items/newNode?mode:includeNode=false { "properties": { "jcr:primaryType": "nt:unstructured", "jcr:mixinTypes": "mix:referenceable", "someProperty": "foo" }, "children": { "newChildNode": { "properties": {"jcr:primaryType": "nt:unstructured"} } } }
The PUT method allows for updates of nodes and properties. If the URI points to a property, the body of the request should be the new JSON-encoded value for the property, which includes the property name (allowing proper determination of whether the values are binary; see the next section"").
PUT http://www.example.com/resources/modeshape%3arepository/default/items/some/existing/node/someProperty { "someProperty" : "bar" }
PUT http://www.example.com/resources/modeshape%3arepository/default/items/some/existing/node { "someProperty": "foobar", "someOtherProperty": "newValue" }
PUT http://www.example.com/resources/modeshape%3arepository/default/items/some/existing/node { "properties": { "someProperty": "foobar", "someOtherProperty": "newValue" } }
A subgraph can be updated all at once using a PUT against a URI of the top node in the subgraph. Note that in this case, very node in the subgraph must be provided in the JSON request (any node not in the request will be removed). This method will attempt to set all of the properties to the new value(s) as specified in the JSON request, plus any descendant node in the JSON request that doesn't reflect an existing node will be created while any existing node not reflected in the JSON request will be removed. (Any specifications of "jcr:primaryType" are ignored if the node already exists.) In other words, the request only needs to contain the properties that are changed. Of course, if a node is being added, all of its properties need to be included in the request.
Here is an example:
PUT http://www.example.com/resources/modeshape%3arepository/default/items/some/existing/node { "properties": { "jcr:primaryType": "nt:unstructured", "jcr:mixinTypes": "mix:referenceable", "someProperty": "foo" }, "children": { "childNode": { "properties": {"jcr:primaryType": "nt:unstructured"} } } }
Queries can be executed through the REST interface by POSTing to the query URI with the query statement in the body of the request. The query language must be specified by setting the appropriate MIME type.
Query Language |
Content Type |
XPath |
application/jcr+xpath |
JCR-SQL |
application/jcr+sql |
JCR-SQL2 |
application/jcr+sql2 |
Full Text Search |
application/jcr+search |
Query Content Types for the ModeShape REST Server
If no content type is specified or the content type for the request is not one of the content types listed above, the request will generate a response code of 400 (BAD REQUEST).
All queries for a given workspace are posted to the same URI and the request body is not JSON-encoded.
POST http://www.example.com/resources/modeshape%3arepository/default/query /a/b/c/d[@foo='bar']
{ "types": { "someProperty": "STRING", "someOtherProperty": "BOOLEAN", "jcr:path": "STRING", "jcr:score": "DECIMAL" }, "rows": { { "someProperty": "foobar", "someOtherProperty": "true", "jcr:path" : "/a/b/c/d", "jcr:score" : 0.9327 }, { "someProperty": "localValue", "someOtherProperty": "false", "jcr:path" : "/a/b/c/d[2]", "jcr:score" : 0.8143 } } }
If ModeShape is used as the underlying JCR implementation, the JSON object in the response will also contain a "types" property. The value of the "types" property is a JSON object that maps column names to their JCR type.
Binary property values are included in any of the the responses or requests, but are represented string values containing the Base 64 encoding of the binary content. Any such property is explicitly annotated such that "/base64/" is appended to the property name. First of all, this makes it very clear to the client and service which properties are encoded, allowing them to properly decode the values before use. Secondly, the "/base64/" suffix was carefully chosen because it cannot be used in a real property name (without escaping). Here's an example of a node containing a "jcr:primaryType" property with a single string value, a "jcr:uuid" property with another single UUID value, another "options" property that has two integer values, and a fourth "content" property that has a single binary value:
{ "properties": { "jcr:primaryType": "nt:unstructured", "jcr:uuid": "163bc5e5-3b57-4e63-b2ae-ededf43d3445" "options": [ "1", "2" ] "content/base64/": "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" }, }
The ModeShape REST server is deployed as a WAR and configured mostly through its web configuration file (web.xml). Here is an example web configuration that is used for integration testing of the ModeShape REST server along with an explanation of its parts.
<?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>ModeShape JCR RESTful Interface</display-name>
This first section is largely boilerplate and should look familiar to anyone who has deployed a servlet-based application before. The display-name can be customized, of course.
The next stanza configures the repository provider.
<!-- This parameter provides the fully-qualified name of a class that implements the o.m.web.jcr.spi.RepositoryProvider interface. It is required by the ModeShapeJcrDeployer that controls the lifecycle for the ModeShape REST server. --> <context-param> <param-name>org.modeshape.web.jcr.REPOSITORY_PROVIDER</param-name> <param-value>org.modeshape.web.jcr.spi.FactoryRepositoryProvider</param-value> </context-param>
As noted above, this parameter informs the ModeShapeJcrDeployer of the specific repository provider in use. Unless you are using the ModeShape REST server to connect to a different JCR implementation, this should never change.
Next we configure the ModeShape JcrEngine itself.
<!-- This parameter, specific to the FactoryRepositoryProvider implementation, specifies the name of the configuration file to initialize the repository or repositories. This configuration file must be on the classpath and is given as a classpath-relative directory. --> <context-param> <param-name>org.modeshape.web.jcr.JCR_URL</param-name> <param-value>file:/configRepository.xml</param-value> </context-param>
If you are not familiar with the file format for a JcrEngine configuration file, you can build one programatically with the JcrConfiguration class and call save(...) instead of build() to output the configuration file that equates to the configuration.
This is followed by a bit of RESTEasy and JAX-RS boilerplate.
<!-- This parameter defines the JAX-RS application class, which is really just a metadata class that lets the JAX-RS engine (RESTEasy in this case) know which classes implement pieces of the JAX-RS specification like exception handling and resource serving. This should not be modified. --> <context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.modeshape.web.jcr.rest.JcrApplication</param-value> </context-param> <!-- Required parameter for RESTEasy - should not be modified --> <listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <!-- Required parameter for ModeShape REST - should not be modified --> <listener> <listener-class>org.modeshape.web.jcr.ModeShapeJcrDeployer</listener-class> </listener> <!-- Required parameter for RESTEasy - should not be modified --> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <!-- Required parameter for ModeShape REST - should not be modified --> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
In general, this part of the web configuration file should not be modified.
Finally, security must be configured for the REST server.
<!-- The ModeShape REST implementation leverages the HTTP credentials to for authentication and authorization within the JCR repository. It makes no sense to try to log into the JCR repository without credentials, so this constraint helps lock down the repository. This should generally not be modified. --> <security-constraint> <display-name>ModeShape REST</display-name> <web-resource-collection> <web-resource-name>RestEasy</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <auth-constraint> <!-- A user must be assigned this role to connect to any JCR repository, in addition to needing the READONLY or READWRITE roles to actually read or modify the data. This is not used internally, so another role could be substituted here. --> <role-name>connect</role-name> </auth-constraint> </security-constraint> <!-- Any auth-method will work for ModeShape. BASIC is used this example for simplicity. --> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- This must match the role-name in the auth-constraint above. --> <security-role> <role-name>connect</role-name> </security-role> </web-app>
As noted above, the REST server will not function properly unless security is configured. All authorization methods supported by the Servlet specification are supported by ModeShape and can be used interchangeable, as long as authenticated users have the connect role listed above.
Just as with the ModeShape WebDAV server, deploying the ModeShape REST server only requires three steps: preparing the web configuration, configuring the users and their roles in your web container (outside the scope of this document), and assembling the WAR. This section describes the requirements for assembling the WAR.
If you are using Maven to build your projects, the WAR can be built from a POM. Here is a portion of the POM used to build the ModeShape REST Server integration subproject.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>modeshape</artifactId> <groupId>org.modeshape</groupId> <version>2.0</version> <relativePath>../..</relativePath> </parent> <artifactId>modeshape-web-jcr-rest-war</artifactId> <packaging>war</packaging> <name>ModeShape JCR REST Servlet</name> <description>ModeShape servlet that provides RESTful access to JCR items</description> <url>http://www.modeshape.org</url> <dependencies> <dependency> <groupId>org.modeshape</groupId> <artifactId>modeshape-web-jcr-rest</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>1.2.1.GA</version> </dependency> </dependencies> </project>
If you use this approach, make sure that web configuration file is in the /src/main/webapp/WEB-INF directory.
The JBoss REST Server WAR is still easy enough to build if you are not using Maven. Simply construct a WAR with the following contents:
+ /WEB-INF + /classes | + configRepository.xml | + log4j.properties (Optional) + /lib | + activation-1.1.jar | + commons-codec-1.2.jar | + commons-httpclient-3.1.jar | + hamcrest-core-1.1.jar | + httpclient-4.0.jar | + httpcore-4.0.1.jar | + jakarta-regexp-1.4.jar | + javassist-3.6.0.GA.jar | + jaxb-api-2.1.jar | + jaxb-impl-2.1.12.jar | + jaxrs-api-1.2.1.GA.jar | + jcl-over-slf4j-1.5.8.jar | + jcr-2.0.jar | + jettison-1.1.jar | + joda-time-1.6.jar | + jsr250-api-1.0.jar | + junit-dep-4.4.jar | + lucene-analyzers-3.0.0.jar | + lucene-core-3.0.0.jar | + lucene-regex-3.0.0.jar | + lucene-snowball-3.0.0.jar | + modeshape-cnd-2.7.0.Final.jar | + modeshape-common-2.7.0.Final.jar | + modeshape-graph-2.7.0.Final.jar | + modeshape-jcr-2.7.0.Final.jar | + modeshape-jcr-api-2.7.0.Final.jar | + modeshape-repository-2.7.0.Final.jar | + modeshape-search-lucene-2.7.0.Final.jar | + modeshape-web-jcr-2.7.0.Final.jar | + modeshape-web-jcr-rest-2.7.0.Final.jar | + resteasy-jaxb-provider-1.2.1.GA.jar | + resteasy-jaxrs-1.2.1.GA.jar | + resteasy-jettison-provider-1.2.1.GA.jar | + scannotation-1.0.2.jar | + sjsxp-1.0.1.jar | + slf4j-api-1.6.1.jar | + slf4j-log4j12-1.6.1.jar | + slf4j-simple-1.5.8.jar | + stax-api-1.0-2.jar + web.xml
Your servlet container may already provide a logging system, and you may need to remove the "slf4j-log4j12-1.5.8.jar" and replace with the appropriate SLF4J binding jar. Or, if your servlet container already uses SLF4J globally, you may want to remove all of the "slf4j*.jar" files.
This WAR can be deployed into your servlet container.
The ModeShape REST Client API provides a POJO way of using the ModeShape REST web service to publish (upload) and unpublish (delete) files from ModeShape repositories. Java objects open the HTTP connection, create the HTTP request URLs, attach the payload associated with PUT and POST requests, parse the HTTP JSON response back into Java objects, and close the HTTP connection.
Here are the Java business objects you will need (all found in the org.modeshape.web.jcr.rest.client.domain package):
Server- hosts one or more ModeShape JCR repositories,
Repository- a ModeShape JCR repository containing one or more workspaces, and
Workspace- a ModeShape JCR repository workspace.
Along with the POJOs above, an org.modeshape.web.jcr.rest.client.IRestClient is needed. The IRestClient is responsible for executing the publishing and unpublishing operations. You can also use the IRestClient to find out what repositories and workspaces are available on a ModeShape server.
The only implementation of IRestClient is JsonRestClient as JSON-encoded responses are all that are currently available.
Here's a code snippet that publishes (uploads) a file:
// Setup POJOs Server server = new Server("http://localhost:8080", "username", "password"); Repository repository = new Repository("repositoryName", server); Workspace workspace = new Workspace("workspaceName", repository); // Publish File file = new File("/path/to/file"); IRestClient restClient = new JsonRestClient(); Status status = restClient.publish(workspace, "/workspace/path/", file); if (status.isError() { // Handle error here }
Successfully executing the above code results in the creation a JCR folder node (nt:folder) for each segment of the workspace path (if the folder didn't already exist). Also, a JCR file node (a node with primary type nt:file) is created or updated under the last folder node and the file contents are encoded and uploaded into a child node of that file node.
Both the ModeShape REST server and the ModeShape WebDAV server can also be used as an interface to to other JCR repositories by creating an implementation of the RepositoryProvider interface that connects to the other repository.
The RepositoryProvider only has a few methods that must be implemented. When the ModeShapeJcrDeployer starts up, it will dynamically load the RepositoryProvider implementation (as noted above) and call the startup(ServletContext) method on the provider. The provider can use this method to load any required configuration parameters from the web configuration (web.xml) and initialize the repository.
As an example, here's the ModeShape JCR provider implementation of this method with exception handling omitted for brevity.
public void startup( ServletContext context ) { String configFile = context.getInitParameter(CONFIG_FILE); InputStream configFileInputStream = getClass().getResourceAsStream(configFile); jcrEngine = new JcrConfiguration().loadFrom(configFileInputStream).build(); jcrEngine.start(); }
The first method returns the set of repository names supported by this repository.
public Set<String> getJcrRepositoryNames() { return new HashSet<String>(jcrEngine.getRepositoryNames()); }
The other required method returns an open JCR Session for the user from the current request in a given repository and workspace. The provider can use the HttpServletRequest to get the authentication credentials for the HTTP user.
public Session getSession( HttpServletRequest request, String repositoryName, String workspaceName ) throws RepositoryException { Repository repository = getRepository(repositoryName); SecurityContext context = new ServletSecurityContext(request); Credentials credentials = new SecurityContextCredentials(context); return repository.login(credentials, workspaceName); }
Finally, the shutdown() method signals that the web context is being undeployed and the JCR repository should shutdown and clean up any resources that are in use.
This chapter has described two ways to access a ModeShape JCR repository remotely through HTTP-based protocols. In the next chapter, the different repository connectors will be described so that you can start to use ModeShape to store new data, connect to existing data through JCR, or both.