Search in sources :

Example 56 with CmisRuntimeException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.

the class CMISConnector method getContentStream.

/**
 * Gets the content from the repository.
 */
public ContentStream getContentStream(CMISNodeInfo info, String streamId, BigInteger offset, BigInteger length) {
    // get the type and check if the object can have content
    TypeDefinitionWrapper type = info.getType();
    checkDocumentTypeForContent(type);
    // looks like a document, now get the content
    ContentStreamImpl result = new ContentStreamImpl();
    result.setFileName(info.getName());
    // if streamId is set, fetch other content
    NodeRef streamNodeRef = info.getNodeRef();
    if ((streamId != null) && (streamId.length() > 0)) {
        CMISNodeInfo streamInfo = createNodeInfo(streamId);
        if (!streamInfo.isVariant(CMISObjectVariant.CURRENT_VERSION)) {
            throw new CmisInvalidArgumentException("Stream id is invalid: " + streamId + ", expected variant " + CMISObjectVariant.CURRENT_VERSION + ", got variant " + streamInfo.getObjectVariant());
        }
        streamNodeRef = streamInfo.getNodeRef();
        type = streamInfo.getType();
        checkDocumentTypeForContent(type);
    }
    // get the stream now
    try {
        ContentReader contentReader = contentService.getReader(streamNodeRef, ContentModel.PROP_CONTENT);
        if (contentReader == null) {
            throw new CmisConstraintException("Document has no content!");
        }
        result.setMimeType(contentReader.getMimetype());
        long contentSize = contentReader.getSize();
        if ((offset == null) && (length == null)) {
            result.setStream(contentReader.getContentInputStream());
            result.setLength(BigInteger.valueOf(contentSize));
            publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), null);
        } else {
            long off = (offset == null ? 0 : offset.longValue());
            long len = (length == null ? contentSize : length.longValue());
            if (off + len > contentSize) {
                len = contentReader.getSize() - off;
            }
            result.setStream(new RangeInputStream(contentReader.getContentInputStream(), off, len));
            result.setLength(BigInteger.valueOf(len));
            publishReadEvent(streamNodeRef, info.getName(), result.getMimeType(), contentSize, contentReader.getEncoding(), off + " - " + len);
        }
    } catch (Exception e) {
        if (e instanceof CmisBaseException) {
            throw (CmisBaseException) e;
        } else {
            StringBuilder msg = new StringBuilder("Failed to retrieve content: " + e.getMessage());
            Throwable cause = e.getCause();
            if (cause != null) {
                // add the cause to the CMIS exception
                msg.append(", ");
                msg.append(cause.getMessage());
            }
            throw new CmisRuntimeException(msg.toString(), e);
        }
    }
    return result;
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) ItemTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.ItemTypeDefinitionWrapper) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) DocumentTypeDefinitionWrapper(org.alfresco.opencmis.dictionary.DocumentTypeDefinitionWrapper) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) InvalidAspectException(org.alfresco.service.cmr.dictionary.InvalidAspectException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisStreamNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)

Example 57 with CmisRuntimeException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.

the class CMISConnector method getRootNodeRef.

/**
 * Returns the root folder node ref.
 */
