Search in sources :

Example 1 with CmisUploadItem

use of org.craftercms.studio.model.rest.CmisUploadItem 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 CmisUploadItem

use of org.craftercms.studio.model.rest.CmisUploadItem in project studio by craftercms.

the class CmisController method uploadContent.

@PostMapping(value = "/api/2/cmis/upload")
public ResponseBody uploadContent(HttpServletRequest httpServletRequest) throws IOException, CmisUnavailableException, CmisPathNotFoundException, CmisTimeoutException, CmisRepositoryNotFoundException, FileUploadException, InvalidParametersException, ConfigurationException {
    ServletFileUpload servletFileUpload = new ServletFileUpload();
    FileItemIterator itemIterator = servletFileUpload.getItemIterator(httpServletRequest);
    String filename = StringUtils.EMPTY;
    String siteId = StringUtils.EMPTY;
    String cmisRepoId = StringUtils.EMPTY;
    String cmisPath = StringUtils.EMPTY;
    CmisUploadItem cmisUploadItem = new CmisUploadItem();
    while (itemIterator.hasNext()) {
        FileItemStream item = itemIterator.next();
        String name = item.getFieldName();
        try (InputStream stream = item.openStream()) {
            if (item.isFormField()) {
                switch(name) {
                    case REQUEST_PARAM_SITEID:
                        siteId = Streams.asString(stream);
                        break;
                    case REQUEST_PARAM_CMIS_REPO_ID:
                        cmisRepoId = Streams.asString(stream);
                        break;
                    case REQUEST_PARAM_CMIS_PATH:
                        cmisPath = Streams.asString(stream);
                        break;
                    default:
                        // Unknown parameter, just skip it...
                        break;
                }
            } else {
                filename = item.getName();
                if (StringUtils.isEmpty(siteId)) {
                    throw new InvalidParametersException("Invalid siteId");
                }
                if (StringUtils.isEmpty(cmisRepoId)) {
                    throw new InvalidParametersException("Invalid cmisRepoId");
                }
                if (StringUtils.isEmpty(cmisPath)) {
                    throw new InvalidParametersException("Invalid cmisPath");
                }
                cmisUploadItem = cmisService.uploadContent(siteId, cmisRepoId, cmisPath, filename, stream);
            }
        }
    }
    ResponseBody responseBody = new ResponseBody();
    ResultOne<CmisUploadItem> result = new ResultOne<CmisUploadItem>();
    result.setResponse(OK);
    result.setEntity(RESULT_KEY_ITEM, cmisUploadItem);
    responseBody.setResult(result);
    return responseBody;
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ResultOne(org.craftercms.studio.model.rest.ResultOne) FileItemStream(org.apache.commons.fileupload.FileItemStream) CmisUploadItem(org.craftercms.studio.model.rest.CmisUploadItem) InputStream(java.io.InputStream) InvalidParametersException(org.craftercms.studio.api.v2.exception.InvalidParametersException) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

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