public static bool ShouldApply(BehaviorChain chain)
        {
            // TODO -- Get the BehaviorChainFilter thing going again?
            if ( (chain.GetRoutePattern() ?? string.Empty).Contains(DiagnosticUrlPolicy.DIAGNOSTICS_URL_ROOT) )
            {
                return false;
            }            

            if (chain.Calls.Any(x => x.HandlerType.Assembly == DiagnosticAssembly))
            {
                return false;
            }

            if (chain.Calls.Any(x => x.HasAttribute<NoDiagnosticsAttribute>()))
            {
                return false;
            }

            if (chain.InputType() != null && chain.InputType().Assembly == DiagnosticAssembly)
            {
                return false;
            }

            if (chain.ResourceType() != null && chain.ResourceType().Assembly == DiagnosticAssembly)
            {
                return false;
            }

            return true;
        }
        public static bool ShouldApply(BehaviorChain chain)
        {
            // TODO -- Get the BehaviorChainFilter thing going again?
            if ((chain.GetRoutePattern() ?? string.Empty).Contains(DiagnosticsRegistration.DIAGNOSTICS_URL_ROOT))
            {
                return(false);
            }

            if (chain.Calls.Any(x => x.HandlerType.Assembly == DiagnosticAssembly))
            {
                return(false);
            }

            if (chain.Calls.Any(x => x.HasAttribute <NoDiagnosticsAttribute>()))
            {
                return(false);
            }

            if (chain.InputType() != null && chain.InputType().Assembly == DiagnosticAssembly)
            {
                return(false);
            }

            if (chain.ResourceType() != null && chain.ResourceType().Assembly == DiagnosticAssembly)
            {
                return(false);
            }

            return(true);
        }
        public static string TitleForChain(BehaviorChain chain)
        {
            if (chain.GetRoutePattern().IsNotEmpty())
            {
                return chain.GetRoutePattern();
            }

            if (chain.GetRoutePattern() == string.Empty)
            {
                return "(home)";
            }

            if (chain.Calls.Any())
            {
                return chain.Calls.Select(x => x.Description).Join(", ");
            }

            if (chain.HasOutput() && chain.Output.Writers.Any())
            {
                return chain.Output.Writers.Select(x => Description.For(x).Title).Join(", ");
            }

            if (chain.InputType() != null)
            {
                return "Handler for " + chain.InputType().FullName;
            }

            return "BehaviorChain " + chain.UniqueId;
        }
        public static string TitleForChain(BehaviorChain chain)
        {
            if (chain.GetRoutePattern().IsNotEmpty())
            {
                return(chain.GetRoutePattern());
            }

            if (chain.GetRoutePattern() == string.Empty)
            {
                return("(home)");
            }

            if (chain.Calls.Any())
            {
                return(chain.Calls.Select(x => x.Description).Join(", "));
            }

            if (chain.HasOutput() && chain.Output.Writers.Any())
            {
                return(chain.Output.Writers.Select(x => Description.For(x).Title).Join(", "));
            }

            if (chain.InputType() != null)
            {
                return("Handler for " + chain.InputType().FullName);
            }

            return("BehaviorChain " + chain.UniqueId);
        }
Beispiel #5
0
        public bool Matches(BehaviorChain chain)
        {
            if (chain.InputType() == null)
            {
                return(false);
            }

            return(chain.InputType().CanBeCastTo <T>());
        }
        public static bool ExemptedFromAuthentication(BehaviorChain chain)
        {
            if (chain.InputType() != null && chain.InputType().HasAttribute<NotAuthenticatedAttribute>()) return true;
            if (chain.OfType<ActionCall>().Any(x => x.HasAttribute<NotAuthenticatedAttribute>())) return true;

            // TODO -- Come up with a better way of doing this
            if (chain.OfType<ActionCall>().Any(x => x.HandlerType.Name == "AssetWriter")) return true;

            return false;
        }
