1) Consolidate creation of template image resolver in Web Scripts

2) Add Help to stand-alone Web Scripts test server
3) Add server-side JavaScript Debugger

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5385 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-03-13 18:54:51 +00:00
parent a6c339a2fc
commit 83e8cf83b8
9 changed files with 152 additions and 63 deletions

View File

@@ -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<String, Object> model = new HashMap<String, Object>(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);
}

View File

@@ -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()
*/

View File

@@ -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<String>()
{
@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();
}

View File

@@ -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
*

View File

@@ -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;
}