Sunday, July 18, 2010

Exploring CMIS: Adding folder and document in alfresco using Http Client

This blog assumes that reader has basic understanding of CMIS. This blog organization is as follows: It starts with creating a folder structure in alfresco CMIS repository using apache client and atom pub binding of repository. Then we will use different discovery services to enquire our newly created folders. Hope you will have good time learning this.
Lets jump in....

First we will start with the code to create a folder name shiv_lib in alfresco. Find below the http client code and sample post xml used.

File input = new File("C:\\Users\\shivendra\\workspace\\ApacheClient\\src\\PostData.xml");
PostMethod post = new PostMethod("http://cmis.alfresco.com/service/cmis/p/children");
RequestEntity entity = new FileRequestEntity(input, "application/atom+xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
httpclient.getState().setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("admin", "admin")
);
try {
int result = httpclient.executeMethod(post);
// Display status code
System.out.println("Response status code: " + result);
System.out.println(post.getResponseBodyAsString());
} finally {
post.releaseConnection();
}

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/"
xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">
<title>Shiv_Lib</title>
<cmisra:object>
<cmis:properties>
<cmis:propertyId propertyDefinitionId="cmis:objectTypeId"><cmis:value>cmis:folder</cmis:value></cmis:propertyId>
</cmis:properties>
</cmisra:object>
</entry>

Notice the post method url(http://cmis.alfresco.com/service/cmis/p/children), since we haven't given any any path after p/{path} so this folder will be created on root folder. Newly created folder can be verified hitting http://cmis.alfresco.com/service/cmis/p/children in IE.

Now lets create some children folder inside this.All you have to do is change the post url and post xml as follows.

PostMethod post = new PostMethod("http://cmis.alfresco.com/service/cmis/p/Shiv_Lib/children");
<title>Meditation</title>
Hit http://cmis.alfresco.com/service/cmis/p/Shiv_Lib/children in IE and you will find out that Meditation folder is created inside Shiv_Lib.

Lets try putting some content in meditation folder. The only thing you have to take care here is cmis:objectTypeId will be cmis:document and the content need to added in document will go as base64 encoded and will be part of atom entry as follows.

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/"
xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">
<title>Suggested Reading11</title>
<summary>Suggested Reading</summary>
<content type="text/plain">
MS4gR2l0YSAKIDIuIEthcm1heW9nYSBieSBWaXZla2FuYW5k
</content>
<cmisra:object>
<cmis:properties>
<cmis:propertyId propertyDefinitionId="cmis:objectTypeId"><cmis:value>cmis:document</cmis:value></cmis:propertyId>
</cmis:properties>
</cmisra:object>
</entry>


Since we want to create this in meditation folder so the URL will be: http://cmis.alfresco.com/service/cmis/p/Shiv_Lib/Meditation/children.

Once you run http client code with above changes, hit http://cmis.alfresco.com/service/cmis/p/shiv_lib/Meditation/children and you should be able to see document added. You can view content of document as plain text.

Next blog in this series I will cover CMIS query and discovery service.

Q and A

1. From where I get the AtomPub services URLs for alfresco.
The answer is alfresco list them at this http://cmis.alfresco.com/service/index/family/CMIS