Exemple #1
0
        private static List <Action> ResourceActionsOf(
            HttpProperties properties,
            string resourceName,
            string[] resourceActionNames)
        {
            var resourceActions = new List <Action>(resourceActionNames.Length);

            foreach (var actionName in resourceActionNames)
            {
                try
                {
                    var keyPrefix = $"action.{resourceName}.{actionName}.";

                    var actionId = resourceActions.Count;
                    var method   = properties.GetProperty($"{keyPrefix}method", null);
                    var uri      = properties.GetProperty($"{keyPrefix}uri", null);
                    var to       = properties.GetProperty($"{keyPrefix}to", null);
                    var mapper   = properties.GetProperty($"{keyPrefix}mapper", null);

                    resourceActions.Add(new Action(actionId, method, uri, to, mapper));
                }
                catch (Exception e)
                {
                    Console.WriteLine($"vlingo-net/http: Failed to load resource: {resourceName} action:{actionName} because: {e.Message}");
                    throw;
                }
            }

            return(resourceActions);
        }
Exemple #2
0
        private static IDictionary <string, IConfigurationResource> LoadStaticFilesResource(HttpProperties properties)
        {
            var staticFilesResourceActions = new Dictionary <string, IConfigurationResource>();

            var root = properties.GetProperty(StaticFilesResourceRoot);

            if (root == null)
            {
                return(staticFilesResourceActions);
            }

            var poolSize       = properties.GetProperty(StaticFilesResourcePool, "5");
            var validSubPaths  = properties.GetProperty(StaticFilesResourceSubPaths);
            var actionSubPaths = ActionNamesFrom(validSubPaths, StaticFilesResourceSubPaths).OrderByDescending(x => x.Length);

            try
            {
                int resourceSequence = 0;

                foreach (var actionSubPath in actionSubPaths)
                {
                    var mappedParameterRoot          = new Action.MappedParameter("string", root);
                    var mappedParameterValidSubPaths = new Action.MappedParameter("string", validSubPaths);

                    var slash        = actionSubPath.EndsWith("/") ? "" : "/";
                    var resourceName = StaticFilesResource + resourceSequence++;

                    var actions = new List <Action>(1);
                    var additionalParameters = new List <Action.MappedParameter> {
                        mappedParameterRoot, mappedParameterValidSubPaths
                    };
                    actions.Add(new Action(0, Method.Get.Name, actionSubPath + slash + StaticFilesResourcePathParameter, StaticFilesResourceServeFile, null, additionalParameters));
                    var resource = ResourceFor(resourceName, typeof(StaticFilesResource), int.Parse(poolSize), actions);
                    staticFilesResourceActions[resourceName] = resource;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("vlingo-net/http: Failed to load static files resource: " + StaticFilesResource + " because: " + e.Message);
                Console.WriteLine(e.StackTrace);
                throw e;
            }

            return(staticFilesResourceActions);
        }
Exemple #3
0
        private static IDictionary <string, IConfigurationResource> LoadFeedResources(HttpProperties properties)
        {
            var feedResourceActions = new Dictionary <string, IConfigurationResource>();

            foreach (var feedResourceName in FindResources(properties, FeedProducerNamePrefix))
            {
                var feedUri                  = properties.GetProperty(feedResourceName);
                var resourceName             = feedResourceName.Substring(FeedProducerNamePrefix.Length);
                var feedProducerClassnameKey = $"feed.resource.{resourceName}.producer.class";
                var feedProducerClassname    = properties.GetProperty(feedProducerClassnameKey);
                var feedElementsKey          = $"feed.resource.{resourceName}.elements";
                var maybeFeedElements        = int.Parse(properties.GetProperty(feedElementsKey, "20"));
                var feedElements             = maybeFeedElements <= 0 ? 20 : maybeFeedElements;
                var poolKey                  = $"feed.resource.{resourceName}.pool";
                var maybePoolSize            = int.Parse(properties.GetProperty(poolKey, "1"));
                int handlerPoolSize          = maybePoolSize <= 0 ? 1 : maybePoolSize;
                var feedRequestUri           =
                    $"{feedUri?.Replace(resourceName, FeedNamePathParameter)}/{FeedProductIdPathParameter}";

                try
                {
                    var feedClass = ActorClassWithProtocol(feedProducerClassname, typeof(IFeedProducer));
                    var mappedParameterProducerClass   = new Action.MappedParameter("Type", feedClass);
                    var mappedParameterProductElements = new Action.MappedParameter("int", feedElements);

                    var actions = new List <Action>(1);
                    var additionalParameters = new List <Action.MappedParameter> {
                        mappedParameterProducerClass, mappedParameterProductElements
                    };
                    actions.Add(new Action(0, Method.Get.Name, feedRequestUri, FeedProducerFeed, null, additionalParameters));
                    var resource = ResourceFor(resourceName, typeof(FeedResource), handlerPoolSize, actions);
                    feedResourceActions.Add(resourceName, resource);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"vlingo/http: Failed to load feed resource: {resourceName} because: {e.Message}");
                    Console.WriteLine(e.StackTrace);
                    throw e;
                }
            }

            return(feedResourceActions);
        }
