* @return A Calls object that represents the data in the request
*/
@SuppressWarnings("unchecked")
public Calls convertToCalls(HttpServletRequest request)
{
InboundContext inboundContext = new InboundContext();
String pathInfo = request.getPathInfo();
String[] pathParts = pathInfo.split("/");
if (pathParts.length < 4)
{
log.warn("pathInfo '" + pathInfo + "' contains " + pathParts.length + " parts. At least 4 are required.");
throw new JsonpCallException("Bad JSON request. See logs for more details.");
}
if (pathParts.length > 4)
{
for (int i = 4; i < pathParts.length; i++)
{
String key = ProtocolConstants.INBOUND_CALLNUM_PREFIX + 0 +
ProtocolConstants.INBOUND_CALLNUM_SUFFIX +
ProtocolConstants.INBOUND_KEY_PARAM + (i - 4);
inboundContext.createInboundVariable(0, key, "string", pathParts[i]);
}
}
else
{
Map<String, String[]> requestParams = request.getParameterMap();
int i = 0;
while (true)
{
String[] values = requestParams.get("param" + i);
if (values == null)
{
break;
}
else
{
String key = ProtocolConstants.INBOUND_CALLNUM_PREFIX + 0 +
ProtocolConstants.INBOUND_CALLNUM_SUFFIX +
ProtocolConstants.INBOUND_KEY_PARAM + i;
inboundContext.createInboundVariable(0, key, "string", values[0], true);
i++;
}
}
}
Call call = new Call(null, pathParts[2], pathParts[3]);
// JSON does not support batching
Calls calls = new Calls();
calls.addCall(call);
// Which method are we using?
call.findMethod(moduleManager, converterManager, inboundContext, 0);
MethodDeclaration method = call.getMethodDeclaration();
// Check this method is accessible
accessControl.assertGeneralExecutionIsPossible(call.getScriptName(), method);
// We are now sure we have the set of input lined up. They may
// cross-reference so we do the de-referencing all in one go.
try
{
inboundContext.dereference();
}
catch (ConversionException ex)
{
log.warn("Dereferencing exception", ex);
throw new JsonpCallException("Error dereferencing call. See logs for more details.");
}
// Convert all the parameters to the correct types
Object[] params = new Object[method.getParameterTypes().length];
for (int j = 0; j < method.getParameterTypes().length; j++)
{
Class<?> paramType = method.getParameterTypes()[j];
InboundVariable param = inboundContext.getParameter(0, j);
Property property = new ParameterProperty(method, j);
try
{
params[j] = converterManager.convertInbound(paramType, param, property);