Example #1
0
        public static StandardReturType Validate <TOutput, TItem>(FacadeMethodInfo <TOutput, TItem> facade) where TOutput : class, IBasicOutput <TItem>, new()
        {
            // Initialize context
            try
            {
                BrokerContext.Initialize(facade.ApplicationToken, facade.UserToken);
            }
            catch (Exception ex)
            {
                return(StandardReturType.InvalidApplicationToken(facade.ApplicationToken));
            }

            // Validate
            StandardReturType validationRet = facade.ValidateInput();

            if (!StandardReturType.IsSucceeded(validationRet))
            {
                Local.Admin.AddNewLog(TraceEventType.Error, BrokerContext.Current.WebMethodMessageName, TextMessages.InvalidInput, null, null);
                if (validationRet == null)
                {
                    validationRet = StandardReturType.UnspecifiedError("Validation failed");
                }
                return(validationRet);
            }
            return(StandardReturType.OK());
        }
Example #2
0
        private IBasicOutput CreateReturnValue(SoapMessage message)
        {
            Exception ex     = Misc.GetDeepestInnerException(message.Exception);
            var       ret    = Utilities.Reflection.CreateInstance(message.MethodInfo.ReturnType);
            var       output = ret as Schemas.Part.IBasicOutput;

            if (ex is XmlException)
            {
                output.StandardRetur = StandardReturType.MalformedXml();
            }
            else if (ex is HeaderException)
            {
                output.StandardRetur = StandardReturType.NullInput("applicationHeader");
            }
            else if (ex is MissingRequestException)
            {
                output.StandardRetur = StandardReturType.Create(HttpErrorCode.BAD_CLIENT_REQUEST, string.Format("Missing request element: {0}", message.MethodInfo.Name));
            }
            else if (ex is InvalidOperationException && Misc.ExceptionTreeContainsText(message.Exception, "XML"))
            {
                output.StandardRetur = StandardReturType.RequestUnreadable(ex.Message);
            }
            else
            {
                output.StandardRetur = StandardReturType.UnspecifiedError();
            }
            return(output);
        }
Example #3
0
        public virtual StandardReturType BaseValidate <TOutput>(MethodHeader header, TInputElement[] input)
        {
            // Initialize context
            try
            {
                BrokerContext.Initialize(header.ApplicationToken, header.UserToken);
            }
            catch (Exception ex)
            {
                return(StandardReturType.InvalidApplicationToken(header.ApplicationToken));
            }

            // Validate input
            if (input == null || input.Length == 0)
            {
                return(StandardReturType.NullInput());
            }

            var ret = Validate(input);

            if (ret == null)
            {
                ret = StandardReturType.UnspecifiedError("Validation failed");
            }

            return(ret);
        }
Example #4
0
        public static TOutput AggregateResults <TOutput, TItem>(FacadeMethodInfo <TOutput, TItem> facade, SubMethodRunState[] subMethodRunStates) where TOutput : class, IBasicOutput <TItem>, new()
        {
            #region Final aggregation

            var succeededCount = (from mi in subMethodRunStates where mi.Succeeded select mi).Count();
            var subResults     = (from mi in subMethodRunStates select mi.Result).ToArray();

            bool canAggregate = facade.AggregationFailOption == AggregationFailOption.FailNever ||
                                facade.AggregationFailOption == AggregationFailOption.FailOnAll && succeededCount > 0 ||
                                facade.AggregationFailOption == AggregationFailOption.FailOnAny && succeededCount == subMethodRunStates.Length;

            if (canAggregate)
            {
                var outputMainItem = facade.Aggregate(subResults);
                if (facade.IsValidResult(outputMainItem))
                {
                    Local.Admin.AddNewLog(TraceEventType.Information, BrokerContext.Current.WebMethodMessageName, TextMessages.Succeeded, null, null);
                    var output = new TOutput();
                    output.SetMainItem(outputMainItem);
                    if (succeededCount == subMethodRunStates.Length)
                    {
                        output.StandardRetur = StandardReturType.OK();
                    }
                    else
                    {
                        var failedSubMethods         = subMethodRunStates.Where(smi => !smi.Succeeded).Select(smi => smi.SubMethodInfo);
                        var failedSubMethodsByReason = failedSubMethods.GroupBy(smi => smi.PossibleErrorReason());
                        var failuresAndReasons       = failedSubMethodsByReason.ToDictionary(grp => grp.Key, grp => grp.Select(smi => smi.InputToString()));
                        output.StandardRetur = StandardReturType.PartialSuccess(failuresAndReasons);
                    }

                    return(output);
                }
                else
                {
                    string xml = Strings.SerializeObject(outputMainItem);
                    Local.Admin.AddNewLog(TraceEventType.Error, BrokerContext.Current.WebMethodMessageName, TextMessages.ResultGatheringFailed, typeof(TOutput).ToString(), xml);
                    return(new TOutput()
                    {
                        StandardRetur = StandardReturType.UnspecifiedError("Aggregation failed")
                    });
                }
            }
            else
            {
                // TODO: Is it possible to put details why each item has failed?
                return(new TOutput()
                {
                    StandardRetur = StandardReturType.Create(HttpErrorCode.DATASOURCE_UNAVAILABLE)
                });
            }
            #endregion
        }
