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;
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
Aggregations