Beispiel #7
0
        public void uses_the_first_may_have_input_with_non_null()
        {
            var chain = new BehaviorChain();

            chain.AddToEnd(new FakeInputNode(typeof(string)));

            chain.InputType().ShouldEqual(typeof(string));

            chain.Prepend(new FakeInputNode(null));
            chain.InputType().ShouldEqual(typeof(string));

            chain.Prepend(new FakeInputNode(typeof(int)));
            chain.InputType().ShouldEqual(typeof(int));
        }
        public static bool IsAsymmetricJson(this BehaviorChain chain)
        {
            if (chain.ResourceType() != null)
            {
                if (chain.Output.Writers.Count() != 1)
                {
                    return(false);
                }
                if (!chain.Output.UsesFormatter <JsonFormatter>())
                {
                    return(false);
                }
            }

            if (chain.InputType() != null)
            {
                if (chain.Input.Readers.Count() != 2)
                {
                    return(false);
                }
                if (!chain.Input.AllowHttpFormPosts)
                {
                    return(false);
                }

                return(chain.Input.UsesFormatter <JsonFormatter>());
            }

            return(true);
        }
Beispiel #9
0
        public void Modify(BehaviorChain chain)
        {
            if (chain.InputType() == null)
            {
                return;
            }

            chain.Input.AddFormatter <JsonFormatter>();
        }
Beispiel #10
0
        public void Modify(BehaviorChain chain)
        {
            if (chain.InputType() == null)
            {
                return;
            }

            chain.Input.AllowHttpFormPosts = true;
        }
Beispiel #11
0
        public static BehaviorChain ReplaceInputFormatters <T>(this BehaviorChain chain, Type reader) where T : IFormatter
        {
            var mimeTypes = typeof(T).GetCustomAttribute <MimeTypeAttribute>();

            if (chain.InputType() != null)
            {
                if (chain.Input.Readers != null && mimeTypes != null)
                {
                    var readerNode = chain.Input.Readers.FirstOrDefault(
                        x => x.Mimetypes.Any(y => mimeTypes.MimeTypes.Any(z => z == y)));
                    if (readerNode != null)
                    {
                        readerNode.Remove();
                    }
                }
                chain.Input.Readers.AddToEnd(new Reader(reader
                                                        .MakeGenericType(chain.InputType(), typeof(T))));
            }
            return(chain);
        }
Beispiel #12
0
        // SAMPLE: conneg-manipulation
        public static void MessWithConneg(BehaviorChain chain)
        {
            // Remove all readers
            chain.Input.ClearAll();

            // Accept 'application/x-www-form-urlencoded' with model binding
            chain.Input.AllowHttpFormPosts = true;

            // Add basic Json reading
            chain.Input.AddFormatter <JsonFormatter>();

            // Query whether or not the chain uses the basic Json reading
            bool readsJson = chain.Input.UsesFormatter <JsonFormatter>();

            // Add a completely custom Reader
            var specialReader = chain.Input
                                .AddReader <SpecialContentMediaReader>();

            // Reorder the special reader to move it to the first
            // as the default
            specialReader.MoveToFront();

            // Add a new Reader as the last reader
            chain.Input.Readers.AddToEnd(new ModelBind(chain.InputType()));

            // Are there any Readers?
            chain.HasReaders();

            // Is there any output?
            chain.HasOutput();


            // Add the default Conneg policies to this chain
            // model binding, json, or xml in and json or xml out
            chain.ApplyConneg();

            // Manipulate an existing writer
            var writer = chain.Output.Writers.First();

            manipulateWriter(writer);

            // Remove all writers
            chain.Output.ClearAll();

            // Add the HtmlStringWriter
            chain.Output.AddHtml();

            // Add basic Json output
            chain.OutputJson();

            // Add basic Xml output
            chain.OutputXml();
        }
        public void WriteBody(BehaviorChain chain, HtmlTag row, HtmlTag cell)
        {
            Type inputType = chain.InputType();

            if (inputType == null)
            {
                cell.Text(" -");
            }
            else
            {
                cell.Text(inputType.Name).Title(inputType.AssemblyQualifiedName);
            }
        }
