{docbook}
<highlights>
<itemizedlist>
      <listitem>
        <para>
          <emphasis role="bold">
            <link linkend="Dev_CMIS.Usage_code_examples.Login_to_repository">Login to repository</link>
          </emphasis> </para>
          <para>Example of using Java to login to repository.</para>
      </listitem>
      <listitem>
        <para>
          <emphasis role="bold">
            <link linkend="Dev_CMIS.Usage_code_examples.List_of_documents">List of documents (folder, files)</link>
          </emphasis> </para>
          <para>Description about the usage of several methods to get the documents lists, such as <emphasis>getChildren()</emphasis>, <emphasis>getFolderTree()</emphasis> and <emphasis>getDescentants()</emphasis>.</para>
      </listitem>
      <listitem>
        <para>
          <emphasis role="bold">
            <link linkend="Dev_CMIS.Usage_code_examples.Read_document_properties_contentstream">Read document properties and content-stream</link>
          </emphasis> </para>
          <para>Instructions on how to read and get the document properties and content stream.</para>
      </listitem>
      <listitem>
        <para>
          <emphasis role="bold">
            <link linkend="Dev_CMIS.Usage_code_examples.Search_data_syntax_example">Search of data and syntax examples</link>
          </emphasis> </para>
          <para>Examples of using Java and Javascript to search for data and syntax in CMIS.</para>
      </listitem>
      <listitem>
        <para>
          <emphasis role="bold">
            <link linkend="Dev_CMIS.Usage_code_examples.Modification_of_document_properties_or_content">Modification of document properties or content</link>
          </emphasis> </para>
          <para>Instructions on how to use Java and Javascript to update and get document properties or content in CMIS.</para>
      </listitem>
</itemizedlist>
</highlights>
{docbook}

The examples of the CMIS usage may be useful for developers who need to access a repository.
CMIS access code snippets are built using Apache HTTP Client for Java, or using Google gadgets (gadgets.io) for JavaScript examples.
For examples of CURL, visit http://code.google.com/p/xcmis/wiki/xCMISusesWithCurl.

{docbook}
<para>
<prompt>
<emphasis role="bold">See also</emphasis>
</prompt>
</para>
{docbook}

