Search in sources :

Example 1 with ContentProcessException

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

the class PipelineContentImpl method getDocument.

@Override
public Document getDocument() throws ContentProcessException {
    if (_xml && _document == null) {
        if (_contentStream != 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);
                }
                saxReader.setEncoding(_encoding);
                _document = saxReader.read(_contentStream);
                _contentStream = null;
            } catch (DocumentException e) {
                throw new ContentProcessException("Error while converting " + _id + " into document.", e);
            } finally {
                ContentUtils.release(_contentStream);
                _contentStream = null;
            }
        } else {
            throw new ContentProcessException("Error while converting " + _id + " into document. Both document and content stream cannot be null.");
        }
    }
    return _document;
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) SAXException(org.xml.sax.SAXException)

Example 2 with ContentProcessException

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

use of org.craftercms.studio.api.v1.exception.ContentProcessException 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)

Example 4 with ContentProcessException

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

the class AssetDmContentProcessor method process.

public void process(PipelineContent content, ResultTO result) throws ContentProcessException {
    String site = content.getProperty(DmConstants.KEY_SITE);
    String user = content.getProperty(DmConstants.KEY_USER);
    String path = content.getProperty(DmConstants.KEY_PATH);
    String fileName = content.getProperty(DmConstants.KEY_FILE_NAME);
    String widthStr = content.getProperty(DmConstants.KEY_WIDTH);
    String heightStr = content.getProperty(DmConstants.KEY_HEIGHT);
    int width = (widthStr != null) ? Integer.parseInt(widthStr) : -1;
    int height = (heightStr != null) ? Integer.parseInt(heightStr) : -1;
    String unlockValue = content.getProperty(DmConstants.KEY_UNLOCK);
    // default is true for unlocking on save
    boolean unlock = (!StringUtils.isEmpty(unlockValue) && unlockValue.equalsIgnoreCase("false")) ? false : true;
    boolean isPreview = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_IS_PREVIEW));
    boolean isSystemAsset = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_SYSTEM_ASSET));
    boolean createFolders = ContentFormatUtils.getBooleanValue(content.getProperty(DmConstants.KEY_CREATE_FOLDERS));
    try {
        ContentAssetInfoTO oldAssetInfo = (ContentAssetInfoTO) result.getItem();
        ContentAssetInfoTO assetInfo = writeContentAsset(content, site, user, path, fileName, content.getContentStream(), width, height, createFolders, isPreview, unlock, isSystemAsset, result);
        if (oldAssetInfo != null) {
            oldAssetInfo.setFileExtension(assetInfo.getFileExtension());
            oldAssetInfo.setFileName(assetInfo.getFileName());
            oldAssetInfo.setSize(assetInfo.getSize());
            oldAssetInfo.setSizeUnit(assetInfo.getSizeUnit());
            result.setItem(oldAssetInfo);
        } else {
            result.setItem(assetInfo);
        }
    } catch (ServiceLayerException e) {
        throw new ContentProcessException("Failed to write " + content.getId() + ", " + e, e);
    } finally {
        content.closeContentStream();
    }
}
Also used : ContentAssetInfoTO(org.craftercms.studio.api.v1.to.ContentAssetInfoTO) ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException)

Example 5 with ContentProcessException

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

the class CheckImageSizeProcessor method checkForImageSize.

/**
 * check the width and the height of the given image as an inputstream match the width and the height specified
 *
 * @param in
 * @param allowedWidth
 * @param allowedHeight
 * @param lessSize
 * @param assetInfo
 * @return image as input stream
 */
protected InputStream checkForImageSize(InputStream in, int allowedWidth, int allowedHeight, boolean lessSize, ContentAssetInfoTO assetInfo) throws ContentProcessException {
    ByteArrayOutputStream byteOutput = null;
    try {
        byteOutput = new ByteArrayOutputStream();
        // PORT CStudioWebScriptConstants.READ_BUFFER_LENGTH];
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = in.read(buffer)) > 0) {
            byteOutput.write(buffer, 0, read);
        }
        byte[] imageData = byteOutput.toByteArray();
        Image image = Toolkit.getDefaultToolkit().createImage(imageData);
        ImageIcon icon = new ImageIcon(image);
        int height = icon.getIconHeight();
        int width = icon.getIconWidth();
        if (allowedHeight > 0 && allowedWidth > 0) {
            validateImageSize(allowedWidth, allowedHeight, height, width, lessSize);
        }
        assetInfo.setHeight(height);
        assetInfo.setWidth(width);
        return new ByteArrayInputStream(imageData);
    } catch (IOException e) {
        throw new ContentProcessException(e);
    } finally {
        // close the original inputstream
        ContentUtils.release(in);
        ContentUtils.release(byteOutput);
    }
}
Also used : ContentProcessException(org.craftercms.studio.api.v1.exception.ContentProcessException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ContentProcessException (org.craftercms.studio.api.v1.exception.ContentProcessException)6 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ContentAssetInfoTO (org.craftercms.studio.api.v1.to.ContentAssetInfoTO)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)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 ResultTO (org.craftercms.studio.api.v1.to.ResultTO)1 PipelineContentImpl (org.craftercms.studio.impl.v1.content.pipeline.PipelineContentImpl)1 DocumentException (org.dom4j.DocumentException)1 SAXReader (org.dom4j.io.SAXReader)1 SAXException (org.xml.sax.SAXException)1