Question

I've created a custom content type derived from document. I am trying to use CMIS to query my Alfresco server (tried with 4.2.b and 4.2.c) programmatically for my documents using python cmislib. I have a pyramid server that takes REST calls and sends them to my Alfresco server using CMIS.

I get this error:

2013-04-11 11:19:25,526 | ERROR | Exception when serving /access_manager/search_noauth
Traceback (most recent call last):
  File "/home/hbrown/.virtualenvs/access_manager_master/local/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/channel.py", line 329, in service
    task.service()
[...]
  File "/home/hbrown/workspace/spt/access_manager/access_manager/views/search.py", line 223, in cmis_main
    for result in repo.query(whole_query)
  File "/home/hbrown/.virtualenvs/access_manager_master/local/lib/python2.7/site-packages/cmislib/model.py", line 2467, in getContentStream
    assert(len(contentElements) == 1), 'Expected to find exactly one atom:content element.'
AssertionError: Expected to find exactly one atom:content element.

I am using getContentStream() to retrieve content. Based on the code comment, I'd say it is the correct API call:

    >>> doc.getName()
    u'sample-b.pdf'
    >>> o = open('tmp.pdf', 'wb')
    >>> result = doc.getContentStream()
    >>> o.write(result.read())
    >>> result.close()
    >>> o.close()
    >>> import os.path
    >>> os.path.getsize('tmp.pdf')
    117248

The python code in cmislib clearly expects the document to have XML that includes an element named content, and mine does not.

The calling code looks like this:

from cmislib import CmisClient

SERVER = "localhost"
url = "http://{0}:8080/alfresco/cmisatom".format(SERVER)
client = CmisClient(url, 'admin', 'alfresco')
repo = client.defaultRepository
results = repo.query("select * from wg:bulletin")
print results[0].getContentStream().read()

The XML being operated on in getContentStream looks like this:

