diff --git a/config/alfresco/messages/jbpm.properties b/config/alfresco/messages/jbpm.properties deleted file mode 100644 index e994165d7f..0000000000 --- a/config/alfresco/messages/jbpm.properties +++ /dev/null @@ -1,13 +0,0 @@ -processDefinitionsList=Process Definitions List -searchProcessInstances=Search Process Instances -processDefinitions=Process Definitions -variableName=Variable Name -variableValue=Variable Value -search=Search -id=Id -name=Name -version=Version -instances=Instances -start=Start -end=End -value=Value \ No newline at end of file diff --git a/config/alfresco/messages/webscripts-test-help.txt b/config/alfresco/messages/webscripts-test-help.txt new file mode 100644 index 0000000000..1ca522abfa --- /dev/null +++ b/config/alfresco/messages/webscripts-test-help.txt @@ -0,0 +1,37 @@ +## +## Meta commands +## + +ok> help + + List this help. + +ok> r + + Repeat last command. + +ok> user [] + + Switch to specified . If is omitted, the currently + selected user is shown. + +ok> quit | exit + + Quit this Web Script server. + +## +## HTTP Requests +## + +ok> get + + Issue a HTTP GET request to the Web Script located at . The response + is dumped to the console. + + URL relative to /alfresco/service + + e.g. get /blog/category?c=Web20 + +## +## end +## diff --git a/config/alfresco/messages/webscripts.properties b/config/alfresco/messages/webscripts.properties new file mode 100644 index 0000000000..f79e5bd3a8 --- /dev/null +++ b/config/alfresco/messages/webscripts.properties @@ -0,0 +1 @@ +testserver.help=alfresco/messages/webscripts-test-help.txt \ No newline at end of file diff --git a/config/alfresco/web-scripts-application-context-test.xml b/config/alfresco/web-scripts-application-context-test.xml index c724113268..57751164d7 100644 --- a/config/alfresco/web-scripts-application-context-test.xml +++ b/config/alfresco/web-scripts-application-context-test.xml @@ -14,6 +14,15 @@ + + + + + alfresco/messages/webscripts + + + + \ No newline at end of file diff --git a/source/java/org/alfresco/web/scripts/AbstractWebScript.java b/source/java/org/alfresco/web/scripts/AbstractWebScript.java index 7f71a03399..3860dd208b 100644 --- a/source/java/org/alfresco/web/scripts/AbstractWebScript.java +++ b/source/java/org/alfresco/web/scripts/AbstractWebScript.java @@ -37,12 +37,10 @@ import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.ScriptLocation; import org.alfresco.service.cmr.repository.TemplateExtensionImplementation; -import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.service.cmr.repository.TemplateNode; import org.alfresco.service.descriptor.DescriptorService; import org.alfresco.web.scripts.WebScriptDescription.RequiredAuthentication; import org.alfresco.web.scripts.WebScriptDescription.RequiredTransaction; -import org.alfresco.web.ui.common.Utils; /** @@ -63,6 +61,7 @@ public abstract class AbstractWebScript implements WebScript // Initialisation // + /** * @param scriptContext */ @@ -246,15 +245,6 @@ public abstract class AbstractWebScript implements WebScript // create template model Map model = new HashMap(7, 1.0f); - // create an image resolver - TemplateImageResolver imageResolver = new TemplateImageResolver() - { - public String resolveImagePathForName(String filename, boolean small) - { - return Utils.getFileTypeImage(getWebScriptRegistry().getContext(), filename, small); - } - }; - // add repository context if (getDescription().getRequiredAuthentication() != RequiredAuthentication.none && getDescription().getRequiredTransaction() != RequiredTransaction.none) @@ -286,7 +276,7 @@ public abstract class AbstractWebScript implements WebScript // the extensions include custom root helper objects and custom template method objects for (TemplateExtensionImplementation ext : serviceRegistry.getTemplateService().getExtensions()) { - ext.setTemplateImageResolver(imageResolver); + ext.setTemplateImageResolver(getWebScriptRegistry().getTemplateImageResolver()); model.put(ext.getExtensionName(), ext); } diff --git a/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java b/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java index 66f0df3b00..5616b3e380 100644 --- a/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java +++ b/source/java/org/alfresco/web/scripts/DeclarativeWebScriptRegistry.java @@ -36,9 +36,11 @@ import java.util.TreeMap; import javax.servlet.ServletContext; +import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.web.scripts.WebScriptDescription.RequiredAuthentication; import org.alfresco.web.scripts.WebScriptDescription.RequiredTransaction; import org.alfresco.web.scripts.WebScriptDescription.URI; +import org.alfresco.web.ui.common.Utils; import org.aopalliance.intercept.MethodInterceptor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -49,6 +51,7 @@ import org.dom4j.io.SAXReader; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.RegexpMethodPointcutAdvisor; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.web.context.ServletContextAware; @@ -59,7 +62,7 @@ import org.springframework.web.context.ServletContextAware; * * @author davidc */ -public class DeclarativeWebScriptRegistry implements WebScriptRegistry, ApplicationContextAware, ServletContextAware +public class DeclarativeWebScriptRegistry implements WebScriptRegistry, ApplicationContextAware, ServletContextAware, InitializingBean { // Logger private static final Log logger = LogFactory.getLog(DeclarativeWebScriptRegistry.class); @@ -72,6 +75,8 @@ public class DeclarativeWebScriptRegistry implements WebScriptRegistry, Applicat private MethodInterceptor serviceTransaction; private FormatRegistry formatRegistry; private WebScriptStorage storage; + private TemplateImageResolver imageResolver; + // map of web scripts by id // NOTE: The map is sorted by id (ascending order) @@ -161,7 +166,20 @@ public class DeclarativeWebScriptRegistry implements WebScriptRegistry, Applicat { this.applicationContext = applicationContext; } - + + /* (non-Javadoc) + * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() throws Exception + { + this.imageResolver = new TemplateImageResolver() + { + public String resolveImagePathForName(String filename, boolean small) + { + return Utils.getFileTypeImage(getContext(), filename, small); + } + }; + } /** * Initialise Web Scripts @@ -516,6 +534,14 @@ public class DeclarativeWebScriptRegistry implements WebScriptRegistry, Applicat return this.storage.getTemplateProcessor(); } + /* (non-Javadoc) + * @see org.alfresco.web.scripts.WebScriptRegistry#getTemplateImageResolver() + */ + public TemplateImageResolver getTemplateImageResolver() + { + return this.imageResolver; + } + /* (non-Javadoc) * @see org.alfresco.web.scripts.WebScriptRegistry#getScriptProcessor() */ diff --git a/source/java/org/alfresco/web/scripts/TestWebScriptServer.java b/source/java/org/alfresco/web/scripts/TestWebScriptServer.java index 6010d1fd25..08e226b0b5 100644 --- a/source/java/org/alfresco/web/scripts/TestWebScriptServer.java +++ b/source/java/org/alfresco/web/scripts/TestWebScriptServer.java @@ -33,11 +33,11 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; -import org.alfresco.i18n.I18NUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork; import org.alfresco.service.transaction.TransactionService; import org.springframework.context.ApplicationContext; +import org.springframework.context.MessageSource; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.mock.web.MockHttpServletRequest; @@ -64,6 +64,9 @@ public class TestWebScriptServer /** Current user */ private String username = "admin"; + /** I18N Messages */ + private MessageSource m_messages; + /** * Sets the transaction service @@ -85,16 +88,27 @@ public class TestWebScriptServer this.registry = registry; } + /** + * Sets the Messages resource bundle + * + * @param messages + * @throws IOException + */ + public void setMessages(MessageSource messages) + throws IOException + { + this.m_messages = messages; + } + /** * Initialise the Test Web Script Server * * @throws Exception */ - public void init() throws Exception + public void init() { registry.initWebScripts(); - fIn = new BufferedReader(new InputStreamReader(System.in)); } /** @@ -104,10 +118,7 @@ public class TestWebScriptServer { try { - String[] CONFIG_LOCATIONS = new String[] { "classpath:alfresco/application-context.xml", "classpath:alfresco/web-scripts-application-context.xml", "classpath:alfresco/web-scripts-application-context-test.xml" }; - ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS); - TestWebScriptServer testServer = (TestWebScriptServer)context.getBean("webscripts.test"); - testServer.init(); + TestWebScriptServer testServer = getTestServer(); testServer.rep(); } catch(Throwable e) @@ -122,13 +133,54 @@ public class TestWebScriptServer System.exit(0); } } + + /** + * Retrieve an instance of the TestWebScriptServer + * + * @return Test Server + */ + public static TestWebScriptServer getTestServer() + { + String[] CONFIG_LOCATIONS = new String[] { "classpath:alfresco/application-context.xml", "classpath:alfresco/web-scripts-application-context.xml", "classpath:alfresco/web-scripts-application-context-test.xml" }; + ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS); + TestWebScriptServer testServer = (TestWebScriptServer)context.getBean("webscripts.test"); + testServer.init(); + return testServer; + } + + /** + * Submit a Web Script Request + * + * @param method http method + * @param uri web script uri (relative to /alfresco/service) + * @return response + * @throws IOException + */ + public MockHttpServletResponse submitRequest(String method, String uri) + throws IOException + { + MockHttpServletRequest req = createRequest("get", uri); + MockHttpServletResponse res = new MockHttpServletResponse(); + + WebScriptMatch match = registry.findWebScript(req.getMethod(), uri); + if (match == null) + { + throw new WebScriptException("No service bound to uri '" + uri + "'"); + } + + WebScriptRequest apiReq = new WebScriptRequest(req, match); + WebScriptResponse apiRes = new WebScriptResponse(res); + match.getWebScript().execute(apiReq, apiRes); + return res; + } /** * A Read-Eval-Print loop. */ - public void rep() + /*package*/ void rep() { // accept commands + fIn = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("ok> "); @@ -160,12 +212,13 @@ public class TestWebScriptServer * @param line The unparsed command * @return The textual output of the command. */ - public String interpretCommand(final String line) + private String interpretCommand(final String line) throws IOException { // execute command in context of currently selected user return AuthenticationUtil.runAs(new RunAsWork() { + @SuppressWarnings("synthetic-access") public String doWork() throws Exception { return executeCommand(line); @@ -181,7 +234,7 @@ public class TestWebScriptServer * @param line The unparsed command * @return The textual output of the command. */ - protected String executeCommand(String line) + private String executeCommand(String line) throws IOException { String[] command = line.split(" "); @@ -210,8 +263,7 @@ public class TestWebScriptServer // execute command if (command[0].equals("help")) { - // TODO: - String helpFile = I18NUtil.getMessage("test_service.help"); + String helpFile = m_messages.getMessage("testserver.help", null, null); ClassPathResource helpResource = new ClassPathResource(helpFile); byte[] helpBytes = new byte[500]; InputStream helpStream = helpResource.getInputStream(); @@ -247,18 +299,7 @@ public class TestWebScriptServer } String uri = command[1]; - MockHttpServletRequest req = createRequest("get", uri); - MockHttpServletResponse res = new MockHttpServletResponse(); - - WebScriptMatch match = registry.findWebScript(req.getMethod(), uri); - if (match == null) - { - throw new WebScriptException("No service bound to uri '" + uri + "'"); - } - - WebScriptRequest apiReq = new WebScriptRequest(req, match); - WebScriptResponse apiRes = new WebScriptResponse(res); - match.getWebScript().execute(apiReq, apiRes); + MockHttpServletResponse res = submitRequest("get", uri); bout.write(res.getContentAsByteArray()); out.println(); } diff --git a/source/java/org/alfresco/web/scripts/WebScriptRegistry.java b/source/java/org/alfresco/web/scripts/WebScriptRegistry.java index 7d859d2ccf..d856c67d89 100644 --- a/source/java/org/alfresco/web/scripts/WebScriptRegistry.java +++ b/source/java/org/alfresco/web/scripts/WebScriptRegistry.java @@ -28,6 +28,8 @@ import java.util.Collection; import javax.servlet.ServletContext; +import org.alfresco.service.cmr.repository.TemplateImageResolver; + /** * Web Scripts Registry @@ -81,6 +83,13 @@ public interface WebScriptRegistry */ public TemplateProcessor getTemplateProcessor(); + /** + * Gets the Template Image Resolver + * + * @return template image resolver + */ + public TemplateImageResolver getTemplateImageResolver(); + /** * Gets the Script Processor * diff --git a/source/java/org/alfresco/web/scripts/bean/KeywordSearch.java b/source/java/org/alfresco/web/scripts/bean/KeywordSearch.java index 696e116c48..71e87ef2c7 100644 --- a/source/java/org/alfresco/web/scripts/bean/KeywordSearch.java +++ b/source/java/org/alfresco/web/scripts/bean/KeywordSearch.java @@ -33,7 +33,6 @@ import java.util.Map; import org.alfresco.i18n.I18NUtil; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.StoreRef; -import org.alfresco.service.cmr.repository.TemplateImageResolver; import org.alfresco.service.cmr.repository.TemplateNode; import org.alfresco.service.cmr.search.ResultSet; import org.alfresco.service.cmr.search.SearchParameters; @@ -44,7 +43,6 @@ import org.alfresco.web.scripts.DeclarativeWebScript; import org.alfresco.web.scripts.WebScriptException; import org.alfresco.web.scripts.WebScriptRequest; import org.alfresco.web.scripts.WebScriptResponse; -import org.alfresco.web.ui.common.Utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -68,15 +66,6 @@ public class KeywordSearch extends DeclarativeWebScript // dependencies protected SearchService searchService; - // icon resolver - protected TemplateImageResolver iconResolver = new TemplateImageResolver() - { - public String resolveImagePathForName(String filename, boolean small) - { - return Utils.getFileTypeImage(getWebScriptRegistry().getContext(), filename, small); - } - }; - /** * @param searchService */ @@ -364,7 +353,7 @@ public class KeywordSearch extends DeclarativeWebScript */ public SearchTemplateNode(NodeRef nodeRef, float score) { - super(nodeRef, getServiceRegistry(), iconResolver); + super(nodeRef, getServiceRegistry(), getWebScriptRegistry().getTemplateImageResolver()); this.score = score; }