added more HttpRequestExtensions, cleaned up some code in legacyAjaxCalls, adds UmbracoRequestId to UmbracoContext

(for future use)
This commit is contained in:
Shannon Deminick
2013-01-10 03:59:11 +03:00
parent 9c5def35ee
commit 51c1c5c717
3 changed files with 94 additions and 73 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
@@ -35,5 +34,36 @@ namespace Umbraco.Web
return !val.IsNullOrWhiteSpace() ? val : string.Empty;
}
/// <summary>
/// Safely get the item from the query string and convert it to type 'T', otherwise will return default(T).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="request"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T GetItemAs<T>(this HttpRequestBase request, string key)
{
var val = HttpContext.Current.Request[key];
var whitespaceCheck = !val.IsNullOrWhiteSpace() ? val : string.Empty;
if (whitespaceCheck.IsNullOrWhiteSpace())
return (T) typeof (T).GetDefaultValue();
var attempt = val.TryConvertTo<T>();
if (attempt.Success)
return attempt.Result;
return (T)typeof(T).GetDefaultValue();
}
/// <summary>
/// Safely get the item from the query string and convert it to type 'T', otherwise will return default(T).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="request"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T GetItemAs<T>(this HttpRequest request, string key)
{
return new HttpRequestWrapper(request).GetItemAs<T>(key);
}
}
}

View File

@@ -49,6 +49,7 @@ namespace Umbraco.Web
if (applicationContext == null) throw new ArgumentNullException("applicationContext");
ObjectCreated = DateTime.Now;
UmbracoRequestId = Guid.NewGuid();
HttpContext = httpContext;
Application = applicationContext;
@@ -109,6 +110,11 @@ namespace Umbraco.Web
/// </summary>
internal DateTime ObjectCreated { get; private set; }
/// <summary>
/// This is used internally for debugging and also used to define anything required to distinguish this request from another.
/// </summary>
internal Guid UmbracoRequestId { get; private set; }
/// <summary>
/// Gets the current ApplicationContext
/// </summary>

View File

