Ejemplo n.º 1
0
        private static FieldControl CreateFieldControlByHint(string hint)
        {
            FieldControl control = null;

            var designator = hint.Split(new char[] { ':' });

            if (designator.Count() != 2)
            {
                SnLog.WriteWarning($"Malformed field control hint: {hint}, falling back to default.");
                return(null);
            }

            var namespaces = from TagPrefixInfo tag in TagPrefixInfos
                             where tag.TagPrefix.Equals(designator[0], StringComparison.InvariantCultureIgnoreCase)
                             select tag.Namespace;

            var controlType = namespaces
                              .Select(ns => TypeResolver.GetType(string.Concat(ns, ".", designator[1]), false))
                              .FirstOrDefault(t => t != null);

            if (controlType != null)
            {
                control = (FieldControl)Activator.CreateInstance(controlType);
            }

            if (control == null)
            {
                SnLog.WriteWarning($"Failed to instantiate field control by hint: {hint}, falling back to default.");
            }

            return(control);
        }
Ejemplo n.º 2
0
        private static void Load()
        {
            foreach (var type in TypeResolver.GetTypesByInterface(typeof(IEvaluator)))
            {
                try
                {
                    if (!(type.GetCustomAttributes(typeof(ScriptTagNameAttribute), false).FirstOrDefault() is ScriptTagNameAttribute tagAttribute))
                    {
                        SnLog.WriteWarning($"Evaluator does not have a ScriptTagNameAttribute: {type.FullName} " +
                                           $"(Assembly: {type.Assembly})");
                        continue;
                    }

                    var fullTagName = GetFullTagName(tagAttribute.TagName);

                    // check if we already have an evaluator for this tag
                    if (Providers.Instance.GetProvider <IEvaluator>(fullTagName) != null)
                    {
                        continue;
                    }

                    var engine = (IEvaluator)Activator.CreateInstance(type);

                    Providers.Instance.SetProvider(fullTagName, engine);

                    SnLog.WriteInformation("Evaluator loaded: " + tagAttribute.TagName + ": " + engine);
                }
                catch (Exception ex)
                {
                    SnLog.WriteException(ex, $"Error loading script evaluator class. {type.AssemblyQualifiedName}");
                }
            }
        }
