Search in sources :

Example 11 with ContentNotFoundException

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

the class ContentServiceImpl method copyContent.

/**
 * internal method copy that handles
 * Get dependencies is already recursive
 */
protected String copyContent(String site, String fromPath, String toPath, Set<String> processedPaths) {
    String retNewFileName = null;
    String lifecycleOp = DmContentLifeCycleService.ContentLifeCycleOperation.COPY.toString();
    String user = securityService.getCurrentUser();
    String copyPath = null;
    try {
        Map<String, String> copyPathMap = constructNewPathforCutCopy(site, fromPath, toPath, true);
        copyPath = copyPathMap.get("FILE_PATH");
        String copyPathModifier = copyPathMap.get("MODIFIER");
        String copyPathFileName = copyPathMap.get("FILE_NAME");
        String copyPathFolder = copyPathMap.get("FILE_FOLDER");
        String copyPathOnly = copyPath.substring(0, copyPath.lastIndexOf(FILE_SEPARATOR));
        String copyFileName = copyPath.substring(copyPath.lastIndexOf(FILE_SEPARATOR) + 1);
        if (!processedPaths.contains(copyPath)) {
            ContentItemTO fromItem = getContentItem(site, fromPath, 0);
            if (fromItem.isFolder()) {
                createFolder(site, copyPathOnly, copyFileName);
                // copy was successful, return the new name
                retNewFileName = copyPath;
            } else {
                InputStream copyContent = null;
                try {
                    String contentType = fromItem.getContentType();
                    InputStream fromContent = getContent(site, fromPath);
                    if (fromPath.endsWith(DmConstants.XML_PATTERN)) {
                        Document fromDocument = ContentUtils.convertStreamToXml(fromContent);
                        Map<String, String> fromPageIds = getContentIds(fromDocument);
                        logger.debug("copying file for site {0} from {1} to {2}, new name is {3}", site, fromPath, toPath, copyPath);
                        // come up with a new object ID and group ID for the object
                        Map<String, String> copyObjectIds = contentItemIdGenerator.getIds();
                        Map<String, String> copyDependencies = getCopyDependencies(site, fromPath, fromPath);
                        copyDependencies = getItemSpecificDependencies(site, fromPath, fromDocument, copyDependencies);
                        logger.debug("Calculated copy dependencies: {0}, {1}", fromPath, copyDependencies);
                        // Duplicate the children
                        for (String dependencyKey : copyDependencies.keySet()) {
                            String dependencyPath = copyDependencies.get(dependencyKey);
                            String copyDepPath = dependencyPath;
                            // try a simple substitution
                            copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_ID), copyObjectIds.get(KEY_PAGE_ID));
                            copyDepPath = copyDepPath.replaceAll(fromPageIds.get(KEY_PAGE_GROUP_ID), copyObjectIds.get(KEY_PAGE_GROUP_ID));
                            ContentItemTO targetPathItem = getContentItem(site, copyDepPath);
                            if (targetPathItem != null && targetPathItem.isFolder()) {
                                copyDepPath = copyDepPath + FILE_SEPARATOR + FilenameUtils.getName(dependencyKey);
                                copyDepPath = copyDepPath.replaceAll(FILE_SEPARATOR + FILE_SEPARATOR, FILE_SEPARATOR);
                            } else if (!copyDepPath.endsWith(DmConstants.XML_PATTERN)) {
                                copyDepPath = ContentUtils.getParentUrl(copyDepPath);
                            }
                            logger.debug("Translated dependency path from {0} to {1}", dependencyPath, copyDepPath);
                            String newCopyDepthPath = copyContent(site, dependencyKey, copyDepPath, processedPaths);
                            fromDocument = replaceCopyDependency(fromDocument, dependencyKey, newCopyDepthPath);
                        }
                        // update the file name / folder values
                        Document copyDocument = updateContentOnCopy(fromDocument, copyPathFileName, copyPathFolder, copyObjectIds, copyPathModifier);
                        copyContent = ContentUtils.convertDocumentToStream(copyDocument, CONTENT_ENCODING);
                    }
                    // This code is very similar to what is in writeContent. Consolidate this code?
                    Map<String, String> params = new HashMap<String, String>();
                    params.put(DmConstants.KEY_SITE, site);
                    params.put(DmConstants.KEY_PATH, copyPathOnly);
                    params.put(DmConstants.KEY_FILE_NAME, copyFileName);
                    params.put(DmConstants.KEY_USER, user);
                    params.put(DmConstants.KEY_CONTENT_TYPE, contentType);
                    params.put(DmConstants.KEY_CREATE_FOLDERS, "true");
                    params.put(DmConstants.KEY_EDIT, "true");
                    params.put(DmConstants.KEY_ACTIVITY_TYPE, "false");
                    params.put(DmConstants.KEY_SKIP_CLEAN_PREVIEW, "true");
                    params.put(DmConstants.KEY_COPIED_CONTENT, "true");
                    params.put(DmConstants.CONTENT_LIFECYCLE_OPERATION, lifecycleOp);
                    String id = site + ":" + copyPathOnly + ":" + copyFileName + ":" + contentType;
                    // processContent will close the input stream
                    if (copyFileName.endsWith(DmConstants.XML_PATTERN)) {
                        processContent(id, copyContent, true, params, DmConstants.CONTENT_CHAIN_FORM);
                    } else {
                        processContent(id, fromContent, false, params, DmConstants.CONTENT_CHAIN_ASSET);
                    }
                    ItemState itemState = objectStateService.getObjectState(site, copyPath);
                    if (itemState == null) {
                        ContentItemTO copyItem = getContentItem(site, copyPath, 0);
                        objectStateService.insertNewEntry(site, copyItem);
                        objectStateService.setSystemProcessing(site, copyPath, false);
                    }
                    // copy was successful, return the new name
                    retNewFileName = copyPath;
                    // track that we already copied so we don't follow a circular dependency
                    processedPaths.add(copyPath);
                } catch (ContentNotFoundException eContentNotFound) {
                    logger.debug("Content not found while copying content for site {0} from {1} to {2}," + " new name is {3}", eContentNotFound, site, fromPath, toPath, copyPath);
                } catch (DocumentException eParseException) {
                    logger.error("General Error while copying content for site {0} from {1} to {2}," + " new name is {3}", eParseException, site, fromPath, toPath, copyPath);
                } catch (CryptoException e) {
                    logger.error("Unexpected Error while copying content for site {0} from {1} to {2}," + " new name is {3}", e, site, fromPath, toPath, copyPath);
                } finally {
                    IOUtils.closeQuietly(copyContent);
                }
            }
        } else {
            // no need to process
            retNewFileName = copyPath;
        }
    } catch (ServiceLayerException eServiceLayerException) {
        logger.info("General Error while copying content for site {0} from {1} to {2}, new name is {3}", eServiceLayerException, site, fromPath, toPath, copyPath);
    }
    return retNewFileName;
}
Also used : ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) HashMap(java.util.HashMap) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream) ItemState(org.craftercms.studio.api.v1.dal.ItemState) DocumentException(org.dom4j.DocumentException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) Document(org.dom4j.Document) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 12 with ContentNotFoundException

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

