Search in sources :

Example 6 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, String path) throws SiteNotFoundException, ContentNotFoundException, ServiceLayerException {
    Set<String> toRet = new HashSet<String>();
    logger.debug("Resolving dependencies for content site: " + site + " path: " + path);
    Map<String, Set<String>> dependencies = dependencyResolver.resolve(site, path);
    List<DependencyEntity> dependencyEntities = new ArrayList<>();
    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 site: " + site + " path: " + path);
            deleteAllSourceDependencies(site, path);
            logger.debug("Insert all extracted dependencies entries for site: " + site + " path: " + path);
            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 + " path: " + path, e);
        } finally {
            generalLockService.unlock(lock);
        }
    }
    return toRet;
}
Also used : DependencyEntity(org.craftercms.studio.api.v1.dal.DependencyEntity) 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) HashSet(java.util.HashSet)

Example 7 with ContentNotFoundException

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

the class FormDmContentProcessor method writeContent.

protected void writeContent(PipelineContent content, ResultTO result) throws ServiceLayerException {
    String user = content.getProperty(DmConstants.KEY_USER);
    String site = content.getProperty(DmConstants.KEY_SITE);
    String path = content.getProperty(DmConstants.KEY_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String contentType = content.getProperty(DmConstants.KEY_CONTENT_TYPE);
    InputStream input = content.getContentStream();
    boolean isPreview = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_IS_PREVIEW));
    boolean createFolders = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_CREATE_FOLDERS));
    String unlockValue = content.getProperty(DmConstants.KEY_UNLOCK);
    boolean unlock = (!StringUtils.isEmpty(unlockValue) && unlockValue.equalsIgnoreCase("false")) ? false : true;
    String parentContentPath = path;
    if (parentContentPath.endsWith(FILE_SEPARATOR + fileName)) {
        parentContentPath = parentContentPath.replace(FILE_SEPARATOR + fileName, "");
    } else {
        path = path + FILE_SEPARATOR + fileName;
    }
    try {
        // look up the path content first
        ContentItemTO parentItem = contentService.getContentItem(site, parentContentPath, 0);
        boolean parentContentExists = contentService.contentExists(site, parentContentPath);
        if (!parentContentExists && createFolders) {
            parentItem = createMissingFoldersInPath(site, path, isPreview);
        }
        if (parentItem != null) {
            // look up the path content first
            if (parentItem.getName().equals(fileName)) {
                ContentItemTO item = contentService.getContentItem(site, path, 0);
                InputStream existingContent = contentService.getContent(site, path);
                updateFile(site, item, path, input, user, isPreview, unlock, result);
                content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                if (unlock) {
                    // TODO: We need ability to lock/unlock content in repo
                    contentService.unLockContent(site, path);
                    logger.debug("Unlocked the content " + parentContentPath);
                }
                return;
            } else {
                // otherwise, create new one
                if (path.endsWith(DmConstants.XML_PATTERN) && !path.endsWith(DmConstants.INDEX_FILE)) {
                    parentContentPath = path.substring(0, path.lastIndexOf(FILE_SEPARATOR));
                    parentItem = contentService.getContentItem(site, parentContentPath, 0);
                }
                boolean fileExists = contentService.contentExists(site, path);
                if (fileExists) {
                    ContentItemTO contentItem = contentService.getContentItem(site, path, 0);
                    InputStream existingContent = contentService.getContent(site, path);
                    updateFile(site, contentItem, path, input, user, isPreview, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_UPDATE);
                    if (unlock) {
                        // TODO: We need ability to lock/unlock content in repo
                        contentService.unLockContent(site, path);
                        logger.debug("Unlocked the content site: " + site + " path: " + path);
                    }
                    return;
                } else {
                    ContentItemTO newFileItem = createNewFile(site, parentItem, fileName, contentType, input, user, unlock, result);
                    content.addProperty(DmConstants.KEY_ACTIVITY_TYPE, OPERATION_CREATE);
                    return;
                }
            }
        } else {
            throw new ContentNotFoundException(path + " does not exist in site: " + site);
        }
    } catch (ContentNotFoundException | RepositoryLockedException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Error: ", e);
        throw new ContentNotFoundException("Unexpected exception ", e);
    } finally {
        ContentUtils.release(input);
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) RepositoryLockedException(org.craftercms.studio.api.v2.exception.RepositoryLockedException) InputStream(java.io.InputStream) 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 8 with ContentNotFoundException

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

