// <summary> Add a new HTML based web content page or redirect to the system </summary> /// <param name="Response"></param> /// <param name="UrlSegments"></param> /// <param name="Protocol"></param> /// <param name="RequestForm"></param> /// <param name="IsDebug"></param> public void Add_HTML_Based_Content(HttpResponse Response, List<string> UrlSegments, Microservice_Endpoint_Protocol_Enum Protocol, NameValueCollection RequestForm, bool IsDebug) { Custom_Tracer tracer = new Custom_Tracer(); // Add a trace tracer.Add_Trace("WebContentServices.Add_HTML_Based_Content"); //// Validate the web content id exists in the URL //int webcontentId; //if ((UrlSegments.Count == 0) || (!Int32.TryParse(UrlSegments[0], out webcontentId))) //{ // Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Invalid URL. WebContentID missing from URL"), Response, Protocol, null); // Response.StatusCode = 400; // return; //} // Get and validate the required USER (string) posted request object if (String.IsNullOrEmpty(RequestForm["User"])) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Required posted object 'User' is missing"), Response, Protocol, null); Response.StatusCode = 400; return; } string user = RequestForm["User"]; // Get and validate the required CONTENT (HTML_Based_Content) posted request objects if (String.IsNullOrEmpty(RequestForm["Content"])) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Required posted object 'Content' is missing"), Response, Protocol, null); Response.StatusCode = 400; return; } string contentString = RequestForm["Content"]; HTML_Based_Content content = null; try { switch (Protocol) { case Microservice_Endpoint_Protocol_Enum.JSON: case Microservice_Endpoint_Protocol_Enum.JSON_P: content = JSON.Deserialize<HTML_Based_Content>(contentString); break; case Microservice_Endpoint_Protocol_Enum.PROTOBUF: // Deserialize using the Protocol buffer-net library byte[] byteArray = Encoding.ASCII.GetBytes(contentString); MemoryStream mstream = new MemoryStream(byteArray); content = Serializer.Deserialize<HTML_Based_Content>(mstream); break; case Microservice_Endpoint_Protocol_Enum.XML: byte[] byteArray2 = Encoding.UTF8.GetBytes(contentString); MemoryStream mstream2 = new MemoryStream(byteArray2); XmlSerializer x = new XmlSerializer(typeof(Content)); content = (HTML_Based_Content) x.Deserialize(mstream2); break; } } catch (Exception ee) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Unable to deserialize 'Content' parameter to HTML_Based_Content: " + ee.Message), Response, Protocol, null); Response.StatusCode = 400; return; } // If content wasnot successfully deserialized, return error if (content == null) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Unable to deserialize 'Content' parameter to HTML_Based_Content"), Response, Protocol, null); Response.StatusCode = 400; return; } // Level1 must be neither NULL nor empty if (String.IsNullOrEmpty(content.Level1)) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Level1 of the content cannot be null or empty"), Response, Protocol, null); Response.StatusCode = 400; return; } // Since this is a PUT verb, the URL should match the uploaded content bool invalidUrl = ((UrlSegments.Count < 1) || (UrlSegments[0] != content.Level1)); if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level2)) && ((UrlSegments.Count < 2) || (UrlSegments[1] != content.Level2))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level3)) && ((UrlSegments.Count < 3) || (UrlSegments[2] != content.Level3))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level4)) && ((UrlSegments.Count < 4) || (UrlSegments[3] != content.Level4))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level5)) && ((UrlSegments.Count < 5) || (UrlSegments[4] != content.Level5))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level6)) && ((UrlSegments.Count < 6) || (UrlSegments[5] != content.Level6))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level7)) && ((UrlSegments.Count < 7) || (UrlSegments[6] != content.Level7))) invalidUrl = true; if ((!invalidUrl) && (!String.IsNullOrEmpty(content.Level8)) && ((UrlSegments.Count < 8) || (UrlSegments[7] != content.Level8))) invalidUrl = true; // Valiodate the web content id in the URL matches the object if (invalidUrl) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "URL of PUT request does not match Content posted object"), Response, Protocol, null); Response.StatusCode = 400; return; } // Ensure the first segment is not a reserved word foreach (string thisReserved in Engine_ApplicationCache_Gateway.Settings.Reserved_Keywords) { if (String.Compare(thisReserved, content.Level1, StringComparison.OrdinalIgnoreCase) == 0) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Level1 cannot be a system reserved word."), Response, Protocol, null); Response.StatusCode = 400; return; } } // Look for the inherit flag, if it is possible there is a parent (i.e, at least segment 2) if (UrlSegments.Count > 1) { // Get the inherit from parent flag bool inheritFromParent = ((RequestForm["Inherit"] != null) && (RequestForm["Inherit"].ToUpper() == "TRUE")); if (inheritFromParent) { // Copy the current URL segment list List<string> parentCheck = new List<string>(8); parentCheck.AddRange(UrlSegments); parentCheck.RemoveAt(parentCheck.Count - 1); // Remove the last one while (parentCheck.Count > 0) { // Is this a match? WebContent_Hierarchy_Node possibleParent = Engine_ApplicationCache_Gateway.WebContent_Hierarchy.Find(parentCheck); if (possibleParent != null) { // Declare the return object and look this up in the cache by ID HTML_Based_Content parentValue = CachedDataManager.WebContent.Retrieve_Page_Details(possibleParent.WebContentID, tracer); // If nothing was retrieved, build it if (parentValue == null) { // Try to read and return the html based content // Get the details from the database WebContent_Basic_Info parentBasicInfo = Engine_Database.WebContent_Get_Page(possibleParent.WebContentID, tracer); if ((parentBasicInfo != null) && (!String.IsNullOrEmpty(parentBasicInfo.Redirect))) { // Try to Build the HTML content WebContentEndpointErrorEnum errorType; parentValue = read_source_file(parentBasicInfo, tracer, out errorType); if (parentValue != null) { // Since everything was found, copy over the values and stop looking for a parent if (!String.IsNullOrEmpty(parentValue.Banner)) content.Banner = parentValue.Banner; if (!String.IsNullOrEmpty(parentValue.CssFile)) content.CssFile = parentValue.CssFile; if (!String.IsNullOrEmpty(parentValue.Extra_Head_Info)) content.Extra_Head_Info = parentValue.Extra_Head_Info; if (parentValue.IncludeMenu.HasValue) content.IncludeMenu = parentValue.IncludeMenu; if (!String.IsNullOrEmpty(parentValue.JavascriptFile)) content.JavascriptFile = parentValue.JavascriptFile; if (!String.IsNullOrEmpty(parentValue.SiteMap)) content.SiteMap = parentValue.SiteMap; if (!String.IsNullOrEmpty(parentValue.Web_Skin)) content.Web_Skin = parentValue.Web_Skin; break; } } } } parentCheck.RemoveAt(parentCheck.Count - 1); } } } // Get the location for this HTML file to be saved StringBuilder dirBuilder = new StringBuilder(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design\\webcontent\\" + content.Level1); if (!String.IsNullOrEmpty(content.Level2)) { dirBuilder.Append("\\" + content.Level2); if (!String.IsNullOrEmpty(content.Level3)) { dirBuilder.Append("\\" + content.Level3); if (!String.IsNullOrEmpty(content.Level4)) { dirBuilder.Append("\\" + content.Level4); if (!String.IsNullOrEmpty(content.Level5)) { dirBuilder.Append("\\" + content.Level5); if (!String.IsNullOrEmpty(content.Level6)) { dirBuilder.Append("\\" + content.Level6); if (!String.IsNullOrEmpty(content.Level7)) { dirBuilder.Append("\\" + content.Level7); if (!String.IsNullOrEmpty(content.Level8)) { dirBuilder.Append("\\" + content.Level8); } } } } } } } // Ensure this directory exists if (!Directory.Exists(dirBuilder.ToString())) { try { Directory.CreateDirectory(dirBuilder.ToString()); } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to create the directory for this web content page"), Response, Protocol, null); Response.StatusCode = 500; return; } } // Save the HTML file to the file system try { string fileName = Path.Combine(dirBuilder.ToString(), "default.html"); if (!File.Exists(fileName)) { content.Save_To_File(fileName); } } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to save HTML file for this web content page"), Response, Protocol, null); Response.StatusCode = 500; return; } // Save to the database try { int webcontentid = Engine_Database.WebContent_Add_Page(content.Level1, content.Level2, content.Level3, content.Level4, content.Level5, content.Level6, content.Level7, content.Level8, user, content.Title, content.Description, content.Redirect, tracer); Engine_ApplicationCache_Gateway.WebContent_Hierarchy.Add_Single_Node(webcontentid, content.Redirect, content.Level1, content.Level2, content.Level3, content.Level4, content.Level5, content.Level6, content.Level7, content.Level8); } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to save the information for the new web content page to the database"), Response, Protocol, null); Response.StatusCode = 500; return; } // Build return value RestResponseMessage message = new RestResponseMessage(ErrorRestTypeEnum.Successful, "Added new page"); // Set the URL StringBuilder urlBuilder = new StringBuilder(Engine_ApplicationCache_Gateway.Settings.Servers.Base_URL + "/" + content.Level1); if (!String.IsNullOrEmpty(content.Level2)) { urlBuilder.Append("/" + content.Level2); if (!String.IsNullOrEmpty(content.Level3)) { urlBuilder.Append("/" + content.Level3); if (!String.IsNullOrEmpty(content.Level4)) { urlBuilder.Append("/" + content.Level4); if (!String.IsNullOrEmpty(content.Level5)) { urlBuilder.Append("/" + content.Level5); if (!String.IsNullOrEmpty(content.Level6)) { urlBuilder.Append("/" + content.Level6); if (!String.IsNullOrEmpty(content.Level7)) { urlBuilder.Append("/" + content.Level7); if (!String.IsNullOrEmpty(content.Level8)) { urlBuilder.Append("/" + content.Level8); } } } } } } } message.URI = urlBuilder.ToString(); // Use the base class to serialize the object according to request protocol Serialize(message, Response, Protocol, null); }
// <summary> Delete a non-aggregational top-level web content, static HTML page or redirect </summary> /// <param name="Response"></param> /// <param name="UrlSegments"></param> /// <param name="Protocol"></param> /// <param name="RequestForm"></param> /// <param name="IsDebug"></param> public void Update_HTML_Based_Content(HttpResponse Response, List<string> UrlSegments, Microservice_Endpoint_Protocol_Enum Protocol, NameValueCollection RequestForm, bool IsDebug) { Custom_Tracer tracer = new Custom_Tracer(); // Add a trace tracer.Add_Trace("WebContentServices.AddUpdate_HTML_Based_Content"); // Validate the web content id exists in the URL int webcontentId; if ((UrlSegments.Count == 0) || (!Int32.TryParse(UrlSegments[0], out webcontentId))) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Invalid URL. WebContentID missing from URL"), Response, Protocol, null); Response.StatusCode = 400; return; } // Get and validate the required USER (string) posted request object if ((RequestForm["User"] == null) || (String.IsNullOrEmpty(RequestForm["User"]))) { Serialize( new RestResponseMessage(ErrorRestTypeEnum.InputError, "Required posted object 'User' is missing") , Response, Protocol, null); Response.StatusCode = 400; return; } string user = RequestForm["User"]; // Get and validate the required CONTENT (HTML_Based_Content) posted request objects if ((RequestForm["Content"] == null) || (String.IsNullOrEmpty(RequestForm["Content"]))) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Required posted object 'Content' is missing"), Response, Protocol, null); Response.StatusCode = 400; return; } string contentString = RequestForm["Content"]; HTML_Based_Content content = null; try { switch (Protocol) { case Microservice_Endpoint_Protocol_Enum.JSON: case Microservice_Endpoint_Protocol_Enum.JSON_P: content = JSON.Deserialize<HTML_Based_Content>(contentString); break; case Microservice_Endpoint_Protocol_Enum.PROTOBUF: // Deserialize using the Protocol buffer-net library byte[] byteArray = Encoding.ASCII.GetBytes(contentString); MemoryStream mstream = new MemoryStream(byteArray); content = Serializer.Deserialize<HTML_Based_Content>(mstream); break; case Microservice_Endpoint_Protocol_Enum.XML: byte[] byteArray2 = Encoding.UTF8.GetBytes(contentString); MemoryStream mstream2 = new MemoryStream(byteArray2); XmlSerializer x = new XmlSerializer(typeof(Content)); content = (HTML_Based_Content)x.Deserialize(mstream2); break; } } catch (Exception ee) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Unable to deserialize 'Content' parameter to HTML_Based_Content: " + ee.Message), Response, Protocol, null); Response.StatusCode = 400; return; } // If content wasnot successfully deserialized, return error if ( content == null ) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Unable to deserialize 'Content' parameter to HTML_Based_Content"), Response, Protocol, null); Response.StatusCode = 400; return; } // Level1 must be neither NULL nor empty if (String.IsNullOrEmpty(content.Level1)) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "Level1 of the content cannot be null or empty"), Response, Protocol, null); Response.StatusCode = 400; return; } // Valiodate the web content id in the URL matches the object if (webcontentId != content.WebContentID) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.InputError, "WebContentID from URL does not match Content posted object"), Response, Protocol, null); Response.StatusCode = 400; return; } // You can't change the URL segments, so pull the current object to ensure that wasn't done HTML_Based_Content currentContent = CachedDataManager.WebContent.Retrieve_Page_Details(webcontentId, tracer); // If nothing was retrieved, build it if (currentContent == null) { // Try to read and return the html based content // Get the details from the database WebContent_Basic_Info basicInfo = Engine_Database.WebContent_Get_Page(webcontentId, tracer); if (basicInfo == null) { Response.ContentType = "text/plain"; Response.Output.WriteLine("Unable to pull web content data from the database"); Response.StatusCode = 500; return; } // Set the content levels from the database object content.Level1 = basicInfo.Level1; content.Level2 = basicInfo.Level2; content.Level3 = basicInfo.Level3; content.Level4 = basicInfo.Level4; content.Level5 = basicInfo.Level5; content.Level6 = basicInfo.Level6; content.Level7 = basicInfo.Level7; content.Level8 = basicInfo.Level8; // If this has a redirect, return if (!String.IsNullOrEmpty(basicInfo.Redirect)) { currentContent = new HTML_Based_Content { Title = basicInfo.Title, Level1 = basicInfo.Level1, Level2 = basicInfo.Level2, Level3 = basicInfo.Level3, Level4 = basicInfo.Level4, Level5 = basicInfo.Level5, Level6 = basicInfo.Level6, Level7 = basicInfo.Level7, Level8 = basicInfo.Level8, Locked = basicInfo.Locked, Description = basicInfo.Summary, Redirect = basicInfo.Redirect, WebContentID = basicInfo.WebContentID }; } else { // Build the HTML content WebContentEndpointErrorEnum errorType; currentContent = read_source_file(basicInfo, tracer, out errorType); } } // If the current value was pulled, determine what has been changed for the database note string changeMessage = "Updated web page"; if (currentContent != null) { // If the redirect changed, just make that the message string currRedirect = String.Empty; string newRedirect = String.Empty; if (!String.IsNullOrEmpty(content.Redirect)) newRedirect = content.Redirect; if (!String.IsNullOrEmpty(currentContent.Redirect)) currRedirect = currentContent.Redirect; if (String.Compare(newRedirect, currRedirect, StringComparison.OrdinalIgnoreCase) != 0) { if ((newRedirect.Length > 0) && (currRedirect.Length > 0)) { changeMessage = "Changed redirect URL"; } else if ( newRedirect.Length == 0 ) { changeMessage = "Removed redirect URL"; } else { changeMessage = "Added redirect URL"; } } else if (!AreEqual(content.ContentSource, currentContent.ContentSource)) { changeMessage = "Updated the text/html"; } else { List<string> updatesBuilderList = new List<string>(); if (!AreEqual(content.Author, currentContent.Author)) updatesBuilderList.Add("Author"); if (!AreEqual(content.Banner, currentContent.Banner)) updatesBuilderList.Add("Banner"); if (!AreEqual(content.CssFile, currentContent.CssFile)) updatesBuilderList.Add("Stylesheet"); if (!AreEqual(content.Description, currentContent.Description)) updatesBuilderList.Add("Description"); if (!AreEqual(content.Extra_Head_Info, currentContent.Extra_Head_Info)) updatesBuilderList.Add("Header Data"); if (!AreEqual(content.JavascriptFile, currentContent.JavascriptFile)) updatesBuilderList.Add("Javascript"); if (!AreEqual(content.Keywords, currentContent.Keywords)) updatesBuilderList.Add("Keywords"); if (!AreEqual(content.SiteMap, currentContent.SiteMap)) updatesBuilderList.Add("Tree Nav Group"); if (!AreEqual(content.Title, currentContent.Title)) updatesBuilderList.Add("Title"); if (!AreEqual(content.Web_Skin, currentContent.Web_Skin)) updatesBuilderList.Add("Web Skin"); if (updatesBuilderList.Count > 0) { if (updatesBuilderList.Count == 1) { changeMessage = "Updated the " + updatesBuilderList[0]; } if (updatesBuilderList.Count == 2) { changeMessage = "Updated the " + updatesBuilderList[0] + " and " + updatesBuilderList[1]; } if (updatesBuilderList.Count > 2) { StringBuilder updatesBuilder = new StringBuilder("Updated the "); for (int i = 0; i < updatesBuilderList.Count; i++) { if (i == 0) updatesBuilder.Append(updatesBuilderList[0]); else if (i < updatesBuilderList.Count - 1) { updatesBuilder.Append(", " + updatesBuilderList[i]); } else { updatesBuilder.Append(", and " + updatesBuilderList[i]); } } changeMessage = updatesBuilder.ToString(); } } } } // Get the location for this HTML file to be saved StringBuilder dirBuilder = new StringBuilder(Engine_ApplicationCache_Gateway.Settings.Servers.Base_Directory + "design\\webcontent\\" + content.Level1); if (!String.IsNullOrEmpty(content.Level2)) { dirBuilder.Append("\\" + content.Level2); if (!String.IsNullOrEmpty(content.Level3)) { dirBuilder.Append("\\" + content.Level3); if (!String.IsNullOrEmpty(content.Level4)) { dirBuilder.Append("\\" + content.Level4); if (!String.IsNullOrEmpty(content.Level5)) { dirBuilder.Append("\\" + content.Level5); if (!String.IsNullOrEmpty(content.Level6)) { dirBuilder.Append("\\" + content.Level6); if (!String.IsNullOrEmpty(content.Level7)) { dirBuilder.Append("\\" + content.Level7); if (!String.IsNullOrEmpty(content.Level8)) { dirBuilder.Append("\\" + content.Level8); } } } } } } } // Ensure this directory exists if (!Directory.Exists(dirBuilder.ToString())) { try { Directory.CreateDirectory(dirBuilder.ToString()); } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to create the directory for this web content page"), Response, Protocol, null); Response.StatusCode = 500; return; } } // Save the HTML file to the file system try { string fileName = Path.Combine(dirBuilder.ToString(), "default.html"); // Make a backup from today, if none made yet if (File.Exists(fileName)) { DateTime lastWrite = (new FileInfo(fileName)).LastWriteTime; string new_file = fileName.ToLower().Replace(".txt", "").Replace(".html", "").Replace(".htm", "") + lastWrite.Year + lastWrite.Month.ToString().PadLeft(2, '0') + lastWrite.Day.ToString().PadLeft(2, '0') + ".bak"; if (File.Exists(new_file)) File.Delete(new_file); File.Move(fileName, new_file); } // Save the updated file content.Save_To_File(fileName); } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to save HTML file for this web content page"), Response, Protocol, null); Response.StatusCode = 500; return; } // Save to the database bool success; try { success = Engine_Database.WebContent_Edit_Page(webcontentId, content.Title, content.Description, content.Redirect, user, changeMessage, tracer); Engine_ApplicationCache_Gateway.WebContent_Hierarchy.Add_Single_Node(webcontentId, content.Redirect, content.Level1, content.Level2, content.Level3, content.Level4, content.Level5, content.Level6, content.Level7, content.Level8); } catch (Exception) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to save the information for the web content page to the database"), Response, Protocol, null); Response.StatusCode = 500; return; } // Clear the cache CachedDataManager.WebContent.Clear_Page_Details(webcontentId); // If this was a failure, return a message if (!success) { Serialize(new RestResponseMessage(ErrorRestTypeEnum.Exception, "Unable to save the updated information to the database"), Response, Protocol, null); Response.StatusCode = 500; return; } // Build return value RestResponseMessage message = new RestResponseMessage(ErrorRestTypeEnum.Successful, "Updated web page details"); // Set the URL StringBuilder urlBuilder = new StringBuilder(Engine_ApplicationCache_Gateway.Settings.Servers.Base_URL + "/" + content.Level1); if (!String.IsNullOrEmpty(content.Level2)) { urlBuilder.Append("/" + content.Level2); if (!String.IsNullOrEmpty(content.Level3)) { urlBuilder.Append("/" + content.Level3); if (!String.IsNullOrEmpty(content.Level4)) { urlBuilder.Append("/" + content.Level4); if (!String.IsNullOrEmpty(content.Level5)) { urlBuilder.Append("/" + content.Level5); if (!String.IsNullOrEmpty(content.Level6)) { urlBuilder.Append("/" + content.Level6); if (!String.IsNullOrEmpty(content.Level7)) { urlBuilder.Append("/" + content.Level7); if (!String.IsNullOrEmpty(content.Level8)) { urlBuilder.Append("/" + content.Level8); } } } } } } } message.URI = urlBuilder.ToString(); // Use the base class to serialize the object according to request protocol Serialize(message, Response, Protocol, null); }