Beispiel #1
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            HandlersId handlersId = new HandlersId(id);

            Site site = handlersId.SiteId == null ? null : SiteHelper.GetSite(handlersId.SiteId.Value);

            if (handlersId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            // Check for config_scope
            string          configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);;
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, handlersId.Path, configPath);

            HandlersHelper.UpdateFeatureSettings(model, section);

            ManagementUnit.Current.Commit();

            return(HandlersHelper.ToJsonModel(site, handlersId.Path));
        }
Beispiel #2
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.handler == null || !(model.handler is JObject))
            {
                throw new ApiArgumentException("handler");
            }
            string handlersUuid = DynamicHelper.Value(model.handler.id);

            if (handlersUuid == null)
            {
                throw new ApiArgumentException("handler.id");
            }

            // Get the feature id
            HandlersId handlersId = new HandlersId(handlersUuid);

            Site site = handlersId.SiteId == null ? null : SiteHelper.GetSite(handlersId.SiteId.Value);

            string          configPath = ManagementUnit.ResolveConfigScope(model);
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, handlersId.Path, configPath);

            Mapping mapping = MappingsHelper.CreateMapping(model, section);

            MappingsHelper.AddMapping(mapping, section);

            ManagementUnit.Current.Commit();

            dynamic m = MappingsHelper.ToJsonModel(mapping, site, handlersId.Path);

            return(Created(MappingsHelper.GetLocation(m.id), m));
        }
