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