private static MapRepresentation CreateMap(ContextSurface context, object obj)
        {
            var opts = new List <OptionalProperty> {
                new OptionalProperty(JsonPropertyNames.Value, obj)
            };

            if (!string.IsNullOrEmpty(context.Reason))
            {
                opts.Add(new OptionalProperty(JsonPropertyNames.InvalidReason, context.Reason));
            }
            return(Create(opts.ToArray()));
        }
示例#2
0
        private static void CheckForRedirection(ContextSurface context, HttpRequestMessage req)
        {
            var ocs  = context as ObjectContextSurface;
            var arcs = context as ActionResultContextSurface;
            Tuple <string, string> redirected = (ocs != null ? ocs.Redirected : null) ?? (arcs != null && arcs.Result != null ? arcs.Result.Redirected : null);

            if (redirected != null)
            {
                Uri redirectAddress = new UriMtHelper(req).GetRedirectUri(req, redirected.Item1, redirected.Item2);
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MovedPermanently)
                {
                    Headers = { Location = redirectAddress }
                });
            }
        }
        private static MapRepresentation GetMap(HttpRequestMessage req, ContextSurface context, RestControlFlags flags)
        {
            MapRepresentation value;

            // All reasons why we cannot create a linkrep
            if (context.Specification.IsParseable() ||
                context.Specification.IsCollection() ||
                context.ProposedValue == null ||
                context.ProposedNakedObject == null ||
                context.ProposedNakedObject.Specification.IsParseable())
            {
                value = CreateMap(context, context.ProposedValue);
            }
            else
            {
                value = CreateMap(context, RefValueRepresentation.Create(new ObjectRelType(RelValues.Self, new UriMtHelper(req, context.ProposedNakedObject)), flags));
            }
            return(value);
        }
        public static MapRepresentation Create(HttpRequestMessage req, ContextSurface context, Format format, RestControlFlags flags, MediaTypeHeaderValue mt)
        {
            var objectContextSurface       = context as ObjectContextSurface;
            var actionResultContextSurface = context as ActionResultContextSurface;
            MapRepresentation mapRepresentation;


            if (objectContextSurface != null)
            {
                List <OptionalProperty> optionalProperties = objectContextSurface.VisibleProperties.Where(p => p.Reason != null || p.ProposedValue != null).Select(c => new OptionalProperty(c.Id, GetMap(req, c, flags))).ToList();
                if (!string.IsNullOrEmpty(objectContextSurface.Reason))
                {
                    optionalProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, objectContextSurface.Reason));
                }
                mapRepresentation = Create(optionalProperties.ToArray());
            }
            else if (actionResultContextSurface != null)
            {
                List <OptionalProperty> optionalProperties = actionResultContextSurface.ActionContext.VisibleParameters.Select(c => new OptionalProperty(c.Id, GetMap(req, c, flags))).ToList();

                if (!string.IsNullOrEmpty(actionResultContextSurface.Reason))
                {
                    optionalProperties.Add(new OptionalProperty(JsonPropertyNames.XRoInvalidReason, actionResultContextSurface.Reason));
                }
                mapRepresentation = Create(optionalProperties.ToArray());
            }
            else
            {
                mapRepresentation = GetMap(req, context, flags);
            }


            mapRepresentation.SetContentType(mt);


            return(mapRepresentation);
        }
示例#5
0
 private RestSnapshot(ContextSurface context, HttpRequestMessage req, bool validateAsJson)
     : this(req, validateAsJson)
 {
     logger.DebugFormat("RestSnapshot:{0}", context.GetType().FullName);
     CheckForRedirection(context, req);
 }