using System;
using System.ComponentModel;
using System.Net;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.UIControls;
public partial class CMSAdminControls_UI_System_ServerChecker : CMSUserControl, ICallbackEventHandler
{
#region "Variables"
private string mTextBoxControlID = null;
private string mPagePath = null;
private AsyncWorker mWorker = null;
private Guid mCurrentProcessGUID = Guid.Empty;
protected static Hashtable mResults = new Hashtable();
#endregion
#region "Properties"
///
/// Client ID of textbox from which server name will be aquired.
///
[DefaultValue(""), TypeConverter(typeof(ControlIDConverter))]
public string TextBoxControlID
{
get
{
return mTextBoxControlID;
}
set
{
mTextBoxControlID = value;
}
}
///
/// Path to page under server which will checked.
///
public string PagePath
{
get
{
return mPagePath;
}
set
{
mPagePath = value;
}
}
///
/// Async worker.
///
private AsyncWorker Worker
{
get
{
return mWorker ?? (mWorker = new AsyncWorker());
}
}
///
/// Guid of currently processed process.
///
private Guid CurrentProcessGUID
{
get
{
return mCurrentProcessGUID;
}
set
{
mCurrentProcessGUID = value;
}
}
#endregion
#region "Control methods"
///
/// Load event.
///
protected void Page_Load(object sender, EventArgs e)
{
// Register javascripts
if (!RequestHelper.IsCallback())
{
ScriptHelper.RegisterTooltip(Page);
TextBox txtServerName = (TextBox)Parent.FindControl(TextBoxControlID);
if (txtServerName != null)
{
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "ServerChecker_" + ClientID, ScriptHelper.GetScript(
"var processGuid_" + ClientID + "='';\n" +
"function UpdateStatusLabel_" + ClientID + "(value,context){var args = value.split('|'); if(args[0] == 'true') {var item = document.getElementById('" + lblStatus.ClientID + "'); if(item != null) { item.innerHTML = args[1]; }} else { if(args[1] != '') { processGuid_" + ClientID + "= value; window.setTimeout(\"" + Page.ClientScript.GetCallbackEventReference(this, "processGuid_" + ClientID, "UpdateStatusLabel_" + ClientID, "null") + "\",1000);}}}\n" +
"function CheckServer_" + ClientID + "(){ var item = document.getElementById('" + lblStatus.ClientID + "');item.innerHTML = '" + GetString("serverchecker.processing") + "'; var control = document.getElementById('" + txtServerName.ClientID + "'); var value='true|' + control.value;" + Page.ClientScript.GetCallbackEventReference(this, "value", "UpdateStatusLabel_" + ClientID, "null") + ";}"));
}
}
// Initialize controls
btnCheckServer.ImageUrl = UIHelper.GetImageUrl(this.Page, "/Design/Controls/ServerChecker/check.png", IsLiveSite);
btnCheckServer.ToolTip = GetString("serverchecker.checkserver");
btnCheckServer.Attributes.Add("onclick", "CheckServer_" + ClientID + "();return false;");
btnCheckServer.CssClass = "ServerCheckerIcon";
lblStatus.CssClass = "ServerCheckerStatus";
}
///
/// Check server specified with URL availability.
///
/// Async worker parameter
protected void CheckServer(object parameter)
{
CheckServer(parameter.ToString());
}
///
/// Check server specified with URL availability.
///
/// URL to be checked and result identifier
protected void CheckServer(string parameters)
{
string[] param = parameters.Split('|');
string result = null;
try
{
string url = param[0];
// Resolve relative URL to full URL
if (!String.IsNullOrEmpty(url) && url.StartsWithCSafe("~/"))
{
url = URLHelper.GetAbsoluteUrl(url, URLHelper.GetFullDomain(), null, null);
}
// Check if protocol is contained in checked URL and if not complete it
if (!URLHelper.ContainsProtocol(url))
{
// Check if running on secured layer
if (!URLHelper.IsSSL)
{
url = "http://" + url;
}
else
{
url = "https://" + url;
}
}
// Send HTTP request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Process result
result = GetString("serverchecker.serverstatus");
if (HttpStatusCode.OK == response.StatusCode)
{
// HTTP = 200 - server available
result += " " + GetString("general.ok");
}
else
{
// Other status codes
result += " Response status code: " + response.StatusCode.ToString() + "
" + response.StatusDescription) + "');\" onmouseout=\"UnTip();\" >" + GetString("general.error") + "";
}
response.Close();
}
catch (Exception ex)
{
// Handling exceptions
result = GetString("serverchecker.serverstatus") + " " + ScriptHelper.FormatTooltipString(ex.Message) + "
" + ScriptHelper.FormatTooltipString(ex.StackTrace) + "');\" onmouseout=\"UnTip();\" >" + GetString("general.error") + "";
}
finally
{
SetCurrentResult(param[1], result);
}
}
///
/// Gets result of process with specified ID.
///
/// ID of process
/// Result of process
private string GetCurrentResult(string id)
{
return ValidationHelper.GetString(mResults["ServerCheckerResult_" + id], string.Empty);
}
///
/// Sets result of process with specified ID.
///
/// ID of process
/// Value of result
private void SetCurrentResult(string id, string value)
{
mResults["ServerCheckerResult_" + id] = value;
}
#endregion
#region "Callback methods"
///
/// Prepares the callback result.
///
public string GetCallbackResult()
{
string result = GetCurrentResult(CurrentProcessGUID.ToString());
// If worker finished return result
if (!String.IsNullOrEmpty(result))
{
mResults.Remove("ServerCheckerResult_" + CurrentProcessGUID.ToString());
return "true|" + result;
}
return "false|" + CurrentProcessGUID.ToString();
}
///
/// Raises the callback event.
///
public void RaiseCallbackEvent(string eventArgument)
{
string[] args = eventArgument.Split('|');
if (args.Length == 2)
{
// If not checking callback, run new request
if (args[0].ToLowerCSafe() == "true")
{
if (Worker.Status == AsyncWorkerStatusEnum.Stopped)
{
CurrentProcessGUID = Guid.NewGuid();
if (!String.IsNullOrEmpty(args[1]))
{
// Prepare URL
string parameter = args[1];
if (!String.IsNullOrEmpty(PagePath))
{
parameter = parameter.TrimEnd('/') + "//" + PagePath;
}
// Call async request to server
Worker.Parameter = parameter + "|" + CurrentProcessGUID;
Worker.RunAsync(CheckServer, WindowsIdentity.GetCurrent());
}
else
{
SetCurrentResult(CurrentProcessGUID.ToString(), GetString("serverchecker.urlnotavailable"));
}
}
}
else
{
CurrentProcessGUID = new Guid(args[1]);
}
}
}
#endregion
}