Search in sources :

Example 1 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO in project studio by craftercms.

the class ContentServiceImpl method processContent.

@Override
@ValidateParams
public ResultTO processContent(@ValidateStringParam(name = "id") String id, InputStream input, boolean isXml, Map<String, String> params, @ValidateStringParam(name = "contentChainForm") String contentChainForm) throws ServiceLayerException {
    // TODO: SJ: Pipeline Processor is not defined right, we need to refactor in 3.1+
    // TODO: SJ: Pipeline should take input, and give you back output
    // TODO: SJ: Presently, this takes action and performs the action as a side effect of the processor chain
    // TODO: SJ: Furthermore, we have redundancy in the code of the processors
    // get sandbox if not provided
    long start = System.currentTimeMillis();
    try {
        String[] strings = id.split(":");
        long startTime = System.currentTimeMillis();
        ResultTO to = contentProcessor.processContent(id, input, isXml, params, contentChainForm);
        long duration = System.currentTimeMillis() - startTime;
        logger.debug("Write Duration: '{}'", duration);
        return to;
    } finally {
        long end = System.currentTimeMillis();
        logger.debug("Write complete for [" + id + "] in time [" + (end - start) + "]");
    }
}
Also used : ResultTO(org.craftercms.studio.api.v1.to.ResultTO) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 2 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO 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 3 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO in project studio by craftercms.

the class PostActivityProcessor method process.

public void process(PipelineContent content, ResultTO result) throws SiteNotFoundException {
    if (result.getCommitId() != null) {
        String site = content.getProperty(DmConstants.KEY_SITE);
        boolean skipAuditLogInsert = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SKIP_AUDIT_LOG_INSERT));
        if (!skipAuditLogInsert) {
            String type = content.getProperty(DmConstants.KEY_ACTIVITY_TYPE);
            String user = content.getProperty(DmConstants.KEY_USER);
            String activityType = OPERATION_CREATE.equals(type) ? OPERATION_CREATE : OPERATION_UPDATE;
            String folderPath = content.getProperty(DmConstants.KEY_FOLDER_PATH);
            String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
            boolean isSystemAsset = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SYSTEM_ASSET));
            if (isSystemAsset) {
                ContentAssetInfoTO assetInfoTO = (ContentAssetInfoTO) result.getItem();
                fileName = assetInfoTO.getFileName();
            }
            String uri = (folderPath.endsWith(FILE_SEPARATOR)) ? folderPath + fileName : folderPath + FILE_SEPARATOR + fileName;
            SiteFeed siteFeed = siteService.getSite(site);
            AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
            auditLog.setOperation(activityType);
            auditLog.setActorId(user);
            auditLog.setSiteId(siteFeed.getId());
            auditLog.setPrimaryTargetId(site + ":" + uri);
            auditLog.setPrimaryTargetType(TARGET_TYPE_CONTENT_ITEM);
            auditLog.setPrimaryTargetValue(uri);
            auditLog.setPrimaryTargetSubtype(contentService.getContentTypeClass(site, uri));
            auditServiceInternal.insertAuditLog(auditLog);
        }
        contentRepository.markGitLogAudited(site, result.getCommitId());
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Example 4 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO in project studio by craftercms.

the class CheckImageSizeProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String name = content.getProperty(DmConstants.KEY_FILE_NAME);
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String mimetype = mimeTypesMap.getContentType(name);
    boolean process = (StringUtils.isEmpty(mimetype)) ? false : mimetype.startsWith("image/") && !StringUtils.equalsIgnoreCase(mimetype, "image/svg+xml");
    if (process) {
        String allowLessSize = content.getProperty(DmConstants.KEY_ALLOW_LESS_SIZE);
        boolean lessSize = ContentFormatUtils.getBooleanValue(allowLessSize);
        String allowedWidth = content.getProperty(DmConstants.KEY_ALLOWED_WIDTH);
        String allowedHeight = content.getProperty(DmConstants.KEY_ALLOWED_HEIGHT);
        int width = (StringUtils.isEmpty(allowedWidth)) ? -1 : ContentFormatUtils.getIntValue(allowedWidth);
        int height = (StringUtils.isEmpty(allowedHeight)) ? -1 : ContentFormatUtils.getIntValue(allowedHeight);
        InputStream in = content.getContentStream();
        ContentAssetInfoTO assetInfo = (result.getItem() == null) ? new ContentAssetInfoTO() : (ContentAssetInfoTO) result.getItem();
        in = checkForImageSize(in, width, height, lessSize, assetInfo);
        content.getProperties().put(DmConstants.KEY_WIDTH, String.valueOf(assetInfo.getWidth()));
        content.getProperties().put(DmConstants.KEY_HEIGHT, String.valueOf(assetInfo.getHeight()));
        assetInfo.getWidth();
        result.setItem(assetInfo);
        content.setContentStream(in);
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Example 5 with ResultTO

use of org.craftercms.studio.api.v1.to.ResultTO in project studio by craftercms.

the class ExtractDependencyProcessor 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)12 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)8 ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)6 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ArrayList (java.util.ArrayList)5 JSONArray (net.sf.json.JSONArray)5 JSONException (net.sf.json.JSONException)5 JSONObject (net.sf.json.JSONObject)5 ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)5 DmDependencyTO (org.craftercms.studio.api.v1.to.DmDependencyTO)5 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)4 ContentNotFoundException (org.craftercms.studio.api.v1.exception.ContentNotFoundException)4 AuditLog (org.craftercms.studio.api.v2.dal.AuditLog)4 HashSet (java.util.HashSet)3 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)3 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)3 MultiChannelPublishingContext (org.craftercms.studio.api.v1.service.workflow.context.MultiChannelPublishingContext)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2