Web Scripts:

- simplify how web scripts urls & formats are described
- fix 'reset' on index page (when accessed via /)
- fix xml status response (rogue html in template)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5897 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-06-08 21:39:54 +00:00
parent f95c3de04f
commit bb2536fcf5
36 changed files with 101 additions and 189 deletions

View File

@@ -43,7 +43,6 @@ import org.alfresco.util.AbstractLifecycleBean;
import org.alfresco.web.scripts.WebScriptDescription.FormatStyle;
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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -151,9 +150,18 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
*/
public void reset()
{
getTemplateProcessor().resetCache();
getScriptProcessor().resetCache();
initWebScripts();
long startTime = System.currentTimeMillis();
try
{
getTemplateProcessor().resetCache();
getScriptProcessor().resetCache();
initWebScripts();
}
finally
{
if (logger.isInfoEnabled())
logger.info("Registered " + webscriptsById.size() + " Web Scripts " + webscriptsByURL.size() + ", URLs (in " + (System.currentTimeMillis() - startTime) + "ms)");
}
}
/* (non-Javadoc)
@@ -162,7 +170,7 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
@Override
protected void onBootstrap(ApplicationEvent event)
{
initWebScripts();
reset();
}
/* (non-Javadoc)
@@ -178,7 +186,7 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
*
* Note: Each invocation of this method resets the list of the services
*/
public void initWebScripts()
private void initWebScripts()
{
if (logger.isDebugEnabled())
logger.debug("Initialising Web Scripts");
@@ -250,17 +258,16 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
if (logger.isDebugEnabled())
logger.debug("Found Web Script " + id + " (desc: " + serviceDescPath + ", impl: " + serviceImplName + ", auth: " +
serviceDesc.getRequiredAuthentication() + ", trx: " + serviceDesc.getRequiredTransaction() + ", format: " +
serviceDesc.getFormatStyle() + ")");
serviceDesc.getRequiredAuthentication() + ", trx: " + serviceDesc.getRequiredTransaction() + ", format style: " +
serviceDesc.getFormatStyle() + ", default format: " + serviceDesc.getDefaultFormat() + ")");
// register service and its urls
webscriptsById.put(id, serviceImpl);
for (URI uri : serviceDesc.getURIs())
for (String uriTemplate : serviceDesc.getURIs())
{
// establish static part of url template
boolean wildcard = false;
boolean extension = true;
String uriTemplate = uri.getURI();
int queryArgIdx = uriTemplate.indexOf('?');
if (queryArgIdx != -1)
{
@@ -309,9 +316,6 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
registerURIs(serviceImpl);
}
}
if (logger.isDebugEnabled())
logger.debug("Registered " + webscriptsById.size() + " Web Scripts; " + webscriptsByURL.size() + " URLs");
}
/**
@@ -347,10 +351,10 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
private void registerURIs(WebScript script)
{
WebScriptDescription desc = script.getDescription();
for (URI uri : desc.getURIs())
for (String uri : desc.getURIs())
{
Path path = uriByPath.get("/");
String[] parts = uri.getURI().split("/");
String[] parts = uri.split("/");
for (String part : parts)
{
if (part.indexOf("?") != -1)
@@ -429,32 +433,25 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
{
throw new WebScriptException("Expected at least one <url> element");
}
List<WebScriptDescription.URI> uris = new ArrayList<WebScriptDescription.URI>();
List<String> uris = new ArrayList<String>();
Iterator iterElements = urlElements.iterator();
while(iterElements.hasNext())
{
// retrieve url element
Element urlElement = (Element)iterElements.next();
// retrieve url mimetype
String format = urlElement.attributeValue("format");
if (format == null)
{
// default to unspecified format
format = "";
}
// retrieve url template
String template = urlElement.attributeValue("template");
String template = urlElement.getTextTrim();
if (template == null || template.length() == 0)
{
throw new WebScriptException("Expected template attribute on <url> element");
// NOTE: for backwards compatibility only
template = urlElement.attributeValue("template");
if (template == null || template.length() == 0)
{
throw new WebScriptException("Expected <url> element value");
}
}
WebScriptDescriptionImpl.URIImpl uriImpl = new WebScriptDescriptionImpl.URIImpl();
uriImpl.setFormat(format);
uriImpl.setUri(template);
uris.add(uriImpl);
uris.add(template);
}
// retrieve authentication
@@ -492,11 +489,18 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
}
// retrieve format
String defaultFormat = uris.get(0).getFormat();
String defaultFormat = "html";
FormatStyle formatStyle = FormatStyle.any;
Element formatElement = rootElement.element("format");
if (formatElement != null)
{
// establish if default is set explicitly
String attrDefaultValue = formatElement.attributeValue("default");
if (attrDefaultValue != null)
{
defaultFormat = (attrDefaultValue.length() == 0) ? null : attrDefaultValue;
}
// establish format declaration style
String formatStyleStr = formatElement.getTextTrim();
if (formatStyleStr != null && formatStyleStr.length() > 0)
{
@@ -506,11 +510,6 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
throw new WebScriptException("Format Style '" + formatStyle + "' is not a valid value");
}
}
String defaultFormatStr = formatElement.attributeValue("default");
if (defaultFormatStr != null && defaultFormatStr.length() > 0)
{
defaultFormat = defaultFormatStr;
}
}
// construct service description
@@ -524,7 +523,7 @@ public class DeclarativeWebScriptRegistry extends AbstractLifecycleBean
serviceDesc.setRequiredAuthentication(reqAuth);
serviceDesc.setRequiredTransaction(reqTrx);
serviceDesc.setMethod(method);
serviceDesc.setUris(uris.toArray(new WebScriptDescription.URI[uris.size()]));
serviceDesc.setUris(uris.toArray(new String[uris.size()]));
serviceDesc.setDefaultFormat(defaultFormat);
serviceDesc.setFormatStyle(formatStyle);
return serviceDesc;

View File

@@ -140,7 +140,7 @@ public interface WebScriptDescription
*
* @return array of URIs in order specified in service description document
*/
public URI[] getURIs();
public String[] getURIs();
/**
* Gets the style of Format discriminator supported by this web script
@@ -149,45 +149,14 @@ public interface WebScriptDescription
*/
public FormatStyle getFormatStyle();
/**
* Gets a URI by format
*
* @param format the format
* @return the URI (or null, if no URI registered for the format)
*/
public URI getURI(String format);
/**
* Gets the default response format
*
* Note: the default response format is the first listed in the service
* description document
*
* @return default response format
* @return default response format (or null, if format not known until run-time)
*/
public String getDefaultFormat();
/**
* Web Script URL
*
* @author davidc
*/
public interface URI
{
/**
* Gets the URI response format
*
* @return format
*/
public String getFormat();
/**
* Gets the URI
*
* @return uri
*/
public String getURI();
}
}