* [WCM Templates|#CONTref.InsideWCMTemplates]
* [WCM Explorer|#CONTref.InsideWCMExplorer]
* [Extensions|#CONTref.Extensions]
* [Public REST APIs|#CONTref.Devref.PublicRestAPIs]
* [Public Java APIs|#CONTref.JavaServices]
* [Deprecated portlets|#CONTref.Deprecated_portlets]
* [Miscellaneous and Tips|#Adm_CMIS.Miscellaneous_tips]
* [FAQs|#CONTref.FAQ]

h1. Login to repository {anchor:id=Dev_CMIS.Usage_code_examples.Login_to_repositoryRoleNotInToc}

{note}
The CMIS service uses the default authentication in general case, but it can be overridden in case of embedding CMIS into an Application Service.
In these examples, only the Basic HTTP authentication is covered.
{note}

{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Use_java">
<citetitle>
<emphasis role="bold">
Use java
</emphasis>
</citetitle>
</para>
{docbook}

{code:language=java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod; 

HttpClient client = new HttpClient(); 
client.getState().setCredentials(
 new AuthScope("localhost", 8080, "realm"),
 new UsernamePasswordCredentials("root", "exo");
....{code}


h1. List of documents (folder, files) {anchor:id=Dev_CMIS.Usage_code_examples.List_of_documentsRoleNotInToc}

There are several methods to get the documents lists, such as getChildren(), getFolderTree() and getDescentants(), their usage will be described below.
The difference between them is the usage of different URL segments to get data ("/children" for getChildren(), "/foldertree" for getFolderTree(), "/descendants" for getDescentants()),
and a different kind of results (getChildren() returns a flat structure, while a getFolderTree() and getDescentants() have a tree of items in response).

{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.List_of_documents.Use_java">
<citetitle>
<emphasis role="bold">Use Java</emphasis>
</citetitle>
</para>
{docbook}

{code:language=java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;  

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/children/";
url += obj_id;

HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

GetMethod get = new GetMethod(url); 
try {
int result = client.executeMethod(get);
final String strResponse = get.getResponseBodyAsString(); 
} finally {
get.releaseConnection();
}{code}



{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.List_of_document.Use_Javascript">
<citetitle>
<emphasis role="bold">Use JavaScript</emphasis>
</citetitle>
</para>
{docbook}

Creating an URL to make a request (consists of repository name, the method name, for example "/children/", and folderID to get children from):

{code:language=java} var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/children/";
 url += obj_id;{code}

Performing request:

{code:language=java} var params = {};
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
 params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
 gadgets.io.makeRequest(url, handler, params);{code}

Processing results (the code is located in the handler is specified while making a request, the same way it might be used for all examples in this chapter):

{code:language=java}var handler = function(resp) {
  var data = eval(resp.data.Entry);
   for (var i = 0; i < data.length; i++) {
     var doc = data[i];
     alert(doc.Title);  
     alert(doc.Date);  
      ...etc..
   }
}{code}

h1. Read document properties and content-stream {anchor:id=Dev_CMIS.Usage_code_examples.Read_document_properties_contentstreamRoleNotInToc}

Reading the Document properties and content stream are two separate operations.
Getting the content stream is possible after the properties set have been read and the content stream ID was extracted from it.

{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Read_document_properties_contentstream.Use_java">
<citetitle>
<emphasis role="bold">Use Java</emphasis>
</citetitle>
</para>
{docbook}

Get document properties.

{code:language=java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;  

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/object/";
url += obj_id;

HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

GetMethod get = new GetMethod(url); 
try {
int result = client.executeMethod(get);
final String strResponse = get.getResponseBodyAsString(); 
// use response...
} finally {
get.releaseConnection();
}{code}

Get document content-stream.

To get the Document's content stream, an URL must contain "/file" part, object ID, and optionally the content stream ID, which can be used, for example, to obtain
renditions. If no stream ID is specified, the default stream will be returned.

{code:language=java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/file/";
url += obj_id;
//Optionally
url += "?";
url += "streamid=";
url += streamID;

HttpClient client = new HttpClient();
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

GetMethod get = new GetMethod(url);
try { 
int result = client.executeMethod(get);
final InputStream stream = get.getResponseBodyAsStream(); 
try { 
// use stream... 
int dataByte = stream.read(); 
} finally {   
 stream.close();
}
} finally {   
get.releaseConnection();
}{code}


{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Read_document_properties_contentstream.Use_Javascript">
<citetitle>
<emphasis role="bold">Use JavaScrip</emphasis>
</citetitle>
</para>
{docbook}

Get document properties.

Creating an URL to make a request (consists of repository name, method name, for example "/children/", and folder ID to get the children from):

{code:language=java} var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/object/";
 url += obj_id;{code}

Performing request:

{code:language=java} var params = {};
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
 params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
 gadgets.io.makeRequest(url, handler, params);{code}

You can also use the ContentType.DOM parameter to parse the feed in your application (Using DOMParser for example).

Get document content-stream.

{note}
Performing a content stream request in JavaScript will cause the browser dialog for a file download.
{note}

{code:language=java} var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/file/";
 url += obj_id;
 //Optionally
 url += "?";
 url += "streamid=";
 url += streamID;{code}


h1. Search of data and syntax examples {anchor:id=Dev_CMIS.Usage_code_examples.Search_data_syntax_exampleRoleNotInToc}

CMIS supports SQL queries for more handful content search. Query service can handle both GET and POST requests. URL for query consists of repository name and method name "/query".
The GET request must contain query as a parameter named "q", in case of POST request query must be located in a request body.

For more detailed instructions how to construct queries, refer to the [Query examples|#Dev_CMIS.features.Query_examples] chapter.

{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Search_data_syntax_example.Use_Java">
<citetitle>
<emphasis role="bold">Use Java</emphasis>
</citetitle>
</para>
{docbook}

{code:java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/query/";

HttpClient client = new HttpClient();
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

PostMethod post = new PostMethod(url);
String s = "<?xml version='1.0' encoding='utf-8'?>" 
	 + "<cmis:query xmlns='http://www.w3.org/2005/Atom' xmlns:cmis='http://docs.oasis-open.org/ns/cmis/core/200908/'>"
	 + "<cmis:statement>SELECT * FROM cmis:document</cmis:statement>"
	 + "<cmis:maxItems>10</cmis:maxItems>"
	 + "<cmis:skipCount>0</cmis:skipCount>"
	 + "<cmis:searchAllVersions>true</cmis:searchAllVersions>"
	 + "<cmis:includeAllowableActions>true</cmis:includeAllowableActions>"
	 + "</cmis:query>";

RequestEntity entity = new StringRequestEntity(s, "text/xml","utf-8");
try { 
post.setRequestEntity(entity);
int result = client.executeMethod(post);
final String strResponse = post.getResponseBodyAsString(); 
// use response...
} finally {
post.releaseConnection();
}{code}


{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Search_data_syntax_example.Use_Javascript">
<citetitle>
<emphasis role="bold">Use JavaScript</emphasis>
</citetitle>
</para>
{docbook}

{code:java} var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/query/";{code}

{code:language=java}var params = {};
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
 params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(someQuery);
 gadgets.io.makeRequest(url, handler, params);{code}


h1. Modification of document properties or content {anchor:id=Dev_CMIS.Usage_code_examples.Modification_of_document_properties_or_contentRoleNotInToc}

The command of property update uses PUT method. The URL is the same as the one for reading properties, the difference is only in the HTTP method used.
The body of the request must be an Atom document with specified properties  (see spec. 2.2.4.12 for detailed constructing document).

Sending of content stream can be executed via PUT or POST requests. Content-type of the request must be an "multipart/form-data".


{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Modification_of_document_properties_or_content.Use_java">
<citetitle>
<emphasis role="bold">Use Java</emphasis>
</citetitle>
</para>
{docbook}

Update properties:

{code:java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity; 

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/object/";
url += obj_id;

HttpClient client = new HttpClient();
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

String atomDoc = "<?xml version='1.0' encoding='utf-8'?>"
	 + "<entry xmlns='http://www.w3.org/2005/Atom'"
	 + " xmlns:cmis='http://docs.oasis-open.org/ns/cmis/core/200908/'"
	 + " xmlns:cmisra='http://docs.oasis-open.org/ns/cmis/restatom/200908/'>"
	 + "<cmisra:object><cmis:properties>"
	 + "<cmis:propertyString queryName='cmis:name' localName='cmis:name' propertyDefinitionId='cmis:name'>"
	 + "<cmis:value>newName</cmis:value>"
	 + "</cmis:propertyString>"
	 + "</cmis:properties></cmisra:object>"
	 + "</entry>";

PutMethod put = new PutMethod(url);
RequestEntity entity = new StringRequestEntity(atomDoc, "text/xml", "utf-8");
put.setRequestEntity(entity); 

try { 
int result = client.executeMethod(put);
final String strResponse = put.getResponseBodyAsString(); 
} finally {
put.releaseConnection();
}{code}


Set content stream:

{code:java}import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity; 

String url = "http://localhost:8080/rest/private/cmisatom/";
url += repository;
url += "/file/";
url += obj_id;

HttpClient client = new HttpClient();
client.getHttpConnectionManager().
getParams().setConnectionTimeout(10000);

PostMethod post = new PostMethod(url);
RequestEntity entity = new InputStreamRequestEntity(inputStream, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity); 

try { 
int result = client.executeMethod(post);
final String strResponse = post.getResponseBodyAsString(); 
} finally {
post.releaseConnection();
}{code}


{docbook}
<para xml:id="Dev_CMIS.Usage_code_examples.Modification_of_document_properties_or_content.Use_javascript">
<citetitle>
<emphasis role="bold">Use JavaScript</emphasis>
</citetitle>
</para>
{docbook}

Update properties:

{code:language=java}var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/object/";
 url += obj_id;{code}

{code:java}//constructing document
 String atomDoc = "<?xml version='1.0' encoding='utf-8'?>";
       atomDoc += "<entry xmlns='http://www.w3.org/2005/Atom'";
       atomDoc += " xmlns:cmis='http://docs.oasis-open.org/ns/cmis/core/200908/'";
       atomDoc += " xmlns:cmisra='http://docs.oasis-open.org/ns/cmis/restatom/200908/'>";
       atomDoc += "<cmisra:object><cmis:properties>";
       atomDoc += "<cmis:propertyString queryName='cmis:name' localName='cmis:name' propertyDefinitionId='cmis:name'>";
       atomDoc += "<cmis:value>newName</cmis:value>";
       atomDoc += "</cmis:propertyString>";
       atomDoc += "</cmis:properties></cmisra:object></entry>";

 var params = {};
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.PUT;
 params[gadgets.io.RequestParameters.POST_DATA] = atomDoc;
 gadgets.io.makeRequest(url, handler, params);{code}

Set content stream:

{code:java}var url = "http://localhost:8080/rest/private/cmisatom/";
 url += repository;
 url += "/file/";
 url += obj_id;{code}

{code:java}var params = {};
 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
 params[gadgets.io.RequestParameters.CONTENT_TYPE] = "multipart/form-data";
 params[gadgets.io.RequestParameters.POST_DATA] = contentStream;
 gadgets.io.makeRequest(url, handler, params);{code}