Web Scripts:

- allow tunelling of arbitrary http methods via POST (for clients who have restricted HTTP vocabulary support)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5849 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Caruana
2007-06-05 12:02:25 +00:00
parent ac9960758f
commit 7b09497a8f
3 changed files with 101 additions and 1 deletions

View File

@@ -32,6 +32,8 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.config.Config;
import org.alfresco.config.ConfigService;
@@ -204,6 +206,31 @@ public class TestWebScriptServer
return res;
}
/**
* Submit a Web Script Request
*
* @param method http method
* @param uri web script uri (relative to /alfresco/service)
* @param headers headers
* @return response
* @throws IOException
*/
public MockHttpServletResponse submitRequest(String method, String uri, Map<String, String> headers)
throws IOException
{
MockHttpServletRequest req = createRequest(method, uri);
for (Map.Entry<String, String> header: headers.entrySet())
{
req.addHeader(header.getKey(), header.getValue());
}
MockHttpServletResponse res = new MockHttpServletResponse();
WebScriptRuntime runtime = new WebScriptServletRuntime(registry, transactionService, null, req, res, serverConfig);
runtime.executeScript();
return res;
}
/**
* A Read-Eval-Print loop.
*/
@@ -358,6 +385,44 @@ public class TestWebScriptServer
bout.write(res.getContentAsByteArray());
out.println();
}
else if (command[0].equals("tunnel"))
{
if (command.length < 4)
{
return "Syntax Error.\n";
}
if (command[1].equals("param"))
{
String uri = command[3];
if (uri.indexOf('?') == -1)
{
uri += "?alf:method=" + command[2];
}
else
{
uri += "&alf:method=" + command[2];
}
MockHttpServletResponse res = submitRequest("post", uri);
bout.write(res.getContentAsByteArray());
out.println();
}
else if (command[1].equals("header"))
{
Map<String, String> headers = new HashMap<String, String>();
headers.put("X-HTTP-Method-Override", command[2]);
MockHttpServletResponse res = submitRequest("post", command[3], headers);
bout.write(res.getContentAsByteArray());
out.println();
}
else
{
return "Syntax Error.\n";
}
}
else if (command[0].equals("reset"))
{

View File

@@ -70,7 +70,27 @@ public class WebScriptServletRuntime extends WebScriptRuntime
@Override
protected String getScriptMethod()
{
return req.getMethod();
// Is this an overloaded POST request?
String method = req.getMethod();
if (method.equalsIgnoreCase("post"))
{
boolean overloadParam = false;
String overload = req.getHeader("X-HTTP-Method-Override");
if (overload == null || overload.length() == 0)
{
overload = req.getParameter("alf:method");
overloadParam = true;
}
if (overload != null && overload.length() > 0)
{
if (logger.isDebugEnabled())
logger.debug("POST is tunnelling method '" + overload + "' as specified by " + (overloadParam ? "alf:method parameter" : "X-HTTP-Method-Override header"));
method = overload;
}
}
return method;
}
/* (non-Javadoc)