public NodeRef getRootNodeRef() {
    NodeRef rootNodeRef = (NodeRef) singletonCache.get(KEY_CMIS_ROOT_NODEREF);
    if (rootNodeRef == null) {
        rootNodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

            public NodeRef doWork() throws Exception {
                return transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<NodeRef>() {

                    public NodeRef execute() throws Exception {
                        NodeRef root = nodeService.getRootNode(storeRef);
                        List<NodeRef> rootNodes = searchService.selectNodes(root, rootPath, null, namespaceService, false);
                        if (rootNodes.size() != 1) {
                            throw new CmisRuntimeException("Unable to locate CMIS root path " + rootPath);
                        }
                        return rootNodes.get(0);
                    }
                }, true);
            }
        }, AuthenticationUtil.getSystemUserName());
        if (rootNodeRef == null) {
            throw new CmisObjectNotFoundException("Root folder path '" + rootPath + "' not found!");
        }
        singletonCache.put(KEY_CMIS_ROOT_NODEREF, rootNodeRef);
    }
    return rootNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) Collections.singletonList(java.util.Collections.singletonList) ObjectList(org.apache.chemistry.opencmis.commons.data.ObjectList) ArrayList(java.util.ArrayList) List(java.util.List) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) FileExistsException(org.alfresco.service.cmr.model.FileExistsException) InvalidAspectException(org.alfresco.service.cmr.dictionary.InvalidAspectException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisStreamNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 58 with CmisRuntimeException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.

the class AlfrescoCmisServiceImpl method getObjectInfoIntern.

/**
 * Collects the {@link ObjectInfo} about an object.
 *
 * (Provided by OpenCMIS, but optimized for Alfresco.)
 */
@SuppressWarnings("unchecked")
@Override
protected ObjectInfo getObjectInfoIntern(String repositoryId, ObjectData object) {
    // if the object has no properties, stop here
    if (object.getProperties() == null || object.getProperties().getProperties() == null) {
        throw new CmisRuntimeException("No properties!");
    }
    String objectId = object.getId();
    CMISNodeInfo ni = getOrCreateNodeInfo(objectId);
    ObjectInfoImpl info = new ObjectInfoImpl();
    // general properties
    info.setObject(object);
    info.setId(objectId);
    info.setName(getStringProperty(object, PropertyIds.NAME));
    info.setCreatedBy(getStringProperty(object, PropertyIds.CREATED_BY));
    info.setCreationDate(getDateTimeProperty(object, PropertyIds.CREATION_DATE));
    info.setLastModificationDate(getDateTimeProperty(object, PropertyIds.LAST_MODIFICATION_DATE));
    info.setTypeId(getIdProperty(object, PropertyIds.OBJECT_TYPE_ID));
    info.setBaseType(object.getBaseTypeId());
    if (ni.isRelationship()) {
        // versioning
        info.setWorkingCopyId(null);
        info.setWorkingCopyOriginalId(null);
        info.setVersionSeriesId(null);
        info.setIsCurrentVersion(true);
        info.setWorkingCopyId(null);
        info.setWorkingCopyOriginalId(null);
        // content
        info.setHasContent(false);
        info.setContentType(null);
        info.setFileName(null);
        // parent
        info.setHasParent(false);
        // policies and relationships
        info.setSupportsRelationships(false);
        info.setSupportsPolicies(false);
        // renditions
        info.setRenditionInfos(null);
        // relationships
        info.setRelationshipSourceIds(null);
        info.setRelationshipTargetIds(null);
        // global settings
        info.setHasAcl(false);
        info.setSupportsDescendants(false);
        info.setSupportsFolderTree(false);
    } else if (ni.isFolder()) {
        // versioning
        info.setWorkingCopyId(null);
        info.setWorkingCopyOriginalId(null);
        info.setVersionSeriesId(null);
        info.setIsCurrentVersion(true);
        info.setWorkingCopyId(null);
        info.setWorkingCopyOriginalId(null);
        // content
        info.setHasContent(false);
        info.setContentType(null);
        info.setFileName(null);
        // parent
        info.setHasParent(!ni.isRootFolder());
        // policies and relationships
        info.setSupportsRelationships(true);
        info.setSupportsPolicies(true);
        // renditions
        info.setRenditionInfos(null);
        // relationships
        setRelaionshipsToObjectInfo(object, info);
        // global settings
        info.setHasAcl(true);
        info.setSupportsDescendants(true);
        info.setSupportsFolderTree(true);
    } else if (ni.isDocument()) {
        // versioning
        info.setWorkingCopyId(null);
        info.setWorkingCopyOriginalId(null);
        info.setVersionSeriesId(ni.getCurrentNodeId());
        if (ni.isPWC()) {
            info.setIsCurrentVersion(false);
            info.setWorkingCopyId(ni.getObjectId());
            info.setWorkingCopyOriginalId(ni.getCurrentObjectId());
        } else {
            info.setIsCurrentVersion(ni.isCurrentVersion());
            if (ni.hasPWC()) {
                info.setWorkingCopyId(ni.getCurrentNodeId() + CMISConnector.ID_SEPERATOR + CMISConnector.PWC_VERSION_LABEL);
                info.setWorkingCopyOriginalId(ni.getCurrentObjectId());
            } else {
                info.setWorkingCopyId(null);
                info.setWorkingCopyOriginalId(null);
            }
        }
        // content
        String fileName = getStringProperty(object, PropertyIds.CONTENT_STREAM_FILE_NAME);
        String mimeType = getStringProperty(object, PropertyIds.CONTENT_STREAM_MIME_TYPE);
        String streamId = getIdProperty(object, PropertyIds.CONTENT_STREAM_ID);
        BigInteger length = getIntegerProperty(object, PropertyIds.CONTENT_STREAM_LENGTH);
        boolean hasContent = fileName != null || mimeType != null || streamId != null || length != null;
        if (hasContent) {
            info.setHasContent(hasContent);
            info.setContentType(mimeType);
            info.setFileName(fileName);
        } else {
            info.setHasContent(false);
            info.setContentType(null);
            info.setFileName(null);
        }
        // parent
        info.setHasParent(ni.isCurrentVersion() || ni.isPWC());
        // policies and relationships
        info.setSupportsRelationships(true);
        info.setSupportsPolicies(true);
        // renditions
        String renditionFilter = getRequestParameterRenditionFilter();
        List<RenditionInfo> renditionInfos = new ArrayList<>();
        CMISNodeInfo nodeInfo = getOrCreateNodeInfo(objectId);
        NodeRef nodeRef = nodeInfo.getNodeRef();
        if (nodeRef != null) {
            List<RenditionData> renditions = connector.getRenditions(nodeRef, renditionFilter, null, null);
            for (RenditionData rendition : renditions) {
                RenditionInfoImpl renditionInfo = new RenditionInfoImpl();
                renditionInfo.setId(rendition.getStreamId());
                renditionInfo.setKind(rendition.getKind());
                renditionInfo.setContentType(rendition.getMimeType());
                renditionInfo.setTitle(rendition.getTitle());
                renditionInfo.setLength(rendition.getBigLength());
                renditionInfos.add(renditionInfo);
            }
        }
        info.setRenditionInfos(renditionInfos);
        // relationships
        setRelaionshipsToObjectInfo(object, info);
        // global settings
        info.setHasAcl(true);
        info.setSupportsDescendants(true);
        info.setSupportsFolderTree(true);
    } else if (ni.isItem()) {
        info.setHasAcl(true);
        info.setHasContent(false);
    }
    return info;
}
Also used : CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) RenditionInfo(org.apache.chemistry.opencmis.commons.server.RenditionInfo) ArrayList(java.util.ArrayList) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo) ObjectInfoImpl(org.apache.chemistry.opencmis.commons.impl.server.ObjectInfoImpl) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RenditionInfoImpl(org.apache.chemistry.opencmis.commons.impl.server.RenditionInfoImpl) BigInteger(java.math.BigInteger) RenditionData(org.apache.chemistry.opencmis.commons.data.RenditionData)

