/// <summary>
        /// Upgrades the specified connection.
        /// </summary>
        /// <param name="dbmsName">Name of the DBMS.</param>
        /// <param name="connStr">The connection string.</param>
        /// <returns>The upgraded database configuration.</returns>
        /// <exception cref="ConnectionUpgradeException">Unable to upgrade database connection.</exception>
        public IIntegrationDatabaseConfiguration Upgrade(string dbmsName, string connStr)
        {
#pragma warning disable 618
            connStr = SettingsInternal.DecryptStringForUpgrade(connStr);
#pragma warning restore 618

            Dictionary <String, String> providerKeys = new Dictionary <string, string>();
            providerKeys.Add("sql server", "SqlServer");
            providerKeys.Add("oracle", "Oracle");
            bool isSqlServer = dbmsName.ToLower().Equals("sql server");

            IDatabaseProvider provider = pluginProvider.GetImplementation(DatabaseProviderKey.Deserialize(providerKeys[dbmsName.ToLower()]));
            IIntegrationDatabaseConfiguration dbConfig = provider.CreateEmptyIntegrationDatabaseConfiguration();
            MetaDatabaseConfiguration         meta     = new MetaDatabaseConfiguration(dbConfig);

            if (RuntimePlatformUtils.IsJava())
            {
                string connStringOverride_SID  = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(PORT={0})(HOST={1}))(CONNECT_DATA=(SID={2})))";
                Regex  _datasourceJavaSidRegex = new Regex(@"jdbc:oracle:thin:([^/]*)/([^@]*)@([^:]*):([^:]*):([^:]*)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

                string connStringOverride_ServiceName  = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(PORT={0})(HOST={1}))(CONNECT_DATA=(SERVICE_NAME={2})))";
                Regex  _datasourceJavaServiceNameRegex = new Regex(@"jdbc:oracle:thin:([^/]*)/([^@]*)@([^:]*):([^:]*)/([^:]*)", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

                string username, password, tnsDescriptor;
                Match  _sidMatch, _snMatch;

                _sidMatch = _datasourceJavaSidRegex.Match(connStr);
                if (_sidMatch.Success)
                {
                    username      = _sidMatch.Groups[1].Value;
                    password      = _sidMatch.Groups[2].Value;
                    tnsDescriptor = String.Format(connStringOverride_SID, _sidMatch.Groups[4].Value, _sidMatch.Groups[3].Value, _sidMatch.Groups[5].Value);
                }
                else
                {
                    _snMatch = _datasourceJavaServiceNameRegex.Match(connStr);
                    if (!_snMatch.Success)
                    {
                        throw new ConnectionUpgradeException("Unable to upgrade database connection.");
                    }
                    username      = _snMatch.Groups[1].Value;
                    password      = _snMatch.Groups[2].Value;
                    tnsDescriptor = String.Format(connStringOverride_ServiceName, _snMatch.Groups[4].Value, _snMatch.Groups[3].Value, _snMatch.Groups[5].Value);
                }
                meta.GetParameter("Username").Set(username);
                meta.GetParameter("Password").Set(password);
                dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = tnsDescriptor;
                dbConfig.ConnectionStringOverride = String.Empty;
            }
            else
            {
                Regex _datasourceRegex = new Regex(@"data source=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                Regex _portRegex       = new Regex(@"port=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                Regex _catalogRegex    = new Regex(@"initial catalog=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                Regex _userIdRegex     = new Regex(@"user id=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                Regex _passwordRegex   = new Regex(@"password=\s*([^ ;]+)\s*;", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);


                Match userIdMatch, passwordMatch;
                bool  isAdvanced = false;

                List <string> basicKeys = new List <string> {
                    "data source", "user id", "password"
                };
                if (isSqlServer)
                {
                    basicKeys.Add("initial catalog");
                }
                else
                {
                    basicKeys.Add("port");
                }

                List <string> keysInConnection = connStr.Split(';').ToList <string>();
                connStr += connStr.EndsWith(";") ? "" : ";";

                foreach (string key in keysInConnection)
                {
                    if (!basicKeys.Contains(key.Substring(0, key.IndexOf("=")).Trim().ToLower()))
                    {
                        isAdvanced = true;
                        break;
                    }
                }

                userIdMatch = _userIdRegex.Match(connStr);
                meta.GetParameter("Username").Set(userIdMatch.Groups[1].Value.Replace("\"", ""));

                passwordMatch = _passwordRegex.Match(connStr);
                meta.GetParameter("Password").Set(passwordMatch.Groups[1].Value.Replace("\"", ""));

                if (isAdvanced)
                {
                    // In advanced mode the connection string override will hold all keys and values except username and password
                    dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = connStr;
                    if (userIdMatch.Success)
                    {
                        dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = dbConfig.AdvancedConfiguration.AdvancedConnectionStringField.Replace(userIdMatch.Groups[0].Value, "");
                    }
                    if (passwordMatch.Success)
                    {
                        dbConfig.AdvancedConfiguration.AdvancedConnectionStringField = dbConfig.AdvancedConfiguration.AdvancedConnectionStringField.Replace(passwordMatch.Groups[0].Value, "");
                    }
                }
                else
                {
                    if (isSqlServer)
                    {
                        meta.GetParameter("Server").Set(_datasourceRegex.Match(connStr).Groups[1].Value.Replace("\"", ""));
                        meta.GetParameter("Catalog").Set(_catalogRegex.Match(connStr).Groups[1].Value.Replace("\"", ""));
                    }
                    else
                    {
                        var dataSrcValues = _datasourceRegex.Match(connStr).Groups[1].Value.Replace("\"", "").Split('/');
                        var portValue     = _portRegex.Match(connStr).Groups[1].Value.Replace("\"", "");
                        meta.GetParameter("Host").Set(dataSrcValues.IsEmpty() ? "" : dataSrcValues[0]);
                        meta.GetParameter("Port").Set(portValue.IsNullOrEmpty() || portValue.Equals("0") ? "1521" /*default*/ : portValue);
                        meta.GetParameter("ServiceName").Set(dataSrcValues.Length < 1 ? "" : dataSrcValues[1]);
                    }
                }
                dbConfig.ConnectionStringOverride = String.Empty;
            }
            return(dbConfig);
        }
        private static void WriteJavascriptResponse(HttpRequest request, HttpResponse response, string script, bool flush)
        {
            try {
                bool isIE =

                    (request.Browser.Browser == "IE");

                response.ContentType = "text/html";



                // #90781
                // Internet explorer requires the first ajax response to be of at least 256 bytes, otherwise it will
                // only be executed when the receive buffer fills up in subsequent responses or when the request ends.
                // After that, subsequent responses can be of any size that they are executed promptly
                if (IsFirstAjaxResponse && isIE)
                {
                    // This padding string ensures that 256 bytes are sent, even when compression is enabled
                    // Using deflate compression stream, it outputs 256 bytes
                    // Using gzip compression stream, it outputs 266 bytes
                    // Using no compression, it outputs 334 bytes
                    string firstPadding = "<!-- OSPADDING VURGXTTJYZMCVZAFKKOPWKYUBXQDXDADFBRUSOSCDMISJYMZIROBPVAHBEFPZLMQVJXTEGNVRMLKCGGCLQQBLYJFAFTOEYYOARSAWSRCEVURGXTTJYZMCVZAFKKOPWKYUBXQDXDADFBRUSOSCDMISJYMZIROBPVAHBEFPZLMQVJXTEGNVRMLKCGGCLQQBLYJFAFTOEYYOARSAWSRCEVURGXTTJYZMCVZAFKKOPWKYUBXQDXDADFBRUSOSCDMISJYMZIROBPVAHBEFPZLMQVJXTEGNVRMLKCGGCLQQBLYJFAFTOEYYOARSAWSRCE -->";
                    response.Write(firstPadding);
                }

                if (IsFirstAjaxResponse)
                {
                    // Make sure the JSONUpdate function is defined in the current window
                    response.Write(JavascriptBeginTag + GetJsonUpdateScript() + JavascriptEndTag);
                    try {
                        //Add requestInfo necessary data for beacon in performanceprobe.js
                        OSJSONResponse resp       = new OSJSONResponse();
                        RequestTracer  perfTracer = AppInfo.GetAppInfo().OsContext.RequestTracer;
                        resp.AddToJs("OsSetRequestInfo('{0}','{1}','{2}')".F(perfTracer.RequestKey, perfTracer.EntryActionName, perfTracer.EntryActionKey));
                        response.Write(JavascriptBeginTag + FormatJSONResponseString(resp) + JavascriptEndTag);
                    } catch (Exception e) {
                        ErrorLog.LogApplicationError(e, null, "WebWidgets");
                    }
                    IsFirstAjaxResponse = false;
                }

                // This padding ensures that, if we are using a compression filter, that the output
                // is completely flushed to the underlying stream.
                // It is needed because the compression may flush every but the last few bytes.
                // Usually in a compressed flush the last one or two bytes will be missing.
                string padding = "<!-- OSPADDING -->";

                // write the respose and its end tag
                response.Write(JavascriptBeginTag + script + JavascriptEndTag);
                if (RuntimePlatformUtils.IsJava() || flush)
                {
                    response.Flush();
                }
                response.Write(padding);
                if (RuntimePlatformUtils.IsJava() || flush)
                {
                    //the padding goes on a separate chunk to make sure the actual content
                    //get's flushed to the client
                    response.Flush();
                }
            } catch {
                // Ignore errors writting the response (eg: client closed tab)
            }
        }