public HttpResponseMessage Enrich(HttpResponseMessage response)
        {
            PostModel post;
            UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue<PostModel>(out post))
            {
                Enrich(post, urlHelper);
                return response;
            }

            PostFeed feed;
            if (response.TryGetContentValue<PostFeed>(out feed))
            {
                foreach (var p in feed.Posts)
                {
                    Enrich(p, urlHelper);
                }

                string selfUrl = urlHelper.Link("DefaultApi", new { controller = "posts" });
                feed.AddLink(new SelfLink(selfUrl));
            }

            return response;
        }
        private static HttpResponseMessage BuildApiResponse(HttpRequestMessage request, HttpResponseMessage response)
        {
            object content;
            string errorMessage = null;

            if (response.TryGetContentValue(out content) && !response.IsSuccessStatusCode)
            {
                HttpError error = content as HttpError;

                if (error != null)
                {
                    content = null;
                    errorMessage = error.Message;

#if DEBUG
                    errorMessage = string.Concat(errorMessage, error.ExceptionMessage, error.StackTrace);
#endif
                }
            }

            var newResponse = request.CreateResponse(response.StatusCode, new ApiResponse(response.StatusCode, content, errorMessage));

            foreach (var header in response.Headers)
            {
                newResponse.Headers.Add(header.Key, header.Value);
            }

            return newResponse;
        }
        private HttpResponseMessage GetResponseMessage(HttpRequestMessage request, HttpResponseMessage response)
        {
            object content;

            if (response.TryGetContentValue(out content))
            {
                var error = content as HttpError;

                if (content is string && !response.IsSuccessStatusCode)
                {
                    content = new {error = content};
                }
                else if (error != null)
                {
                    if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        error.Message = "Internal server error.";
                    }
                    content = GetErrorResult(error);
                }
                else
                {
                    content = new {result = content};
                }
            }

            if (content != null)
                response.Content = new ObjectContent(content.GetType(), content, JsonFormatter);
            return response;
        }
        public HttpResponseMessage Enrich(HttpResponseMessage response) {

            Car car;
            UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();
            if (response.TryGetContentValue<Car>(out car)) {
                Enrich(car, urlHelper);
            }

            IEnumerable<Car> cars;
            if (response.TryGetContentValue<IEnumerable<Car>>(out cars)) {
                foreach (var carItem in cars) {
                    Enrich(carItem, urlHelper);
                }
            }

            return response;
        }
        public HttpResponseMessage Enrich(HttpResponseMessage response)
        {
            TodoRepresentation todo;

            var urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue(out todo))
            {
                Enrich(todo, urlHelper);
                return response;
            }

            List<TodoRepresentation> representations;
            if (response.TryGetContentValue(out representations))
            {
                representations.ToList().ForEach(p => Enrich(p, urlHelper));
            }

            return response;
        }
        public HttpResponseMessage Enrich(HttpResponseMessage response) {

            MediaModel media;

            var urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue<MediaModel>(out media)) {
                Enrich(media, urlHelper);
            }

            return response;
        }
        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            if (_configuration.SetNoCacheForAllResponses)
            {
                //Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache = true,
                    NoStore = true,
                    MaxAge = TimeSpan.Zero,
                    MustRevalidate = true
                };
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? _configuration.DefaultWrapResultAttribute;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            if (IsIgnoredUrl(request.RequestUri))
            {
                return;
            }

            object resultObject;
            if (!response.TryGetContentValue(out resultObject) || resultObject == null)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new ObjectContent<AjaxResponse>(
                    new AjaxResponse(),
                    _configuration.HttpConfiguration.Formatters.JsonFormatter
                    );
                return;
            }

            if (resultObject is AjaxResponseBase)
            {
                return;
            }

            response.Content = new ObjectContent<AjaxResponse>(
                new AjaxResponse(resultObject),
                _configuration.HttpConfiguration.Formatters.JsonFormatter
                );
        }
Example #8
0
        /// <summary>
        /// SDataContent transforms OData/Web API Content/Response to SData
        /// </summary>
        /// <param name="response">The response containing the Content/Payload to transform</param>
        public SDataContent(HttpResponseMessage response)
        {
            origResponse = response;
            originalContent = origResponse.Content;

            if (originalContent == null)
            {
                throw new ArgumentNullException("response");
            }

            foreach (var header in originalContent.Headers)
            {
                Headers.Add(header.Key, header.Value);
            }

            origResponse.TryGetContentValue(out responseObject);
        }
        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? DontWrapResultAttribute.Default;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            object resultObject;
            if (!response.TryGetContentValue(out resultObject))
            {
                return;
            }

            if (resultObject == null)
            {
                response.Content = new ObjectContent<AjaxResponse>(
                    new AjaxResponse(),
                    _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                    );
            }

            if (resultObject is AjaxResponse)
            {
                return;
            }

            response.Content = new ObjectContent<AjaxResponse>(
                new AjaxResponse(resultObject),
                _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                );
        }