View File

@@ -26,8 +26,6 @@ package org.alfresco.web.scripts;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
@@ -47,9 +45,8 @@ public class WebScriptDescriptionImpl implements WebScriptDescription
private RequiredTransaction requiredTransaction;
private FormatStyle formatStyle;
private String httpMethod;
private URI[] uris;
private String[] uris;
private String defaultFormat;
private Map<String, URI> uriByFormat;
/**
@@ -246,32 +243,19 @@ public class WebScriptDescriptionImpl implements WebScriptDescription
*
* @param uris
*/
public void setUris(URI[] uris)
public void setUris(String[] uris)
{
this.uriByFormat = new HashMap<String, URI>(uris.length);
for (URI uri : uris)
{
this.uriByFormat.put(uri.getFormat(), uri);
}
this.uris = uris;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getURIs()
*/
public URI[] getURIs()
public String[] getURIs()
{
return this.uris;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription#getURI(java.lang.String)
*/
public URI getURI(String format)
{
return this.uriByFormat.get(format);
}
/**
* Sets the default response format
*
@@ -290,52 +274,4 @@ public class WebScriptDescriptionImpl implements WebScriptDescription
return this.defaultFormat;
}
/**
* Web Script URL Implementation
*
* @author davidc
*/
public static class URIImpl implements WebScriptDescription.URI
{
private String format;
private String uri;
/**
* Sets the URI response format
*
* @param format
*/
public void setFormat(String format)
{
this.format = format;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription.URI#getFormat()
*/
public String getFormat()
{
return this.format;
}
/**
* Sets the URI
*
* @param uri
*/
public void setUri(String uri)
{
this.uri = uri;
}
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptDescription.URI#getURI()
*/
public String getURI()
{
return this.uri;
}
}
}