Search in sources :

Example 6 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent 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 7 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent in project studio by craftercms.

the class ProcessContentExecutorImpl method processContent.

@Override
public ResultTO processContent(String id, InputStream input, boolean isXml, Map<String, String> params, String chainName) throws ServiceLayerException {
    final ContentProcessorPipeline chain = processorChains.get(chainName);
    try {
        if (chain != null) {
            if (StringUtils.isEmpty(params.get(DmConstants.KEY_USER))) {
                String user = securityService.getCurrentUser();
                params.put(DmConstants.KEY_USER, user);
            }
            final ResultTO result = new ResultTO();
            try {
                final PipelineContent content = new PipelineContentImpl(id, input, isXml, null, StudioConstants.CONTENT_ENCODING, params);
                chain.processContent(content, result);
            } catch (ContentProcessException e) {
                logger.error("Error in chain for write content", e);
                throw e;
            } catch (RuntimeException e) {
                logger.error("Error in chain for write content", e);
                throw e;
            } finally {
                ContentUtils.release(input);
            }
            return result;
        } else {
            ContentUtils.release(input);
            throw new ServiceLayerException(chainName + " is not defined.");
        }
    } finally {
        String s = params.get(DmConstants.KEY_USER);
    // AuthenticationUtil.setFullyAuthenticatedUser(s);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) PipelineContentImpl(org.craftercms.studio.impl.v1.content.pipeline.PipelineContentImpl) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PipelineContent(org.craftercms.studio.api.v1.content.pipeline.PipelineContent) ContentProcessorPipeline(org.craftercms.studio.api.v1.content.pipeline.ContentProcessorPipeline) ResultTO(org.craftercms.studio.api.v1.to.ResultTO)

Example 8 with PipelineContent

use of org.craftercms.studio.api.v1.content.pipeline.PipelineContent in project studio by craftercms.

the class ExtractAssetDependencyProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String site = content.getProperty(DmConstants.KEY_SITE);
    String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String path = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
    try {
        dependencyService.upsertDependencies(site, path);
    } catch (ServiceLayerException e) {
        throw new ContentProcessException(e);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)6 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)5 ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)4 InputStream (java.io.InputStream)2 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 ContentProcessorPipeline (org.craftercms.studio.api.v1.content.pipeline.ContentProcessorPipeline)1 PipelineContent (org.craftercms.studio.api.v1.content.pipeline.PipelineContent)1 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)1 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)1 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)1 RepositoryLockedException (org.craftercms.studio.api.v2.exception.RepositoryLockedException)1 PipelineContentImpl (org.craftercms.studio.impl.v1.content.pipeline.PipelineContentImpl)1