mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged HEAD-BUG-FIX (4.3/Cloud) to HEAD (4.3/Cloud)
57476: Merged V4.2-BUG-FIX (4.2.1) to HEAD-BUG-FIX (Cloud/4.3) 57286: Merged V4.1-BUG-FIX (4.1.7) to V4.2-BUG-FIX (4.2.1) 57220: MNT-9350: Registered Mimetypes WebScript doesn't know about the Alfresco Transformation Server - Added the notion of known workers and label message map - Added ability to override the label message map for custom implementations - Separated building of labels for complex and proxy transformers into their own methods - Added message formatting for bean names git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@61818 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -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";
|
||||
private Map<String, String> knownWorkerBeanLabels;
|
||||
private Map<ContentTransformerWorker, String> knownWorkers;
|
||||
|
||||
/** So we can spot if it goes through JODConverter */
|
||||
ContentTransformerWorker jodWorker;
|
||||
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;
|
||||
}
|
||||
}
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
if(applicationContext.containsBean(JOD_WORKER_BEAN))
|
||||
{
|
||||
Object bean = applicationContext.getBean(JOD_WORKER_BEAN);
|
||||
if(bean instanceof ContentTransformerWorker)
|
||||
{
|
||||
jodWorker = (ContentTransformerWorker)bean;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
// If no override has been supplied use the default known list
|
||||
if (knownWorkerBeanLabels == null)
|
||||
{
|
||||
knownWorkerBeanLabels = new HashMap<String, String>();
|
||||
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<ContentTransformerWorker, String>();
|
||||
for (String workerName : knownWorkerBeanLabels.keySet())
|
||||
{
|
||||
if(applicationContext.containsBean(workerName))
|
||||
{
|
||||
Object bean = applicationContext.getBean(workerName);
|
||||
if(bean instanceof ContentTransformerWorker)
|
||||
{
|
||||
knownWorkers.put((ContentTransformerWorker) bean, workerName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,6 +134,16 @@ 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<String, String> knownWorkerBeanLabels)
|
||||
{
|
||||
this.knownWorkerBeanLabels = knownWorkerBeanLabels;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> 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
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 <code>mimetypesGet.getTransformer</code> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user