<atom:entry>
    <atom:author>
        <atom:name/>
    </atom:author>
    <atom:id>http://chemistry.apache.org/aWQtMQ==</atom:id>
    <atom:published>2013-04-12T03:22:38Z</atom:published>
    <atom:title>Query Result id-1</atom:title>
    <app:edited>2013-04-12T03:22:38Z</app:edited>
    <atom:updated>2013-04-12T03:22:38Z</atom:updated>
    <cmisra:object xmlns:ns3="http://docs.oasis-open.org/ns/cmis/messaging/200908/">
        <cmis:properties>
            <cmis:propertyInteger displayName="Content Stream Length" localName="contentStreamLength" propertyDefinitionId="cmis:contentStreamLength" queryName="b.cmis:contentStreamLength">
                <cmis:value>249</cmis:value>
            </cmis:propertyInteger>
            <cmis:propertyId displayName="Object Type Id" localName="objectTypeId" propertyDefinitionId="cmis:objectTypeId" queryName="b.cmis:objectTypeId">
                <cmis:value>D:wg:bulletin</cmis:value>
            </cmis:propertyId>
            <cmis:propertyString displayName="Version Series Checked Out By" localName="versionSeriesCheckedOutBy" propertyDefinitionId="cmis:versionSeriesCheckedOutBy" queryName="b.cmis:versionSeriesCheckedOutBy"/>
            <cmis:propertyId displayName="Version Series Checked Out Id" localName="versionSeriesCheckedOutId" propertyDefinitionId="cmis:versionSeriesCheckedOutId" queryName="b.cmis:versionSeriesCheckedOutId"/>
            <cmis:propertyId displayName="Version series id" localName="versionSeriesId" propertyDefinitionId="cmis:versionSeriesId" queryName="b.cmis:versionSeriesId">
                <cmis:value>workspace://SpacesStore/1cd2053d-1fc4-4e85-b780-ba80284f0841</cmis:value>
            </cmis:propertyId>
            <cmis:propertyString displayName="wg:account" localName="account" propertyDefinitionId="wg:account" queryName="b.wg:account"/>
            <cmis:propertyString displayName="Version Label" localName="versionLabel" propertyDefinitionId="cmis:versionLabel" queryName="b.cmis:versionLabel">
                <cmis:value>1.0</cmis:value>
            </cmis:propertyString>
            <cmis:propertyBoolean displayName="Is Latest Version" localName="isLatestVersion" propertyDefinitionId="cmis:isLatestVersion" queryName="b.cmis:isLatestVersion">
                <cmis:value>true</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyBoolean displayName="Is Version Series Checked Out" localName="isVersionSeriesCheckedOut" propertyDefinitionId="cmis:isVersionSeriesCheckedOut" queryName="b.cmis:isVersionSeriesCheckedOut">
                <cmis:value>false</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyString displayName="Last Modified By" localName="lastModifiedBy" propertyDefinitionId="cmis:lastModifiedBy" queryName="b.cmis:lastModifiedBy">
                <cmis:value>admin</cmis:value>
            </cmis:propertyString>
            <cmis:propertyString displayName="Created by" localName="createdBy" propertyDefinitionId="cmis:createdBy" queryName="b.cmis:createdBy">
                <cmis:value>admin</cmis:value>
            </cmis:propertyString>
            <cmis:propertyDateTime displayName="wg:displayUntil" localName="displayUntil" propertyDefinitionId="wg:displayUntil" queryName="b.wg:displayUntil"/>
            <cmis:propertyId displayName="Alfresco Node Ref" localName="nodeRef" propertyDefinitionId="alfcmis:nodeRef" queryName="b.alfcmis:nodeRef">
                <cmis:value>workspace://SpacesStore/1cd2053d-1fc4-4e85-b780-ba80284f0841</cmis:value>
            </cmis:propertyId>
            <cmis:propertyString displayName="wg:email" localName="email" propertyDefinitionId="wg:email" queryName="b.wg:email"/>
            <cmis:propertyBoolean displayName="wg:isActive" localName="isActive" propertyDefinitionId="wg:isActive" queryName="b.wg:isActive">
                <cmis:value>false</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyString displayName="wg:username" localName="username" propertyDefinitionId="wg:username" queryName="b.wg:username"/>
            <cmis:propertyBoolean displayName="Is Latest Major Version" localName="isLatestMajorVersion" propertyDefinitionId="cmis:isLatestMajorVersion" queryName="b.cmis:isLatestMajorVersion">
                <cmis:value>true</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyId displayName="Content Stream Id" localName="contentStreamId" propertyDefinitionId="cmis:contentStreamId" queryName="b.cmis:contentStreamId">
                <cmis:value>store://2013/4/10/15/29/20b185d0-afae-4a7f-a06e-58eab399bdc9.bin</cmis:value>
            </cmis:propertyId>
            <cmis:propertyString displayName="Name" localName="name" propertyDefinitionId="cmis:name" queryName="b.cmis:name">
                <cmis:value>.pythonrc</cmis:value>
            </cmis:propertyString>
            <cmis:propertyString displayName="Content Stream MIME Type" localName="contentStreamMimeType" propertyDefinitionId="cmis:contentStreamMimeType" queryName="b.cmis:contentStreamMimeType">
                <cmis:value>text/plain</cmis:value>
            </cmis:propertyString>
            <cmis:propertyDateTime displayName="Creation Date" localName="creationDate" propertyDefinitionId="cmis:creationDate" queryName="b.cmis:creationDate">
                <cmis:value>2013-04-10T15:29:18.146-04:00</cmis:value>
            </cmis:propertyDateTime>
            <cmis:propertyString displayName="Change token" localName="changeToken" propertyDefinitionId="cmis:changeToken" queryName="b.cmis:changeToken"/>
            <cmis:propertyString displayName="wg:state" localName="state" propertyDefinitionId="wg:state" queryName="b.wg:state"/>
            <cmis:propertyDateTime displayName="wg:displayFrom" localName="displayFrom" propertyDefinitionId="wg:displayFrom" queryName="b.wg:displayFrom"/>
            <cmis:propertyString displayName="Checkin Comment" localName="checkinComment" propertyDefinitionId="cmis:checkinComment" queryName="b.cmis:checkinComment"/>
            <cmis:propertyString displayName="wg:application" localName="application" propertyDefinitionId="wg:application" queryName="b.wg:application"/>
            <cmis:propertyId displayName="Object Id" localName="objectId" propertyDefinitionId="cmis:objectId" queryName="b.cmis:objectId">
                <cmis:value>workspace://SpacesStore/1cd2053d-1fc4-4e85-b780-ba80284f0841;1.0</cmis:value>
            </cmis:propertyId>
            <cmis:propertyBoolean displayName="Is Immutable" localName="isImmutable" propertyDefinitionId="cmis:isImmutable" queryName="b.cmis:isImmutable">
                <cmis:value>false</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyBoolean displayName="Is Major Version" localName="isMajorVersion" propertyDefinitionId="cmis:isMajorVersion" queryName="b.cmis:isMajorVersion">
                <cmis:value>true</cmis:value>
            </cmis:propertyBoolean>
            <cmis:propertyString displayName="wg:institution" localName="institution" propertyDefinitionId="wg:institution" queryName="b.wg:institution"/>
            <cmis:propertyId displayName="Base Type Id" localName="baseTypeId" propertyDefinitionId="cmis:baseTypeId" queryName="b.cmis:baseTypeId">
                <cmis:value>cmis:document</cmis:value>
            </cmis:propertyId>
            <cmis:propertyString displayName="Content Stream Filename" localName="contentStreamFileName" propertyDefinitionId="cmis:contentStreamFileName" queryName="b.cmis:contentStreamFileName">
                <cmis:value>.pythonrc</cmis:value>
            </cmis:propertyString>
            <cmis:propertyDateTime displayName="Last Modified Date" localName="lastModificationDate" propertyDefinitionId="cmis:lastModificationDate" queryName="b.cmis:lastModificationDate">
                <cmis:value>2013-04-10T15:29:23.384-04:00</cmis:value>
            </cmis:propertyDateTime>
        </cmis:properties>
    </cmisra:object>