Example 59 with CmisRuntimeException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.

the class AlfrescoCmisServiceImpl method create.

// --- object service ---
@Override
public String create(String repositoryId, Properties properties, String folderId, ContentStream contentStream, VersioningState versioningState, List<String> policies, ExtensionsData extension) {
    checkRepositoryId(repositoryId);
    // check properties
    if (properties == null || properties.getProperties() == null) {
        throw new CmisInvalidArgumentException("Properties must be set!");
    }
    // get the type
    String objectTypeId = connector.getObjectTypeIdProperty(properties);
    // find the type
    TypeDefinitionWrapper type = connector.getOpenCMISDictionaryService().findType(objectTypeId);
    if (type == null) {
        throw new CmisInvalidArgumentException("Type '" + objectTypeId + "' is unknown!");
    }
    // create object
    String newId = null;
    switch(type.getBaseTypeId()) {
        case CMIS_DOCUMENT:
            versioningState = getDocumentDefaultVersioningState(versioningState, type);
            newId = createDocument(repositoryId, properties, folderId, contentStream, versioningState, policies, null, null, extension);
            break;
        case CMIS_FOLDER:
            newId = createFolder(repositoryId, properties, folderId, policies, null, null, extension);
            break;
        case CMIS_POLICY:
            newId = createPolicy(repositoryId, properties, folderId, policies, null, null, extension);
            break;
        case CMIS_ITEM:
            newId = createItem(repositoryId, properties, folderId, policies, null, null, extension);
            break;
        default:
            break;
    }
    // check new object id
    if (newId == null) {
        throw new CmisRuntimeException("Creation failed!");
    }
    boolean isObjectInfoRequired = getContext().isObjectInfoRequired();
    if (isObjectInfoRequired) {
        try {
            getObjectInfo(repositoryId, newId, "*", IncludeRelationships.NONE);
        } catch (InvalidNodeRefException e) {
            throw new CmisRuntimeException("Creation failed! New object not found!");
        }
    }
    // return the new object id
    return newId;
}
Also used : CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) TypeDefinitionWrapper(org.alfresco.opencmis.dictionary.TypeDefinitionWrapper) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 60 with CmisRuntimeException

