Skip to end of metadata
Go to start of metadata

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.

The ModeShape WebDAV Server

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.

Configuring the ModeShape 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.

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.

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.

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 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.

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:

Alternatively, if the SingleRepositoryRequestResolver class is to be used, then two additional properties must define the repository name and workspace name:

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:

Finally, security must be configured for the WebDAV server.

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

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.

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:


If you are using sequencers or any connectors other than the in-memory or federated connector, you will also have to add the JARs for those dependencies into the WEB-INF/lib directory as well. You will also have to change the version numbers on the JARs to reflect the current version of ModeShape.

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 Server

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.

Supported Resources and Methods

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.

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:


The actual response wouldn't be pretty-printed like the example, but the format would be the same. The name of the repository ("repository" URL-encoded) is mapped to a repository object that contains a name (the redundant "repository") and a list of available resources within the repository and their respective URIs. Note that ModeShape supports deploying multiple JCR repositories side-by-side on the same server, so this response could easily contain multiple repositories in a real deployment.

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:


This request (and all of the following requests) actually create a JCR Session to service the request and require that security be configured. This process is described in more detail in a later section. Assuming that security has been properly configured, the response would look something like this:

Like the first response, this response consists of a list of workspace names mapped to metadata about the workspaces. The example above only lists one workspace for simplicity, but there could be many different workspaces returned in a real deployment. Note that the "items" resource builds the full URI to the root of the items hierarchy, including the encoding of the repository name and the workspace name and the "query" resource builds the full URI needed to execute queries.

Now a request can be built to retrieve the root item of the repository.


Any other item in the repository could be accessed by appending its path to the URI above. In a default repository with no content, this would return the following response:

The response contains a mapping of property names to their values and an array of child names. Had one of the properties been multi-valued, the values for that property would have been provided as an array as well, as will shortly be shown.

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:


Then the response would have contained details for the children of the root node as well.

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.

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.


Note that protected properties like jcr:uuid are not provided but that the primary type and mixin types are provided as properties. The REST server will translate these into the appropriate calls behind the scenes. The JSON-encoded response from the request will contain the node that you just posted, including any autocreated properties and child nodes.

If you do not need this information, add mode:includeNode=false as a query parameter to your URL.


This will instruct the REST server to only return the path of the newly-created node in the response.

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"").


Setting multiple properties at once can be performed by providing a URI to a node instead of a property. The body of the request should then be a JSON object that maps property names to their new values.

The JSON request can even contain a properties container:

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:


This will update the existing node at "/some/existing/node" with the specified properties, and ensure that it contains one child node named "childNode". Note that the body of this request is identical in structure to that of the POST requests.

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.


Assuming that the request above was POSTed with a content type of application/jcr+xpath, a response would be generated that consisted of a JSON object that contained a property named "rows". The "rows" property would contain an array of rows with each element being a JSON object that represented one row in the query result set.

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 properties

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:


All values of a property will always be Base 64 encoded if at least one of the values is binary. If there are multiple values, then they will be separated by commas and will appear within '[' and ']' characters (just like other properties).

Configuring the ModeShape REST Server

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.

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.

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.

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.

In general, this part of the web configuration file should not be modified.

Finally, security must be configured for the REST server.

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.

Deploying the ModeShape REST Server

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.

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:


If you are using sequencers or any connectors other than the in-memory or federated connector, you will also have to add the JARs for those dependencies into the WEB-INF/lib directory as well. You will also have to change the version numbers on the JARs to reflect the current version of ModeShape.

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 REST Client API

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:

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.

Repository Providers

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.


As you can see, the name of configuration file for the JcrEngine is read from the servlet context and used to initialize the engine. Once the repository has been started, it is now ready to accept the main methods that provide the interface to the repository.

The first method returns the set of repository names supported by this repository.


The ModeShape JCR repository does support multiple repositories on the same server. Other JCR implementations that don't support multiple repositories are free to return a singleton set containing any string from this method.

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.


The getSession(...) method is used by most of the REST server methods to access the JCR repository and return results as needed.

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.

Summary

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.

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.