Search in sources :

Example 1 with ContentNotFoundException

use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.

the class GitContentRepository method getContent.

@Override
public InputStream getContent(String site, String path) throws ContentNotFoundException, CryptoException {
    InputStream toReturn = null;
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
    if (repo == null) {
        throw new ContentNotFoundException("Repository not found for site " + site);
    }
    try {
        RevTree tree = helper.getTreeForLastCommit(repo);
        try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
            // pick the first item in the list
            if (tw != null && tw.getObjectId(0) != null) {
                ObjectId id = tw.getObjectId(0);
                ObjectLoader objectLoader = repo.open(id);
                toReturn = objectLoader.openStream();
                tw.close();
            }
        } catch (IOException e) {
            logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
        }
    } catch (IOException e) {
        logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
    }
    return toReturn;
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 2 with ContentNotFoundException

use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.

the class GitContentRepository method getContentVersion.

@Override
public InputStream getContentVersion(String site, String path, String version) throws ContentNotFoundException {
    InputStream toReturn = null;
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
        RevTree tree = helper.getTreeForCommit(repo, version);
        if (tree != null) {
            try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
                if (tw != null) {
                    ObjectId id = tw.getObjectId(0);
                    ObjectLoader objectLoader = repo.open(id);
                    toReturn = objectLoader.openStream();
                    tw.close();
                }
            } catch (IOException e) {
                logger.error("Error while getting content for file at site: " + site + " path: " + path + " version: " + version, e);
            }
        }
    } catch (IOException | CryptoException e) {
        logger.error("Failed to create RevTree for site: " + site + " path: " + path + " version: " + version, e);
    }
    return toReturn;
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 3 with ContentNotFoundException

use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.

the class GitContentRepository method revertContent.

@Override
public String revertContent(String site, String path, String version, boolean major, String comment) {
    // TODO: SJ: refactor to remove the notion of a major/minor for 3.1+
    String commitId = null;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        InputStream versionContent = getContentVersion(site, path, version);
        commitId = writeContent(site, path, versionContent);
        createVersion(site, path, major);
    } catch (ContentNotFoundException err) {
        logger.error("error reverting content for site:  " + site + " path: " + path, err);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream)

Example 4 with ContentNotFoundException

use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.

the class FormDmContentProcessor method createNewFile.

/**
 * create new file to the given path. If the path is a file name, it will
 * create a new folder with the same name as the file name (without the
 * prefix) and move the existing file to the folder created. Then it creates
 * new file to the folder
 *
 * @param site
 *            Site name
 * @param fileName
 *            new file name
 * @param contentType
 * 			content type
 * @param input
 *            file content
 * @param user
 *            current user
 * @throws ContentNotFoundException
 */
protected ContentItemTO createNewFile(String site, ContentItemTO parentItem, String fileName, String contentType, InputStream input, String user, boolean unlock, ResultTO result) throws ContentNotFoundException, SiteNotFoundException {
    ContentItemTO fileItem = null;
    if (parentItem != null) {
        // convert file to folder if target path is a file
        String folderPath = fileToFolder(site, parentItem.getUri());
        try {
            contentService.writeContent(site, parentItem.getUri() + FILE_SEPARATOR + fileName, input);
            if (!objectMetadataManager.metadataExist(site, parentItem.getUri() + FILE_SEPARATOR + fileName)) {
                objectMetadataManager.insertNewObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
            }
            Map<String, Object> properties = new HashMap<>();
            properties.put(ItemMetadata.PROP_NAME, fileName);
            properties.put(ItemMetadata.PROP_MODIFIED, ZonedDateTime.now(ZoneOffset.UTC));
            properties.put(ItemMetadata.PROP_CREATOR, user);
            properties.put(ItemMetadata.PROP_MODIFIER, user);
            properties.put(ItemMetadata.PROP_OWNER, user);
            if (unlock) {
                properties.put(ItemMetadata.PROP_LOCK_OWNER, StringUtils.EMPTY);
            } else {
                properties.put(ItemMetadata.PROP_LOCK_OWNER, user);
            }
            objectMetadataManager.setObjectMetadata(site, parentItem.getUri() + FILE_SEPARATOR + fileName, properties);
            result.setCommitId(objectMetadataManager.getProperties(site, parentItem.getUri() + FILE_SEPARATOR + fileName).getCommitId());
        } catch (Exception e) {
            logger.error("Error writing new file: " + fileName, e);
        } finally {
            IOUtils.closeQuietly(input);
        }
        // unlock the content upon save
        if (unlock) {
            contentRepository.unLockItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName);
        } else {
        }
        fileItem = contentService.getContentItem(site, parentItem.getUri() + FILE_SEPARATOR + fileName, 0);
        return fileItem;
    } else {
        throw new ContentNotFoundException(parentItem.getUri() + " does not exist in site: " + site);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException)

Example 5 with ContentNotFoundException

use of org.craftercms.studio.api.v1.exception.ContentNotFoundException in project studio by craftercms.

the class DependencyServiceImpl method upsertDependencies.

@Override
public Set<String> upsertDependencies(String site, List<String> paths) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
    Set<String> toRet = new HashSet<String>();
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    StringBuilder sbPaths = new StringBuilder();
    logger.debug("Resolving dependencies for list of paths.");
    for (String path : paths) {
        sbPaths.append("\n").append(path);
        logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
        Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
        if (dependencies != null) {
            logger.debug("Found " + dependencies.size() + " dependencies. " + "Create entities to insert into database.");
            for (String type : dependencies.keySet()) {
                dependencyEntities.addAll(createDependencyEntities(site, path, dependencies.get(type), type, toRet));
            }
        }
    }
    logger.debug("Preparing transaction for database updates.");
    DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
    defaultTransactionDefinition.setName("upsertDependencies");
    String lock = site + ":upsertDependencies";
    generalLockService.lock(lock);
    logger.debug("Starting transaction.");
    TransactionStatus txStatus = transactionManager.getTransaction(defaultTransactionDefinition);
    try {
        logger.debug("Delete all source dependencies for list of paths site: " + site);
        for (String path : paths) {
            deleteAllSourceDependencies(site, path);
        }
        logger.debug("Insert all extracted dependencies entries lof list of paths for site: " + site);
        insertDependenciesIntoDatabase(dependencyEntities);
        logger.debug("Committing transaction.");
        transactionManager.commit(txStatus);
    } catch (Exception e) {
        logger.debug("Rolling back transaction.", e);
        transactionManager.rollback(txStatus);
        throw new ServiceLayerException("Failed to upsert dependencies for site: " + site + " paths: " + sbPaths.toString(), e);
    } finally {
        generalLockService.unlock(lock);
    }
    return toRet;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) HashSet(java.util.HashSet)

Aggregations

ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)11 InputStream (java.io.InputStream)7 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 IOException (java.io.IOException)4 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)4 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CryptoException (org.craftercms.commons.crypto.CryptoException)3 Document (org.dom4j.Document)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)2 DependencyEntity (org.craftercms.studio.api.v1.dal.DependencyEntity)2 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)2 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)2 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)2 RepositoryLockedException (org.craftercms.studio.api.v2.exception.RepositoryLockedException)2 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)2 DocumentException (org.dom4j.DocumentException)2