Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidjgonzalez/66e481b54aafb1b900a579ee95848d8f to your computer and use it in GitHub Desktop.
Save davidjgonzalez/66e481b54aafb1b900a579ee95848d8f to your computer and use it in GitHub Desktop.
Asset Share Commons - Binary Resource discovery and Internal Redirect dispatching
@Override
public AssetRendition getRendition(final AssetModel assetModel, final AssetRenditionParameters parameters) {
throw new UnsupportedOperationException(String.format("[ %s ] is not supported by the AEM Async Asset Download Framework.",
this.getClass().getName()));
/*
// Commented out implementation for AEM as a Cloud Service as this only supports a sub-set of the original use-case.
final String expression = mappings.get(parameters.getRenditionName());
if (StringUtils.isNotBlank(expression)) {
final String evaluatedExpression = assetRenditions.evaluateExpression(assetModel, parameters.getRenditionName(), expression);
final PathInfo pathInfo = new PathInfo(assetModel.getResource().getResourceResolver(), evaluatedExpression);
// We have to manually clean up the pathInfo resourcePath due to issues with the PathInfo impl when /etc/map is in play
final String resourcePath = Text.unescape(cleanPathInfoRequestPath(pathInfo.getResourcePath()));
if (isBinaryNode(assetModel, resourcePath)) {
log.debug("Download internal redirect rendition [ {} ] for expression [ {} ] and resolved rendition name [ {} ]",
resourcePath,
evaluatedExpression,
parameters.getRenditionName());
Resource resource = assetModel.getResource().getResourceResolver().resolve(resourcePath);
return new AssetRendition(resourcePath,
getBinaryResourceSizeInBytes(resource),
getBinaryResourceMimeType(resource));
}
}
return null;
*/
}
private boolean isBinaryNode(AssetModel assetModel, String resourcePath) {
if (StringUtils.isNotBlank(resourcePath)) {
return false;
}
final Resource internalRedirectResource = assetModel.getResource().getResourceResolver().resolve(resourcePath);
if (internalRedirectResource != null && (internalRedirectResource.isResourceType(NT_RESOURCE) || internalRedirectResource.isResourceType(NT_FILE))) {
return true;
}
log.warn("Internal redirect path [ {} ]does not resolve to a binary node in the JCR, therefore the Async Asset Download framework cannot handle this request", resourcePath);
return false;
}
protected long getBinaryResourceSizeInBytes(final Resource resource) {
if (resource != null && !ResourceUtil.isNonExistingResource(resource)) {
Node node = resource.adaptTo(Node.class);
try {
if (node == null) {
// Do nothing, return 0L below
} else if (node.hasProperty(JCR_DATA)) {
return node.getProperty(JCR_DATA).getBinary().getSize();
} else if (node.hasNode(JCR_CONTENT)) {
node = node.getNode(JCR_CONTENT);
if (node.hasProperty(JCR_DATA)) {
return node.getProperty(JCR_DATA).getBinary().getSize();
}
}
} catch (RepositoryException e) {
log.error("Error obtaining binary size for node [ {} ] - returning a size of 0 bytes.", resource.getPath());
}
}
return 0L;
}
protected String getBinaryResourceMimeType(final Resource resource) {
if (resource == null || ResourceUtil.isNonExistingResource(resource)) {
return null;
}
return resource.getValueMap().get(JCR_MIMETYPE, resource.getValueMap().get(JCR_CONTENT + "/" + JCR_MIMETYPE, String.class));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment