Web Scripts:

- optionally force status in response header to success regardless of actual status; status in reponse document still reflects actual status
- to support the limitations of Flex runtime without Flex data services
- set alf-force-success-response to true in request header

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5873 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-06-06 22:13:47 +00:00
parent 78342496ab
commit 955db41f51
8 changed files with 93 additions and 18 deletions

View File

@@ -6,5 +6,4 @@
<url format="rss" template="/api/search/keyword.rss?q={searchTerms}&amp;p={startPage?}&amp;c={count?}&amp;l={language?}&amp;guest={alf:guest?}"/> <url format="rss" template="/api/search/keyword.rss?q={searchTerms}&amp;p={startPage?}&amp;c={count?}&amp;l={language?}&amp;guest={alf:guest?}"/>
<url format="portlet" template="/api/search/keyword.portlet?q={searchTerms}&amp;p={startPage?}&amp;c={count?}&amp;l={language?}&amp;guest={alf:guest?}"/> <url format="portlet" template="/api/search/keyword.portlet?q={searchTerms}&amp;p={startPage?}&amp;c={count?}&amp;l={language?}&amp;guest={alf:guest?}"/>
<authentication>guest</authentication> <authentication>guest</authentication>
<format default="html">any</format>
</webscript> </webscript>

View File

@@ -31,6 +31,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.repo.jscript.Node; import org.alfresco.repo.jscript.Node;
import org.alfresco.repo.jscript.ScriptableHashMap; import org.alfresco.repo.jscript.ScriptableHashMap;
import org.alfresco.repo.template.AbsoluteUrlMethod; import org.alfresco.repo.template.AbsoluteUrlMethod;
@@ -358,12 +360,13 @@ public abstract class AbstractWebScript implements WebScript
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
logger.debug("Force success status header in response: " + req.forceSuccessStatus());
logger.debug("Sending status " + statusCode + " (Template: " + template.path + ")"); logger.debug("Sending status " + statusCode + " (Template: " + template.path + ")");
logger.debug("Rendering response: content type=" + mimetype); logger.debug("Rendering response: content type=" + mimetype);
} }
res.reset(); res.reset();
res.setStatus(statusCode); res.setStatus(req.forceSuccessStatus() ? HttpServletResponse.SC_OK : statusCode);
res.setContentType(mimetype + ";charset=UTF-8"); res.setContentType(mimetype + ";charset=UTF-8");
renderTemplate(template.path, model, res.getWriter()); renderTemplate(template.path, model, res.getWriter());
} }

View File

@@ -129,8 +129,10 @@ public class DeclarativeWebScript extends AbstractWebScript
{ {
// render output // render output
int statusCode = status.getCode(); int statusCode = status.getCode();
if (statusCode != HttpServletResponse.SC_OK) if (statusCode != HttpServletResponse.SC_OK && !req.forceSuccessStatus())
{ {
logger.debug("Force success status header in response: " + req.forceSuccessStatus());
logger.debug("Setting status " + statusCode);
res.setStatus(statusCode); res.setStatus(statusCode);
} }

View File

@@ -79,6 +79,9 @@ public class TestWebScriptServer
/** Current user */ /** Current user */
private String username = "admin"; private String username = "admin";
/** Current headers */
private Map<String, String> headers = new HashMap<String, String>();
/** I18N Messages */ /** I18N Messages */
private MessageSource m_messages; private MessageSource m_messages;
@@ -391,7 +394,9 @@ public class TestWebScriptServer
command[0].equals("delete")) command[0].equals("delete"))
{ {
String uri = (command.length > 1) ? command[1] : null; String uri = (command.length > 1) ? command[1] : null;
MockHttpServletResponse res = submitRequest(command[0], uri); MockHttpServletResponse res = submitRequest(command[0], uri, headers);
bout.write(("Response status: " + res.getStatus()).getBytes());
out.println();
bout.write(res.getContentAsByteArray()); bout.write(res.getContentAsByteArray());
out.println(); out.println();
} }
@@ -414,16 +419,17 @@ public class TestWebScriptServer
{ {
uri += "&alf:method=" + command[2]; uri += "&alf:method=" + command[2];
} }
MockHttpServletResponse res = submitRequest("post", uri); MockHttpServletResponse res = submitRequest("post", uri, headers);
bout.write(res.getContentAsByteArray()); bout.write(res.getContentAsByteArray());
out.println(); out.println();
} }
else if (command[1].equals("header")) else if (command[1].equals("header"))
{ {
Map<String, String> headers = new HashMap<String, String>(); Map<String, String> tunnelheaders = new HashMap<String, String>();
headers.put("X-HTTP-Method-Override", command[2]); tunnelheaders.putAll(headers);
MockHttpServletResponse res = submitRequest("post", command[3], headers); tunnelheaders.put("X-HTTP-Method-Override", command[2]);
MockHttpServletResponse res = submitRequest("post", command[3], tunnelheaders);
bout.write(res.getContentAsByteArray()); bout.write(res.getContentAsByteArray());
out.println(); out.println();
} }
@@ -434,6 +440,43 @@ public class TestWebScriptServer
} }
} }
else if (command[0].equals("header"))
{
if (command.length == 1)
{
for (Map.Entry<String, String> entry : headers.entrySet())
{
out.println(entry.getKey() + " = " + entry.getValue());
}
}
else if (command.length == 2)
{
String[] param = command[1].split("=");
if (param.length == 0)
{
return "Syntax Error.\n";
}
if (param.length == 1)
{
headers.remove(param[0]);
out.println("deleted header " + param[0]);
}
else if (param.length == 2)
{
headers.put(param[0], param[1]);
out.println("set header " + param[0] + " = " + headers.get(param[0]));
}
else
{
return "Syntax Error.\n";
}
}
else
{
return "Syntax Error.\n";
}
}
else if (command[0].equals("reset")) else if (command[0].equals("reset"))
{ {
registry.reset(); registry.reset();

View File

@@ -141,13 +141,6 @@ public interface WebScriptRequest
*/ */
public FormatStyle getFormatStyle(); public FormatStyle getFormatStyle();
/**
* Get the JSON callback method
*
* @return method (or null, if not specified)
*/
public String getJSONCallback();
/** /**
* Get User Agent * Get User Agent
* *
@@ -157,4 +150,21 @@ public interface WebScriptRequest
*/ */
public String getAgent(); public String getAgent();
/**
* Get the JSON callback method
*
* @return method (or null, if not specified)
*/
public String getJSONCallback();
/**
* Force response to return SUCCESS (200) code
*
* Note: This is to support clients who cannot support non-success codes
* e.g. Flash player
*
* @return true => force return of 200, otherwise return status explicitly set
*/
public boolean forceSuccessStatus();
} }

View File

@@ -147,4 +147,12 @@ public abstract class WebScriptRequestImpl implements WebScriptRequest
return getParameter("alf_callback"); return getParameter("alf_callback");
} }
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptRequest#forceSuccessStatus()
*/
public boolean forceSuccessStatus()
{
return false;
}
} }

View File

@@ -138,8 +138,7 @@ public abstract class WebScriptRuntime
String reqFormat = scriptReq.getFormat(); String reqFormat = scriptReq.getFormat();
String format = (reqFormat == null || reqFormat.length() == 0) ? "default" : scriptReq.getFormat(); String format = (reqFormat == null || reqFormat.length() == 0) ? "default" : scriptReq.getFormat();
WebScriptDescription desc = scriptReq.getServiceMatch().getWebScript().getDescription(); WebScriptDescription desc = scriptReq.getServiceMatch().getWebScript().getDescription();
logger.debug("Format style: " + desc.getFormatStyle()); logger.debug("Format style: " + desc.getFormatStyle() + ", Default format: " + desc.getDefaultFormat());
logger.debug("Default format: " + desc.getDefaultFormat());
logger.debug("Invoking Web Script " + description.getId() + (user == null ? " (unauthenticated)" : " (authenticated as " + user + ") (format " + format + ") (" + locale + ")")); logger.debug("Invoking Web Script " + description.getId() + (user == null ? " (unauthenticated)" : " (authenticated as " + user + ") (format " + format + ") (" + locale + ")"));
} }
@@ -224,12 +223,13 @@ public abstract class WebScriptRuntime
// render output // render output
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
logger.debug("Force success status header in response: " + req.forceSuccessStatus());
logger.debug("Sending status " + statusCode + " (Template: " + templatePath + ")"); logger.debug("Sending status " + statusCode + " (Template: " + templatePath + ")");
logger.debug("Rendering response: content type=" + MimetypeMap.MIMETYPE_HTML); logger.debug("Rendering response: content type=" + MimetypeMap.MIMETYPE_HTML);
} }
res.reset(); res.reset();
res.setStatus(statusCode); res.setStatus(req.forceSuccessStatus() ? HttpServletResponse.SC_OK : statusCode);
res.setContentType(MimetypeMap.MIMETYPE_HTML + ";charset=UTF-8"); res.setContentType(MimetypeMap.MIMETYPE_HTML + ";charset=UTF-8");
try try
{ {

View File

@@ -248,4 +248,14 @@ public class WebScriptServletRequest extends WebScriptRequestImpl
return port; return port;
} }
/* (non-Javadoc)
* @see org.alfresco.web.scripts.WebScriptRequestImpl#forceSuccessStatus()
*/
@Override
public boolean forceSuccessStatus()
{
String forceSuccess = req.getHeader("alf-force-success-response");
return Boolean.valueOf(forceSuccess);
}
} }