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;
}
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();
}
}
Aggregations