Web Scripts:

- filename convention changed from _ to .

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5793 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-05-29 09:32:04 +00:00
parent c3b15747fa
commit 3e0cee8351
50 changed files with 105 additions and 75 deletions

View File

@@ -96,7 +96,7 @@ public class ClassPathStore implements WebScriptStore, InitializingBean
{
int filePathLength = fileDir.getAbsolutePath().length() +1;
List<String> documentPaths = new ArrayList<String>();
Resource[] resources = resolver.getResources("classpath*:" + classPath + "/**/*_desc.xml");
Resource[] resources = resolver.getResources("classpath*:" + classPath + "/**/*.desc.xml");
for (Resource resource : resources)
{
if (resource instanceof FileSystemResource)

View File

@@ -53,7 +53,7 @@ public class DeclarativeWebScript extends AbstractWebScript
// Logger
private static final Log logger = LogFactory.getLog(DeclarativeWebScript.class);
private String baseTemplatePath;
private String basePath;
private ScriptLocation executeScript;
@@ -64,10 +64,10 @@ public class DeclarativeWebScript extends AbstractWebScript
public void init(WebScriptRegistry apiRegistry)
{
super.init(apiRegistry);
baseTemplatePath = getDescription().getId().replace('.', '/');
basePath = getDescription().getId();
// Test for "execute" script
String scriptPath = baseTemplatePath + ".js";
String scriptPath = basePath + ".js";
executeScript = getWebScriptRegistry().getScriptProcessor().findScript(scriptPath);
}
@@ -210,7 +210,7 @@ public class DeclarativeWebScript extends AbstractWebScript
final protected void renderFormatTemplate(String format, Map<String, Object> model, Writer writer)
{
format = (format == null) ? "" : format;
String templatePath = baseTemplatePath + "_" + format + ".ftl";
String templatePath = basePath + "." + format + ".ftl";
if (logger.isDebugEnabled())
logger.debug("Rendering template '" + templatePath + "'");

View File

@@ -222,8 +222,8 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
{
WebScript existingService = webscriptsById.get(id);
WebScriptDescription existingDesc = existingService.getDescription();
String msg = "Web Script description document " + serviceDesc.getSourceStore() + "/" + serviceDesc.getSourceLocation();
msg += " overridden by " + existingDesc.getSourceStore() + "/" + existingDesc.getSourceLocation();
String msg = "Web Script description document " + serviceDesc.getStorePath() + "/" + serviceDesc.getDescPath();
msg += " overridden by " + existingDesc.getStorePath() + "/" + existingDesc.getDescPath();
logger.debug(msg);
}
continue;
@@ -231,13 +231,14 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
// construct service implementation
ApplicationContext applicationContext = getApplicationContext();
String serviceImplName = (applicationContext.containsBean("webscript." + id)) ? "webscript." + id : defaultWebScript;
String beanName = "webscript." + id.replace('/', '.');
String serviceImplName = (applicationContext.containsBean(beanName)) ? beanName : defaultWebScript;
AbstractWebScript serviceImpl = (AbstractWebScript)applicationContext.getBean(serviceImplName);
serviceImpl.setDescription(serviceDesc);
serviceImpl.init(this);
if (logger.isDebugEnabled())
logger.debug("Found Web Script " + serviceDescPath + " (id: " + id + ", impl: " + serviceImplName + ", auth: " + serviceDesc.getRequiredAuthentication() + ", trx: " + serviceDesc.getRequiredTransaction() + ")");
logger.debug("Found Web Script " + id + " (desc: " + serviceDescPath + ", impl: " + serviceImplName + ", auth: " + serviceDesc.getRequiredAuthentication() + ", trx: " + serviceDesc.getRequiredTransaction() + ")");
// register service and its urls
webscriptsById.put(id, serviceImpl);
@@ -263,7 +264,7 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
WebScript existingService = webscriptsByURL.get(uriIdx);
if (!existingService.getDescription().getId().equals(serviceDesc.getId()))
{
String msg = "Web Script document " + serviceDesc.getSourceLocation() + " is attempting to define the url '" + uriIdx + "' already defined by " + existingService.getDescription().getSourceLocation();
String msg = "Web Script document " + serviceDesc.getDescPath() + " is attempting to define the url '" + uriIdx + "' already defined by " + existingService.getDescription().getDescPath();
throw new WebScriptException(msg);
}
}
@@ -303,16 +304,19 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
throw new WebScriptException("Expected <webscript> root element - found <" + rootElement.getName() + ">");
}
// retrieve id
String id = serviceDescPath.substring(0, serviceDescPath.lastIndexOf("_desc.xml")).replace('/', '.');
// retrieve script path
String scriptPath = serviceDescPath.substring(0, serviceDescPath.lastIndexOf('/'));
// retrieve script id
String id = serviceDescPath.substring(0, serviceDescPath.lastIndexOf(".desc.xml"));
// retrieve http method
int methodIdx = id.lastIndexOf('_');
int methodIdx = id.lastIndexOf('.');
if (methodIdx == id.length() - 1)
{
throw new WebScriptException("Unable to establish HTTP Method from web script description: naming convention must be <name>_<method>_desc.xml");
throw new WebScriptException("Unable to establish HTTP Method from web script description: naming convention must be <name>.<method>.desc.xml");
}
String method = id.substring(id.lastIndexOf('_') + 1).toUpperCase();
String method = id.substring(id.lastIndexOf('.') + 1).toUpperCase();
// retrieve short name
Element shortNameElement = rootElement.element("shortname");
@@ -400,8 +404,9 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
// construct service description
WebScriptDescriptionImpl serviceDesc = new WebScriptDescriptionImpl();
serviceDesc.setSourceStore(store);
serviceDesc.setSourceLocation(serviceDescPath);
serviceDesc.setStore(store);
serviceDesc.setScriptPath(scriptPath);
serviceDesc.setDescPath(serviceDescPath);
serviceDesc.setId(id);
serviceDesc.setShortName(shortName);
serviceDesc.setDescription(description);

View File

@@ -268,14 +268,17 @@ public class RepoStore implements WebScriptStore, ApplicationContextAware, Appli
int baseDirLength = baseDir.length() +1;
List<String> documentPaths = new ArrayList<String>();
String query = "PATH:\"" + repoPath + "//*\" AND @cm\\:name:\"*_desc.xml\"";
String query = "PATH:\"" + repoPath + "//*\" AND @cm\\:name:\"*desc.xml\"";
ResultSet resultSet = searchService.query(repoStore, SearchService.LANGUAGE_LUCENE, query);
List<NodeRef> nodes = resultSet.getNodeRefs();
for (NodeRef nodeRef : nodes)
{
String nodeDir = getPath(nodeRef);
String documentPath = nodeDir.substring(baseDirLength);
documentPaths.add(documentPath);
if (nodeDir.endsWith(".desc.xml"))
{
String documentPath = nodeDir.substring(baseDirLength);
documentPaths.add(documentPath);
}
}
return documentPaths.toArray(new String[documentPaths.size()]);

View File

@@ -126,7 +126,6 @@ public class TestWebScriptServer
*/
public void init()
{
registry.initWebScripts();
Config config = configService.getConfig("Server");
serverConfig = (ServerConfigElement)config.getConfigElement(ServerConfigElement.CONFIG_ELEMENT_ID);
}

View File

@@ -54,29 +54,36 @@ public interface WebScriptDescription
required,
requiresnew
}
/**
* Gets the source document of this web script
* Gets the root path of the store of this web script
*
* @return source document
* @return root path of store
*/
public InputStream getSourceDocument()
throws IOException;
public String getStorePath();
/**
* Gets the store this web script is located in
* Gets the path within the store of this web script
*
* @return path to store
* @return path within store
*/
public String getSourceStore();
public String getScriptPath();
/**
* Gets the source document location of this service description
* Gets the path of the description xml document for this web script
*
* @return document location (path)
*/
public String getSourceLocation();
public String getDescPath();
/**
* Gets the description xml document for this web script
*
* @return source document
*/
public InputStream getDescDocument()
throws IOException;
/**
* Gets the id of this service

View File

@@ -37,8 +37,9 @@ import java.util.Map;
*/
public class WebScriptDescriptionImpl implements WebScriptDescription
{
private WebScriptStore sourceStore;
private String sourceLocation;
private WebScriptStore store;
private String scriptPath;
private String descPath;
private String id;
private String shortName;
private String description;
@@ -51,48 +52,66 @@ public class WebScriptDescriptionImpl implements WebScriptDescription
/**
* Sets the source store
* Sets the web description store
*
* @param store source store
* @param store store
*/
public void setSourceStore(WebScriptStore store)
public void setStore(WebScriptStore store)
{
this.sourceStore = store;
this.store = store;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getSourceDocument()
* @see org.alfresco.web.scripts.WebScriptDescription#getStorePath()
*/
public InputStream getSourceDocument()
throws IOException
public String getStorePath()
{
return this.sourceStore.getDescriptionDocument(sourceLocation);
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getSourceStore()
*/
public String getSourceStore()
{
return this.sourceStore.getBasePath();
return store.getBasePath();
}
/**
* Sets the source location
* Sets the script path
*
* @param sourceLocation
* @param scriptPath
*/
public void setSourceLocation(String sourceLocation)
public void setScriptPath(String scriptPath)
{
this.sourceLocation = sourceLocation;
this.scriptPath = scriptPath;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getSourceLocation()
* @see org.alfresco.web.scripts.WebScriptDescription#getScriptPath()
*/
public String getSourceLocation()
public String getScriptPath()
{
return this.sourceLocation;
return scriptPath;
}
/**
* Sets the desc path
*
* @param descPath
*/
public void setDescPath(String descPath)
{
this.descPath = descPath;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getDescPath()
*/
public String getDescPath()
{
return descPath;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getDescDocument()
*/
public InputStream getDescDocument()
throws IOException
{
return store.getDescriptionDocument(descPath);
}
/**

View File

@@ -48,12 +48,11 @@ public class ServiceDescription extends AbstractWebScript
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
{
// extract web script id
String extensionPath = req.getExtensionPath();
if (extensionPath == null || extensionPath.length() == 0)
String scriptId = req.getExtensionPath();
if (scriptId == null || scriptId.length() == 0)
{
throw new WebScriptException("Web Script Id not provided");
}
String scriptId = extensionPath.replace("/", ".");
// locate web script
WebScript script = getWebScriptRegistry().getWebScript(scriptId);
@@ -67,7 +66,7 @@ public class ServiceDescription extends AbstractWebScript
InputStream serviceDescIS = null;
try
{
serviceDescIS = desc.getSourceDocument();
serviceDescIS = desc.getDescDocument();
OutputStream out = res.getOutputStream();
res.setContentType(MimetypeMap.MIMETYPE_XML + ";charset=UTF-8");
byte[] buffer = new byte[2048];