From 0d1cf872d3f77a502d50f2d0d2ddfe7f5be9ed92 Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 4 Dec 2013 17:05:05 +1100 Subject: [PATCH] Fixes WebApi issue with creating HttpResponseMessage manually - this shouldn't be done and we'll end up with very strange YSODs --- src/Umbraco.Web/Editors/ContentController.cs | 18 ++--- src/Umbraco.Web/Editors/MacroController.cs | 30 ++++----- src/Umbraco.Web/Editors/MediaController.cs | 28 ++++---- .../WebApi/Binders/ContentItemBaseBinder.cs | 16 ++--- .../WebServices/DomainsApiController.cs | 22 +++--- .../ExamineManagementApiController.cs | 67 +++++++++---------- 6 files changed, 83 insertions(+), 98 deletions(-) diff --git a/src/Umbraco.Web/Editors/ContentController.cs b/src/Umbraco.Web/Editors/ContentController.cs index c97cba4bc8..b18329d567 100644 --- a/src/Umbraco.Web/Editors/ContentController.cs +++ b/src/Umbraco.Web/Editors/ContentController.cs @@ -124,10 +124,8 @@ namespace Umbraco.Web.Editors public HttpResponseMessage GetNiceUrl(int id) { var url = Umbraco.NiceUrl(id); - var response = new HttpResponseMessage(HttpStatusCode.OK) - { - Content = new StringContent(url, Encoding.UTF8, "application/json") - }; + var response = Request.CreateResponse(HttpStatusCode.OK); + response.Content = new StringContent(url, Encoding.UTF8, "application/json"); return response; } @@ -481,10 +479,8 @@ namespace Umbraco.Web.Editors Services.ContentService.Move(toMove, move.ParentId); - var response = new HttpResponseMessage(HttpStatusCode.OK) - { - Content = new StringContent(toMove.Path, Encoding.UTF8, "application/json") - }; + var response = Request.CreateResponse(HttpStatusCode.OK); + response.Content = new StringContent(toMove.Path, Encoding.UTF8, "application/json"); return response; } @@ -500,10 +496,8 @@ namespace Umbraco.Web.Editors var c = Services.ContentService.Copy(toCopy, copy.ParentId, copy.RelateToOriginal); - var response = new HttpResponseMessage(HttpStatusCode.OK) - { - Content = new StringContent(c.Path, Encoding.UTF8, "application/json") - }; + var response = Request.CreateResponse(HttpStatusCode.OK); + response.Content = new StringContent(c.Path, Encoding.UTF8, "application/json"); return response; } diff --git a/src/Umbraco.Web/Editors/MacroController.cs b/src/Umbraco.Web/Editors/MacroController.cs index 1d63ae7072..7b989bf674 100644 --- a/src/Umbraco.Web/Editors/MacroController.cs +++ b/src/Umbraco.Web/Editors/MacroController.cs @@ -69,12 +69,12 @@ namespace Umbraco.Web.Editors //if it isn't supposed to be rendered in the editor then return an empty string if (macro.DontRenderInEditor) { - return new HttpResponseMessage() - { - //need to create a specific content result formatted as html since this controller has been configured - //with only json formatters. - Content = new StringContent(string.Empty, Encoding.UTF8, "text/html") - }; + var response = Request.CreateResponse(); + //need to create a specific content result formatted as html since this controller has been configured + //with only json formatters. + response.Content = new StringContent(string.Empty, Encoding.UTF8, "text/html"); + + return response; } //because macro's are filled with insane legacy bits and pieces we need all sorts of wierdness to make them render. @@ -86,16 +86,14 @@ namespace Umbraco.Web.Editors UmbracoContext.HttpContext.Items["pageElements"] = legacyPage.Elements; UmbracoContext.HttpContext.Items[global::Umbraco.Core.Constants.Conventions.Url.AltTemplate] = null; - return new HttpResponseMessage() - { - //need to create a specific content result formatted as html since this controller has been configured - //with only json formatters. - Content = new StringContent( - Umbraco.RenderMacro(macro, macroParams, legacyPage).ToString(), - Encoding.UTF8, - "text/html" - ) - }; + var result = Request.CreateResponse(); + //need to create a specific content result formatted as html since this controller has been configured + //with only json formatters. + result.Content = new StringContent( + Umbraco.RenderMacro(macro, macroParams, legacyPage).ToString(), + Encoding.UTF8, + "text/html"); + return result; } } diff --git a/src/Umbraco.Web/Editors/MediaController.cs b/src/Umbraco.Web/Editors/MediaController.cs index 75071b4351..85ea353db4 100644 --- a/src/Umbraco.Web/Editors/MediaController.cs +++ b/src/Umbraco.Web/Editors/MediaController.cs @@ -78,12 +78,12 @@ namespace Umbraco.Web.Editors var media = Services.MediaService.GetById(mediaId); if (media == null) { - return new HttpResponseMessage(HttpStatusCode.NotFound); + return Request.CreateResponse(HttpStatusCode.NotFound); } var imageProp = media.Properties[Constants.Conventions.Media.File]; if (imageProp == null) { - return new HttpResponseMessage(HttpStatusCode.NotFound); + return Request.CreateResponse(HttpStatusCode.NotFound); } var imagePath = imageProp.Value.ToString(); @@ -109,7 +109,7 @@ namespace Umbraco.Web.Editors var origFilePath = IOHelper.MapPath(imagePath); if (System.IO.File.Exists(origFilePath) == false) { - return new HttpResponseMessage(HttpStatusCode.NotFound); + return Request.CreateResponse(HttpStatusCode.NotFound); } var mediaFileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); using (var fileStream = new FileStream(origFilePath, FileMode.Open, FileAccess.Read)) @@ -127,7 +127,7 @@ namespace Umbraco.Web.Editors } } - var result = new HttpResponseMessage(HttpStatusCode.OK); + var result = Request.CreateResponse(HttpStatusCode.OK); //NOTE: That we are not closing this stream as the framework will do that for us, if we try it will // fail. See http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api var stream = new FileStream(thumbFilePath, FileMode.Open, FileAccess.Read); @@ -297,10 +297,8 @@ namespace Umbraco.Web.Editors Services.MediaService.Move(toMove, move.ParentId); - var response = new HttpResponseMessage(HttpStatusCode.OK) - { - Content = new StringContent(toMove.Path, Encoding.UTF8, "application/json") - }; + var response = Request.CreateResponse(HttpStatusCode.OK); + response.Content = new StringContent(toMove.Path, Encoding.UTF8, "application/json"); return response; } @@ -465,18 +463,16 @@ namespace Umbraco.Web.Editors //must have a file if (result.FileData.Count == 0) { - return new HttpResponseMessage(HttpStatusCode.NotFound); + return Request.CreateResponse(HttpStatusCode.NotFound); } //get the string json from the request int parentId; if (int.TryParse(result.FormData["currentFolder"], out parentId) == false) { - throw new HttpResponseException( - new HttpResponseMessage(HttpStatusCode.BadRequest) - { - ReasonPhrase = "The request was not formatted correctly, the currentFolder is not an integer" - }); + var response = Request.CreateResponse(HttpStatusCode.BadRequest); + response.ReasonPhrase = "The request was not formatted correctly, the currentFolder is not an integer"; + throw new HttpResponseException(response); } //ensure the user has access to this folder by parent id! @@ -485,7 +481,7 @@ namespace Umbraco.Web.Editors Security.CurrentUser, Services.MediaService, parentId) == false) { - return new HttpResponseMessage(HttpStatusCode.Unauthorized); + return Request.CreateResponse(HttpStatusCode.Unauthorized); } //get the files @@ -515,7 +511,7 @@ namespace Umbraco.Web.Editors System.IO.File.Delete(file.LocalFileName); } - return new HttpResponseMessage(HttpStatusCode.OK); + return Request.CreateResponse(HttpStatusCode.OK); } diff --git a/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs b/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs index a25ce7d18c..2c47a1a67b 100644 --- a/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs +++ b/src/Umbraco.Web/WebApi/Binders/ContentItemBaseBinder.cs @@ -111,11 +111,9 @@ namespace Umbraco.Web.WebApi.Binders if (result.FormData["contentItem"] == null) { - throw new HttpResponseException( - new HttpResponseMessage(HttpStatusCode.BadRequest) - { - ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter" - }); + var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest); + response.ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter"; + throw new HttpResponseException(response); } //get the string json from the request @@ -138,11 +136,9 @@ namespace Umbraco.Web.WebApi.Binders var parts = file.Headers.ContentDisposition.Name.Trim(new char[] { '\"' }).Split('_'); if (parts.Length != 2) { - throw new HttpResponseException( - new HttpResponseMessage(HttpStatusCode.BadRequest) - { - ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited" - }); + var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest); + response.ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited"; + throw new HttpResponseException(response); } var propAlias = parts[1]; diff --git a/src/Umbraco.Web/WebServices/DomainsApiController.cs b/src/Umbraco.Web/WebServices/DomainsApiController.cs index 43bf9bf092..062a9dfa62 100644 --- a/src/Umbraco.Web/WebServices/DomainsApiController.cs +++ b/src/Umbraco.Web/WebServices/DomainsApiController.cs @@ -25,18 +25,20 @@ namespace Umbraco.Web.WebServices var node = ApplicationContext.Current.Services.ContentService.GetById(model.NodeId); if (node == null) - throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) - { - Content = new StringContent(string.Format("There is no content node with id {0}.", model.NodeId)), - ReasonPhrase = "Node Not Found." - }); + { + var response = Request.CreateResponse(HttpStatusCode.BadRequest); + response.Content = new StringContent(string.Format("There is no content node with id {0}.", model.NodeId)); + response.ReasonPhrase = "Node Not Found."; + throw new HttpResponseException(response); + } if (UmbracoUser.GetPermissions(node.Path).Contains(ActionAssignDomain.Instance.Letter) == false) - throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized) - { - Content = new StringContent("You do not have permission to assign domains on that node."), - ReasonPhrase = "Permission Denied." - }); + { + var response = Request.CreateResponse(HttpStatusCode.BadRequest); + response.Content = new StringContent("You do not have permission to assign domains on that node."); + response.ReasonPhrase = "Permission Denied."; + throw new HttpResponseException(response); + } model.Valid = true; var domains = Routing.DomainHelper.GetNodeDomains(model.NodeId, true); diff --git a/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs b/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs index 0bbf96f77d..2106cef51e 100644 --- a/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs +++ b/src/Umbraco.Web/WebServices/ExamineManagementApiController.cs @@ -55,7 +55,10 @@ namespace Umbraco.Web.WebServices public ISearchResults GetSearchResults(string searcherName, string query, string queryType) { if (queryType == null) - throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); + { + throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); + } + if (query.IsNullOrWhiteSpace()) return SearchResults.Empty(); @@ -72,7 +75,7 @@ namespace Umbraco.Web.WebServices { return searcher.Search(searcher.CreateSearchCriteria().RawQuery(query)); } - throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); + throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } throw new HttpResponseException(msg); } @@ -90,13 +93,12 @@ namespace Umbraco.Web.WebServices { indexer.OptimizeIndex(); } - catch (System.Exception ex) + catch (Exception ex) { - return new HttpResponseMessage(HttpStatusCode.Conflict) - { - Content = new StringContent(string.Format("The index could not be optimized, most likely there is another thread currently writing to the index. Error: {0}", ex)), - ReasonPhrase = "Could Not Optimize" - }; + var response = Request.CreateResponse(HttpStatusCode.Conflict); + response.Content = new StringContent(string.Format("The index could not be optimized, most likely there is another thread currently writing to the index. Error: {0}", ex)); + response.ReasonPhrase = "Could Not Optimize"; + return response; } } return msg; @@ -117,13 +119,12 @@ namespace Umbraco.Web.WebServices { indexer.RebuildIndex(); } - catch (System.Exception ex) + catch (Exception ex) { - return new HttpResponseMessage(HttpStatusCode.Conflict) - { - Content = new StringContent(string.Format("The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {0}", ex)), - ReasonPhrase = "Could Not Rebuild" - }; + var response = Request.CreateResponse(HttpStatusCode.Conflict); + response.Content = new StringContent(string.Format("The index could not be rebuilt at this time, most likely there is another thread currently writing to the index. Error: {0}", ex)); + response.ReasonPhrase = "Could Not Rebuild"; + return response; } } return msg; @@ -206,22 +207,21 @@ namespace Umbraco.Web.WebServices searcher = ExamineManager.Instance.SearchProviderCollection[searcherName] as LuceneSearcher; if (searcher == null) { - return new HttpResponseMessage(HttpStatusCode.BadRequest) - { - Content = new StringContent(string.Format("The searcher {0} is not of type {1}", searcherName, typeof(LuceneSearcher))), - ReasonPhrase = "Wrong Searcher Type" - }; + var response = Request.CreateResponse(HttpStatusCode.BadRequest); + response.Content = new StringContent(string.Format("The searcher {0} is not of type {1}", searcherName, typeof(LuceneSearcher))); + response.ReasonPhrase = "Wrong Searcher Type"; + return response; } //return Ok! return Request.CreateResponse(HttpStatusCode.OK); } searcher = null; - return new HttpResponseMessage(HttpStatusCode.BadRequest) - { - Content = new StringContent(string.Format("No searcher found with name = {0}", searcherName)), - ReasonPhrase = "Searcher Not Found" - }; + + var response1 = Request.CreateResponse(HttpStatusCode.BadRequest); + response1.Content = new StringContent(string.Format("No searcher found with name = {0}", searcherName)); + response1.ReasonPhrase = "Searcher Not Found"; + return response1; } private HttpResponseMessage ValidateLuceneIndexer(string indexerName, out LuceneIndexer indexer) @@ -231,22 +231,21 @@ namespace Umbraco.Web.WebServices indexer = ExamineManager.Instance.IndexProviderCollection[indexerName] as LuceneIndexer; if (indexer == null) { - return new HttpResponseMessage(HttpStatusCode.BadRequest) - { - Content = new StringContent(string.Format("The indexer {0} is not of type {1}", indexerName, typeof(LuceneIndexer))), - ReasonPhrase = "Wrong Indexer Type" - }; + var response1 = Request.CreateResponse(HttpStatusCode.BadRequest); + response1.Content = new StringContent(string.Format("The indexer {0} is not of type {1}", indexerName, typeof(LuceneIndexer))); + response1.ReasonPhrase = "Wrong Indexer Type"; + return response1; } //return Ok! return Request.CreateResponse(HttpStatusCode.OK); } indexer = null; - return new HttpResponseMessage(HttpStatusCode.BadRequest) - { - Content = new StringContent(string.Format("No indexer found with name = {0}", indexerName)), - ReasonPhrase = "Indexer Not Found" - }; + + var response = Request.CreateResponse(HttpStatusCode.BadRequest); + response.Content = new StringContent(string.Format("No indexer found with name = {0}", indexerName)); + response.ReasonPhrase = "Indexer Not Found"; + return response; } } }