Example #5
0
        /// <summary>
        /// Gets the result for an array of input items
        /// </summary>
        /// <typeparam name="TOutput"></typeparam>
        /// <param name="header">Header tokens</param>
        /// <param name="input">Array of input items</param>
        /// <returns>Composite result for the operation</returns>
        public TOutput GetBatch <TOutput>(MethodHeader header, TInputElement[] input)
            where TOutput : IBasicOutput <TOutputElement[]>, new()
        {
            try
            {
                // Validate
                var ret = BaseValidate <TOutput>(header, input);

                if (!StandardReturType.IsSucceeded(ret))
                {
                    return new TOutput()
                           {
                               StandardRetur = ret
                           }
                }
                ;

                // Data providers
                IEnumerable <TInterface> dataProviders;
                // TODO: Create an execution plan here
                var providerMethod = new ProviderMethod <TInputElement, TOutputElement, Element, object, TInterface>();

                ret = CreateDataProviders(providerMethod, out dataProviders);

                if (!StandardReturType.IsSucceeded(ret))
                {
                    return new TOutput()
                           {
                               StandardRetur = ret
                           }
                }
                ;

                // Call data providers
                var allElements = providerMethod.CallDataProviders(dataProviders, input);

                // Aggregate
                return(Aggregate <TOutput>(allElements));
            }
            catch (Exception ex)
            {
                Local.Admin.LogException(ex);
                return(new TOutput()
                {
                    StandardRetur = StandardReturType.UnspecifiedError()
                });
            }
        }
Example #6
0
        public static TOutput GetMethodOutput <TOutput, TItem>(FacadeMethodInfo <TOutput, TItem> facade) where TOutput : class, IBasicOutput <TItem>, new()
        {
            try
            {
                StandardReturType   standardRetur;
                SubMethodRunState[] subMethodRunStates;

                standardRetur = Validate <TOutput, TItem>(facade);
                if (!StandardReturType.IsSucceeded(standardRetur))
                {
                    return(new TOutput()
                    {
                        StandardRetur = standardRetur
                    });
                }


                standardRetur = Initialize <TOutput, TItem>(facade, out subMethodRunStates);
                if (!StandardReturType.IsSucceeded(standardRetur))
                {
                    return(new TOutput()
                    {
                        StandardRetur = standardRetur
                    });
                }

                RunThreads <TOutput, TItem>(facade, subMethodRunStates);
                return(AggregateResults <TOutput, TItem>(facade, subMethodRunStates));
            }
            catch (Exception ex)
            {
                Local.Admin.LogException(ex);
                return(new TOutput()
                {
                    StandardRetur = StandardReturType.UnspecifiedError()
                });
            }
        }
Example #7
0
        public static TOutput GetBatchMethodOutput <TInterface, TOutput, TSingleInputItem, TSingleOutputItem>(BatchFacadeMethodInfo <TInterface, TOutput, TSingleInputItem, TSingleOutputItem> facade)
            where TInterface : class, IDataProvider
            where TOutput : class, IBasicOutput <TSingleOutputItem[]>, new()
        {
            try
            {
                StandardReturType   standardRetur;
                SubMethodRunState[] subMethodRunStates;

                standardRetur = Validate <TOutput, TSingleOutputItem[]>(facade);
                if (!StandardReturType.IsSucceeded(standardRetur))
                {
                    return(new TOutput()
                    {
                        StandardRetur = standardRetur
                    });
                }

                standardRetur = Initialize <TOutput, TSingleOutputItem[]>(facade, out subMethodRunStates);
                if (!StandardReturType.IsSucceeded(standardRetur))
                {
                    return(new TOutput()
                    {
                        StandardRetur = standardRetur
                    });
                }

                return(facade.Run(subMethodRunStates));
            }
            catch (Exception ex)
            {
                Local.Admin.LogException(ex);
                return(new TOutput()
                {
                    StandardRetur = StandardReturType.UnspecifiedError()
                });
            }
        }