use of org.craftercms.studio.api.v2.exception.configuration.ConfigurationException 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.exception.configuration.ConfigurationException 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.exception.configuration.ConfigurationException in project studio by craftercms.
the class ConfigurationServiceImpl method geRoleMappings.
@Override
@SuppressWarnings("unchecked")
public Map<String, List<String>> geRoleMappings(String siteId) throws ConfigurationException {
// TODO: Refactor this to use Apache's Commons Configuration
Map<String, List<String>> roleMappings = new HashMap<>();
String roleMappingsConfigPath = getSiteRoleMappingsConfigPath(siteId);
Document document;
try {
document = contentService.getContentAsDocument(siteId, roleMappingsConfigPath);
if (document != null) {
Element root = document.getRootElement();
if (root.getName().equals(DOCUMENT_ROLE_MAPPINGS)) {
List<Node> groupNodes = root.selectNodes(DOCUMENT_ELM_GROUPS_NODE);
for (Node node : groupNodes) {
String name = node.valueOf(DOCUMENT_ATTR_PERMISSIONS_NAME);
if (StringUtils.isNotEmpty(name)) {
List<Node> roleNodes = node.selectNodes(DOCUMENT_ELM_PERMISSION_ROLE);
List<String> roles = new ArrayList<>();
for (Node roleNode : roleNodes) {
roles.add(roleNode.getText());
}
roleMappings.put(name, roles);
}
}
}
}
} catch (DocumentException e) {
throw new ConfigurationException("Error while reading role mappings file for site " + siteId + " @ " + roleMappingsConfigPath);
}
return roleMappings;
}
use of org.craftercms.studio.api.v2.exception.configuration.ConfigurationException in project studio by craftercms.
the class GroupServiceInternalImpl method getSiteGroups.
@Override
public List<String> getSiteGroups(String siteId) throws ServiceLayerException {
Map<String, List<String>> groupRoleMapping;
try {
groupRoleMapping = configurationService.geRoleMappings(siteId);
} catch (ConfigurationException e) {
throw new ServiceLayerException("Unable to get role mappings config for site '" + siteId + "'", e);
}
List<String> groups = new ArrayList<>();
groups.addAll(groupRoleMapping.keySet());
return groups;
}
use of org.craftercms.studio.api.v2.exception.configuration.ConfigurationException in project studio by craftercms.
the class CmisController method list.
@GetMapping("/api/2/cmis/list")
public ResponseBody list(@RequestParam(value = "siteId", required = true) String siteId, @RequestParam(value = "cmisRepoId", required = true) String cmisRepoId, @RequestParam(value = "path", required = false, defaultValue = StringUtils.EMPTY) String path, @RequestParam(value = "offset", required = false, defaultValue = "0") int offset, @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) throws CmisRepositoryNotFoundException, CmisTimeoutException, CmisUnavailableException, ConfigurationException {
List<CmisContentItem> cmisContentItems = cmisService.list(siteId, cmisRepoId, path);
List<CmisContentItem> paginatedItems = PaginationUtils.paginate(cmisContentItems, offset, limit, StringUtils.EMPTY);
ResponseBody responseBody = new ResponseBody();
PaginatedResultList<CmisContentItem> result = new PaginatedResultList<>();
result.setTotal(cmisContentItems.size());
result.setOffset(offset);
result.setLimit(paginatedItems.size());
result.setResponse(OK);
responseBody.setResult(result);
result.setEntities(RESULT_KEY_ITEMS, paginatedItems);
return responseBody;
}
Aggregations