Search in sources :

Example 1 with ConfigurationException

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;
}
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 ConfigurationException

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;
}
Also used : DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) CmisRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.CmisRepositoryNotFoundException)

Example 3 with ConfigurationException

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;
}
Also used : HashMap(java.util.HashMap) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) InvalidConfigurationException(org.craftercms.studio.api.v2.exception.configuration.InvalidConfigurationException) Element(org.dom4j.Element) Node(org.dom4j.Node) DocumentException(org.dom4j.DocumentException) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.dom4j.Document)

Example 4 with ConfigurationException

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;
}
Also used : ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with ConfigurationException

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;
}
Also used : CmisContentItem(org.craftercms.studio.api.v2.dal.CmisContentItem) PaginatedResultList(org.craftercms.studio.model.rest.PaginatedResultList) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

ArrayList (java.util.ArrayList)5 CmisContentItem (org.craftercms.studio.api.v2.dal.CmisContentItem)4 DataSourceRepository (org.craftercms.studio.api.v2.dal.DataSourceRepository)4 StringTokenizer (java.util.StringTokenizer)3 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)3 Document (org.apache.chemistry.opencmis.client.api.Document)3 Session (org.apache.chemistry.opencmis.client.api.Session)3 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)3 ConfigurationException (org.craftercms.studio.api.v2.exception.configuration.ConfigurationException)3 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Folder (org.apache.chemistry.opencmis.client.api.Folder)2 BlobStore (org.craftercms.commons.file.blob.BlobStore)2 StudioBlobStore (org.craftercms.studio.api.v2.repository.blob.StudioBlobStore)2 CmisUploadItem (org.craftercms.studio.model.rest.CmisUploadItem)2 PaginatedResultList (org.craftercms.studio.model.rest.PaginatedResultList)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 Test (org.testng.annotations.Test)2 InputStream (java.io.InputStream)1