diff --git a/source/java/org/alfresco/repo/web/scripts/content/MimetypesGet.java b/source/java/org/alfresco/repo/web/scripts/content/MimetypesGet.java index ddfa5340f5..3874270fd4 100644 --- a/source/java/org/alfresco/repo/web/scripts/content/MimetypesGet.java +++ b/source/java/org/alfresco/repo/web/scripts/content/MimetypesGet.java @@ -18,6 +18,7 @@ */ package org.alfresco.repo.web.scripts.content; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -34,6 +35,7 @@ import org.alfresco.repo.content.transform.ProxyContentTransformer; import org.alfresco.service.cmr.repository.MimetypeService; import org.alfresco.service.cmr.repository.TransformationOptions; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.extensions.webscripts.Cache; @@ -49,23 +51,25 @@ import org.springframework.extensions.webscripts.WebScriptRequest; * @author Nick Burch * @since 3.4.b */ -public class MimetypesGet extends DeclarativeWebScript implements ApplicationContextAware +public class MimetypesGet extends DeclarativeWebScript implements ApplicationContextAware, InitializingBean { public static final String MODEL_MIMETYPES = "mimetypes"; public static final String MODEL_EXTENSIONS = "extensions"; public static final String MODEL_MIMETYPE_DETAILS = "details"; + private ApplicationContext applicationContext; private MimetypeService mimetypeService; private ContentTransformerRegistry contentTransformerRegistry; private MetadataExtracterRegistry metadataExtracterRegistry; - - /** So we can spot if it goes via Direct OO */ - ContentTransformerWorker ooDirectWorker; - protected static final String OODIRECT_WORKER_BEAN = "transformer.worker.OpenOffice"; - /** So we can spot if it goes through JODConverter */ - ContentTransformerWorker jodWorker; + private Map knownWorkerBeanLabels; + private Map knownWorkers; + + protected static final String OODIRECT_WORKER_BEAN = "transformer.worker.OpenOffice"; protected static final String JOD_WORKER_BEAN = "transformer.worker.JodConverter"; + protected static final String RTS_WORKER_BEAN = "transformer.worker.remoteServer"; + + protected static final String PROXY_LABEL_DEFAULT_MESSAGE = "Proxy via: {0} ({1})"; /** * Uses the context to find OpenOffice related beans. @@ -74,23 +78,34 @@ public class MimetypesGet extends DeclarativeWebScript implements ApplicationCon @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - if(applicationContext.containsBean(OODIRECT_WORKER_BEAN)) - { - Object bean = applicationContext.getBean(OODIRECT_WORKER_BEAN); - if(bean instanceof ContentTransformerWorker) - { - ooDirectWorker = (ContentTransformerWorker)bean; - } - } - - if(applicationContext.containsBean(JOD_WORKER_BEAN)) - { - Object bean = applicationContext.getBean(JOD_WORKER_BEAN); - if(bean instanceof ContentTransformerWorker) - { - jodWorker = (ContentTransformerWorker)bean; - } - } + this.applicationContext = applicationContext; + } + + @Override + public void afterPropertiesSet() throws Exception + { + // If no override has been supplied use the default known list + if (knownWorkerBeanLabels == null) + { + knownWorkerBeanLabels = new HashMap(); + knownWorkerBeanLabels.put(OODIRECT_WORKER_BEAN, "Using a Direct Open Office Connection"); + knownWorkerBeanLabels.put(JOD_WORKER_BEAN, "Using JOD Converter / Open Office"); + knownWorkerBeanLabels.put(RTS_WORKER_BEAN, "Using the Remote Transformation Server v{1}"); + } + + // Build the map of known worker bean instances to bean names + knownWorkers = new HashMap(); + for (String workerName : knownWorkerBeanLabels.keySet()) + { + if(applicationContext.containsBean(workerName)) + { + Object bean = applicationContext.getBean(workerName); + if(bean instanceof ContentTransformerWorker) + { + knownWorkers.put((ContentTransformerWorker) bean, workerName); + } + } + } } /** @@ -119,7 +134,17 @@ public class MimetypesGet extends DeclarativeWebScript implements ApplicationCon this.metadataExtracterRegistry = metadataExtracterRegistry; } - + /** + * Sets the map of content transformer worker bean names to + * message formatting labels + * + * @param knownWorkerBeanLabels + */ + public void setKnownWorkerBeanLabels(Map knownWorkerBeanLabels) + { + this.knownWorkerBeanLabels = knownWorkerBeanLabels; + } + @Override protected Map executeImpl(WebScriptRequest req, Status status, Cache cache) { @@ -210,30 +235,67 @@ public class MimetypesGet extends DeclarativeWebScript implements ApplicationCon if(ct instanceof ComplexContentTransformer) { - ComplexContentTransformer cct = (ComplexContentTransformer)ct; - String text = "Complex via: "; - for(String imt : cct.getIntermediateMimetypes()) { - text += imt + " "; - } - return text; + return getComplexTransformerLabel((ComplexContentTransformer)ct); } if(ct instanceof ProxyContentTransformer) { - ProxyContentTransformer pct = (ProxyContentTransformer)ct; - ContentTransformerWorker ctw = pct.getWorker(); - - if(ctw.equals(jodWorker)) - return "Using JOD Converter / Open Office"; - if(ctw.equals(ooDirectWorker)) - return "Using a Direct Open Office Connection"; - - String text = "Proxy via: " + - ctw.getClass().getName() + - "(" + ctw.getVersionString() + ")"; - return text; + String proxyLabel = getProxyTransformerLabel((ProxyContentTransformer)ct); + if (proxyLabel != null) + { + return proxyLabel; + } } return ct.getClass().getName(); } + + /** + * Gets the display label for complex transformers + * + * @param cct + * @return the transformer display label + */ + protected String getComplexTransformerLabel(ComplexContentTransformer cct) + { + String text = "Complex via: "; + for(String imt : cct.getIntermediateMimetypes()) { + text += imt + " "; + } + return text; + } + + /** + * Gets the display label for proxy content transformers + * + * @param pct + * @return the transformer display label + */ + protected String getProxyTransformerLabel(ProxyContentTransformer pct) + { + ContentTransformerWorker ctw = pct.getWorker(); + + String message = PROXY_LABEL_DEFAULT_MESSAGE; + + String beanName = getWorkerBeanName(ctw); + if (beanName != null) + { + message = knownWorkerBeanLabels.get(beanName); + } + return MessageFormat.format(message, ctw.getClass().getName(), ctw.getVersionString()); + } + + /** + * Gets the given ContentTransformerWorker's bean name from the cache of known workers + *

+ * In the future ContentTransformerWorker may be made bean name aware. + * + * @param ctw + * @return the bean name + */ + protected String getWorkerBeanName(ContentTransformerWorker ctw) + { + return knownWorkers.get(ctw); + } + } \ No newline at end of file diff --git a/source/test-java/org/alfresco/repo/web/scripts/content/MimetypesGetTest.java b/source/test-java/org/alfresco/repo/web/scripts/content/MimetypesGetTest.java new file mode 100644 index 0000000000..f612de0443 --- /dev/null +++ b/source/test-java/org/alfresco/repo/web/scripts/content/MimetypesGetTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.web.scripts.content; + +import org.alfresco.repo.content.MimetypeMap; +import org.alfresco.repo.content.transform.ContentTransformerRegistry; +import org.alfresco.repo.content.transform.PdfBoxContentTransformer; +import org.alfresco.repo.web.scripts.BaseWebScriptTest; +import org.springframework.context.ApplicationContext; + +/** + * Tests the {@link MimetypesGet} endpoint + */ +public class MimetypesGetTest extends BaseWebScriptTest +{ + + private ApplicationContext ctx; + private ContentTransformerRegistry contentTransformerRegistry; + + @Override + protected void setUp() throws Exception + { + super.setUp(); + ctx = getServer().getApplicationContext(); + contentTransformerRegistry = (ContentTransformerRegistry) ctx.getBean("contentTransformerRegistry"); + } + + /** + * Tests the mimetypesGet.getTransformer method directly for + * varefication of label text + * + * @throws Exception + */ + public void testGetTransformer() throws Exception + { + MimetypesGet mimetypesGet = new MimetypesGet(); + mimetypesGet.setApplicationContext(ctx); + mimetypesGet.setContentTransformerRegistry(contentTransformerRegistry); + mimetypesGet.afterPropertiesSet(); + + // Test a Java transformer name + String transformerName = mimetypesGet.getTransformer(MimetypeMap.MIMETYPE_PDF, 1000, MimetypeMap.MIMETYPE_TEXT_PLAIN); + assertEquals(PdfBoxContentTransformer.class.getCanonicalName(), transformerName); + + // Test a generic proxy transformer name + transformerName = mimetypesGet.getTransformer(MimetypeMap.MIMETYPE_IMAGE_JPEG, 1000, MimetypeMap.MIMETYPE_IMAGE_PNG); + assertNotNull(transformerName); + assertTrue("Expected transformerName to contain 'Proxy' but was " + transformerName, + transformerName.contains("Proxy via")); + + boolean oodirectPresent = ctx.containsBean(MimetypesGet.OODIRECT_WORKER_BEAN); + boolean jodPresent = ctx.containsBean(MimetypesGet.JOD_WORKER_BEAN); + + // Test the office transformer name + transformerName = mimetypesGet.getTransformer(MimetypeMap.MIMETYPE_WORD, 1000, MimetypeMap.MIMETYPE_PDF); + assertNotNull(transformerName); + if (oodirectPresent) + { + assertEquals("Using a Direct Open Office Connection", transformerName); + } + else if (jodPresent) + { + assertEquals("Using JOD Converter / Open Office", transformerName); + } + } + +}