Ejemplo n.º 3
0
        private Dictionary <string, FieldDescriptor> ParseContentTypeElement(XPathNavigator contentTypeElement, IXmlNamespaceResolver nsres)
        {
            Dictionary <string, FieldDescriptor> result = null;

            foreach (XPathNavigator subElement in contentTypeElement.SelectChildren(XPathNodeType.Element))
            {
                switch (subElement.LocalName)
                {
                case "DisplayName":
                    _displayName = subElement.Value;
                    break;

                case "Description":
                    _description = subElement.Value;
                    break;

                case "Icon":
                    _icon = subElement.Value;
                    break;

                case "Fields":
                    result = ParseFieldElements(subElement, nsres);
                    break;

                case "Actions":
                    SnLog.WriteWarning("Ignoring obsolete Actions element in List definition: " + this.Name);
                    break;

                default:
                    throw new NotSupportedException(String.Concat("Unknown element in ContentListDefinition: ", subElement.LocalName));
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        private static void LogLine(string msg, EventType eventType)
        {
            switch (eventType)
            {
            case EventType.Error:
                Logger.WriteError(EventId.NotDefined, msg, AdSyncLogCategory);
                break;

            case EventType.Warning:
                SnLog.WriteWarning(msg, categories: AdSyncLogCategory);
                break;

            case EventType.Info:
                SnLog.WriteInformation(msg, categories: AdSyncLogCategory);
                break;

            case EventType.Verbose:
                Logger.WriteVerbose(string.Format("  {0}", msg), AdSyncLogCategory);
                break;
            }

            // log event for subscriber of the current thread
            StringBuilder sb;

            if (Subscribers.TryGetValue(Thread.CurrentThread.GetHashCode(), out sb))
            {
                if (sb != null)
                {
                    sb.AppendLine(GetMsgWithTimeStamp(msg));
                }
            }
        }
Ejemplo n.º 5
0
        protected override void CreateChildControls()
        {
            if (Cacheable && CanCache && IsInCache)
            {
                return;
            }

            // TODO: later this portlet should only generate a script request
            // instead of pouring everything into the html directly.

            var scriptText = string.Empty;

            try
            {
                scriptText = ClientContext.GenerateScript();
            }
            catch (Exception ex)
            {
                scriptText = "//// context error: " + SenseNet.ContentRepository.Security.Sanitizer.Sanitize(ex.Message);

                SnLog.WriteWarning("Error during client context generation: " + ex, EventId.ClientEvent);
            }

            this.Controls.Clear();
            this.Controls.Add(new LiteralControl($"<script>SN.Context = {scriptText}</script>"));
        }
Ejemplo n.º 6
0
        private static ActionBase CreateActionWithPermissions(Application app, Content context, string backUri, object parameters)
        {
            if (app == null)
            {
                return(null);
            }

            ActionBase action = null;

            try
            {
                action = app.CreateAction(context, backUri, parameters);
            }
            catch (InvalidContentActionException ex)
            {
                if (ex.Reason != InvalidContentActionReason.UnknownAction)
                {
                    throw;
                }
                SnLog.WriteWarning("Application content refers to an unknown action class.", EventId.ActionFramework
                                   , properties: new Dictionary <string, object> {
                    { "ActionTypeName", app.ActionTypeName }, { "Path", app.Path }
                });
            }
            if (action == null)
            {
                return(null);
            }

            CheckRequiredPermissions(action, context);

            return(action);
        }
Ejemplo n.º 7
0
        internal static void CheckComponentVersions(SnComponentInfo[] components, bool release)
        {
            foreach (var component in components)
            {
                if (string.IsNullOrEmpty(component.ComponentId))
                {
                    SnLog.WriteWarning($"Component class {component.GetType().FullName} is invalid, it does not provide a ComponentId.");
                    continue;
                }

                var componentVersion = Instance.Components.FirstOrDefault(c => c.ComponentId == component.ComponentId)?.Version;

                if (IsComponentAllowed(component, componentVersion))
                {
                    SnTrace.System.Write($"Component {component.ComponentId} is allowed to run (version: {componentVersion})");
                    continue;
                }

                if (release)
                {
                    throw new ApplicationException($"Component and assembly version mismatch. Component {component.ComponentId} is not allowed to run."
                                                   + $" Installed version: {componentVersion}, expected minimal version: {component.SupportedVersion}."
                                                   + " Please check assembly versions and available ugrades before starting the repository.");
                }

                SnTrace.System.Write($"Component {component.ComponentId} is allowed to run only in the DEBUG mode."
                                     + $" Installed version: {componentVersion}, expected minimal version: {component.SupportedVersion}.");
            }
        }
Ejemplo n.º 8
0
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try
            {
                this.InitTemplates();
            }
            catch (Exception ex)
            {
                SnLog.WriteWarning("Error during field control init. " + ex.Message,
                                   EventId.Portal,
                                   properties: new Dictionary <string, object>
                {
                    { "Field", _fieldName },
                    { "Content", this.Content == null ? "" : this.Content.Path }
                });
            }

            SetTitleAndDescription();

            if (_field == null)
            {
                return;
            }
            SetDataInternal();
        }
Ejemplo n.º 9
0
        public static void CheckComponentVersions()
        {
            //TODO: have a pinned list of components in the Providers class
            // so that the instances can be replaced by tests.
            foreach (var componentType in TypeResolver.GetTypesByInterface(typeof(ISnComponent)).Where(vct => !vct.IsAbstract))
            {
                var component = TypeResolver.CreateInstance(componentType.FullName) as ISnComponent;
                if (component == null)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(component.ComponentId))
                {
                    SnLog.WriteWarning($"Component class {component.GetType().FullName} is invalid, it does not provide a ComponentId.");
                    continue;
                }

                var componentVersion = Instance.Components.FirstOrDefault(c => c.ComponentId == component.ComponentId)?.Version;

                if (component.IsComponentAllowed(componentVersion))
                {
                    SnTrace.System.Write($"Component {component.ComponentId} is allowed to run (version: {componentVersion})");
                    continue;
                }

                throw new ApplicationException($"Component and assembly version mismatch. Component {component.ComponentId} (version: {componentVersion}) is not allowed to run. Please check assembly versions and available ugrades before starting the repository.");
            }
        }
Ejemplo n.º 10
0
        protected string ExtractiFilter(Stream stream, out bool success)
        {
            success = true;

            try
            {
                // extract text using IFilter
                return(SnIFilter.GetText(stream, ".pdf"));
            }
            catch (OutOfMemoryException ex)
            {
                SnLog.WriteWarning("Pdf text extract failed with out of memory exception. " + ex,
                                   EventId.Indexing,
                                   properties: new Dictionary <string, object> {
                    { "Stream size", stream.Length }
                });
            }
            catch (Exception ex)
            {
                // log iFilter error only once
                if (!_iFilterErrorLogged)
                {
                    SnLog.WriteWarning("Pdf IFilter error: " + ex.Message, EventId.Indexing);
                    _iFilterErrorLogged = true;
                }
            }

            success = false;

            return(string.Empty);
        }
Ejemplo n.º 11
0
        public override string GetFormattedValue()
        {
            var val    = Convert.ToDecimal(this.GetData());
            var fs     = this.FieldSetting as CurrencyFieldSetting;
            var digits = Math.Min(fs == null || !fs.Digits.HasValue ? 0 : fs.Digits.Value, 29);

            try
            {
                if (fs != null && !string.IsNullOrEmpty(fs.Format))
                {
                    var cultForField = CultureInfo.GetCultureInfo(fs.Format);
                    var cultCurrent  = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

                    cultCurrent.NumberFormat.CurrencySymbol          = cultForField.NumberFormat.CurrencySymbol;
                    cultCurrent.NumberFormat.CurrencyPositivePattern = cultForField.NumberFormat.CurrencyPositivePattern;
                    cultCurrent.NumberFormat.CurrencyGroupSeparator  = cultForField.NumberFormat.CurrencyGroupSeparator;

                    return(val.ToString("C" + digits, cultCurrent));
                }
            }
            catch (Exception ex)
            {
                SnLog.WriteWarning(ex);
            }

            return(base.GetFormattedValue());
        }
Ejemplo n.º 12
0
        public void Write(ClientLogEntry entry)
        {
            switch (entry.Severity)
            {
            case TraceEventType.Critical:
            case TraceEventType.Error:
                SnLog.WriteError(entry.Message, entry.EventId, entry.Categories, entry.Priority, entry.Title, entry.Properties);
                break;

            case TraceEventType.Warning:
                SnLog.WriteWarning(entry.Message, entry.EventId, entry.Categories, entry.Priority, entry.Title, entry.Properties);
                break;

            case TraceEventType.Verbose:
                // do nothing: verbose log should be written using SnTrace
                break;

            case TraceEventType.Information:
                SnLog.WriteInformation(entry.Message, entry.EventId, entry.Categories, entry.Priority, entry.Title, entry.Properties);
                break;

            default:
                SnLog.WriteInformation(entry.Severity + ": " + entry.Message, entry.EventId, entry.Categories, entry.Priority, entry.Title, entry.Properties);
                break;
            }
        }
Ejemplo n.º 13
0
        internal static WopiDiscovery GetInstance(string officeOnlineUrl)
        {
            return(Instances.GetOrAdd(officeOnlineUrl.TrimEnd('/'), oosUrl => new Lazy <WopiDiscovery>(() =>
            {
                var discoveryXml = new XmlDocument();

                Retrier.Retry(3, 500, () =>
                {
                    using (var client = new HttpClient())
                    {
                        using (var discoveryStream = client.GetAsync($"{oosUrl}/hosting/discovery")
                                                     .GetAwaiter().GetResult().Content.ReadAsStreamAsync().GetAwaiter().GetResult())
                        {
                            discoveryXml.Load(discoveryStream);
                        }
                    }
                }, (i, ex) => ex == null || i > 3);

                if (discoveryXml.DocumentElement == null)
                {
                    SnLog.WriteWarning($"Could not connect to Office Online Server {oosUrl} for available actions.");
                }
                else
                {
                    SnLog.WriteInformation($"Connected to Office Online Server {oosUrl} for available actions.");
                }


                return FromXmlDocument(discoveryXml);
            })).Value);
        }
Ejemplo n.º 14
0
        private static void InitializeOAuthProviders()
        {
            var providerTypeNames = new List <string>();

            foreach (var providerType in TypeResolver.GetTypesByInterface(typeof(IOAuthProvider)).Where(t => !t.IsAbstract))
            {
                if (!(TypeResolver.CreateInstance(providerType.FullName) is IOAuthProvider provider))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(provider.ProviderName))
                {
                    SnLog.WriteWarning($"OAuth provider type {providerType.FullName} does not expose a valid ProviderName value, therefore cannot be initialized.");
                    continue;
                }
                if (string.IsNullOrEmpty(provider.IdentifierFieldName))
                {
                    SnLog.WriteWarning($"OAuth provider type {providerType.FullName} does not expose a valid IdentifierFieldName value, therefore cannot be initialized.");
                    continue;
                }

                Providers.Instance.SetProvider(OAuthProviderTools.GetProviderRegistrationName(provider.ProviderName), provider);
                providerTypeNames.Add($"{providerType.FullName} ({provider.ProviderName})");
            }

            if (providerTypeNames.Any())
            {
                SnLog.WriteInformation("OAuth providers registered: " + Environment.NewLine +
                                       string.Join(Environment.NewLine, providerTypeNames));
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// This method deletes orphaned rows from the database physically.
        /// </summary>
        private async Task CleanupFilesDeleteRowsAsync(CancellationToken cancellationToken)
        {
            var deleteCount = 0;

            try
            {
                SnTrace.Database.Write(SnMaintenance.TracePrefix + "Cleanup files: deleting rows...");

                // keep deleting orphaned binary rows while there are any
                while (await BlobStorage.CleanupFilesAsync(cancellationToken).ConfigureAwait(false))
                {
                    deleteCount++;
                }

                if (deleteCount > 0)
                {
                    SnLog.WriteInformation($"{deleteCount} orphaned rows were deleted from the binary table during cleanup.",
                                           EventId.RepositoryRuntime);
                }
            }
            catch (Exception ex)
            {
                SnLog.WriteWarning("Error in file cleanup background process. " + ex, EventId.RepositoryRuntime);
            }
        }
Ejemplo n.º 16
0
        public static void RemoveUnnecessaryDirectories()
        {
            var root = StorageContext.Search.IndexDirectoryPath;

            if (!System.IO.Directory.Exists(root))
            {
                return;
            }
            var unnecessaryDirs = System.IO.Directory.GetDirectories(root)
                                  .Where(a => Char.IsDigit(System.IO.Path.GetFileName(a)[0]))
                                  .OrderByDescending(s => s)
                                  .Skip(2).Where(x => Deletable(x));

            foreach (var dir in unnecessaryDirs)
            {
                try
                {
                    System.IO.Directory.Delete(dir, true);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(String.Concat("Cannot delete the directory: ", dir, ", ", e.Message));
                    SnLog.WriteWarning("Cannot delete the directory: " + dir, properties: new Dictionary <string, object> {
                        { "Reason", e.Message }, { "StackTrace", e.StackTrace }
                    });
                }
            }
        }
Ejemplo n.º 17
0
        internal static void Shutdown()
        {
            if (_instance == null)
            {
                _started = false;

                SnLog.WriteWarning("Repository shutdown has already completed.");
                return;
            }

            lock (_startStopSync)
            {
                if (_instance == null)
                {
                    _started = false;

                    SnLog.WriteWarning("Repository shutdown has already completed.");
                    return;
                }

                SnTrace.Repository.Write("Sending a goodbye message.");

                _instance.ConsoleWriteLine();

                _instance.ConsoleWriteLine("Sending a goodbye message...");
                DistributedApplication.ClusterChannel.ClusterMemberInfo.NeedToRecover = false;
                var pingMessage = new PingMessage();
                pingMessage.SendAsync(CancellationToken.None).GetAwaiter().GetResult();

                foreach (var svc in _instance.serviceInstances)
                {
                    SnTrace.Repository.Write("Shutting down {0}", svc.GetType().Name);
                    svc.Shutdown();
                }

                SnTrace.Repository.Write("Shutting down {0}", DistributedApplication.ClusterChannel.GetType().Name);
                DistributedApplication.ClusterChannel.ShutDownAsync(CancellationToken.None).GetAwaiter().GetResult();

                SnTrace.Repository.Write("Shutting down Security.");
                SecurityHandler.ShutDownSecurity();

                SnTrace.Repository.Write("Shutting down IndexingEngine.");
                IndexManager.ShutDown();

                ContextHandler.Reset();

                var t   = DateTime.UtcNow - _instance._startupInfo.Starting;
                var msg = $"Repository has stopped. Running time: {t.Days}.{t.Hours:d2}:{t.Minutes:d2}:{t.Seconds:d2}";

                SnTrace.Repository.Write(msg);
                SnTrace.Flush();

                _instance.ConsoleWriteLine(msg);
                _instance.ConsoleWriteLine();
                SnLog.WriteInformation(msg);

                _instance = null;
                _started  = false;
            }
        }
Ejemplo n.º 18
0
        public void EventLogger_Acceptance()
        {
            var logger = new EventLoggerForTests();
            var loggerBackup = SnLog.Instance;
            SnLog.Instance = logger;
            try
            {
                var thisMethodName = MethodBase.GetCurrentMethod().Name;
                var information = (int)TraceEventType.Information;
                var warning = (int)TraceEventType.Warning;

                SnLog.WriteInformation(thisMethodName + "_INFOMESSAGE", 42, null, 61, thisMethodName + "_TITLE");
                SnLog.WriteWarning(thisMethodName + "_WARNINGMESSAGE", 43, null, 61, thisMethodName + "_TITLE");

                var expected =
                    $"{{\"Message\":\"{thisMethodName}_INFOMESSAGE\",\"Categories\":[],\"Priority\":61,\"EventId\":42,\"Severity\":{information},\"Title\":\"{thisMethodName}_TITLE\",\"Properties\":{{}}}}" + Environment.NewLine +
                    $"{{\"Message\":\"{thisMethodName}_WARNINGMESSAGE\",\"Categories\":[],\"Priority\":61,\"EventId\":43,\"Severity\":{warning},\"Title\":\"{thisMethodName}_TITLE\",\"Properties\":{{}}}}";
                var actual = string.Join(Environment.NewLine, logger.Entries.Select(x => x.ToString()));
                Assert.AreEqual(expected, actual);
            }
            finally
            {
                SnLog.Instance = loggerBackup;
            }
        }
Ejemplo n.º 19
0
        // ================================================================================= Helper methods

        private SenseNetPerformanceCounter GetCounter(string counterName)
        {
            if (string.IsNullOrEmpty(counterName))
            {
                throw new ArgumentNullException("counterName");
            }

            var counter = CounterManager.Current.Counters.FirstOrDefault(c => c.CounterName == counterName);

            if (counter == null)
            {
                if (_invalidCounters.ContainsKey(counterName))
                {
                    return(null);
                }

                lock (_counterLockObject)
                {
                    if (!_invalidCounters.ContainsKey(counterName))
                    {
                        _invalidCounters.Add(counterName, true);

                        SnLog.WriteWarning("Performance counter does not exist: " + counterName);
                    }
                }
            }

            return(counter);
        }
Ejemplo n.º 20
0
        private static void CheckCulture()
        {
            var calendar = Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar;

            if (calendar.MinSupportedDateTime <= _dateTimeMinValue && calendar.MaxSupportedDateTime >= _dateTimeMaxValue)
            {
                return;
            }

            var logMsg = Thread.CurrentThread.CurrentCulture.Name + ": " + calendar + " is changed to ";

            Calendar[] optCals = Thread.CurrentThread.CurrentCulture.OptionalCalendars;
            foreach (var cal in optCals)
            {
                if (cal.MinSupportedDateTime <= _dateTimeMinValue && cal.MaxSupportedDateTime >= _dateTimeMaxValue)
                {
                    Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar   = cal;
                    Thread.CurrentThread.CurrentUICulture.DateTimeFormat.Calendar = cal;
                    SnLog.WriteInformation(logMsg + cal);
                    return;
                }
            }
            SnLog.WriteWarning("This locale cannot be used: " + Thread.CurrentThread.CurrentCulture.Name + ". Current culture is assigned to invariant culture.");
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
        }
Ejemplo n.º 21
0
        protected NodeEnumerator(string path, ExecutionHint executionHint, string filter, int?depth)
        {
            RootPath        = path;
            _currentLevel   = new Stack <int[]>();
            _currentIndices = new Stack <int>();
            _hint           = executionHint;
            _depth          = depth.HasValue ? Math.Max(1, depth.Value) : depth;
            if (filter != null)
            {
                if (filter.Length == 0)
                {
                    filter = null;
                }
                else if (filter.StartsWith("<"))
                {
                    SnLog.WriteWarning(
                        "NodeEnumerator cannot be initialized with filter that is a NodeQuery. Use content query text instead.",
                        properties: new Dictionary <string, object> {
                        { "InvalidFilter", filter }
                    });

                    filter = null;
                }
            }
            _filter = filter;
        }
Ejemplo n.º 22
0
        static ContentNamingProvider()
        {
            var className = Providers.ContentNamingProviderClassName;

            ContentNamingProvider instance = null;

            if (string.IsNullOrEmpty(className))
            {
                instance = new CharReplacementContentNamingProvider();
            }
            else
            {
                try
                {
                    instance = (ContentNamingProvider)TypeResolver.CreateInstance(className);
                }
                catch (Exception)
                {
                    SnLog.WriteWarning("Error loading ContentNamingProvider type: " + className, EventId.RepositoryLifecycle);
                }
            }

            if (instance == null)
            {
                instance = new CharReplacementContentNamingProvider();
            }

            SnLog.WriteInformation("ContentNamingProvider created: " + instance);

            __instance = instance;
        }
        private void MergeExternalData(User user, string email, string fullName, string externalProviderData,
                                       string provider, string userId)
        {
            var userContent         = Content.Create(user);
            var currentExternalData = (string)userContent["ExternalUserProviders"];

            if (string.IsNullOrEmpty(currentExternalData))
            {
                // no previous external data, simply set it
                SaveNewExternalData(externalProviderData);
            }
            else
            {
                var currentExternalJObject = JsonConvert.DeserializeObject <JObject>(currentExternalData ?? string.Empty);
                if (currentExternalJObject.ContainsKey(provider))
                {
                    // provider exists in previous external data
                    var currentProviderId = currentExternalJObject[provider]["Id"]?.Value <string>() ?? string.Empty;
                    if (string.IsNullOrEmpty(currentProviderId))
                    {
                        // the id is empty for some reason, simply set it
                        currentExternalJObject[provider]["Id"] = userId;

                        SaveNewExternalData(currentExternalJObject.ToString());
                    }
                    else if (!string.Equals(currentProviderId, userId))
                    {
                        // This should not happen: we've found a user with the same email and provider
                        // but with a different user id.
                        SnLog.WriteWarning($"CreateProviderUser: found an existing user with a matching email but non-matching " +
                                           $"provider id. Provider: {provider} Email: {email} Current id: {currentProviderId}" +
                                           $"New id: {userId}");

                        throw new InvalidOperationException($"Ambiguous user id for user {email} and provider {provider}");
                    }

                    // otherwise: the correct provider and id is already set, move on silently
                }
                else
                {
                    // merge the new provider to the existing list
                    var newExternalJObject = JsonConvert.DeserializeObject <JObject>(externalProviderData);
                    currentExternalJObject.Merge(newExternalJObject);

                    SaveNewExternalData(currentExternalJObject.ToString());
                }
            }

            void SaveNewExternalData(string externalData)
            {
                userContent["ExternalUserProviders"] = externalData;
                if (!string.IsNullOrEmpty(fullName))
                {
                    userContent["FullName"] = fullName;
                }

                userContent.SaveSameVersion();
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Returns a setting value by the given key of the specified <see cref="Settings"/>.
        /// </summary>
        /// <typeparam name="T">Type of the return value</typeparam>
        /// <param name="settingsName">Name of the <see cref="Settings"/> (e.g. Indexing or Portal).</param>
        /// <param name="key">The name of the requested value.</param>
        /// <param name="contextPath">The content where the search for the settings will start.</param>
        /// <param name="defaultValue">Value if the "value" is null.</param>
        public static T GetValue <T>(string settingsName, string key, string contextPath = null, T defaultValue = default(T))
        {
            using (new SystemAccount())
            {
                // load the settings file
                Settings settingsFile;

                settingsFile = GetSettingsByName <Settings>(settingsName, contextPath);

                // file not found, even in the global folder
                if (settingsFile == null)
                {
                    SnLog.WriteWarning("Settings file not found: " + settingsName + "." + EXTENSION);
                    return(defaultValue);
                }

                // Try to get setting value from cache
                object settingValue;
                if (settingsFile.SettingValues.TryGetValue(key, out settingValue))
                {
                    return(ConvertSettingValue <T>(settingValue, defaultValue));
                }

                // Load the value from the Binary (xml or json format): this method should return a value
                // that is already converted to type 'T' from string, otherwise the received default value.
                bool found;
                settingValue = settingsFile.GetValueFromBinary(key, defaultValue, out found);

                // the value was found on the settings file
                if (found)
                {
                    settingValue = ConvertSettingValue <T>(settingValue, defaultValue);
                    settingsFile.AddValueToCache(key, settingValue);
                    return((T)settingValue);
                }

                // load the value from a content field if possible
                var settingsContent = Content.Create(settingsFile);
                if (settingsContent.Fields.ContainsKey(key))
                {
                    // NOTE: no need to add to cache here, we suppose that the content fields are already in the memory
                    //       (also, the dynamic fields of Settings are added to the cache in GetProperty)

                    settingValue = ConvertSettingValue <T>(settingsContent[key], defaultValue);
                    return((T)settingValue);
                }

                // if this is a local setting, try to find the value upwards
                if (!settingsFile.Path.StartsWith(SETTINGSCONTAINERPATH))
                {
                    // find the path above the settings folder
                    var newPath = RepositoryPath.GetParentPath(GetParentContextPath(settingsFile.Path));
                    return(GetValue(settingsName, key, newPath, defaultValue));
                }

                return(defaultValue);
            }
        }
Ejemplo n.º 25
0
 private static void WriteFeatureWarning()
 {
     if (_isFeatureWarningWritten)
     {
         return;
     }
     SnLog.WriteWarning("Exclusive lock feature is not available.");
     _isFeatureWarningWritten = true;
 }
Ejemplo n.º 26
0
        protected void WriteActions(XmlWriter writer, string path, IEnumerable <ActionBase> actions)
        {
            if (actions == null)
            {
                return;
            }
            if (actions.Count() == 0)
            {
                return;
            }

            writer.WriteStartElement("Actions");
            foreach (var action in actions)
            {
                try
                {
                    if (action.Active)
                    {
                        // if the name does not follow the standard xml element name rules, we must log this and skip it
                        if (!Regex.IsMatch(action.Name, XML_ELEMENTNAME_REGEX))
                        {
                            SnLog.WriteWarning("Content actions serialization error: invalid xml element. Path: " + path + ". Action name: " + action.Name);
                            continue;
                        }

                        if (action.IncludeBackUrl)
                        {
                            writer.WriteElementString(action.Name, action.Uri);
                        }
                        else
                        {
                            var actionUrl    = action.Uri;
                            var urlSeparator = (actionUrl != null && actionUrl.Contains("?")) ? "&" : "?";
                            var back         = $"{urlSeparator}{ActionBase.BackUrlParameterName}={action.BackUri}";

                            writer.WriteStartElement(action.Name);
                            writer.WriteAttributeString(ActionBase.BackUrlParameterName, back);
                            writer.WriteString(actionUrl);
                            writer.WriteEndElement();
                        }
                    }
                }
                catch (Exception ex)
                {
                    // log exception, but continue writing actions
                    SnLog.WriteException(ex);

                    // no point in continuing the process if the writer is closed
                    if (writer.WriteState == WriteState.Error || writer.WriteState == WriteState.Closed)
                    {
                        break;
                    }
                }
            }

            writer.WriteEndElement();
        }
Ejemplo n.º 27
0
        private T GetValueFromJsonInternal <T>(JToken token, string key)
        {
            if (token == null)
            {
                return(default(T));
            }

            // Get the value from the virtual method that returns null in the default implementation.
            // If a custom inheritor implements this method, it can override the default conversion
            // behavior implemented below.
            var convertedValue = GetValueFromJson(token, key);

            if (convertedValue != null)
            {
                return((T)convertedValue);
            }

            // get the value from the json token
            try
            {
                var tt = typeof(T);

                // check for Enum type
                if (tt.IsEnum)
                {
                    return((T)Enum.Parse(tt, token.Value <string>(), true));
                }

                // check for Array type
                var jArray = token as JArray;
                if (jArray != null)
                {
                    if (!typeof(IEnumerable).IsAssignableFrom(tt))
                    {
                        throw new InvalidOperationException(string.Format("Cannot convert a JArray to {0}.", tt.FullName));
                    }

                    return(jArray.ToObject <T>());
                }

                // handle custom objects
                if (token is JObject)
                {
                    return(token.ToObject <T>());
                }

                // any other type
                return(token.Value <T>());
            }
            catch (Exception ex)
            {
                SnLog.WriteWarning(
                    $"Error during setting value JSON conversion. Path: {this.Path}. Key: {key}. Expected type: {typeof(T).FullName}. Exception: {ex.Message}");
            }

            return(default(T));
        }
Ejemplo n.º 28
0
        public override string Extract(Stream stream, TextExtractorContext context)
        {
            try
            {
                // extract text using IFilter
                return(SnIFilter.GetText(stream, ".pdf"));
            }
            catch (OutOfMemoryException ex)
            {
                SnLog.WriteWarning("Pdf text extract failed with out of memory exception. " + ex,
                                   EventId.Indexing,
                                   properties: new Dictionary <string, object> {
                    { "Stream size", stream.Length }
                });

                return(string.Empty);
            }
            catch (Exception ex)
            {
                // log iFilter error only once
                if (!_iFilterErrorLogged)
                {
                    SnLog.WriteWarning("Pdf IFilter error: " + ex.Message, EventId.Indexing);
                    _iFilterErrorLogged = true;
                }
            }

            // fallback to the other mechanism in case the pdf IFilter is missing
            var text = new StringBuilder();

            try
            {
                var pdfReader = new PdfReader(stream);
                for (var page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    // extract text using the old version (4.1.6) of iTextSharp
                    var pageText = ExtractTextFromPdfBytes(pdfReader.GetPageContent(page));
                    if (string.IsNullOrEmpty(pageText))
                    {
                        continue;
                    }

                    text.Append(pageText);
                }
            }
            catch (OutOfMemoryException ex)
            {
                SnLog.WriteWarning("Pdf text extract failed with out of memory exception. " + ex,
                                   EventId.Indexing,
                                   properties: new Dictionary <string, object> {
                    { "Stream size", stream.Length }
                });
            }

            return(text.ToString());
        }
Ejemplo n.º 29
0
        // ============================================================ Helper methods

        private static void PreloadTypes()
        {
            try
            {
                var missingTypes = new List <string>();

                // preload types by name
                foreach (var typeName in TypesToPreloadByName)
                {
                    try
                    {
                        TypeResolver.GetType(typeName);
                    }
                    catch (TypeNotFoundException)
                    {
                        missingTypes.Add(typeName);
                    }
                }

                // preload types by base
                foreach (var typeName in TypesToPreloadByBase)
                {
                    try
                    {
                        TypeResolver.GetTypesByBaseType(TypeResolver.GetType(typeName));
                    }
                    catch (TypeNotFoundException)
                    {
                        missingTypes.Add(typeName);
                    }
                }

                // preload types by interface
                foreach (var typeName in TypesToPreloadByInterface)
                {
                    try
                    {
                        TypeResolver.GetTypesByInterface(TypeResolver.GetType(typeName));
                    }
                    catch (TypeNotFoundException)
                    {
                        missingTypes.Add(typeName);
                    }
                }

                if (missingTypes.Any())
                {
                    SnLog.WriteWarning("Types not found during warmup: " + string.Join(", ", missingTypes));
                }
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex);
            }
        }
Ejemplo n.º 30
0
        private static IEnumerable <CacheHeaderSetting> ParseCacheHeaderSettings(XmlNode xmlNode)
        {
            var cacheHeaderList = new List <CacheHeaderSetting>();

            foreach (XmlNode cacheHeaderNode in xmlNode.ChildNodes)
            {
                // skip text nodes
                if (!(cacheHeaderNode is XmlElement))
                {
                    continue;
                }

                var cacheHeader = new CacheHeaderSetting();
                var attrCt      = cacheHeaderNode.Attributes["ContentType"];
                var attrPath    = cacheHeaderNode.Attributes["Path"];
                var attrExt     = cacheHeaderNode.Attributes["Extension"];
                var attrMaxAge  = cacheHeaderNode.Attributes["MaxAge"];

                // if the value is not a real integer, skip this setting
                int value;
                if (attrMaxAge == null || !int.TryParse(attrMaxAge.Value, out value))
                {
                    continue;
                }

                cacheHeader.MaxAge = value;

                if (attrCt != null && !string.IsNullOrEmpty(attrCt.Value))
                {
                    cacheHeader.ContentType = attrCt.Value;
                }
                if (attrPath != null && !string.IsNullOrEmpty(attrPath.Value))
                {
                    cacheHeader.Path = attrPath.Value;
                }
                if (attrExt != null && !string.IsNullOrEmpty(attrExt.Value))
                {
                    cacheHeader.Extension = attrExt.Value.ToLower().Trim(new[] { ' ', '.' });
                }

                // if none of the above were set, skip this setting
                if (string.IsNullOrEmpty(cacheHeader.ContentType) &&
                    string.IsNullOrEmpty(cacheHeader.Path) &&
                    string.IsNullOrEmpty(cacheHeader.Extension))
                {
                    SnLog.WriteWarning("Empty client cache header setting found in Portal settings.");
                    continue;
                }

                cacheHeaderList.Add(cacheHeader);
            }

            return(cacheHeaderList);
        }