Exemple #4
0
        private static IDictionary <string, IConfigurationResource> LoadStaticFilesResource(HttpProperties properties, ILogger logger)
        {
            var staticFilesResourceActions = new Dictionary <string, IConfigurationResource>();

            var root = properties.GetProperty(StaticFilesResourceRoot);

            if (root == null)
            {
                return(staticFilesResourceActions);
            }

            var poolSize       = properties.GetProperty(StaticFilesResourcePool, "5") !;
            var validSubPaths  = properties.GetProperty(StaticFilesResourceSubPaths);
            var actionSubPaths = ActionNamesFrom(validSubPaths, StaticFilesResourceSubPaths);

            LoadStaticFileResource(staticFilesResourceActions, root, poolSize, validSubPaths, actionSubPaths, logger);

            return(staticFilesResourceActions);
        }
Exemple #5
0
        private static IConfigurationResource LoadResource(HttpProperties properties, string resourceNameKey)
        {
            var resourceName             = resourceNameKey.Substring(ResourceNamePrefix.Length);
            var resourceActionNames      = ActionNamesFrom(properties.GetProperty(resourceNameKey), resourceNameKey);
            var resourceHandlerKey       = $"resource.{resourceName}.handler";
            var resourceHandlerClassname = properties.GetProperty(resourceHandlerKey);
            var handlerPoolKey           = $"resource.{resourceName}.pool";
            var maybeHandlerPoolSize     = int.Parse(properties.GetProperty(handlerPoolKey, "1"));
            var handlerPoolSize          = maybeHandlerPoolSize <= 0 ? 1 : maybeHandlerPoolSize;

            try
            {
                var resourceActions = ResourceActionsOf(properties, resourceName, resourceActionNames);

                var resourceHandlerClass = ConfigurationResource <ResourceHandler> .NewResourceHandlerTypeFor(resourceHandlerClassname);

                return(ResourceFor(resourceName, resourceHandlerClass !, handlerPoolSize, resourceActions));
            }
            catch (Exception e)
            {
                Console.WriteLine("vlingo-net/http: Failed to load resource: " + resourceName + " because: " + e.Message);
                throw;
            }
        }
Exemple #6
0
        private static IDictionary <string, IConfigurationResource> LoadSseResources(HttpProperties properties)
        {
            var sseResourceActions = new Dictionary <string, IConfigurationResource>();

            foreach (var streamResourceName in FindResources(properties, SsePublisherNamePrefix))
            {
                var streamUri         = properties.GetProperty(streamResourceName);
                var resourceName      = streamResourceName.Substring(SsePublisherNamePrefix.Length);
                var feedClassnameKey  = $"sse.stream.{resourceName}.feed.class";
                var feedClassname     = properties.GetProperty(feedClassnameKey);
                var feedPayloadKey    = "sse.stream." + resourceName + ".feed.payload";
                var maybeFeedPayload  = int.Parse(properties.GetProperty(feedPayloadKey, "20"));
                var feedPayload       = maybeFeedPayload <= 0 ? 20 : maybeFeedPayload;
                var feedIntervalKey   = $"sse.stream.{resourceName}.feed.interval";
                var maybeFeedInterval = int.Parse(properties.GetProperty(feedIntervalKey, "1000"));
                var feedInterval      = maybeFeedInterval <= 0 ? 1000 : maybeFeedInterval;
                var feedDefaultIdKey  = $"sse.stream.{resourceName}.feed.default.id";
                var feedDefaultId     = properties.GetProperty(feedDefaultIdKey, "");
                var poolKey           = $"sse.stream.{resourceName}.pool";
                var maybePoolSize     = int.Parse(properties.GetProperty(poolKey, "1"));
                var handlerPoolSize   = maybePoolSize <= 0 ? 1 : maybePoolSize;
                var subscribeUri      = streamUri?.Replace(resourceName, SsePublisherNamePathParameter);
                var unsubscribeUri    = subscribeUri + "/" + SsePublisherIdPathParameter;

                try
                {
                    var feedClass                = ActorClassWithProtocol(feedClassname, typeof(ISseFeed));
                    var mappedParameterClass     = new Action.MappedParameter("Type", feedClass);
                    var mappedParameterPayload   = new Action.MappedParameter("int", feedPayload);
                    var mappedParameterInterval  = new Action.MappedParameter("int", feedInterval);
                    var mappedParameterDefaultId = new Action.MappedParameter("string", feedDefaultId);

                    var actions = new List <Action>(2);
                    var additionalParameters = new List <Action.MappedParameter> {
                        mappedParameterClass, mappedParameterPayload, mappedParameterInterval, mappedParameterDefaultId
                    };
                    actions.Add(new Action(0, Method.Get.Name, subscribeUri, SsePublisherSubscribeTo, null, additionalParameters));
                    actions.Add(new Action(1, Method.Delete.Name, unsubscribeUri, SsePublisherUnsubscribeTo, null));
                    var resource = ResourceFor(resourceName, typeof(SseStreamResource), handlerPoolSize, actions);
                    sseResourceActions[resourceName] = resource;
                }
                catch (Exception e)
                {
                    Console.WriteLine("vlingo-net/http: Failed to load SSE resource: " + streamResourceName + " because: " + e.Message);
                    Console.WriteLine(e.StackTrace);
                    throw e;
                }
            }

            return(sseResourceActions);
        }