Example #1
0
            public Task Submit(HttpContext http)
            {
                string       accept = "application/json";
                StringValues headers;

                if (http.Request.Headers.TryGetValue("accept", out headers))
                {
                    accept = headers[0];
                }
                T command;

                try
                {
                    command = (T)serialization.Deserialize(http.Request.Body, typeof(T), http.Request.ContentType, streamingContext);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.ToString());
                    return(ReturnError(http.Response, $"Error deserializing command: {ex.Message}", 400));
                }
                try
                {
                    Dictionary <string, List <string> > errors;
                    using (var context = objectFactory.DoWork())
                    {
                        context.Submit(command);
                        errors = command.GetValidationErrors();
                        if (errors.Count == 0)
                        {
                            context.Commit();
                        }
                    }
                    http.Response.ContentType = accept;
                    if (errors.Count == 0)
                    {
                        http.Response.StatusCode = 200;
                        serialization.Serialize(command, accept, http.Response.Body);
                    }
                    else
                    {
                        http.Response.StatusCode = 400;
                        serialization.Serialize(errors, accept, http.Response.Body);
                    }
                    //ideally Revenj could provide async DB API :(
                    return(Task.CompletedTask);
                }
                catch (CustomException ex)
                {
                    logger.LogError(ex.ToString());
                    return(ReturnError(http.Response, ex.Message, 400));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.ToString());
                    return(ReturnError(http.Response, "Unknown error", 500));
                }
            }
Example #2
0
        /// <summary>
        /// Deserialize typesafe object without providing context information.
        /// .NET objects or value objects don't require context so they can be deserialized
        /// without IServiceLocator in context.
        /// </summary>
        /// <typeparam name="T">object type</typeparam>
        /// <param name="serialization">serialization service</param>
        /// <param name="source">serialized object in specified format</param>
        /// <param name="contentType">MIME type specifying the format</param>
        /// <returns>deserialized object</returns>
        public static T Deserialize <T>(
            this IWireSerialization serialization,
            Stream source,
            string contentType)
        {
            Contract.Requires(serialization != null);

            return((T)serialization.Deserialize(source, typeof(T), contentType, default(StreamingContext)));
        }
Example #3
0
 public static object Deserialize(
     IWireSerialization serialization,
     Stream input,
     Type target,
     string contentType,
     StreamingContext context)
 {
     return(serialization.Deserialize(input, target, contentType, context));
 }
Example #4
0
        public static Try <object> ParseObject(
            IWireSerialization serializer,
            Try <Type> maybeType,
            Stream data,
            bool canCreate,
            IServiceProvider locator,
            HttpContext context)
        {
            if (maybeType.IsFailure)
            {
                return(Try <object> .Error);
            }
            var type = maybeType.Result;

            if (type == null)
            {
                return(Try <object> .Empty);
            }
            if (data == null)
            {
                if (canCreate == false)
                {
                    return(Try.Fail <object>(@"{0} must be provided. Example: 
{1}".With(type.FullName, serializer.EmptyInstanceString(type, context)), context.Response));
                }
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception ex)
                {
                    return(Try.Fail <object>(@"Can't create instance of {0}. Data must be provided. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
                }
            }
            try
            {
                //TODO: deserialize async
                var sc = new StreamingContext(StreamingContextStates.All, locator);
                //TODO: objects deserialized here will have global scope access. Do OnDeserialized again later in scope
                return(serializer.Deserialize(data, type, context.Request.ContentType, sc));
            }
            catch (TargetInvocationException tie)
            {
                var ex = tie.InnerException ?? tie;
                return(Try.Fail <object>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
            }
            catch (Exception ex)
            {
                return(Try.Fail <object>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
            }
        }
Example #5
0
        public Stream FindFrom(string domainObject, string order, Stream body)
        {
            var type = DomainModel.Find(domainObject);

            if (type != null && typeof(IAggregateRoot).IsAssignableFrom(type))
            {
                var uris = Serialization.Deserialize <string[]>(body, ThreadContext.Request.ContentType);
                return(CachingService.ReadFromCache(type, uris, order == "match", Locator));
            }
            return(DomainCommands.FindFrom(domainObject, order, body));
        }
Example #6
0
        public Stream GetHistoryFrom(string root, Stream body)
        {
            Utility.CheckAggregateRoot(DomainModel, root);
            var uris = Serialization.Deserialize <string[]>(body, ThreadContext.Request.ContentType);

            return
                (Converter.PassThrough <GetRootHistory, GetRootHistory.Argument>(
                     new GetRootHistory.Argument
            {
                Name = root,
                Uri = uris
            }));
        }
Example #7
0
        public Stream FindFrom(string domainObject, Stream body)
        {
            Utility.CheckIdentifiable(DomainModel, domainObject);
            var uris = Serialization.Deserialize <string[]>(body, ThreadContext.Request.ContentType);

            return
                (Converter.PassThrough <GetDomainObject, GetDomainObject.Argument>(
                     new GetDomainObject.Argument
            {
                Name = domainObject,
                Uri = uris
            }));
        }
Example #8
0
 public static object ParseExpressionSpecification(IWireSerialization serializer, Type domainType, Stream data)
 {
     try
     {
         var expressionNode = serializer.Deserialize <LambdaExpressionNode>(data, ThreadContext.Request.ContentType);
         return(Activator.CreateInstance(typeof(SpecificationFromNode <>).MakeGenericType(domainType), expressionNode));
     }
     catch (Exception ex)
     {
         if (ex.InnerException != null)
         {
             ex = ex.InnerException;
         }
         Utility.ThrowError(@"Error deserializing expression. " + ex.Message, HttpStatusCode.BadRequest);
     }
     return(null);
 }
Example #9
0
        public static object ParseObject(
            IWireSerialization serializer,
            Type type,
            Stream data,
            bool canCreate,
            IServiceLocator locator)
        {
            if (data == null)
            {
                if (canCreate == false)
                {
                    ThrowError(@"{0} must be provided. Example: 
{1}".With(type.FullName, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
                }
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception ex)
                {
                    ThrowError(@"Can't create instance of {0}. Data must be provided. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
                }
            }
            try
            {
                var sc = new StreamingContext(StreamingContextStates.All, locator);
                //TODO: objects deserialized here will have global scope access. Do OnDeserialized again later in scope
                return(serializer.Deserialize(data, type, ThreadContext.Request.ContentType, sc));
            }
            catch (TargetInvocationException tie)
            {
                var ex = tie.InnerException ?? tie;
                throw new WebFaultException <string>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                throw new WebFaultException <string>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
            }
        }
Example #10
0
 public static Either <object> ParseExpressionSpecification(IWireSerialization serializer, Either <Type> domainType, Stream data)
 {
     if (domainType.IsFailure)
     {
         return(domainType.Error);
     }
     try
     {
         var expressionNode = serializer.Deserialize <LambdaExpressionNode>(data, ThreadContext.Request.ContentType);
         return(Activator.CreateInstance(typeof(SpecificationFromNode <>).MakeGenericType(domainType.Result), expressionNode));
     }
     catch (Exception ex)
     {
         if (ex.InnerException != null)
         {
             ex = ex.InnerException;
         }
         return(@"Error deserializing expression. " + ex.Message);
     }
 }
Example #11
0
 public static object Deserialize(
     IWireSerialization serialization,
     Stream input,
     Type target,
     string contentType,
     StreamingContext context)
 {
     return serialization.Deserialize(input, target, contentType, context);
 }