Search in sources :

Example 1 with CmisPathNotFoundException

use of org.craftercms.studio.api.v1.exception.CmisPathNotFoundException in project studio by craftercms.

the class CmisServiceImpl method uploadContent.

@Override
@HasPermission(type = DefaultPermission.class, action = "upload_content_cmis")
public CmisUploadItem uploadContent(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepoId, String cmisPath, String filename, InputStream content) throws CmisUnavailableException, CmisTimeoutException, CmisRepositoryNotFoundException, CmisPathNotFoundException, ConfigurationException {
    DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepoId);
    CmisUploadItem cmisUploadItem = new CmisUploadItem();
    logger.debug("Create new CMIS session");
    Session session = createCMISSession(repositoryConfig);
    if (session != null) {
        String contentPath = Paths.get(repositoryConfig.getBasePath(), cmisPath).toString();
        logger.debug("Find object for CMIS path: " + contentPath);
        CmisObject cmisObject = session.getObjectByPath(contentPath);
        if (cmisObject != null) {
            if (BaseTypeId.CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
                CmisObject docObject = null;
                try {
                    docObject = session.getObjectByPath(Paths.get(contentPath, filename).toString());
                } catch (CmisBaseException e) {
                    // Content does not exist - no error
                    logger.debug("File " + filename + " does not exist at " + contentPath);
                }
                MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
                String mimeType = mimeTypesMap.getContentType(filename);
                ContentStream contentStream = session.getObjectFactory().createContentStream(filename, -1, mimeType, content);
                Folder folder = (Folder) cmisObject;
                cmisUploadItem.setName(filename);
                cmisUploadItem.setFolder(false);
                cmisUploadItem.setFileExtension(FilenameUtils.getExtension(filename));
                if (docObject != null) {
                    Document doc = (Document) docObject;
                    doc.setContentStream(contentStream, true);
                    String contentId = doc.getId();
                    StringTokenizer st = new StringTokenizer(contentId, ";");
                    if (st.hasMoreTokens()) {
                        cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
                    }
                    session.removeObjectFromCache(doc.getId());
                } else {
                    Map<String, Object> properties = new HashMap<String, Object>();
                    properties.put(OBJECT_TYPE_ID, CMIS_DOCUMENT.value());
                    properties.put(NAME, filename);
                    Document newDoc = folder.createDocument(properties, contentStream, null);
                    session.removeObjectFromCache(newDoc.getId());
                    String contentId = newDoc.getId();
                    StringTokenizer st = new StringTokenizer(contentId, ";");
                    if (st.hasMoreTokens()) {
                        cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
                    }
                }
                session.clear();
            } else if (CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId())) {
                throw new CmisPathNotFoundException();
            }
        } else {
            throw new CmisPathNotFoundException();
        }
    } else {
        throw new CmisUnauthorizedException();
    }
    return cmisUploadItem;
}
Also used : DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) HashMap(java.util.HashMap) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) StringTokenizer(java.util.StringTokenizer) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisUploadItem(org.craftercms.studio.model.rest.CmisUploadItem) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) CmisPathNotFoundException(org.craftercms.studio.api.v1.exception.CmisPathNotFoundException) Session(org.apache.chemistry.opencmis.client.api.Session) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 2 with CmisPathNotFoundException

use of org.craftercms.studio.api.v1.exception.CmisPathNotFoundException in project studio by craftercms.

the class CmisServiceImpl method cloneContent.

@Override
@HasPermission(type = DefaultPermission.class, action = "clone_content_cmis")
public void cloneContent(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepoId, String cmisPath, @ProtectedResourceId(PATH_RESOURCE_ID) String studioPath) throws StudioPathNotFoundException, CmisRepositoryNotFoundException, CmisUnavailableException, CmisTimeoutException, CmisPathNotFoundException, ServiceLayerException {
    if (!contentService.contentExists(siteId, studioPath))
        throw new StudioPathNotFoundException("Studio repository path does not exist for site " + siteId + " (path: " + studioPath + ")");
    DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepoId);
    logger.debug("Create new CMIS session");
    Session session = createCMISSession(repositoryConfig);
    if (session != null) {
        String contentPath = Paths.get(repositoryConfig.getBasePath(), cmisPath).toString();
        logger.debug("Find object for CMIS path: " + contentPath);
        CmisObject cmisObject = session.getObjectByPath(contentPath);
        if (cmisObject != null) {
            if (BaseTypeId.CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
                throw new CmisPathNotFoundException();
            } else if (CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId())) {
                Document cmisDoc = (Document) cmisObject;
                String fileName = cmisDoc.getName();
                String savePath = studioPath + FILE_SEPARATOR + fileName;
                ContentStream cs = cmisDoc.getContentStream();
                logger.debug("Save CMIS file to: " + savePath);
                contentService.writeContent(siteId, savePath, cs.getStream());
            }
        } else {
            throw new CmisPathNotFoundException();
        }
    } else {
        throw new CmisUnauthorizedException();
    }
}
Also used : StudioPathNotFoundException(org.craftercms.studio.api.v1.exception.StudioPathNotFoundException) DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) Document(org.apache.chemistry.opencmis.client.api.Document) CmisPathNotFoundException(org.craftercms.studio.api.v1.exception.CmisPathNotFoundException) Session(org.apache.chemistry.opencmis.client.api.Session) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Aggregations

CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)2 Document (org.apache.chemistry.opencmis.client.api.Document)2 Session (org.apache.chemistry.opencmis.client.api.Session)2 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)2 CmisUnauthorizedException (org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException)2 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)2 CmisPathNotFoundException (org.craftercms.studio.api.v1.exception.CmisPathNotFoundException)2 DataSourceRepository (org.craftercms.studio.api.v2.dal.DataSourceRepository)2 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 Folder (org.apache.chemistry.opencmis.client.api.Folder)1 CmisBaseException (org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException)1 StudioPathNotFoundException (org.craftercms.studio.api.v1.exception.StudioPathNotFoundException)1 CmisUploadItem (org.craftercms.studio.model.rest.CmisUploadItem)1