use of org.craftercms.studio.api.v2.dal.DataSourceRepository 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.v2.dal.DataSourceRepository in project studio by craftercms.
the class CmisServiceImpl method getConfiguration.
private DataSourceRepository getConfiguration(String site, String cmisRepo) throws CmisRepositoryNotFoundException, ConfigurationException {
HierarchicalConfiguration<?> config = configurationService.getXmlConfiguration(site, getConfigLocation());
HierarchicalConfiguration<?> repo = config.childConfigurationsAt(REPOSITORY_CONFIG_KEY).stream().filter(r -> cmisRepo.equals(r.getString(ID_PROPERTY))).findFirst().orElseThrow(CmisRepositoryNotFoundException::new);
DataSourceRepository repositoryConfig = new DataSourceRepository();
repositoryConfig.setId(repo.getString(ID_PROPERTY));
repositoryConfig.setType(repo.getString(TYPE_PROPERTY));
repositoryConfig.setUrl(repo.getString(URL_PROPERTY));
repositoryConfig.setUsername(repo.getString(USERNAME_PROPERTY));
repositoryConfig.setPassword(repo.getString(PASSWORD_PROPERTY));
repositoryConfig.setBasePath(repo.getString(BASE_PATH_PROPERTY));
repositoryConfig.setDownloadUrlRegex(repo.getString(DOWNLOAD_URL_REGEX_PROPERTY));
repositoryConfig.setUseSsl(repo.getBoolean(USE_SSL_PROPERTY, false));
return repositoryConfig;
}
use of org.craftercms.studio.api.v2.dal.DataSourceRepository in project studio by craftercms.
the class CmisServiceImpl method list.
@Override
@HasPermission(type = DefaultPermission.class, action = "list_cmis")
public List<CmisContentItem> list(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepo, String path) throws CmisRepositoryNotFoundException, CmisUnavailableException, CmisTimeoutException, ConfigurationException {
List<CmisContentItem> items = new ArrayList<CmisContentItem>();
DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepo);
Session session = createCMISSession(repositoryConfig);
if (session != null) {
String contentPath = Paths.get(repositoryConfig.getBasePath(), path).toString();
CmisObject cmisObject = session.getObjectByPath(contentPath);
if (cmisObject != null && CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
Folder folder = (Folder) cmisObject;
Iterable<CmisObject> iterable = folder.getChildren();
for (CmisObject cmisItem : iterable) {
CmisContentItem item = new CmisContentItem();
item.setItemName(cmisItem.getName());
if (CMIS_DOCUMENT.equals(cmisItem.getBaseTypeId())) {
Document cmisDoc = (Document) cmisItem;
item.setItemPath(cmisDoc.getPaths().get(0));
item.setMimeType(cmisDoc.getContentStreamMimeType());
String contentId = cmisDoc.getId();
StringTokenizer st = new StringTokenizer(contentId, ";");
if (st.hasMoreTokens()) {
item.setItemId(st.nextToken());
}
item.setSize(cmisDoc.getContentStreamLength());
items.add(item);
} else if (CMIS_FOLDER.equals(cmisItem.getBaseTypeId())) {
Folder cmisFolder = (Folder) cmisItem;
item.setItemId(cmisFolder.getId());
item.setItemPath(cmisFolder.getPath());
item.setMimeType(MIME_TYPE_FOLDER);
item.setSize(-1);
items.add(item);
}
}
}
}
return items;
}
use of org.craftercms.studio.api.v2.dal.DataSourceRepository 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();
}
}
use of org.craftercms.studio.api.v2.dal.DataSourceRepository in project studio by craftercms.
the class CmisServiceImpl method createCMISSession.
private Session createCMISSession(DataSourceRepository config) throws CmisUnavailableException, CmisTimeoutException {
if (config.isUseSsl()) {
SSLContext sc = null;
try {
sc = getSSLContext();
// Ignore differences between given hostname and certificate hostname
HostnameVerifier hv = (hostname, session) -> true;
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
logger.error("Error initializing SSL context", e);
}
}
// Create a SessionFactory and set up the SessionParameter map
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String, String> parameter = new HashMap<String, String>();
parameter.put(USER, config.getUsername());
parameter.put(PASSWORD, config.getPassword());
// connection settings - we're connecting to a public cmis repo,
// using the AtomPUB binding, but there are other options here,
// or you can substitute your own URL
parameter.put(ATOMPUB_URL, config.getUrl());
parameter.put(BINDING_TYPE, ATOMPUB.value());
parameter.put(COOKIES, "true");
// find all the repositories at this URL - there should only be one.
List<Repository> repositories = new ArrayList<Repository>();
repositories = sessionFactory.getRepositories(parameter);
// create session with the first (and only) repository
Repository repository = repositories.get(0);
parameter.put(SessionParameter.REPOSITORY_ID, repository.getId());
Session session = null;
try {
session = sessionFactory.createSession(parameter);
} catch (CmisConnectionException e) {
throw new CmisTimeoutException(e);
} catch (CmisBaseException e) {
throw new CmisUnavailableException(e);
}
return session;
}
Aggregations