From df3c1b89e28b0c751da4343ffdbdfbf15b696ea1 Mon Sep 17 00:00:00 2001 From: Ray Gauss Date: Tue, 19 Feb 2013 17:31:21 +0000 Subject: [PATCH] ALF-14306: Add priorities to transformers - Added TransformerConfigSupported isMimetype and isMimetypeSubtypeWildcard methods which check a given string - Added TransformerConfigSupported getMatchingMimetypes which gets all matching mime types from the mimetypeService if wildcards are present, otherwise passes the given mimetype on - Modified TransformerConfigSupported setSupported to check for mimetypes and call getMatchingMimetypes in addition to existing support for extensions git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@46807 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../transform/TransformerConfigSupported.java | 102 ++++++++++++++++-- 1 file changed, 96 insertions(+), 6 deletions(-) diff --git a/source/java/org/alfresco/repo/content/transform/TransformerConfigSupported.java b/source/java/org/alfresco/repo/content/transform/TransformerConfigSupported.java index 7de77fc9e3..1775faea48 100644 --- a/source/java/org/alfresco/repo/content/transform/TransformerConfigSupported.java +++ b/source/java/org/alfresco/repo/content/transform/TransformerConfigSupported.java @@ -23,8 +23,10 @@ import static org.alfresco.repo.content.transform.TransformerConfig.CONTENT; import static org.alfresco.repo.content.transform.TransformerConfig.MIMETYPES_SEPARATOR; import static org.alfresco.repo.content.transform.TransformerConfig.SUPPORTED; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; @@ -41,6 +43,11 @@ import org.alfresco.util.Triple; */ public class TransformerConfigSupported extends TransformerPropertyNameExtractor { + private static final String IS_MIMETYPE_REGEX = + "(application|audio|image|message|model|multipart|text|video)/.+"; + private static final String IS_MIMETYPE_SUBTYPE_WILDCARD_REGEX = + "(application|audio|image|message|model|multipart|text|video)/.*\\" + ANY + ".*"; + // Holds configured (entries only exist if configured rather than for all possible combinations) // of supported and unsupported mimetypes transformations for a transformer. // SourceMimetype and targetMimetype may be 'ANY' values to act as wild cards. @@ -50,6 +57,69 @@ public class TransformerConfigSupported extends TransformerPropertyNameExtractor { setSupported(subsystem, mimetypeService); } + + /** + * Determines if the given string is a mime type expression. + * + * @param extension + * @return whether or not the string is a mime type + */ + private boolean isMimetype(String extension) + { + return (extension != null && extension.matches(IS_MIMETYPE_REGEX)); + } + + /** + * Determines if the given string is a mime type expression containing a wildcard + * in the subtype. + * + * @param mimetype + * @return whether or not the mime type contains a wildcard subtype + */ + private boolean isMimetypeSubtypeWildcard(String mimetype) + { + return (mimetype != null && mimetype.matches(IS_MIMETYPE_SUBTYPE_WILDCARD_REGEX)); + } + + /** + * Gets the mimetypes which match the given configMimetype from the given + * mimetypeService. + *

+ * If the given mime type string contains one or more wildcards (*) in the subtype, the string + * is converted to a regular expression and any mimetypes which match are returned. + *

+ * If the given mime type string has no wildcards a list with only the given + * mime type is returned. + * + * @param configMimetype + * @param mimetypeService + * @return the list of mime types which match the wildcard, or a list with just the given mimetype + */ + private List getMatchingMimetypes( + String configMimetype, MimetypeService mimetypeService) + { + if (configMimetype == null) + { + return null; + } + List matchingMimetypes = new ArrayList(1); + if (isMimetypeSubtypeWildcard(configMimetype)) + { + String mimetypeWildcardRegex = configMimetype.replaceAll("\\" + ANY, ".*"); + for (String mimetype : mimetypeService.getMimetypes()) + { + if (mimetype.matches(mimetypeWildcardRegex)) + { + matchingMimetypes.add(mimetype); + } + } + } + else + { + matchingMimetypes.add(configMimetype); + } + return matchingMimetypes; + } /** * Sets the supported/unsupported mimetype transformations created from system properties. @@ -62,17 +132,12 @@ public class TransformerConfigSupported extends TransformerPropertyNameExtractor Set> transformerNamesAndMimetypes = getTransformerNamesAndExt(MIMETYPES_SEPARATOR, Collections.singletonList(SUPPORTED), false, subsystem, mimetypeService); - - - // Populate the transformer values for (Triple triple: transformerNamesAndMimetypes) { String transformerName = triple.getFirst(); String sourceExt = triple.getSecond(); String targetExt = triple.getThird(); - String sourceMimetype = ANY.equals(sourceExt) ? ANY : mimetypeService.getMimetype(sourceExt); - String targetMimetype = ANY.equals(targetExt) ? ANY : mimetypeService.getMimetype(targetExt); SupportedAndUnsupportedTransformations supportedBytransformer = this.supported.get(transformerName); @@ -82,7 +147,32 @@ public class TransformerConfigSupported extends TransformerPropertyNameExtractor this.supported.put(transformerName, supportedBytransformer); } boolean supported = getValueFromProperties(transformerName, sourceExt, targetExt, subsystem, SUPPORTED); - supportedBytransformer.put(sourceMimetype, targetMimetype, supported); + + List sourceMimetypes = new ArrayList(1); + List targetMimetypes = new ArrayList(1); + if (isMimetype(sourceExt)) + { + sourceMimetypes.addAll(getMatchingMimetypes(sourceExt, mimetypeService)); + } + else + { + sourceMimetypes.add(ANY.equals(sourceExt) ? ANY : mimetypeService.getMimetype(sourceExt)); + } + if (isMimetype(targetExt)) + { + targetMimetypes.addAll(getMatchingMimetypes(targetExt, mimetypeService)); + } + else + { + targetMimetypes.add(ANY.equals(targetExt) ? ANY : mimetypeService.getMimetype(targetExt)); + } + for (String sourceMimetype : sourceMimetypes) + { + for (String targetMimetype : targetMimetypes) + { + supportedBytransformer.put(sourceMimetype, targetMimetype, supported); + } + } } }