Search in sources :

Example 1 with DataSourceRepository

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

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

Example 3 with DataSourceRepository

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;
}
Also used : CmisContentItem(org.craftercms.studio.api.v2.dal.CmisContentItem) DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) Folder(org.apache.chemistry.opencmis.client.api.Folder) Document(org.apache.chemistry.opencmis.client.api.Document) Session(org.apache.chemistry.opencmis.client.api.Session) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission)

Example 4 with DataSourceRepository

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();
    }
}
Also used : StudioPathNotFoundException(org.craftercms.studio.api.v1.exception.StudioPathNotFoundException) DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) Document(org.apache.chemistry.opencmis.client.api.Document) 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 5 with DataSourceRepository

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;
}
Also used : X509Certificate(java.security.cert.X509Certificate) CONFIGURATION_SITE_DATA_SOURCES_CONFIG_LOCATION(org.craftercms.studio.api.v2.utils.StudioConfiguration.CONFIGURATION_SITE_DATA_SOURCES_CONFIG_LOCATION) SSLContext(javax.net.ssl.SSLContext) ATOMPUB_URL(org.apache.chemistry.opencmis.commons.SessionParameter.ATOMPUB_URL) OBJECT_ID(org.apache.chemistry.opencmis.commons.PropertyIds.OBJECT_ID) QueryResult(org.apache.chemistry.opencmis.client.api.QueryResult) TrustManager(javax.net.ssl.TrustManager) SessionParameter(org.apache.chemistry.opencmis.commons.SessionParameter) SecureRandom(java.security.SecureRandom) SITE_ID_RESOURCE_ID(org.craftercms.studio.permissions.PermissionResolverImpl.SITE_ID_RESOURCE_ID) OBJECT_TYPE_ID(org.apache.chemistry.opencmis.commons.PropertyIds.OBJECT_TYPE_ID) CmisUnavailableException(org.craftercms.studio.api.v1.exception.CmisUnavailableException) Map(java.util.Map) PATH_RESOURCE_ID(org.craftercms.studio.permissions.PermissionResolverImpl.PATH_RESOURCE_ID) HostnameVerifier(javax.net.ssl.HostnameVerifier) Folder(org.apache.chemistry.opencmis.client.api.Folder) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) CmisService(org.craftercms.studio.api.v2.service.cmis.CmisService) PASSWORD(org.apache.chemistry.opencmis.commons.SessionParameter.PASSWORD) KeyManagementException(java.security.KeyManagementException) List(java.util.List) FILE_SEPARATOR(org.craftercms.studio.api.v1.constant.StudioConstants.FILE_SEPARATOR) ItemIterable(org.apache.chemistry.opencmis.client.api.ItemIterable) StudioConfiguration(org.craftercms.studio.api.v2.utils.StudioConfiguration) BINDING_TYPE(org.apache.chemistry.opencmis.commons.SessionParameter.BINDING_TYPE) CmisContentItem(org.craftercms.studio.api.v2.dal.CmisContentItem) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) DefaultPermission(org.craftercms.commons.security.permissions.DefaultPermission) FilenameUtils(org.apache.commons.io.FilenameUtils) CmisRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.CmisRepositoryNotFoundException) CMIS_FOLDER(org.apache.chemistry.opencmis.commons.enums.BaseTypeId.CMIS_FOLDER) NAME(org.apache.chemistry.opencmis.commons.PropertyIds.NAME) CmisTimeoutException(org.craftercms.studio.api.v1.exception.CmisTimeoutException) Logger(org.craftercms.studio.api.v1.log.Logger) HashMap(java.util.HashMap) Session(org.apache.chemistry.opencmis.client.api.Session) BaseTypeId(org.apache.chemistry.opencmis.commons.enums.BaseTypeId) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) ArrayList(java.util.ArrayList) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) StringTokenizer(java.util.StringTokenizer) SessionFactory(org.apache.chemistry.opencmis.client.api.SessionFactory) LoggerFactory(org.craftercms.studio.api.v1.log.LoggerFactory) CmisPathNotFoundException(org.craftercms.studio.api.v1.exception.CmisPathNotFoundException) CmisUploadItem(org.craftercms.studio.model.rest.CmisUploadItem) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) SessionFactoryImpl(org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl) ConfigurationService(org.craftercms.studio.api.v2.service.config.ConfigurationService) ContentService(org.craftercms.studio.api.v1.service.content.ContentService) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) ConfigurationException(org.craftercms.studio.api.v2.exception.configuration.ConfigurationException) CmisConnectionException(org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException) HasPermission(org.craftercms.commons.security.permissions.annotations.HasPermission) ProtectedResourceId(org.craftercms.commons.security.permissions.annotations.ProtectedResourceId) StudioPathNotFoundException(org.craftercms.studio.api.v1.exception.StudioPathNotFoundException) USER(org.apache.chemistry.opencmis.commons.SessionParameter.USER) CMIS_DOCUMENT(org.apache.chemistry.opencmis.commons.enums.BaseTypeId.CMIS_DOCUMENT) Repository(org.apache.chemistry.opencmis.client.api.Repository) Paths(java.nio.file.Paths) X509TrustManager(javax.net.ssl.X509TrustManager) Document(org.apache.chemistry.opencmis.client.api.Document) COOKIES(org.apache.chemistry.opencmis.commons.SessionParameter.COOKIES) ATOMPUB(org.apache.chemistry.opencmis.commons.enums.BindingType.ATOMPUB) InputStream(java.io.InputStream) SessionFactory(org.apache.chemistry.opencmis.client.api.SessionFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) HostnameVerifier(javax.net.ssl.HostnameVerifier) DataSourceRepository(org.craftercms.studio.api.v2.dal.DataSourceRepository) Repository(org.apache.chemistry.opencmis.client.api.Repository) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisConnectionException(org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException) CmisUnavailableException(org.craftercms.studio.api.v1.exception.CmisUnavailableException) CmisTimeoutException(org.craftercms.studio.api.v1.exception.CmisTimeoutException) Session(org.apache.chemistry.opencmis.client.api.Session)

Aggregations

DataSourceRepository (org.craftercms.studio.api.v2.dal.DataSourceRepository)6 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)5 Document (org.apache.chemistry.opencmis.client.api.Document)5 Session (org.apache.chemistry.opencmis.client.api.Session)5 HasPermission (org.craftercms.commons.security.permissions.annotations.HasPermission)5 StringTokenizer (java.util.StringTokenizer)4 ArrayList (java.util.ArrayList)3 Folder (org.apache.chemistry.opencmis.client.api.Folder)3 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)3 CmisUnauthorizedException (org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException)3 CmisPathNotFoundException (org.craftercms.studio.api.v1.exception.CmisPathNotFoundException)3 CmisContentItem (org.craftercms.studio.api.v2.dal.CmisContentItem)3 HashMap (java.util.HashMap)2 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)2 QueryResult (org.apache.chemistry.opencmis.client.api.QueryResult)2 StudioPathNotFoundException (org.craftercms.studio.api.v1.exception.StudioPathNotFoundException)2 InputStream (java.io.InputStream)1 Paths (java.nio.file.Paths)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1