コード例 #1
0
        /// <summary>
        /// Process the response
        /// </summary>
        /// <param name="requestedMediaRange">Content type requested by the client</param>
        /// <param name="model">The model for the given media range</param>
        /// <param name="context">The nancy context</param>
        /// <returns>A response</returns>
        public virtual Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            var pomonaResponse = (PomonaResponse)model;
            string jsonString;

            if (pomonaResponse.Entity == PomonaResponse.NoBodyEntity)
                return new Response { StatusCode = pomonaResponse.StatusCode };

            jsonString =
                GetSerializerFactory(context,
                    context.GetSerializationContextProvider())
                    .GetSerializer().SerializeToString(pomonaResponse.Entity,
                        new SerializeOptions()
                        {
                            ExpandedPaths = pomonaResponse.ExpandedPaths,
                            ExpectedBaseType = pomonaResponse.ResultType
                        });

            if (IsTextHtmlContentType(requestedMediaRange))
            {
                // Wrap in html
                var response = new Response();
                HtmlJsonPrettifier.CreatePrettifiedHtmlJsonResponse(response,
                    string.Empty,
                    jsonString,
                    "http://failfailtodo");
                return response;
            }
            else
            {
                var bytes = Encoding.UTF8.GetBytes(jsonString);
                var response = new Response
                {
                    //Headers = {{"Content-Length", bytes.Length.ToString()}},
                    Contents = s => s.Write(bytes, 0, bytes.Length),
                    ContentType = ContentType,
                    StatusCode = pomonaResponse.StatusCode
                };

                if (pomonaResponse.ResponseHeaders != null)
                {
                    foreach (var kvp in pomonaResponse.ResponseHeaders)
                        response.Headers.Add(kvp);
                }

                // Add etag header
                var transformedResultType = pomonaResponse.ResultType as TransformedType;
                if (transformedResultType != null)
                {
                    var etagProperty = transformedResultType.ETagProperty;
                    if (pomonaResponse.Entity != null && etagProperty != null)
                    {
                        var etagValue = (string)etagProperty.Getter(pomonaResponse.Entity);
                        if (etagValue != null)
                        {
                            // I think defining this as a weak etag will be correct, since we can specify $expand which change data (byte-by-byte).
                            response.Headers["ETag"] = string.Format("W/\"{0}\"", etagValue);
                        }
                    }
                }

                return response;
            }
        }