the class WorkflowServiceImpl method submitToGoLive.

protected List<DmError> submitToGoLive(List<DmDependencyTO> submittedItems, ZonedDateTime scheduledDate, boolean sendEmail, boolean submitForDeletion, RequestContext requestContext, String submissionComment, String environment) throws ServiceLayerException {
    List<DmError> errors = new ArrayList<DmError>();
    String site = requestContext.getSite();
    String submittedBy = requestContext.getUser();
    for (DmDependencyTO submittedItem : submittedItems) {
        try {
            DependencyRules rule = new DependencyRules(site);
            rule.setContentService(contentService);
            rule.setObjectStateService(objectStateService);
            submitThisAndReferredComponents(submittedItem, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
            List<DmDependencyTO> children = submittedItem.getChildren();
            if (children != null && !submitForDeletion) {
                for (DmDependencyTO child : children) {
                    if (!child.isReference()) {
                        submitThisAndReferredComponents(child, site, scheduledDate, sendEmail, submitForDeletion, submittedBy, rule, submissionComment, environment);
                    }
                }
            }
        } catch (ContentNotFoundException e) {
            errors.add(new DmError(site, submittedItem.getUri(), e));
        }
    }
    notificationService.notifyApprovesContentSubmission(site, null, getDeploymentPaths(submittedItems), submittedBy, scheduledDate, submitForDeletion, submissionComment, Locale.ENGLISH);
    return errors;
}
Also used : ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DmError(org.craftercms.studio.api.v1.to.DmError) ArrayList(java.util.ArrayList) DependencyRules(org.craftercms.studio.api.v1.service.dependency.DependencyRules) DmDependencyTO(org.craftercms.studio.api.v1.to.DmDependencyTO)

Example 9 with ContentNotFoundException

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

the class ContentTypeServiceImpl method changeContentType.

@Override
@ValidateParams
public boolean changeContentType(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path, @ValidateStringParam(name = "contentType") String contentType) throws ServiceLayerException {
    ContentTypeConfigTO contentTypeConfigTO = getContentType(site, contentType);
    if (contentTypeConfigTO.getFormPath().equalsIgnoreCase(DmConstants.CONTENT_TYPE_CONFIG_FORM_PATH_SIMPLE)) {
        // Simple form engine is not using templates - skip copying template and merging content
        return true;
    }
    // get new template and the current data and merge data
    ContentItemTO item = contentService.getContentItem(site, path, 0);
    if (item != null) {
        contentService.lockContent(site, path);
        Document original = null;
        try {
            original = contentService.getContentAsDocument(site, path);
        } catch (DocumentException e) {
            logger.error("Error while getting document for site: " + site + " path: " + path, e);
            return false;
        }
        throw new RuntimeException("Is it getting here?");
    } else {
        throw new ContentNotFoundException(path + " is not a valid content path.");
    }
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ContentTypeConfigTO(org.craftercms.studio.api.v1.to.ContentTypeConfigTO) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 10 with ContentNotFoundException

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

the class ContentServiceImpl method getContentAsDocument.

@Override
@ValidateParams
public Document getContentAsDocument(@ValidateStringParam(name = "site") String site, @ValidateSecurePathParam(name = "path") String path) throws DocumentException {
    // TODO: SJ: Refactor in 4.x as this already exists in Crafter Core (which is part of the new Studio)
    Document retDocument = null;
    InputStream is = null;
    try {
        is = this.getContent(site, path);
    } catch (ContentNotFoundException | CryptoException e) {
        logger.debug("Content not found for path {0}", e, path);
    }
    if (is != null) {
        try {
            SAXReader saxReader = new SAXReader();
            try {
                saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
                saxReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
                saxReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            } catch (SAXException ex) {
                logger.error("Unable to turn off external entity loading, This could be a security risk.", ex);
            }
            retDocument = saxReader.read(is);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException err) {
                logger.debug("Error closing stream for path {0}", err, path);
            }
        }
    }
    return retDocument;
}
Also used : ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) IOException(java.io.IOException) Document(org.dom4j.Document) CryptoException(org.craftercms.commons.crypto.CryptoException) SAXException(org.xml.sax.SAXException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

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