2012-02-07 05:08:07 +00:00
|
|
|
using System;
|
2012-02-05 06:34:36 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
2012-02-07 05:08:07 +00:00
|
|
|
using NLog;
|
2012-02-05 06:34:36 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace NzbDrone.Services.Service
|
|
|
|
{
|
|
|
|
public class JsonModelBinder : DefaultModelBinder
|
|
|
|
{
|
2012-02-07 05:08:07 +00:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-02-05 06:34:36 +00:00
|
|
|
|
|
|
|
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
|
|
|
|
{
|
2012-02-16 06:16:57 +00:00
|
|
|
var input = "[NULL]";
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
try
|
2012-02-05 06:34:36 +00:00
|
|
|
{
|
2012-02-07 05:08:07 +00:00
|
|
|
var request = controllerContext.HttpContext.Request;
|
2012-02-05 06:34:36 +00:00
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
if (!IsJsonRequest(request))
|
2012-02-05 06:34:36 +00:00
|
|
|
{
|
2012-02-07 05:08:07 +00:00
|
|
|
return base.BindModel(controllerContext, bindingContext);
|
|
|
|
}
|
|
|
|
|
2012-02-16 06:55:43 +00:00
|
|
|
using (var stream = request.InputStream)
|
2012-02-07 05:08:07 +00:00
|
|
|
{
|
2012-02-16 06:55:43 +00:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
input = reader.ReadToEnd();
|
|
|
|
}
|
2012-02-05 06:34:36 +00:00
|
|
|
}
|
|
|
|
|
2012-02-16 06:16:57 +00:00
|
|
|
var deserializedObject = JsonConvert.DeserializeObject(input, bindingContext.ModelMetadata.ModelType);
|
|
|
|
|
2012-02-07 05:08:07 +00:00
|
|
|
return deserializedObject;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2012-02-16 06:55:43 +00:00
|
|
|
logger.FatalException("Error deserilizing request. " + input, e);
|
2012-02-07 05:08:07 +00:00
|
|
|
throw;
|
|
|
|
}
|
2012-02-05 06:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsJsonRequest(HttpRequestBase request)
|
|
|
|
{
|
|
|
|
return request.ContentType.ToLower().Contains("application/json");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|