Example #1
0
        /// <summary>
        /// Die Daten eines Elements analysieren und daraus ein Objekt erstellen.
        /// </summary>
        /// <param name="id">Die ID des Element, zum Beispiel GR_389F860B088563B1.</param>
        /// <returns>Ein Objekt, das die Daten des Elements in Enable Now enthält</returns>
        internal async Task <Element> CrawlElementAsync(string id)
        {
            Element element = new(id);

            FillInitialFields(element);
            MetaDataCollection metaData = await MetaAnalyzer.LoadMetaFilesAsync(element);

            FillFields(element, metaData);
            AddAssets(element, metaData);
            string           autostartId      = GetAutostartId(metaData);
            StatisticService statisticService = StatisticService.GetService(_jobConfig.Id);

            if (autostartId != null)
            {
                try
                {
                    Element autostartElement = await CrawlElementAsync(autostartId);

                    OverwriteValuesByAutostartElement(element, autostartElement);
                    statisticService.IncreaseAutostartElementsCount();
                }
                catch
                {
                    _log.Warn(LocalizationService.FormatResourceString("ElementCrawlerMessage01"));
                }
            }
            element.Hash = element.GenerateHashCode();
            SetDateValue(element);
            statisticService.IncreaseFoundDocumentsCount();
            return(element);
        }
        /// <summary>
        /// Creates this query context instance that can be executed against the test server.
        /// </summary>
        /// <param name="subscriptionId">The subscription identifier to assign to the created sub.</param>
        /// <returns>GraphQueryContext.</returns>
        public virtual SubcriptionExecutionContext Build(string subscriptionId = null)
        {
            subscriptionId = subscriptionId ?? Guid.NewGuid().ToString();
            var metaData = new MetaDataCollection();

            // unchangable items about the request
            var request = new Mock <IGraphOperationRequest>();

            // updateable items about the request
            var context = new SubcriptionExecutionContext(
                _client,
                this.OperationRequest,
                subscriptionId,
                _metrics,
                _eventLogger,
                metaData);

            foreach (var kvp in _sourceData)
            {
                var mockField = new Mock <IGraphField>();
                mockField.Setup(x => x.FieldSource).Returns(GraphQL.AspNet.Internal.TypeTemplates.GraphFieldTemplateSource.Action);
                mockField.Setup(x => x.Route).Returns(kvp.Key);
                context.DefaultFieldSources.AddSource(mockField.Object, kvp.Value);
            }

            return(context);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FieldContextBuilder" /> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="user">The user.</param>
        /// <param name="graphField">The graph field.</param>
        /// <param name="schema">The schema.</param>
        /// <param name="graphMethod">The metadata describing the method/functon to be invoked by a resolver.</param>
        public FieldContextBuilder(
            IServiceProvider serviceProvider,
            ClaimsPrincipal user,
            IGraphField graphField,
            ISchema schema,
            IGraphMethod graphMethod)
        {
            _schema            = Validation.ThrowIfNullOrReturn(schema, nameof(schema));
            _graphField        = Validation.ThrowIfNullOrReturn(graphField, nameof(graphField));
            _user              = Validation.ThrowIfNullOrReturn(user, nameof(user));
            _messageCollection = new GraphMessageCollection();

            this.ServiceProvider = Validation.ThrowIfNullOrReturn(serviceProvider, nameof(serviceProvider));

            Type expectedInputType = null;

            if (!Validation.IsCastable <GraphDirective>(graphMethod.Parent.ObjectType) &&
                !Validation.IsCastable <GraphController>(graphMethod.Parent.ObjectType))
            {
                expectedInputType = graphMethod.Parent.ObjectType;
            }

            var metaData = new MetaDataCollection();

            _mockRequest           = new Mock <IGraphFieldRequest>();
            _mockInvocationContext = new Mock <IGraphFieldInvocationContext>();

            // fake the request for the field data (normally generated by the primary query exeuction context)
            var id = Guid.NewGuid().ToString("N");

            _mockRequest.Setup(x => x.Id).Returns(id);
            _mockRequest.Setup(x => x.Origin).Returns(SourceOrigin.None);
            _mockRequest.Setup(x => x.Items).Returns(metaData);
            _mockRequest.Setup(x => x.Field).Returns(_graphField);
            _mockRequest.Setup(x => x.InvocationContext).Returns(_mockInvocationContext.Object);

            _mockInvocationContext.Setup(x => x.ExpectedSourceType).Returns(expectedInputType);
            _mockInvocationContext.Setup(x => x.Field).Returns(_graphField);
            _mockInvocationContext.Setup(x => x.Arguments).Returns(_arguments);
            _mockInvocationContext.Setup(x => x.Name).Returns(_graphField.Name);
            _mockInvocationContext.Setup(x => x.Directives).Returns(new List <IDirectiveInvocationContext>());
            _mockInvocationContext.Setup(x => x.ChildContexts).Returns(new FieldInvocationContextCollection());
            _mockInvocationContext.Setup(x => x.Origin).Returns(SourceOrigin.None);

            this.GraphMethod = new Mock <IGraphMethod>();
            this.GraphMethod.Setup(x => x.Parent).Returns(graphMethod.Parent);
            this.GraphMethod.Setup(x => x.ObjectType).Returns(graphMethod.ObjectType);
            this.GraphMethod.Setup(x => x.ExpectedReturnType).Returns(graphMethod.ExpectedReturnType);
            this.GraphMethod.Setup(x => x.Method).Returns(graphMethod.Method);
            this.GraphMethod.Setup(x => x.IsAsyncField).Returns(graphMethod.IsAsyncField);
            this.GraphMethod.Setup(x => x.Name).Returns(graphMethod.Name);
            this.GraphMethod.Setup(x => x.InternalFullName).Returns(graphMethod.InternalFullName);
            this.GraphMethod.Setup(x => x.InternalName).Returns(graphMethod.InternalName);
            this.GraphMethod.Setup(x => x.Route).Returns(graphMethod.Route);
            this.GraphMethod.Setup(x => x.Arguments).Returns(graphMethod.Arguments);
        }
Example #4
0
        /// <summary>
        /// Creates this query context instance that can be executed against the test server.
        /// </summary>
        /// <returns>GraphQueryContext.</returns>
        public GraphQueryExecutionContext Build()
        {
            var metaData = new MetaDataCollection();

            // unchangable items about the request
            var request = new Mock <IGraphOperationRequest>();

            // updateable items about the request
            return(new GraphQueryExecutionContext(this.OperationRequest, _serviceProvider, _user, _metrics, _eventLogger, metaData));
        }
Example #5
0
        public void MergingCollectionsWithNull_HasNoEffect()
        {
            var collection = new MetaDataCollection();

            collection.TryAdd("key1", "value1");

            collection.Merge(null);

            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual("value1", collection["key1"]);
        }
Example #6
0
        /// <summary>
        /// Die Autostart-ID eines Elements bestimmen.
        /// </summary>
        /// <param name="metaData"></param>
        /// <returns>Gibt entweder die ID des Autostart-Elements zurück oder null, falls kein Autostart-Element definiert ist.</returns>
        private string GetAutostartId(MetaDataCollection metaData)
        {
            Config config = ConfigManager.GetConfigManager().ConnectorConfig;

            if (metaData.Entity?[config.AutostartIdentifier] == null)
            {
                return(null);
            }

            return(metaData.Entity[config.AutostartIdentifier]?.Value <string>().Split('!')[1]);
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphQueryExecutionContext" /> class.
 /// </summary>
 /// <param name="request">The request to be processed through the query pipeline.</param>
 /// <param name="serviceProvider">The service provider passed on the HttpContext.</param>
 /// <param name="user">The user authenticated by the Asp.net runtime.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 public GraphQueryExecutionContext(
     IGraphOperationRequest request,
     IServiceProvider serviceProvider,
     ClaimsPrincipal user = null,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
     : base(serviceProvider, user, metrics, logger, items)
 {
     this.Request      = Validation.ThrowIfNullOrReturn(request, nameof(request));
     this.FieldResults = new List <GraphDataItem>();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SubcriptionExecutionContext" /> class.
 /// </summary>
 /// <param name="client">The client.</param>
 /// <param name="request">The request to be processed through the query pipeline.</param>
 /// <param name="subscriptionId">The unique id to assign to the created subscription, when one is made.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 public SubcriptionExecutionContext(
     ISubscriptionClientProxy client,
     IGraphOperationRequest request,
     string subscriptionId,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
     : base(request, client?.ServiceProvider, client?.User, metrics, logger, items)
 {
     this.Client         = Validation.ThrowIfNullOrReturn(client, nameof(client));
     this.SubscriptionId = Validation.ThrowIfNullWhiteSpaceOrReturn(subscriptionId, nameof(subscriptionId));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseGraphMiddlewareContext" /> class.
 /// </summary>
 /// <param name="serviceProvider">The service provider passed on the HttpContext.</param>
 /// <param name="user">The user authenticated by the Asp.net runtime.</param>
 /// <param name="metrics">The metrics package to profile this request, if any.</param>
 /// <param name="logger">The logger instance to record events related to this context.</param>
 /// <param name="items">A key/value pair collection for random access data.</param>
 protected BaseGraphMiddlewareContext(
     IServiceProvider serviceProvider,
     ClaimsPrincipal user = null,
     IGraphQueryExecutionMetrics metrics = null,
     IGraphEventLogger logger            = null,
     MetaDataCollection items            = null)
 {
     this.ServiceProvider = Validation.ThrowIfNullOrReturn(serviceProvider, nameof(serviceProvider));
     this.User            = user;
     this.Metrics         = metrics;
     this.Items           = items ?? new MetaDataCollection();
     this.Messages        = new GraphMessageCollection();
     this.Logger          = logger;
 }
Example #10
0
        /// <summary>
        /// Reads the MetaData.
        /// </summary>
        /// <param name="type">The Type.</param>
        /// <returns>MetaDataCollection.</returns>
        public static MetaDataCollection ReadMetaData(Type type)
        {
            MetaDataAttribute[] data = AttributeHelper.GetAttributes<MetaDataAttribute>(type);
            var dataCollection = new MetaDataCollection();

            foreach (MetaDataAttribute metaData in data)
            {
                if (dataCollection.ContainsKey(metaData.Name)) continue;

                dataCollection.Add(metaData.Name, metaData.Data);
            }

            return dataCollection;
        }
Example #11
0
        public void MergingCollectionsWithSharedKeys_KeysInCollectionAreUpdated()
        {
            var collection  = new MetaDataCollection();
            var collection2 = new MetaDataCollection();

            collection.TryAdd("key1", "value1");
            collection2.TryAdd("key1", "value2");

            Assert.AreEqual("value1", collection["key1"]);

            collection.Merge(collection2);

            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual("value2", collection["key1"]);
        }
        public string GetDocumentOutputData(HttpListenerContext ct, ActionInfo hi)
        {
            MetaDataCollection  mdc         = GetRequestObject <MetaDataCollection>(ct);
            NameValueCollection querystring = ct.Request.QueryString;
            string result;

            ct.Response.ContentType = "image/jpeg";

            using (var stream = File.Open("small.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                StreamReader reader = new StreamReader(stream);
                result = reader.ReadToEnd();
            }

            return(result);
        }
Example #13
0
        public void MergingCollectionsWithNonSharedKeys_AllKeysIncluded()
        {
            var collection  = new MetaDataCollection();
            var collection2 = new MetaDataCollection();

            collection.TryAdd("key1", "value1");
            collection2.TryAdd("key2", "value2");

            collection.Merge(collection2);

            Assert.AreEqual(2, collection.Count);
            Assert.AreEqual("value1", collection["key1"]);
            Assert.AreEqual("value2", collection["key2"]);

            Assert.IsFalse(collection2.ContainsKey("key1"));
        }
Example #14
0
        /// <summary>
        /// Reads the MetaData.
        /// </summary>
        /// <param name="type">The Type.</param>
        /// <returns>MetaDataCollection.</returns>
        public static MetaDataCollection ReadMetaData(Type type)
        {
            MetaDataAttribute[] data = AttributeHelper.GetAttributes <MetaDataAttribute>(type);
            var dataCollection       = new MetaDataCollection();

            foreach (MetaDataAttribute metaData in data)
            {
                if (dataCollection.ContainsKey(metaData.Name))
                {
                    continue;
                }

                dataCollection.Add(metaData.Name, metaData.Data);
            }

            return(dataCollection);
        }
Example #15
0
        public override object GetValue(DataManager manager)
        {
            if (data == null && manager.PrimaryKeyValue is Guid)
            {
                data = Tag.Find(
                    manager.Settings.BaseActiveObjectType.FullName,
                    (Guid)manager.PrimaryKeyValue
                );
            }

            if (data == null)
            {
                data = new MetaDataCollection();
            }

            return data;
        }
Example #16
0
        /// <summary>
        /// Creates this query context instance that can be executed against the test server.
        /// </summary>
        /// <returns>GraphQueryContext.</returns>
        public virtual GraphQueryExecutionContext Build()
        {
            var metaData = new MetaDataCollection();

            // unchangable items about the request
            var request = new Mock <IGraphOperationRequest>();

            // updateable items about the request
            var context = new GraphQueryExecutionContext(this.OperationRequest, _serviceProvider, _user, _metrics, _eventLogger, metaData);

            foreach (var kvp in _sourceData)
            {
                var mockField = new Mock <IGraphField>();
                mockField.Setup(x => x.FieldSource).Returns(Internal.TypeTemplates.GraphFieldTemplateSource.Action);
                mockField.Setup(x => x.Route).Returns(kvp.Key);
                context.DefaultFieldSources.AddSource(mockField.Object, kvp.Value);
            }

            return(context);
        }
Example #17
0
        /// <summary>
        /// Analysiere die Metadateien, extrahiere die passenden Werte und fülle damit die Felder des Objekts.
        /// <para>Es wird durch alle Feldnamen, die indexiert werden sollen, iteriert. Den Feldern sind Listen zugeordnet. Sie enthalten
        /// statische Werte oder Variablen. Die Variablen werden durch die Inhalte in den Metadateien ersetzt.</para>
        /// </summary>
        /// <param name="element">Das Objekt, dessen Felder gefüllt werden sollen.</param>
        /// <param name="metaData">Die Metadateien mit dem Inhalt, zum Beispiel entity.txt, lesson.js und slide.js.</param>
        private void FillFields(Element element, MetaDataCollection metaData)
        {
            var keys = element.Fields.Keys.ToList();
            ExpressionEvaluator expressionEvaluator = new();

            foreach (string fieldName in keys)
            {
                List <string> values    = element.Fields[fieldName];
                int           lastIndex = values.Count - 1;
                for (int valueIndex = lastIndex; valueIndex >= 0; valueIndex--)
                {
                    string   temporaryValue = values[valueIndex];
                    string[] resultValues   = EvaluateField(temporaryValue, expressionEvaluator, metaData);
                    AddOrRemoveFields(values, valueIndex, resultValues);
                }

                if (values.Count == 0)
                {
                    element.Fields.Remove(fieldName);
                }
            }
        }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="temporaryValue"></param>
        /// <param name="expressionEvaluator"></param>
        /// <param name="metaData"></param>
        /// <returns>Eine Liste mit den Werten oder null, wenn der Ausdruck kein Ergebnis liefert.</returns>
        private string[] EvaluateField(string temporaryValue, ExpressionEvaluator expressionEvaluator, MetaDataCollection metaData)
        {
            if (!ExpressionEvaluator.IsExpression(temporaryValue, out string expression))
            {
                _log.Debug(LocalizationService.FormatResourceString("ElementCrawlerMessage02", temporaryValue));
                return(string.IsNullOrWhiteSpace(temporaryValue) ? null : new string[] { temporaryValue });
            }

            if (ExpressionEvaluator.IsVariableExpression(expression, out string variableName))
            {
                string value = Util.RemoveMarkup(MetaAnalyzer.ExtractValue(metaData, variableName));
                _log.Debug(LocalizationService.FormatResourceString("ElementCrawlerMessage03", expression, value));
                return(string.IsNullOrWhiteSpace(value) ? null : new string[] { value });
            }
            if (ExpressionEvaluator.IsConverterExpression(expression, out string converterClassName, out string[] converterParameterNames))
            {
                int      parameterCount           = converterParameterNames.Length;
                string[] converterParameterValues = new string[parameterCount];
                for (int i = 0; i < parameterCount; i++)
                {
                    string converterParameter = converterParameterNames[i];
                    bool   isVariable         = ExpressionEvaluator.IsVariableExpression(converterParameter, out string converterVariableName);
                    converterParameterValues[i] = isVariable ?
                                                  Util.RemoveMarkup(MetaAnalyzer.ExtractValue(metaData, converterVariableName)) :
                                                  converterParameterNames[i];
                }
                _log.Debug(LocalizationService.FormatResourceString("ElementCrawlerMessage04", expression, converterClassName, ""));
                return(ExpressionEvaluator.EvaluateAsConverter(converterClassName, converterParameterValues));
            }
            return(null);
        }
Example #19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="element"></param>
 /// <param name="metaData"></param>
 private void AddAssets(Element element, MetaDataCollection metaData)
 {
     MetaAnalyzer.ExtractAssets(metaData, out string[] childrenIds, out string[] attachementIds);
     element.ChildrenIds      = childrenIds;
     element.AttachementNames = attachementIds;
 }