public void Handle(IHttpRequest request, IHttpResponse response)
        {
            Dictionary <Type, ActivityHandlerAttribute[]> handlers = _container.GetAttributedTypes <ActivityHandlerAttribute>(typeof(IActivityHandler));
            List <object> handlerInfoList = new List <object>();

            foreach (KeyValuePair <Type, ActivityHandlerAttribute[]> keyValuePair in handlers)
            {
                foreach (ActivityHandlerAttribute attribute in keyValuePair.Value)
                {
                    var instance = (IActivityHandler)_container.GetInstance(keyValuePair.Key);
                    handlerInfoList.Add(new
                    {
                        path               = attribute.Path,
                        verb               = attribute.Verb.ToString(),
                        description        = attribute.Description,
                        expectedParameters = attribute.ExpectedParameters,
                        exampleCall        =
                            $"{attribute.Path}{(attribute.ExpectedParameters.Any() ? "?" : "")}{string.Join("&", attribute.ExpectedParameters.Select(a => a + "=<value>"))}",
                        exampleRequestDocument  = instance.ExampleRequestDocument,
                        exampleResponseDocument = instance.ExampleResponseDocument,
                    });
                }
            }
            response.SetObjectContent(handlerInfoList);
        }
Example #2
0
        public void Initialise()
        {
            Dictionary <Type, ActivityHandlerAttribute[]> attributes = _container.GetAttributedTypes <ActivityHandlerAttribute>(typeof(IActivityHandler));

            // Send a "Register" message on the bus for each activity
            foreach (KeyValuePair <Type, ActivityHandlerAttribute[]> keyValuePair in attributes)
            {
                foreach (ActivityHandlerAttribute attribute in keyValuePair.Value.Where(a => a.ActivityName != null && !a.SkipAuthorisation))
                {
                    queue.PublishMessage(new Message <string, string>
                    {
                        Method = "RegisterActivityForAuthorisation",
                        Need   = attribute.ActivityName
                    });
                }
            }
        }
Example #3
0
        private IActivityHandler GetRequestHandler(IHttpRequest request, out ActivityHandlerAttribute activityHandler)
        {
            var baseUrlParts = _configurationManager.Get("BaseUrl", "/").ToLowerInvariant().Split('/').Where(x => x != string.Empty).ToArray();

            activityHandler = null;
            bool Predicate(ActivityHandlerAttribute attr) => RequestPathMatchesAttributePath(request, attr, baseUrlParts);

            IDictionary <Type, ActivityHandlerAttribute[]> handlers = activityHandlers ??
                                                                      (activityHandlers =
                                                                           _container.GetAttributedTypes <ActivityHandlerAttribute>(
                                                                               typeof(IActivityHandler)));

            logger.Debug(nameof(RequestBroker) + "." + nameof(GetRequestHandler),
                         new LogItem("Event", "Got handlers"),
                         new LogItem("Handlers", string.Join(",", handlers.Select(kvp => kvp.Key.Name))));

            KeyValuePair <Type, ActivityHandlerAttribute[]> handlerInfo =
                handlers.FirstOrDefault(kvp => kvp.Value.Any(Predicate));

            logger.Debug(nameof(RequestBroker) + "." + nameof(GetRequestHandler),
                         new LogItem("HandlerInfo", handlerInfo.Key?.Name));

            if (handlerInfo.Key == null)
            {
                return(null);
            }

            ActivityHandlerAttribute attribute = handlerInfo.Value.First(Predicate);

            activityHandler = attribute;

            var path = attribute.Path;

            if (baseUrlParts.Length > 0)
            {
                path = $"/{string.Join("/",baseUrlParts)}{path}";
            }

            request.SetUriPattern(path);

            IActivityHandler instance = (IActivityHandler)_container.GetInstance(handlerInfo.Key);

            return(instance);
        }