AnsweredAssumed Answered

CMIS setContentStream problem

Question asked by jake-hyland on Apr 20, 2009
Latest reply on Apr 22, 2009 by jake-hyland
Hey all,

I have been working on a CMIS access layer in C#, and I have run into an issue that I cannot figure out. I am trying to set the content stream of a checked out document before checking it in.

When I create a document from a file on my local machine, everything works fine. The document is created and returned, and I can retrieve the content of the document. The problem occurs when I am updating the content stream of a checked in document. When I run the code below, the content stream is updated, but as far as I can tell the base64 encoded content is not decoded. all files that I update with this code get turned into plain text files containing base64 strings, but they keep their original file extensions.

I have examined the specification (0.5), and the way I interpret that version of setContentStream is to send the raw bits over as the PUT content, but if I do this (code not shown because it's simple) i get a 500 internal server error.

Any help would be very appreciated.

Here is my save to server code:

        /// <summary>
        /// Commits the changes to the document's data back to the server.
        /// </summary>
        public void Commit()
        {
            if(_contentChanged)
            {
                XDeclaration dec = new XDeclaration("1.0", "utf-8", "no");
                XNamespace atom = "http://www.w3.org/2005/Atom";
                XElement ele =
                    new XElement(atom + "entry",
                    new XElement(atom + "title", Title),
                        new XElement(atom + "content", Convert.ToBase64String(_data),
                            new XAttribute("type", MimeType),
                            new XAttribute("encoding", "base64")
                        )
                    );
                byte[] data = System.Text.Encoding.UTF8.GetBytes(dec.ToString() + ele.ToString());
                _repo.Server.PutRequest(_editMedia, data, "application/atom+xml;type=entry", new WebHeaderCollection());
            }
        }
and the PutRequest method:

        /// <summary>
        /// Executes an HTTP POST against a URI.
        /// </summary>
        /// <param name="uri">The URI to send the POST to.</param>
        /// <param name="data">The data to include in the POST</param>
        /// <param name="contentType">The MIME content type of the post.</param>
        /// <param name="headers">The PUT headers for this request.</param>
        /// <returns>The response stream.</returns>
        internal Stream PutRequest(Uri uri, byte[] data, string contentType, WebHeaderCollection headers)
        {
            try
            {
                WebRequest request = WebRequest.Create(uri);
                request.Headers = headers;
                request.Method = "PUT";
                request.Credentials = _credentials;
                request.ContentType = contentType;
                using(Stream str = request.GetRequestStream())
                {
                    str.Write(data, 0, data.Length);
                }
                WebResponse response = request.GetResponse();
                return response.GetResponseStream();
            }
            catch(WebException e)
            {
                throw new CmisException(uri, "There was a problem submitting the PUT request.", e);
            }
        }

For those not familiar with C#, what this does is create an ATOM entry for the document, enclosing the content in a base64 string, and then encoding the whole thing in Utf-8 for transfer.

Outcomes