| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- using Microsoft.Win32;
- using System.Diagnostics;
- using System.Text;
- namespace com.cloudsoft.utils
- {
- /// <summary>
- /// Base class for Business and data access options
- /// </summary>
- /// <remarks>
- /// <example> Example code
- /// <code>
- /// public class Security_nTX : com.cloudsoft.utils.ErrHelper
- /// {
- /// public Security_nTX()
- /// {
- /// zgSystem = "GSK_SECURITY";
- /// }
- /// public String AuthoriseUserAction(String zPayload)
- /// {
- /// try
- /// {
- /// Security_DA oDA = new Security_DA();
- /// return oDA.AuthoriseUserAction(zPayload);
- /// }
- /// catch (Exception ex)
- /// {
- /// return handleError(ex);
- /// }
- /// }
- /// </code>
- /// </example>
- /// </remarks>
- public class ErrHelper
- {
- /// <summary>
- /// Returns HTML for a WI Error
- /// </summary>
- /// <param name="oError">WI compliant error</param>
- /// <returns>HTML for a WI Error</returns>
- public string getParamsHTML(XMLHelper oError)
- {
- StringBuilder sbParams = new StringBuilder("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
- sbParams.Append("<tr><th colspan=\"2\" align=\"left\">Form values</th></tr>");
- if (oError.nodeCount("values/formvalues/value") <= 0)
- {
- sbParams.Append("<tr><td colspan=\"2\" align=\"left\">None</td></tr>");
- }
- else
- {
- foreach (XMLHelper value in oError.getNodes("values/formvalues/value"))
- {
- if (value.getString("@id") != "__VIEWSTATE")
- sbParams.Append("<tr><td>" + value.getString("@id") + "</td><td>" + value.getValue() + "</td></tr>");
- }
- }
- sbParams.Append("<tr><th colspan=\"2\" align=\"left\"><hr/></th></tr>");
- sbParams.Append("<tr><th colspan=\"2\" align=\"left\">Querystring values</th></tr>");
- if (oError.nodeCount("values/querystringvalues/value") <= 0)
- {
- sbParams.Append("<tr><td colspan=\"2\" align=\"left\">None</td></tr>");
- }
- else
- {
- foreach (XMLHelper value in oError.getNodes("values/querystringvalues/value"))
- {
- if (value.getString("@id") != "__VIEWSTATE")
- sbParams.Append("<tr><td>" + value.getString("@id") + "</td><td>" + value.getValue() + "</td></tr>");
- }
- }
- sbParams.Append("</table>");
- return sbParams.ToString();
- }
- /// <summary>
- /// Logs an error using WI mechanisms, emailing if configured to do so
- /// </summary>
- /// <param name="oError">WI compliant error</param>
- public void logError(XMLHelper oError)
- {
- short category = 4;
- int id = 1;
- // Log the entry to the event log...
- throw new Exception(oError.getString("message"));
- // EventLog.WriteEntry(zgSystem, oError.getString("message") + " \rPath: " + oError.getString("path") + " \rUser: " + oError.getString("user"), EventLogEntryType.Error, id, category, ba);
- }
- /// <summary>
- /// Enum for Error Types
- /// </summary>
- /// <remarks>
- /// <list type="bullet">
- /// <item><term>System</term><description> - System Error</description></item>
- /// <item><term>Business</term><description> - Business Error</description></item>
- /// <item><term>Information</term><description> - Information (not an error)</description></item>
- /// </list>
- /// </remarks>
- public enum ErrorType {
- /// <summary>System</summary>
- System,
- /// <summary>Business</summary>
- Business,
- /// <summary>Information</summary>
- Information
- };
- /// <summary>
- /// The System. Used to log events
- /// </summary>
- private String _zgSystem;
- public String zgSystem
- {
- get
- {
- return _zgSystem;
- }
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="ErrHelper"/> class.
- /// </summary>
- public ErrHelper()
- : this("GSK")
- { }
- /// <summary>
- /// Initializes a new instance of the <see cref="ErrHelper"/> class with a system.
- /// </summary>
- public ErrHelper(String zSystem)
- {
- _zgSystem = zSystem;
- }
- /// <summary>
- /// Handles the error.
- /// </summary>
- /// <param name="ex">The Exception</param>
- /// <returns>Nothing , but rethrows the error with formatted xml as the error message</returns>
- public String handleError(Exception ex)
- {
- ErrorType et = ErrorType.System;
- if (ex.Source == "Oledb")
- et = ErrorType.Business;
- return handleError(ex, et);
- }
- /// <summary>
- /// Handles the error.
- /// </summary>
- /// <param name="ex">The Exception</param>
- /// <param name="et">The error type. <see cref="ErrorType"/></param>
- /// <returns>Nothing , but rethrows the error with formatted xml as the error message</returns>
- public String handleError(Exception ex, ErrorType et)
- {
- //WADS: Detailed exception info no longer given to the user...
- throw new Exception(et + " error : " + ex.Message);
- //XMLHelper oError = new XMLHelper();
- //if (oError.load(ex.Message) == false)
- //{
- // String zType = "SYSTEMERROR";
- // switch (et)
- // {
- // case ErrorType.Business:
- // zType = "BUSINESSEXCEPTION";
- // break;
- // case ErrorType.Information:
- // zType = "INFORMATION";
- // break;
- // }
- // StackTrace oST = new StackTrace();
- // int iFrame = 1;
- // String zMethod = oST.GetFrame(iFrame).GetMethod().Name;
- // while (zMethod == "throwError" || zMethod == "handleError" || zMethod == "validatePayload")
- // zMethod = oST.GetFrame(iFrame++).GetMethod().Name;
- // oError.createRoot("gskutils");
- // oError = oError.appendNode("error");
- // oError.appendAttribute("time", DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss"));
- // oError.appendAttribute("type", zType);
- // oError.appendAttribute("class", this.GetType().ToString());
- // oError.appendAttribute("method", zMethod);
- // oError.appendNode("source", (ex.Source == null ? this.GetType().ToString() : ex.Source));
- // oError.appendNode("description", ex.Message);
- // oError.appendNode("stack_trace", ex.StackTrace);
- // if (zType == "SYSTEMERROR")
- // writeEvent(zgSystem, oError.getRootXML(), et);
- //}
- ////WADS: Error detail...
- //logError(oError);
- //throw new Exception("WI Handled error has occured. Please report to the helpdesk.");
- }
- /// <summary>
- /// Throws a Business error.
- /// </summary>
- /// <param name="zMessage">The Business Error message.</param>
- public void throwError(String zMessage)
- {
- throwError(zMessage, ErrorType.Business);
- }
-
- /// <summary>
- /// Throws an error.
- /// </summary>
- /// <param name="zMessage">The error message.</param>
- /// <param name="et">The error type. <see cref="ErrorType"/></param>
- public void throwError(String zMessage, ErrorType et)
- {
- handleError(new Exception(zMessage), et);
- }
- /// <summary>
- /// Writes a debug message using the win32 api
- /// </summary>
- /// <param name="zMessage">The message.</param>
- public void debugMsg(Object zMessage)
- {
- String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
- Debug.Write(this.GetType().Name + "." + zMethod + " : " + zMessage);
- }
- public XMLHelper validatePayload(String zPayload)
- {
- XMLHelper domToLoad = new XMLHelper();
- if (domToLoad.load(zPayload) == false)
- throwError("Invalid Payload. XML is invalid");
- // Get the calling Method
- String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
- return validatePayload(domToLoad, zMethod);
- }
- public XMLHelper validatePayload(XMLHelper oPayload)
- {
- // Get the calling Method
- String zMethod = new StackTrace().GetFrame(1).GetMethod().Name;
- return validatePayload(oPayload, zMethod);
- }
- /// <summary>
- /// Validates the payload.
- /// </summary>
- /// <param name="zPayload">The payload.</param>
- /// <returns>Returns an XML Helper DOM of the Payload</returns>
- private XMLHelper validatePayload(XMLHelper oPayload, String zMethod)
- {
- XMLHelper domToLoad = oPayload;
- String zRequestType = domToLoad.getString("/request/@type");
-
- // Check to see that payload is right for method
- if (zRequestType == "")
- throwError("The request does not have a type attribute");
- // Check to see that payload is right for method
- if (zMethod.ToUpper() != zRequestType.ToUpper() && (zRequestType.ToUpper() != "SYSTEM"))
- throwError("The payload sent to the method " + zMethod + " is invalid. Not equal to " + zRequestType);
-
- // Build security payload xml
- String zUsername = domToLoad.getString("mud_id");
- if (domToLoad.getString("@sec") != "off")
- {
- XMLHelper oRequest = new XMLHelper().createStandardRequest("ValidatePayload", zUsername, domToLoad.getString("system"));
- oRequest.appendNode("resource", zMethod.ToUpper());
- oRequest.appendNode("action", "true");
- // Create instance of security object
- //Security_nTX oSecurity = new Security_nTX();
- //// Check user rights
- //XMLHelper oResponse = new XMLHelper();
- //oResponse.load(oSecurity.AuthoriseUserAction(oRequest.getRootXML()));
- //if (oResponse.getString("poReturnValue").StartsWith("TRUE") == false)
- // throwError("User '" + zUsername + "' does not have rights to perform the action '" + zMethod + "'.");
- ////throwError("Request : " + oRequest.getXML() + " Response : " + oResponse.getXML());
- }
-
- //Create System Audit
- if (zRequestType != "SYSTEM")
- {
- //Set oAudit = CreateObject("GSKUtils.Auditor")
- //If Len(zpPayload) < 2000 Then
- //oAudit.SystemAudit "CIMS", "Unknown", zpMethod, zUsername, zpPayload
- //Else
- // oAudit.SystemAudit "CIMS", "Unknown", zpMethod, zUsername
- //End If
- }
-
- return domToLoad;
- }
- /// <summary>
- /// Writes to the event log.
- /// </summary>
- /// <param name="zSource">The source.</param>
- /// <param name="zMessage">The message.</param>
- /// <param name="et">The Error type</param>
- private void writeEvent(String zSource, String zMessage, ErrorType et)
- {
- RegistryKey kLM = Registry.LocalMachine;
- String zLogDir;
- lock (kLM)
- {
- RegistryKey kSystem = kLM.OpenSubKey("Software\\GlaxoWellcome\\" + zSource);
- zLogDir = (String)kSystem.GetValue("ErrorLog");
- if (zLogDir == null)
- {
- kSystem = kLM.OpenSubKey("Software\\GlaxoWellcome\\UN");
- zLogDir = (String)kSystem.GetValue("ErrorLog");
- }
- }
- if (zLogDir != null)
- {
- try
- {
- StreamWriter osw;
- FileInfo ofi = new FileInfo(zLogDir + "\\error.log");
- if (!ofi.Exists)
- osw = ofi.CreateText();
- else
- osw = ofi.AppendText();
- osw.WriteLine(zMessage);
- osw.Close();
- }
- catch { }
- }
- }
- }
- }
|