the class AssetProcessingServiceImpl method processAsset.

@Override
public Map<String, Object> processAsset(String site, String folder, String assetName, InputStream in, String isImage, String allowedWidth, String allowedHeight, String allowLessSize, String draft, String unlock, String systemAsset) {
    String repoPath = UrlUtils.concat(folder, assetName);
    InputStream configIn;
    try {
        try {
            configIn = contentService.getContent(site, configPath);
        } catch (ContentNotFoundException e) {
            // Ignore if file couldn't be found
            configIn = null;
        }
        if (configIn != null) {
            List<ProcessorPipelineConfiguration> pipelinesConfig = configReader.readConfig(configIn);
            if (CollectionUtils.isNotEmpty(pipelinesConfig)) {
                Asset input = createAssetFromInputStream(repoPath, in);
                try {
                    Set<Asset> finalOutputs = new LinkedHashSet<>();
                    for (ProcessorPipelineConfiguration pipelineConfig : pipelinesConfig) {
                        AssetProcessorPipeline pipeline = pipelineResolver.getPipeline(pipelineConfig);
                        List<Asset> outputs = pipeline.processAsset(pipelineConfig, input);
                        if (CollectionUtils.isNotEmpty(outputs)) {
                            finalOutputs.addAll(outputs);
                        }
                    }
                    if (CollectionUtils.isNotEmpty(finalOutputs)) {
                        List<Map<String, Object>> results = writeOutputs(site, finalOutputs, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
                        // one main result specified by config -- Alfonso
                        if (CollectionUtils.isNotEmpty(results)) {
                            return results.get(0);
                        } else {
                            return Collections.emptyMap();
                        }
                    } else {
                        // No outputs mean that the input wasn't matched by any pipeline and processing was skipped
                        logger.debug("No pipeline matched for {0}. Skipping asset processing...", repoPath);
                        // We already read input so open the temp file
                        try (InputStream assetIn = Files.newInputStream(input.getFilePath())) {
                            return contentService.writeContentAsset(site, folder, assetName, assetIn, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
                        }
                    }
                } finally {
                    try {
                        Files.delete(input.getFilePath());
                    } catch (IOException e) {
                    // delete silently
                    }
                }
            } else {
                // Ignore if no pipelines config
                logger.debug("No asset processing pipelines config found at {0}. Skipping asset processing...", repoPath);
                return contentService.writeContentAsset(site, folder, assetName, in, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
            }
        } else {
            logger.debug("No asset processing config found at {0}. Skipping asset processing...", repoPath);
            return contentService.writeContentAsset(site, folder, assetName, in, isImage, allowedWidth, allowedHeight, allowLessSize, draft, unlock, systemAsset);
        }
    } catch (Exception e) {
        logger.error("Error processing asset", e);
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", e.getMessage());
        result.put("error", e);
        return result;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) InputStream(java.io.InputStream) IOException(java.io.IOException) AssetProcessingException(org.craftercms.studio.api.v1.exception.AssetProcessingException) IOException(java.io.IOException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) ProcessorPipelineConfiguration(org.craftercms.studio.api.v1.asset.processing.ProcessorPipelineConfiguration) AssetProcessorPipeline(org.craftercms.studio.api.v1.asset.processing.AssetProcessorPipeline) Asset(org.craftercms.studio.api.v1.asset.Asset) HashMap(java.util.HashMap) Map(java.util.Map)

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