use of org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException in project alfresco-repository by Alfresco.

the class AlfrescoCmisServiceImpl method getFolderParent.

@Override
public ObjectData getFolderParent(String repositoryId, String folderId, String filter, ExtensionsData extension) {
    checkRepositoryId(repositoryId);
    // get the node ref
    CMISNodeInfo info = getOrCreateFolderInfo(folderId, "Folder");
    // the root folder has no parent
    if (info.isRootFolder()) {
        throw new CmisInvalidArgumentException("Root folder has no parent!");
    }
    // get the parent
    List<CMISNodeInfo> parentInfos = info.getParents();
    if (parentInfos.isEmpty()) {
        throw new CmisRuntimeException("Folder has no parent and is not the root folder?!");
    }
    CMISNodeInfo parentInfo = addNodeInfo(parentInfos.get(0));
    ObjectData result = connector.createCMISObject(parentInfo, filter, false, IncludeRelationships.NONE, CMISConnector.RENDITION_NONE, false, false);
    boolean isObjectInfoRequired = getContext().isObjectInfoRequired();
    if (isObjectInfoRequired) {
        getObjectInfo(repositoryId, parentInfo.getObjectId(), IncludeRelationships.NONE);
    }
    return result;
}
Also used : CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) CMISNodeInfo(org.alfresco.opencmis.dictionary.CMISNodeInfo)

Aggregations

CmisRuntimeException (org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException)60 DateTimeFormat (org.apache.chemistry.opencmis.commons.enums.DateTimeFormat)35 JSONObject (org.apache.chemistry.opencmis.commons.impl.json.JSONObject)35 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)23 BigInteger (java.math.BigInteger)12 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)11 Properties (org.apache.chemistry.opencmis.commons.data.Properties)10 Acl (org.apache.chemistry.opencmis.commons.data.Acl)8 JSONArray (org.apache.chemistry.opencmis.commons.impl.json.JSONArray)8 Holder (org.apache.chemistry.opencmis.commons.spi.Holder)8 IOException (java.io.IOException)7 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)7 IUserGroupObject (com.pogeyan.cmis.api.auth.IUserGroupObject)6 IncludeRelationships (org.apache.chemistry.opencmis.commons.enums.IncludeRelationships)6 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ObjectList (org.apache.chemistry.opencmis.commons.data.ObjectList)5 CmisBaseException (org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException)5 CmisContentAlreadyExistsException (org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException)4