Beispiel #14
0
        public void WriteBody(BehaviorChain chain, HtmlTag row, HtmlTag cell)
        {
            Type inputType = chain.InputType();

            if (inputType == null)
            {
                cell.Text(" -");
            }
            else
            {
                cell.Text(inputType.Name).Title(inputType.AssemblyQualifiedName);
            }
        }
        private void attachMediaHandling(BehaviorChain chain, IConfigurationObserver observer)
        {
            var firstAction = chain.FirstCall();
            if (firstAction == null) return;

            observer.RecordCallStatus(firstAction, "Meets criteria {0} for Conneg".ToFormat(_description));

            var node = new ConnegNode(){
                InputType = chain.InputType(),
                OutputType = chain.Calls.Where(x => x.HasOutput).Select(x => x.OutputType()).LastOrDefault()
            };

            firstAction.AddBefore(node);
        }
        public static void MakeAsymmetricJson(this BehaviorChain chain)
        {
            if (chain.InputType() != null)
            {
                chain.Input.ClearAll();
                chain.Input.AllowHttpFormPosts = true;
                chain.Input.AddFormatter <JsonFormatter>();
            }

            if (chain.ResourceType() != null)
            {
                chain.Output.ClearAll();
                chain.Output.AddFormatter <JsonFormatter>();
            }
        }
