Working with Content

CMIS 1.0 CMIS 1.1 Spec 2.1.4.2

Content Streams

Content Stream objects are used to send and fetch content. They contain a file name, a MIME type, the stream length, and the stream.

In some cases, the stream length is unknown (null). OpenCMIS and PortCMIS don’t rely on it and your application shouldn’t either.

You can create content stream objects manually or implement the interface yourself, but OpenCMIS and PortCMIS provide some convenience methods for this.

OpenCMIS (Java)

// create a simple ContentStream object
ContentStream cs1 = session.getObjectFactory().createContentStream(filename, length, mimeType, stream);

// create a ContentStream object from a byte array
ContentStream cs2 = ContentStreamUtils.createByteArrayContentStream(filename, bytes, mimeType);

// create a ContentStream object from a string
ContentStream cs3 = ContentStreamUtils.createTextContentStream(filename, content);

// create a ContentStream object from file
ContentStream cs4 = ContentStreamUtils.createFileContentStream(file);

PortCMIS (C#)

// create a simple IContentStream object
IContentStream cs1 = Session.ObjectFactory.CreateContentStream(filename, length, mimeType, stream);

// create a IContentStream object from a byte array
IContentStream cs2 = ContentStreamUtils.CreateByteArrayContentStream(filename, bytes, mimeType);

// create a IContentStream object from a string
IContentStream cs3 = ContentStreamUtils.CreateTextContentStream(filename, content);

MIME Types

When you create a new document or update the content of a document, you have to provide a MIME type. If you don’t know the MIME type, use application/octet-stream.

OpenCMIS can guess the MIME type based on the file extension. If you need a more thorough MIME type detection, have a look at Apache Tika.

OpenCMIS (Java)

String mimeType = MimeTypes.getMIMEType("txt"); // MIME type for a .txt file

For .Net 4.5+ you can use System.Web.MimeMapping.GetMimeMapping.

PortCMIS (C#)

string mimeType = MimeMapping.GetMimeMapping("text.txt");

Getting Content

CMIS 1.0 CMIS 1.1

The code snippets below show how to get the content of document.

Note

CMIS differentiates between documents with no content and documents with a content of 0 bytes.
If a document has no content, getContentStream() returns null. If a document has a 0 byte content, getContentStream() returns a content stream object with an empty stream.

OpenCMIS (Java)

Document document = ...
ContentStream contentStream = document.getContentStream();
InputStream stream = contentStream.getStream();

PortCMIS (C#)

IDocument document = ...
IContentStream contentStream = document.GetContentStream();
Stream stream = contentStream.Stream;

Getting Partial Content

CMIS 1.0 CMIS 1.1

It’s also possible to get only a part of the content.

OpenCMIS (Java)

Document document = ...

// skip the first 100 bytes
// use null to start from the beginning
BigInteger offset = BigInteger.valueOf(100);

// only read 200 bytes
// use null to read to the end of the stream
BigInteger length = BigInteger.valueOf(200); 

ContentStream contentStream = document.getContentStream(offset, length);
InputStream stream = contentStream.getStream();

PortCMIS (C#)

IDocument document = ...

// skip the first 100 bytes
// use null to start from the beginning
long? offset = 100;

// only read 200 bytes
// use null to read to the end of the stream
long? length = 200;

IContentStream contentStream = document.GetContentStream(offset, length);
Stream stream = contentStream.Stream;

Getting a Content URL

The AtomPub Binding and the Browser Binding can provide a URL to the content of a document. Depending on the repository and the binding, the server might not return the content but an error message because this URL does not include authentication data. A user may have to re-authenticate to get the content.

OpenCMIS (Java)

Document document = ...

String link = document.getContentUrl();

Updating Content

Overwriting Content

CMIS 1.0 CMIS 1.1

OpenCMIS (Java)

Document document = ...

ContentStream contentStream = ...

document.setContentStream(contentStream, true);

PortCMIS (C#)

IDocument document = ...

IContentStream contentStream = ...

document.SetContentStream(contentStream, true);

Deleting Content

CMIS 1.0 CMIS 1.1

Warning

Some repositories don’t support documents without content. Check the repository capabilities if that’s the case or check the Allowable Actions of the document.

OpenCMIS (Java)

Document document = ...
document.deleteContentStream();

PortCMIS (C#)

IDocument document = ...
document.DeleteContentStream();

Appending Content

CMIS 1.1

Warning

Not all repositories support appending content. It is not possible to discover wheter a repository supports it or not. If you append content be prepared to catch a notSupported exception.

OpenCMIS (Java)

Document document = ...

ContentStream contentStream = ...
boolean isLastChunk = true; // indicates that this is the last part of the content 

document.appendContentStream(contentStream, isLastChunk);

PortCMIS (C#)

IDocument document = ...

IContentStream contentStream = ...
bool isLastChunk = true; // indicates that this is the last part of the content 

document.AppendContentStream(contentStream, isLastChunk);

Working with Renditions

CMIS 1.0 CMIS 1.1 Spec 2.1.4.2

Renditions are alternative versions of a document. For example, a rendition could be a PDF of an Office document. Thumbnails are a special kind of renditions and could also exist for non-document objects.
CMIS only supports server managed renditions. A CMIS client cannot upload, modify, or delete a rendition.

Getting the List of Renditions

Note

The list of renditions is only available if it has been requested with an Operation Context.

OpenCMIS (Java)

Document document = ...

for (Rendition rendition: document.getRenditions()) {
    System.out.println(rendition.getTitle() + ": " + rendition.getStreamId());
}

PortCMIS (C#)

IDocument document = ...

foreach (IRendition rendition in folder.Renditions) {
    Console.WriteLine(rendition.Title + ": " + rendition.StreamId);
}

Getting Rendition Content

The rendition content can be retrieved either for an Rendition object (see above) or directly from the document with a stream ID.

OpenCMIS (Java)

Document document = ...
Rendition rendition = ...

ContentStream cs1 = document.getContentStream(rendition.getStreamId());

ContentStream cs2 = rendition.getContentStream();

PortCMIS (C#)

IDocument document = ...
IRendition rendition = ...

IContentStream cs1 = document.GetContentStream(rendition.StreamId);

IContentStream cs2 = rendition.GetContentStream();