Beispiel #3
0
        public object Patch(string id, [FromBody] dynamic model)
        {
            MappingId mappingId = new MappingId(id);

            Site site = mappingId.SiteId == null ? null : SiteHelper.GetSite(mappingId.SiteId.Value);

            if (mappingId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            string          configPath = ManagementUnit.ResolveConfigScope(model);
            HandlersSection section    = HandlersHelper.GetHandlersSection(site, mappingId.Path, configPath);
            Mapping         mapping    = section.Mappings.FirstOrDefault(u => u.Name.Equals(mappingId.Name));

            if (mapping == null)
            {
                return(NotFound());
            }

            MappingsHelper.UpdateMapping(model, mapping, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic m = MappingsHelper.ToJsonModel(mapping, site, mappingId.Path);

            if (m.id != id)
            {
                return(LocationChanged(MappingsHelper.GetLocation(m.id), m));
            }

            return(m);
        }
        private void ConfigureHandlers()
        {
            Environment.Host.RouteBuilder.MapWebApiRoute(Defines.Resource.Guid, $"{Defines.PATH}/{{id?}}", new { controller = "handlers" });

            Environment.Hal.ProvideLink(Defines.Resource.Guid, "self", handlers => new { href = HandlersHelper.GetLocation(handlers.id) });

            // Web Server
            Environment.Hal.ProvideLink(WebServer.Defines.Resource.Guid, Defines.Resource.Name, _ => {
                var id = GetHandlersId(null, null);
                return(new { href = HandlersHelper.GetLocation(id.Uuid) });
            });

            // Site
            Environment.Hal.ProvideLink(Sites.Defines.Resource.Guid, Defines.Resource.Name, site => {
                var siteId = new SiteId((string)site.id);
                Site s     = SiteHelper.GetSite(siteId.Id);
                var id     = GetHandlersId(s, "/");
                return(new { href = HandlersHelper.GetLocation(id.Uuid) });
            });

            // Application
            Environment.Hal.ProvideLink(Applications.Defines.Resource.Guid, Defines.Resource.Name, app => {
                var appId = new ApplicationId((string)app.id);
                Site s    = SiteHelper.GetSite(appId.SiteId);
                var id    = GetHandlersId(s, appId.Path);
                return(new { href = HandlersHelper.GetLocation(id.Uuid) });
            });
        }
Beispiel #5
0
        public object Get(string id)
        {
            HandlersId handlersId = new HandlersId(id);

            Site site = handlersId.SiteId == null ? null : SiteHelper.GetSite(handlersId.SiteId.Value);

            if (handlersId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            return(HandlersHelper.ToJsonModel(site, handlersId.Path));
        }
Beispiel #6
0
        public object Get()
        {
            Site   site = ApplicationHelper.ResolveSite();
            string path = ApplicationHelper.ResolvePath();

            if (path == null)
            {
                return(NotFound());
            }

            dynamic d = HandlersHelper.ToJsonModel(site, path);

            return(LocationChanged(HandlersHelper.GetLocation(d.id), d));
        }
Beispiel #7
0
        public void Delete(string id)
        {
            HandlersId handlersId = new HandlersId(id);

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;

            Site site = (handlersId.SiteId != null) ? SiteHelper.GetSite(handlersId.SiteId.Value) : null;

            if (site == null)
            {
                return;
            }

            HandlersSection section = HandlersHelper.GetHandlersSection(site, handlersId.Path, ManagementUnit.ResolveConfigScope());

            section.RevertToParent();

            ManagementUnit.Current.Commit();
        }
Beispiel #8
0
        public void Delete(string id)
        {
            MappingId mappingId = new MappingId(id);

            Site site = mappingId.SiteId == null ? null : SiteHelper.GetSite(mappingId.SiteId.Value);

            if (mappingId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                return;
            }

            Mapping mapping = MappingsHelper.GetMappings(site, mappingId.Path).FirstOrDefault(u => u.Name.Equals(mappingId.Name));

            if (mapping != null)
            {
                var section = HandlersHelper.GetHandlersSection(site, mappingId.Path, ManagementUnit.ResolveConfigScope());

                MappingsHelper.DeleteMapping(mapping, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
        public static List <Mapping> GetMappings(Site site, string path, string configPath = null)
        {
            HandlersSection section = HandlersHelper.GetHandlersSection(site, path, configPath);

            return(section.Mappings.ToList());
        }
        internal static object ToJsonModel(Mapping mapping, Site site, string path, Fields fields = null, bool full = true)
        {
            if (mapping == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj = new ExpandoObject();

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = mapping.Name;
            }

            //
            // id
            obj.id = new MappingId(site?.Id, path, mapping.Name).Uuid;

            //
            // path
            if (fields.Exists("path"))
            {
                obj.path = mapping.Path;
            }

            //
            // verbs
            if (fields.Exists("verbs"))
            {
                obj.verbs = mapping.Verb;
            }

            //
            // type
            if (fields.Exists("type"))
            {
                obj.type = mapping.Type;
            }

            //
            // modules
            if (fields.Exists("modules"))
            {
                obj.modules = mapping.Modules;
            }

            //
            // script_processor
            if (fields.Exists("script_processor"))
            {
                obj.script_processor = mapping.ScriptProcessor;
            }

            //
            // resource_type
            if (fields.Exists("resource_type"))
            {
                obj.resource_type = Enum.GetName(typeof(ResourceType), mapping.ResourceType).ToLower();
            }

            //
            // require_access
            if (fields.Exists("require_access"))
            {
                obj.require_access = Enum.GetName(typeof(HandlerRequiredAccess), mapping.RequireAccess).ToLower();
            }

            //
            // allow_path_info
            if (fields.Exists("allow_path_info"))
            {
                obj.allow_path_info = mapping.AllowPathInfo;
            }

            //
            // precondition
            if (fields.Exists("precondition"))
            {
                obj.precondition = mapping.PreCondition;
            }

            //
            // response_buffer_limit
            if (fields.Exists("response_buffer_limit"))
            {
                obj.response_buffer_limit = mapping.ResponseBufferLimit;
            }

            //
            // handler
            if (fields.Exists("handler"))
            {
                obj.handler = HandlersHelper.ToJsonModelRef(site, path);
            }

            return(Core.Environment.Hal.Apply(Defines.MappingsResource.Guid, obj, full));
        }
 private HandlersId GetHandlersId(Site s, string path)
 {
     return(new HandlersId(s?.Id, path, HandlersHelper.IsSectionLocal(s, path)));
 }