@@ -16,8 +16,8 @@ using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Net;
using System.Web.UI;
using Umbraco.Core.IO;
using umbraco.businesslogic.Exceptions;
using umbraco.IO;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.media;
using umbraco.BasePages;
@@ -205,15 +205,15 @@ namespace umbraco.presentation.webservices
switch (fileType)
{
case "xslt":
return saveXslt(fileName, fileContents, ignoreDebug);
return SaveXslt(fileName, fileContents, ignoreDebug);
case "python":
return "true";
case "css":
return saveCss(fileName, fileContents, fileID);
return SaveCss(fileName, fileContents, fileID);
case "script":
return saveScript(fileName, fileContents);
return SaveScript(fileName, fileContents);
case "template":
return saveTemplate(fileName, fileAlias, fileContents, fileID, masterID);
return SaveTemplate(fileName, fileAlias, fileContents, fileID, masterID);
default:
throw new ArgumentException(String.Format("Invalid fileType passed: '{0}'", fileType));
}
@@ -228,38 +228,34 @@ namespace umbraco.presentation.webservices
}
private string saveCss(string fileName, string fileContents, int fileID)
private static string SaveCss(string fileName, string fileContents, int fileID)
{
string returnValue = "false";
cms.businesslogic.web.StyleSheet stylesheet = new cms.businesslogic.web.StyleSheet(fileID);
string returnValue;
var stylesheet = new StyleSheet(fileID) {Content = fileContents, Text = fileName};
if (stylesheet != null)
{
stylesheet.Content = fileContents;
stylesheet.Text = fileName;
try
{
stylesheet.saveCssToFile();
returnValue = "true";
}
catch (Exception ee)
{
throw new Exception("Couldn't save file", ee);
}
try
{
stylesheet.saveCssToFile();
returnValue = "true";
}
catch (Exception ee)
{
throw new Exception("Couldn't save file", ee);
}
//this.speechBubble(speechBubbleIcon.save, ui.Text("speechBubbles", "editStylesheetSaved", base.getUser()), "");
}
return returnValue;
//this.speechBubble(speechBubbleIcon.save, ui.Text("speechBubbles", "editStylesheetSaved", base.getUser()), "");
return returnValue;
}
private string saveXslt(string fileName, string fileContents, bool ignoreDebugging)
{
StreamWriter SW;
string tempFileName = IOHelper.MapPath(SystemDirectories.Xslt + "/" + System.DateTime.Now.Ticks + "_temp.xslt");
SW = File.CreateText(tempFileName);
SW.Write(fileContents);
SW.Close();
private string SaveXslt(string fileName, string fileContents, bool ignoreDebugging)
{
var tempFileName = IOHelper.MapPath(SystemDirectories.Xslt + "/" + System.DateTime.Now.Ticks + "_temp.xslt");
using (var sw = File.CreateText(tempFileName))
{
sw.Write(fileContents);
sw.Close();
}
// Test the xslt
string errorMessage = "";
if (!ignoreDebugging)
@@ -363,13 +359,15 @@ namespace umbraco.presentation.webservices
if (errorMessage == "" && fileName.ToLower().EndsWith(".xslt"))
{
//Hardcoded security-check... only allow saving files in xslt directory...
string savePath = IOHelper.MapPath(SystemDirectories.Xslt + "/" + fileName);
var savePath = IOHelper.MapPath(SystemDirectories.Xslt + "/" + fileName);
if (savePath.StartsWith(IOHelper.MapPath(SystemDirectories.Xslt)))
{
SW = File.CreateText(savePath);
SW.Write(fileContents);
SW.Close();
using (var sw = File.CreateText(savePath))
{
sw.Write(fileContents);
sw.Close();
}
errorMessage = "true";
}
else
@@ -378,26 +376,19 @@ namespace umbraco.presentation.webservices
}
}
System.IO.File.Delete(tempFileName);
File.Delete(tempFileName);
return errorMessage;
}
private string savePython(string filename, string contents)
private static string SaveScript(string filename, string contents)
{
return "true";
}
private string saveScript(string filename, string contents)
{
string val = contents;
string returnValue = "false";
var val = contents;
string returnValue;
try
{
string savePath = IOHelper.MapPath(SystemDirectories.Scripts + "/" + filename);
var savePath = IOHelper.MapPath(SystemDirectories.Scripts + "/" + filename);
//Directory check.. only allow files in script dir and below to be edited
if (savePath.StartsWith(IOHelper.MapPath(SystemDirectories.Scripts + "/")))
@@ -422,34 +413,28 @@ namespace umbraco.presentation.webservices
return returnValue;
}
private string saveTemplate(string templateName, string templateAlias, string templateContents, int templateID, int masterTemplateID)
private static string SaveTemplate(string templateName, string templateAlias, string templateContents, int templateID, int masterTemplateID)
{
var tp = new cms.businesslogic.template.Template(templateID);
var retVal = "false";
cms.businesslogic.template.Template _template = new global::umbraco.cms.businesslogic.template.Template(templateID);
string retVal = "false";
tp.Text = templateName;
tp.Alias = templateAlias;
tp.MasterTemplate = masterTemplateID;
tp.Design = templateContents;
if (_template != null)
{
_template.Text = templateName;
_template.Alias = templateAlias;
_template.MasterTemplate = masterTemplateID;
_template.Design = templateContents;
retVal = "true";
retVal = "true";
// Clear cache in rutime
if (UmbracoSettings.UseDistributedCalls)
cache.dispatcher.Refresh(
new Guid("dd12b6a0-14b9-46e8-8800-c154f74047c8"),
_template.Id);
else
template.ClearCachedTemplate(_template.Id);
}
else
return "false";
// Clear cache in rutime
if (UmbracoSettings.UseDistributedCalls)
cache.dispatcher.Refresh(
new Guid("dd12b6a0-14b9-46e8-8800-c154f74047c8"),
tp.Id);
else
template.ClearCachedTemplate(tp.Id);
return retVal;
return retVal;
}