About today

Node.js Modeshape REST Client

· by ogiron · Read in about 2 min · (409 Words)
java JavasScript JCR Modeshape Node.js

Modeshape

Modeshape is an hierarchical data store written in Java and is part of the JBoss portfolio . ModeShape clients are based on JSR 283 also known as JCR (Java Content Repository). There’s another popular implementation of JCR called Jackrabbit, part of the Apache Software Foundation.

Content Repositories such ModeShape are great for data organized in a tree like structure such Content Management Systems, Digital Assets Managers, Document Management Systems, Social Collaboration Systems etc.

Here’s a visualization of the default ModeShape data/content admin tool:

mode-shape-repository

Content Repository Features

  • Content Management (add/edit/delete).
  • Hierarchy and sort order management
  • Query/ search
  • Versioning
  • Access Control
  • Workspace management.

This is not a complete list but you can get an idea.

ModeShape’s REST Service

ModeShape provides a REST API that you can use to create and manage content using an HTTP client.

ModeShape Node.js REST Client

ModeShapeRestClient is a Node.js module I wrote which abstracts most of the ModeShape’s REST operations, for example; the creation of a single content node a binary file upload etc.

Examples

Client Initialization

var ModeShapeRestClient = require('modeshape-rest-client');

//creates a new instance of the client
var client = ModeShapeRestClient({
            user: 'admin',
            password: 'admin',
            repository: 'sample',
            workspace: 'default'
        });

You can also provide other parameters such protocol (maybe https), port and host, if you don’t, the client will use predefined defaults.

Creating a new node

var nodeDataToAdd = {
            "jcr:primaryType":"nt:unstructured",
            "testProperty":"testValue",
            "multiValuedProperty":["value1", "value2"],
            "children":{
                "childNode":{
                    "nestedProperty":"nestedValue"
                }
            }
        };

var path = '/test';
var options = { path: path }
client.addNode(options, nodeToAdd, function(err, createdNodeData) {
       console.log(createdNodeData);
});

The “createdNodeData” object should be similar to this:

{
    "self": "http://localhost:8080/modeshape-rest/sample/default/items/test%5b5%5d",
    "up": "http://localhost:8080/modeshape-rest/sample/default/items/",
    "id": "0a461a99-2a97-43b6-9e14-f30cbf01f399",
    "jcr:primaryType": "nt:unstructured",
    "multiValuedProperty": [
        "value1",
        "value2"
    ],
    "testProperty": "testValue",
    "children": {
        "childNode": {
            "self": "http://localhost:8080/modeshape-rest/sample/default/items/test%5b5%5d/childNode",
            "up": "http://localhost:8080/modeshape-rest/sample/default/items/test%5b5%5d",
            "id": "76858e3b-3868-407f-a164-13d3d694d20a"
        }
    }
}

Uploading a Binary File

var fs = require('fs');

//..... client initialization here....
var options = { path: '/testbinaryUpload' };
//create a stream from a file
var nodeStream = fs.createReadStream('binaryTest');
client.uploadBinary(options, nodeStream, function(err , result) {
    console.log(result);
});

Please check additional examples here https://github.com/ottogiron/modeshape-rest-client/blob/master/test/index.js.

ModeShapeRestClient code is available here https://github.com/ottogiron/modeshape-rest-client.

Conclusion

ModeShape allows you to create applications that are centered around content which naturally fits in a hierarchical data structure, a great example of that is a Content Management System (check Magnolia CMS). One of the ModeShape features is a great REST API that you can use in your favorite language with any HTTP client. ModeShapeRestClient is a Node.js module that makes the use of ModeShape REST API much easier.

Comments