</atom:entry>

There is clearly no XML element named content here for the python code to extract.

Is this a misconfiguration of my custom content document or is it a change in CMIS that cmislib does not track or am I calling the wrong API function to get the content or something else?


Later: the minimum change to fix this is to make calls to either reload or getAllowableActions.

This was the original code:

def cmis_main(props, settings):
    """
    Create a CMIS query based on props and execute against Alfresco
    """
    def cmis_query(props, mapping):
        """
        Create CMIS query of AND-separated OR-clauses
        """
        # Code that formats a query string from dictionaries...

    cmis_mapping = {
        # Dictionary config for call to cmis_query
        # Nothing to see here. Move on.
        "app_sids": {
            "where_fmt": IN_WHERE_FMT,
            "key": "{0}:application".format(CMIS_NAMESPACE),
            "fn": set_format,
        },
    }

    cmis_url, cmis_user, cmis_password = cmis_args(settings)
    cmisclient = CmisClient(cmis_url, cmis_user, cmis_password)
    repo = cmisclient.getDefaultRepository()
    whole_query = cmis_query(props, cmis_mapping)
    logger.debug(whole_query)

    return [
        {
            'name': result.name,
            'content': result.getContentStream().read(),
            'content_mime_type': result.properties["cmis:contentStreamMimeType"],
        }
        for result in repo.query(whole_query)
    ]

And it was broken. So I changed the code to this:

    results = list(repo.query(whole_query))
    for result in results:
        print(result.getAllowableActions())
        # or: result.reload()

    return [
        {
            'name': result.name,
            'content': result.getContentStream().read(),
            'content_mime_type': result.properties["cmis:contentStreamMimeType"],
        }
        for result in results
    ]

And it worked. I changed it to this:

    results = list(repo.query(whole_query))
    for result in results:
        pass

    return [
        {
            'name': result.name,
            'content': result.getContentStream().read(),
            'content_mime_type': result.properties["cmis:contentStreamMimeType"],
        }
        for result in results
    ]

And it broke. So the XML does not appear to be fully loaded in the CMISLIB object.

Was it helpful?

Solution

Try doing results[0].reload() before calling getContentStream(). That shouldn't be required but it may force the object to reload with the content element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top