Index: main/plugin-cms/src/org/ametys/cms/transformation/LocalURIResolver.java =================================================================== --- main/plugin-cms/src/org/ametys/cms/transformation/LocalURIResolver.java (revision 59359) +++ main/plugin-cms/src/org/ametys/cms/transformation/LocalURIResolver.java (working copy) @@ -78,33 +78,20 @@ @Override public String resolve(String uri, boolean download, boolean absolute, boolean internal) { - // uri are like content://UUID@metadata;data/file.ext - int i = uri.indexOf('@'); - int j = uri.indexOf(';', i); - String id = uri.substring(0, i); - String metadata = uri.substring(i + 1, j); - String path = uri.substring(j + 1); + URIInfo localURI = getInfos(uri, false); try { - Request request = ContextHelper.getRequest(_context); - - String contentVersion = null; - Content content = (Content) request.getAttribute(Content.class.getName()); - if (content != null && id.equals(content.getId())) - { - contentVersion = ((VersionableAmetysObject) content).getRevision(); - } - StringBuilder resultPath = new StringBuilder(); resultPath.append(getUriPrefix(download, absolute, internal)) .append("/plugins/cms" + (download ? "/download/" : "/view/")) - .append(path); + .append(localURI.getPath()); Map params = new HashMap(); - params.put("contentId", id); - params.put("metadata", metadata); + params.put("contentId", localURI.getContentId()); + params.put("metadata", localURI.getMetadata()); + String contentVersion = localURI.getContentVersion(); if (contentVersion != null) { params.put("contentVersion", contentVersion); @@ -183,26 +170,13 @@ */ protected String resolveImageAsBase64(String uri, int height, int width, int maxHeight, int maxWidth) { - // uri are like content://UUID@metadata;data/file.ext - int i = uri.indexOf('@'); - int j = uri.indexOf(';', i); - String id = uri.substring(0, i); - String metadata = uri.substring(i + 1, j); - String path = uri.substring(j + 1); + URIInfo localURI = getInfos(uri, true); InputStream dataIs = null; try { - Request request = ContextHelper.getRequest(_context); - - Content content = (Content) request.getAttribute(Content.class.getName()); - if (!content.getId().equals(id)) - { - content = _ametysObjectResolver.resolveById(id); - } - - RichText richText = _getMeta(content.getMetadataHolder(), metadata); - File file = _getFile(richText.getAdditionalDataFolder(), path); + RichText richText = _getMeta(localURI.getContent().getMetadataHolder(), localURI.getMetadata()); + File file = _getFile(richText.getAdditionalDataFolder(), localURI.getPath()); Resource resource = file.getResource(); dataIs = resource.getInputStream(); @@ -314,4 +288,151 @@ return file; } + + /** + * Parses the uri. + * @param uri the incoming uri. + * @param resolveContent true if the Content should be actually resolved if not found in the request. + * @return an object containing all parsed infos. + */ + protected URIInfo getInfos(String uri, boolean resolveContent) + { + // uri are like content://UUID@metadata;data/file.ext + int i = uri.indexOf('@'); + int j = uri.indexOf(';', i); + String id = uri.substring(0, i); + String metadata = uri.substring(i + 1, j); + String path = uri.substring(j + 1); + + Request request = ContextHelper.getRequest(_context); + + String contentVersion = null; + // The content should be the one from request (UUID) but in that case, getRevision will be always the head on + Content content = (Content) request.getAttribute(Content.class.getName()); + if (content == null || !id.equals(content.getId())) + { + // Some time (such as frontoffice edition) the image is rendered with no content in attrbiute + content = resolveContent ? (Content) _ametysObjectResolver.resolveById(id) : null; + } + else + { + contentVersion = ((VersionableAmetysObject) content).getRevision(); + } + + URIInfo infos = new URIInfo(); + infos.setContentId(id); + infos.setContentVersion(contentVersion); + infos.setMetadata(metadata); + infos.setPath(path); + infos.setContent(content); + + return infos; + } + + /** + * Helper class containg all infos parsed from URI. + */ + protected static class URIInfo + { + /** The content id. */ + private String _contentId; + /** The relevant attribute */ + private String _metadata; + /** The path to the resource */ + private String _path; + /** The content version, if any. */ + private String _contentVersion; + /** The resolved content, if any. */ + private Content _content; + + /** + * Returns the content id. + * @return the content id. + */ + public String getContentId() + { + return _contentId; + } + + /** + * Set the content id. + * @param contentId the content id. + */ + public void setContentId(String contentId) + { + _contentId = contentId; + } + + /** + * Returns the metadata. + * @return the metadata. + */ + public String getMetadata() + { + return _metadata; + } + + /** + * Set the metadata. + * @param metadata the metadata. + */ + public void setMetadata(String metadata) + { + _metadata = metadata; + } + + /** + * Returns the resource path. + * @return the path + */ + public String getPath() + { + return _path; + } + + /** + * Set the resource path. + * @param path the path. + */ + public void setPath(String path) + { + _path = path; + } + + /** + * Returns the content version, if any. + * @return the content version. + */ + public String getContentVersion() + { + return _contentVersion; + } + + /** + * Set the content version. + * @param contentVersion the content version. + */ + public void setContentVersion(String contentVersion) + { + _contentVersion = contentVersion; + } + + /** + * Returns the resolved content, if any. + * @return the content. + */ + public Content getContent() + { + return _content; + } + + /** + * Set the content. + * @param content the content. + */ + public void setContent(Content content) + { + _content = content; + } + } }