private void HandleRequest(WoopsaVerb verb, HTTPRequest request, HTTPResponse response)
{
try
{
// This is the first thing we do, that way even 404 errors have the right headers
if (AllowCrossOrigin)
{
// TODO: constantes symboliques
response.SetHeader("Access-Control-Allow-Origin", "*");
}
string result = null;
ExecuteBeforeWoopsaModelAccess();
try
{
switch (verb)
{
case WoopsaVerb.Meta:
result = GetMetadata(request.Subroute);
break;
case WoopsaVerb.Read:
result = ReadValue(request.Subroute);
break;
case WoopsaVerb.Write:
result = WriteValue(request.Subroute, request.Body["value"]);
break;
case WoopsaVerb.Invoke:
result = InvokeMethod(request.Subroute, request.Body);
break;
}
}
finally
{
ExecuteAfterWoopsaModelAccess();
}
response.SetHeader(HTTPHeader.ContentType, MIMETypes.Application.JSON);
if (result != null)
{
response.WriteString(result);
}
}
catch (WoopsaNotFoundException e)
{
response.WriteError(HTTPStatusCode.NotFound, e.Message, WoopsaFormat.Serialize(e), MIMETypes.Application.JSON);
}
catch (WoopsaInvalidOperationException e)
{
response.WriteError(HTTPStatusCode.BadRequest, e.Message, WoopsaFormat.Serialize(e), MIMETypes.Application.JSON);
}
catch (WoopsaException e)
{
response.WriteError(HTTPStatusCode.InternalServerError, e.Message, WoopsaFormat.Serialize(e), MIMETypes.Application.JSON);
}
catch (Exception e)
{
response.WriteError(HTTPStatusCode.InternalServerError, e.Message, WoopsaFormat.Serialize(e), MIMETypes.Application.JSON);
}
}