Beispiel #17
0
            public TemplateDef(TemplateGraph parent, BehaviorChain chain)
            {
                _parent = parent;
                _chain  = chain;

                var view = chain.Output.DefaultView();

                if (view == null)
                {
                    _name = chain.InputType().Name;
                }
                else
                {
                    _name = Path.GetFileNameWithoutExtension(view.Name());
                    _file = view.FilePath;
                }
            }
        public static void ShouldBeAsymmetricJson(this BehaviorChain chain)
        {
            if (chain.ResourceType() != null)
            {
                chain.Output.MimeTypes()
                .OrderBy(x => x)
                .ShouldHaveTheSameElementsAs("application/json", "text/json");
            }

            if (chain.InputType() != null)
            {
                var mimetypes = chain.Input.Mimetypes.ToArray();
                mimetypes
                .OrderBy(x => x)
                .ShouldHaveTheSameElementsAs("application/json", MimeType.HttpFormMimetype.ToString(), MimeType.MultipartMimetype.ToString(), "text/json");
            }
        }
        public static bool IsAsymmetricJson(this BehaviorChain chain)
        {
            if (chain.ConnegInputNode() == null && chain.ConnegOutputNode() == null)
            {
                return(false);
            }

            if (chain.ActionOutputType() != null)
            {
                var output = chain.ConnegOutputNode();
                if (output.SelectedFormatterTypes.Count() != 1)
                {
                    return(false);
                }

                if (output.SelectedFormatterTypes.Single() != typeof(JsonFormatter))
                {
                    return(false);
                }
            }

            if (chain.InputType() != null)
            {
                var input = chain.ConnegInputNode();

                if (!input.AllowHttpFormPosts)
                {
                    return(false);
                }

                if (input.SelectedFormatterTypes.Count() != 1)
                {
                    return(false);
                }

                if (input.SelectedFormatterTypes.Single() != typeof(JsonFormatter))
                {
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Sets up very basic content negotiation for an endpoint.
        /// Accepts http form posts, xml, and json
        /// Returns xml or json
        /// </summary>
        /// <param name="chain"></param>
        public static void ApplyConneg(this BehaviorChain chain)
        {
            chain.RemoveConneg();

            if (chain.InputType() != null)
            {
                chain.Input.ClearAll();
                chain.Input.AllowHttpFormPosts = true;
                chain.Input.AddFormatter <JsonFormatter>();
                chain.Input.AddFormatter <XmlFormatter>();
            }

            if (chain.HasResourceType())
            {
                chain.Output.ClearAll();
                chain.Output.AddFormatter <JsonFormatter>();
                chain.Output.AddFormatter <XmlFormatter>();
            }
        }
Beispiel #21
0
        private void attachMediaHandling(BehaviorChain chain, IConfigurationObserver observer)
        {
            var firstAction = chain.FirstCall();

            if (firstAction == null)
            {
                return;
            }

            observer.RecordCallStatus(firstAction, "Meets criteria {0} for Conneg".ToFormat(_description));

            var node = new ConnegNode()
            {
                InputType  = chain.InputType(),
                OutputType = chain.Calls.Where(x => x.HasOutput).Select(x => x.OutputType()).LastOrDefault()
            };

            firstAction.AddBefore(node);
        }
        // This should not do anything if there are conneg nodes
        public static void ApplyConneg(this BehaviorChain chain)
        {
            var inputType = chain.InputType();

            if (chain.ConnegInputNode() == null && inputType != null)
            {
                var inputNode = new ConnegInputNode(inputType);
                var action    = chain.FirstCall();
                action.AddBefore(inputNode);
            }

            var actionOutputType = chain.ActionOutputType();

            if (chain.ConnegOutputNode() == null && actionOutputType != null && actionOutputType != typeof(void) && actionOutputType != typeof(HttpStatusCode))
            {
                var outputNode = new ConnegOutputNode(actionOutputType);
                var action     = chain.Last(x => x is ActionCall);
                action.AddAfter(outputNode);
            }
        }
Beispiel #23
0
        public object InvokeFast(BehaviorChain chain, object input = null)
        {
            _request.Set(OutputPartialBehavior.None);
            if (input != null)
            {
                _request.Set(chain.InputType(), input);
            }

            try
            {
                var partial = _factory.BuildPartial(chain);
                partial.InvokePartial();

                // TODO -- how to detect authorization failures here?
                var resourceType = chain.ResourceType();
                return(_request.Has(resourceType) ? _request.Get(resourceType) : null);
            }
            finally
            {
                _request.Set(OutputPartialBehavior.Write);
            }
        }
        public void uses_the_first_may_have_input_with_non_null()
        {
            var chain = new BehaviorChain();
            chain.AddToEnd(new FakeInputNode(typeof (string)));

            chain.InputType().ShouldEqual(typeof (string));

            chain.Prepend(new FakeInputNode(null));
            chain.InputType().ShouldEqual(typeof (string));

            chain.Prepend(new FakeInputNode(typeof(int)));
            chain.InputType().ShouldEqual(typeof(int));
        }
 public bool Matches(BehaviorChain chain)
 {
     return chain.InputType() != null;
 }
Beispiel #26
0
 public bool Matches(BehaviorChain chain)
 {
     return(typeof(T).Equals(chain.InputType()));
 }
Beispiel #27
0
 public void the_chain_can_still_decipher_its_input_type()
 {
     theChain.InputType().ShouldEqual(typeof(HtmlTag));
 }
Beispiel #28
0
        public string Text(BehaviorChain chain)
        {
            Type inputType = chain.InputType();

            return(inputType == null ? " -" : "{0} ({1})".ToFormat(inputType.Name, inputType.FullName));
        }
Beispiel #29
0
        public static string GetInputTypeName(this BehaviorChain chain)
        {
            var type = chain.InputType();

            return(type == null ? string.Empty : type.Name);
        }
 public string Text(BehaviorChain chain)
 {
     Type inputType = chain.InputType();
     return inputType == null ? " -" : "{0} ({1})".ToFormat(inputType.Name, inputType.FullName);
 }
 public static bool IsAggregatedChain(this BehaviorChain chain)
 {
     return(chain.InputType().IsClientMessage() || chain.ResourceType().IsClientMessage());
 }
 public void input_type_should_be_the_input_type_of_the_publisher_method()
 {
     chain.InputType().ShouldBe(typeof(Message1Input));
 }
Beispiel #33
0
 public bool Matches(BehaviorChain chain)
 {
     return(chain.InputType() != null);
 }