WSO2 Registry - RemoteRegistry

Accessing Registry Remotely through an API - RemoteRegistry

In simple words, the RemoteRegistry is a Java API for interacting with a Registry instance. Regardless of where a given Registry instance is located, either locally or remotely, we can still talk to it using RemoteRegistry. The RemoteRegistry is yet another implementation of org.wso2.registry.Registry interface. However, the difference between the RemoteRegistry and a standard Registry is that, the RemoteRegistry is not an actual registry but a client for interacting with a registry instance.

Communication between the Registry and the RemoteRegistry takes place using the Atom Publishing Protocol (APP). When we start the registry server, an atom feed is generated . Thereafter, if you browse https://localhost:9443/registry/atom/ (this address changes depending on your application server settings), you'd be able to see the atom feed from the registry. The structure and format of the feed is described in the following location: http://www.wso2.org/wiki/display/registry/Registry+Protocol.

How to Create an Instance of the RemoteRegistry

To create an instance of the RemoteRegistry, what we need first is the URL of a registry. If the root of the registry is http://foo.com/, the URL for the RemoteRegistry instance would be http://foo.com/registry/atom. Once we have the URL figured out, we can create an instance of the RemoteRegistry using the following code:

System.setProperty("javax.net.ssl.trustStore", "CARBON_HOME/resources/security/client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
System.setProperty("javax.net.ssl.trustStoreType","JKS");
RemoteRegistry remote_registry = new RemoteRegistry(new URL("https://localhost:9443/registry"));

Creating an instance of the RemoteRegistry in this manner, is similar to accessing a registry without login into it with communication taking place as an anonymous user.

If you have a user name and password for the registry, you can pass them along when creating the remote_registry instance. At that point, all permissions granted for the particular username/password pair will apply to the owner of the remote_registry instance. Creating the remote_registry with user name and the password can be done using:

RemoteRegistry remote_registry = new RemoteRegistry(new URL("https://localhost:9443/registry"), "admin", "admin");
      
In this example, we have created the remote_registry instance with username="admin" and the password as also "admin".

Using the Registry API

Reading a resource

Once we have the registry instance in place (embedded registry or remote registry), navigation is straightforward. It is the same as working with the Registry API. Say we have a resource called "/foo/c1" in the Registry and you can access it using the registry instance as given below:

Resource resource = registry.get("/foo/c1");

The resource object will represent the actual resource object in the Registry. But if the resource is a collection, then the object will, of course, represent the collection.

As you can see in the code sample above, once we have a registry instance we do not need to pass a complete URL for all invocations. It is adequate to pass only a relative path to a resource.

Adding a resource

To add a resource to the registry instance, first thing we need to do is to create a Resource object and then to call its put method:

First let's try to cerate a collection called "/c1/c2"

Collection collection = registry.newCollection();   
  
registry.put("/c1/c2", collection);

If you call the get method then you'd be able to access that resource created. Now let's try to add a resource with content. For that, we need to first create a Resource object and then set content.

Resource r1 = registry.newResource();
 
String str = "My File Content";

r1.setContent(str.getBytes());

registry.put("/c1/c2/r1", r1);

Checking for the Existence of a Resource

We can use the following code to confirm whether the resource exists:

boolean value = registry.resourceExists("/c1/c2/r1");

If the resource does exist, a boolean value of true will be returned, else false.

Deleting a Resource

Additionally, we can use the registry instance to delete resources. Deleting a resource is a matter of calling the delete method of the registry. Let's just say we want to delete "/c1/c2/r1". Then we can use the following code:

registry.delete("/c1/c2/r1");

Renaming a resource

We can rename individual resources or collections. To rename a resource, use the following lines of code. Same code can be used to rename a collection as well.

registry.rename("/c1/c2/r1", "/c1/c2/r2");


Above line of code renames "/c1/c2/r1" to "/c1/c2/r2".

Creating versions of resources/collections

Resources are automatically versioned when they are added or updated. But collections are not versioned automatically due to performance considerations. However, you can create versions of collections using the API. It is recommended that collection versions are created only for making checkpoints of the subtree you are working on.

registry.createVersion("/c1/c2");

Retrieving different Versions of a Given Resource

We can list all of the versions of a given resource using the code given below. The result would be an array of String, containing links to the different versions of the resource.

String [] versions = registry.getVersions("/c1/c2");

Restoring to an old version

Since the Registry comes with versioning, we can restore a resource to any of its versions. This can be done using the registry as well. In the previous section, we discussed how to retrieve versions for a give resource. The following line of code demonstrates we can restore back an old version of the registry instance.

registry.restoreVersion ("/c1/c2;version=2");

Adding a Tag

We can perform tagging operations using the registry. To tag a resource, we need the resource path and the tagging words. Let's say we need to tag a resource named "/c1/c2/r2" as "rename resource". We can do this as:

registry.applyTag("/c1/c2/r2" , "rename resource");

Retrieving All Tags of a Given Resource

We can use the registry to retrieve tags for a give resource. It will return an array of Tag type and we can iterate the array to see the content.

Tag[] tags = registry.getTags("/c1/c2/r2");

Deleting a Tag

We can remove a tag using the tag name. Use the following code:

registry.removeTag("/c1/c2/r2","rename resource");

Commenting a Resource

We can also comment on a resource using the registry. Here, we will create a comment object and call the registry instance. Following lines of code illustrates how we can achieve this:

Comment c1 = new Comment();

c1.setText("This is my comment");
 
String commentpath = registry.addComment("/c1/c2/r2", c1), c1);

The above lines of code will add a comment to the resource named: "c1/c2/r2 ".

Edit a Comment

We can also make changes to comments we have already made using the registry instance. Here, we need the path and new text for the comment we are adding. Say we want to change from "This is my comment" to "This is cool", For that, we can do the following:

registry.editComment(commentpath,"This is cool");

Rating a Resource

In order to rate a resource based on our judgment, we can again use the registry instance. Rating a resource can be done using following line of code:

registry.rateResource("c1/c2/r2 " , 4);


In addition to the above operations, there are a number of others that we can perform using the registry instance. In this document we have discussed only the most commonly used operations.

Importing the Local File System to the Registry

We can use the RemoteRegistry to export our entire local file system into the Registry. What we need to do is to give the location of the file system and the location where we want to put them in the Registry. Once we do that, inside the registry it will create the same structure as the file system and upload all files into the Registry. We can consider this as any check-in operation on any kind of version management system. Following code demonstrates how to import a local file system into the Registry:

File file = new File("Path of the file");

RemoteRegistry remote_registry = new RemoteRegistry(new URL("http://localhost:9443/registry"), "admin", "admin");

RegistryClientUtils.importToRegistry(file ,"/myfile/filesystem" ,remote_registry);

Exporting the Registry to a File System

In the previous section, we discussed how to import a local file system into the Registry. There is another way round of this and it is to check-out the Registry to a local file system. To export, we can select either the entire Registry or a selected node. Depending on the node we select, the remote_registry instance will create the same structure within the file system. Even if we have the resource with binary data, then it will create all of the necessary files in the file system.

Let's say we want to export "/myfile/filesystem" into my local file system then we can use the following code:

File toFile = new File("Path of the new file");

RemoteRegistry remote_registry = new RemoteRegistry(new URL("http://localhost:9443/registry"), "admin", "admin");

RegistryClientUtils.importToRegistry( toFile, "/myfile/filesystem" ,remote_registry);