public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { object result = base.Read(messageReader, type, readContext); if (result is ODataActionParameters) { ODataActionParameters parameters = result as ODataActionParameters; foreach (var parameter in parameters.Select(p => new { p.Key, p.Value }).ToList()) { if (parameter.Value != null) { if (parameter.Value is DateTimeOffset) { IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); DateTimeOffset date = timeZoneManager.MapFromClientToServer(((DateTimeOffset)parameter.Value)); parameters[parameter.Key] = date; } else if (parameter.Value is IEnumerable <DateTimeOffset> ) { IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); IEnumerable <DateTimeOffset> dates = (IEnumerable <DateTimeOffset>)parameter.Value; parameters[parameter.Key] = FixIEnumerableOfDateTimes(dates, timeZoneManager); } else if (parameter.Value is string) { string initialString = parameter.Value.ToString(); foreach (IStringCorrector stringCorrector in _stringCorrectors) { initialString = stringCorrector.CorrectString(initialString); } parameters[parameter.Key] = initialString; } else if (parameter.Value is IEnumerable <string> ) { parameters[parameter.Key] = FixIEnumerableOfStrings((IEnumerable <string>)parameter.Value); } } } } else { throw new NotSupportedException(); } return(result); }
public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext == null) { throw new ArgumentNullException(nameof(actionContext)); } Uri url = new Uri(actionContext.Request.RequestUri.ToString().AsUnescaped()); if (!string.IsNullOrEmpty(url.Query)) { IDependencyResolver dependencyResolver = actionContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); string urlAsTextToFix = url.ToString(); const string isoDateRegExp = "(\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d+([+-][0-2]\\d:[0-5]\\d|Z))|(\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d([+-][0-2]\\d:[0-5]\\d|Z))|(\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d([+-][0-2]\\d:[0-5]\\d|Z))"; urlAsTextToFix = Regex.Replace(urlAsTextToFix, "\'date\"(.*?)\"\'", (match) => { string?result = null; Regex.Replace(match.Value, isoDateRegExp, (innerMatch) => { DateTimeOffset baseDate = DateTimeOffset.Parse(innerMatch.Value, CultureInfo.InvariantCulture); DateTimeOffset date = baseDate; date = timeZoneManager.MapFromClientToServer(date); result = date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); return(result); }, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); return(result); }, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); urlAsTextToFix = Regex.Replace(urlAsTextToFix, isoDateRegExp, (match) => { DateTimeOffset date = DateTimeOffset.Parse(match.Value, CultureInfo.InvariantCulture); date = timeZoneManager.MapFromClientToServer(date); return(date.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.00Z", CultureInfo.InvariantCulture)); }, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); actionContext.Request.RequestUri = new Uri(urlAsTextToFix, UriKind.Absolute); } base.OnActionExecuting(actionContext); }
public override ODataPrimitiveValue CreateODataPrimitiveValue(object graph, IEdmPrimitiveTypeReference primitiveType, ODataSerializerContext writeContext) { ODataPrimitiveValue result = base.CreateODataPrimitiveValue(graph, primitiveType, writeContext); if (result?.Value is DateTimeOffset date) { IDependencyResolver dependencyResolver = writeContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); result = new ODataPrimitiveValue(timeZoneManager.MapFromServerToClient(date)); } return(result); }
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { HttpActionDescriptor actionDescriptor = readContext.Request.GetActionDescriptor(); if (actionDescriptor != null && !actionDescriptor.GetCustomAttributes <ActionAttribute>().Any()) { throw new InvalidOperationException($"{nameof(DefaultODataActionParameterDeserializer)} is designed for odata actions only"); } TypeInfo typeInfo = type.GetTypeInfo(); IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); using (StreamReader requestStreamReader = new StreamReader(readContext.Request.Content.ReadAsStreamAsync().GetAwaiter().GetResult())) { using (JsonTextReader requestJsonReader = new JsonTextReader(requestStreamReader)) { JsonSerializer deserilizer = JsonSerializer.Create(new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, Converters = new JsonConverter[] { _odataJsonDeserializerEnumConverter, _stringFormatterConvert, new ODataJsonDeSerializerDateTimeOffsetTimeZone(timeZoneManager) } }); object result = deserilizer.Deserialize(requestJsonReader, typeInfo); return(result); } } }
public override void ApplyStructuralProperty(object resource, ODataProperty structuralProperty, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext) { if (structuralProperty?.Value != null) { if (structuralProperty.Value is DateTimeOffset) { IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); structuralProperty.Value = timeZoneManager.MapFromClientToServer(((DateTimeOffset)structuralProperty.Value)); } else if (structuralProperty.Value is string) { string rawString = structuralProperty.Value.ToString(); foreach (IStringCorrector stringCorrector in _stringCorrectors) { rawString = stringCorrector.CorrectString(rawString); } structuralProperty.Value = rawString; } } try { base.ApplyStructuralProperty(resource, structuralProperty, structuredType, readContext); } catch (ODataException ex) { if (ex.Message != "Does not support untyped value in non-open type.") { throw; } } }
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { HttpActionDescriptor actionDescriptor = readContext.Request.GetActionDescriptor(); if (actionDescriptor != null && !actionDescriptor.GetCustomAttributes <ActionAttribute>().Any() && !actionDescriptor.GetCustomAttributes <CreateAttribute>().Any() && !actionDescriptor.GetCustomAttributes <UpdateAttribute>().Any()) { throw new InvalidOperationException($"{nameof(DefaultODataActionCreateUpdateParameterDeserializer)} is designed for odata actions|creates|updates only"); } TypeInfo typeInfo = type.GetTypeInfo(); IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); using (StreamReader requestStreamReader = new StreamReader(readContext.Request.Content.ReadAsStreamAsync().GetAwaiter().GetResult())) { using (JsonTextReader requestJsonReader = new JsonTextReader(requestStreamReader)) { void Error(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs e) { if (e.ErrorContext.Error is JsonSerializationException && e.ErrorContext.Error.Message.StartsWith("Could not find member ")) { if (e.CurrentObject is IOpenType openDto) { openDto.Properties = openDto.Properties ?? new Dictionary <string, object>(); if (requestJsonReader.Read()) { openDto.Properties.Add((string)e.ErrorContext.Member, requestJsonReader.Value); } } e.ErrorContext.Handled = true; } } JsonSerializerSettings settings = DefaultJsonContentFormatter.DeSerializeSettings(); settings.Converters = new JsonConverter[] { _odataJsonDeserializerEnumConverter, _stringFormatterConvert, new ODataJsonDeSerializerDateTimeOffsetTimeZone(timeZoneManager) }; settings.MissingMemberHandling = MissingMemberHandling.Error; JsonSerializer deserilizer = JsonSerializer.Create(settings); deserilizer.Error += Error; try { object result = deserilizer.Deserialize(requestJsonReader, typeInfo); return(result); } finally { deserilizer.Error -= Error; } } } }
public ODataJsonDeSerializerDateTimeOffsetTimeZone(ITimeZoneManager timeZoneManager) { _timeZoneManager = timeZoneManager; }
public override object Read(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { HttpActionDescriptor actionDescriptor = readContext.Request.GetActionDescriptor(); if (actionDescriptor != null && !actionDescriptor.GetCustomAttributes <ActionAttribute>().Any() && !actionDescriptor.GetCustomAttributes <CreateAttribute>().Any() && !actionDescriptor.GetCustomAttributes <UpdateAttribute>().Any() && !actionDescriptor.GetCustomAttributes <PartialUpdateAttribute>().Any()) { throw new InvalidOperationException($"{nameof(DefaultODataActionCreateUpdateParameterDeserializer)} is designed for odata actions|creates|updates|partialUpdates only"); } TypeInfo typeInfo = type.GetTypeInfo(); IDependencyResolver dependencyResolver = readContext.Request.GetOwinContext() .GetDependencyResolver(); ITimeZoneManager timeZoneManager = dependencyResolver.Resolve <ITimeZoneManager>(); string requestJsonBody = (string)readContext.Request.Properties["ContentStreamAsJson"]; using (StringReader jsonStringReader = new StringReader(requestJsonBody)) using (JsonTextReader requestJsonReader = new JsonTextReader(jsonStringReader)) { void Error(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs e) { if (e.ErrorContext.Error is JsonSerializationException && e.ErrorContext.Error.Message.StartsWith("Could not find member ", StringComparison.InvariantCultureIgnoreCase)) { if (e.CurrentObject is IOpenType openDto) { openDto.Properties = openDto.Properties ?? new Dictionary <string, object>(); if (requestJsonReader.Read()) { openDto.Properties.Add((string)e.ErrorContext.Member, requestJsonReader.Value); } } e.ErrorContext.Handled = true; } } JsonSerializerSettings settings = DefaultJsonContentFormatter.DeSerializeSettings(); settings.Converters = new JsonConverter[] { _odataJsonDeserializerEnumConverter, _stringCorrectorsConverters, new ODataJsonDeSerializerDateTimeOffsetTimeZone(timeZoneManager) }; settings.MissingMemberHandling = MissingMemberHandling.Error; JsonSerializer deserilizer = JsonSerializer.Create(settings); deserilizer.Error += Error; try { object result = null; if (!typeof(Delta).GetTypeInfo().IsAssignableFrom(typeInfo)) { result = deserilizer.Deserialize(requestJsonReader, typeInfo); } else { List <string> changedPropNames = new List <string>(); using (JsonTextReader jsonReaderForGettingSchema = new JsonTextReader(new StringReader(requestJsonBody))) { while (jsonReaderForGettingSchema.Read()) { if (jsonReaderForGettingSchema.Value != null && jsonReaderForGettingSchema.TokenType == JsonToken.PropertyName) { changedPropNames.Add(jsonReaderForGettingSchema.Value.ToString()); } else { } } } TypeInfo dtoType = typeInfo.GetGenericArguments().ExtendedSingle("Finding dto type from delta").GetTypeInfo(); object modifiedDto = deserilizer.Deserialize(requestJsonReader, dtoType); Delta delta = (Delta)Activator.CreateInstance(typeInfo); if (modifiedDto is IOpenType openTypeDto && openTypeDto.Properties?.Any() == true) { delta.TrySetPropertyValue(nameof(IOpenType.Properties), openTypeDto); } foreach (string changedProp in changedPropNames.Where(p => p != nameof(IOpenType.Properties) && dtoType.GetProperty(p) != null)) { delta.TrySetPropertyValue(changedProp, dtoType.GetProperty(changedProp).GetValue(modifiedDto)); } result = delta; } return(result); } finally { deserilizer.Error -= Error; } } }
private IEnumerable <DateTimeOffset> FixIEnumerableOfDateTimes(IEnumerable <DateTimeOffset> dates, ITimeZoneManager timeZoneManager) { foreach (DateTimeOffset date in dates) { yield return(timeZoneManager.MapFromClientToServer(date)); } }
public StatsManager(IStatsRepository repository, ITimeZoneManager timeZoneManager) { _repository = repository; _timeZoneManager = timeZoneManager; }