public static void Add(NameValueConfigurationElement header, HttpProtocolSection section)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            if (section.RedirectHeaders.FirstOrDefault(h => h.Name.Equals(header.Name, StringComparison.OrdinalIgnoreCase)) != null)
            {
                throw new AlreadyExistsException("header");
            }

            try {
                section.RedirectHeaders.Add(header);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        public static void Delete(NameValueConfigurationElement header, HttpProtocolSection section)
        {
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }

            if (header == null)
            {
                return;
            }

            // Need to pull the element out of the collection we are removing it from
            header = section.CustomHeaders.FirstOrDefault(h => h.Name.Equals(header.Name, StringComparison.OrdinalIgnoreCase));

            if (header != null)
            {
                try {
                    section.CustomHeaders.Remove(header);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
Пример #3
0
        public object Patch(string id, dynamic model)
        {
            CustomHeaderId headerId = new CustomHeaderId(id);

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

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

            NameValueConfigurationElement header = CustomHeadersHelper.GetCustomHeaders(site, headerId.Path).FirstOrDefault(h => h.Name.Equals(headerId.Name, StringComparison.OrdinalIgnoreCase));

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

            var configScope = ManagementUnit.ResolveConfigScope(model);
            var section     = HttpResponseHeadersHelper.GetSection(site, headerId.Path, configScope);

            CustomHeadersHelper.Update(header, model, section);

            ManagementUnit.Current.Commit();

            dynamic ch = CustomHeadersHelper.ToJsonModel(header, site, headerId.Path);

            if (ch.id != id)
            {
                return(LocationChanged(CustomHeadersHelper.GetLocation(ch.id), ch));
            }

            return(ch);
        }
        public static object ToJsonModelRef(NameValueConfigurationElement header, Site site, string path)
        {
            if (header == null)
            {
                return(null);
            }

            RedirectHeaderId id = new RedirectHeaderId(site?.Id, path, header.Name);

            var obj = new {
                name  = header.Name,
                value = header.Value,
                id    = id.Uuid
            };

            return(Core.Environment.Hal.Apply(Defines.RedirectHeadersResource.Guid, obj, false));
        }
        internal static object ToJsonModel(NameValueConfigurationElement header, Site site, string path)
        {
            if (header == null)
            {
                return(null);
            }

            RedirectHeaderId id = new RedirectHeaderId(site?.Id, path, header.Name);

            var obj = new {
                name  = header.Name,
                value = header.Value,
                id    = id.Uuid,
                http_response_headers = HttpResponseHeadersHelper.ToJsonModelRef(site, path),
            };

            return(Core.Environment.Hal.Apply(Defines.RedirectHeadersResource.Guid, obj));
        }
Пример #6
0
        public object Get(string id)
        {
            CustomHeaderId headerId = new CustomHeaderId(id);

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

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

            NameValueConfigurationElement header = CustomHeadersHelper.GetCustomHeaders(site, headerId.Path).FirstOrDefault(h => h.Name.Equals(headerId.Name, StringComparison.OrdinalIgnoreCase));

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

            return(CustomHeadersHelper.ToJsonModel(header, site, headerId.Path));
        }
Пример #7
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.http_response_headers == null)
            {
                throw new ApiArgumentException("http_response_headers");
            }
            if (!(model.http_response_headers is JObject))
            {
                throw new ApiArgumentException("http_response_headers");
            }

            string uuid = DynamicHelper.Value(model.http_response_headers.id);

            if (uuid == null)
            {
                throw new ApiArgumentException("http_response_headers.id");
            }

            HttpResponseHeadersId id = new HttpResponseHeadersId(uuid);

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

            string configPath           = ManagementUnit.ResolveConfigScope(model);
            HttpProtocolSection section = HttpResponseHeadersHelper.GetSection(site, id.Path, configPath);

            NameValueConfigurationElement header = CustomHeadersHelper.Create(model, section);

            CustomHeadersHelper.Add(header, section);

            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic ch = CustomHeadersHelper.ToJsonModel(header, site, id.Path);

            return(Created(CustomHeadersHelper.GetLocation(ch.id), ch));
        }
        public static void Update(NameValueConfigurationElement header, dynamic model, HttpProtocolSection section)
        {
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }
            if (section == null)
            {
                throw new ArgumentNullException("section");
            }
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            try {
                string name = DynamicHelper.Value(model.name);
                if (!string.IsNullOrEmpty(name))
                {
                    if (section.RedirectHeaders.Any(h => h.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new AlreadyExistsException("header.name");
                    }

                    header.Name = name;
                }

                header.Value = DynamicHelper.Value(model.value) ?? header.Value;
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Пример #9
0
        public void Delete(string id)
        {
            var headerId = new CustomHeaderId(id);

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

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

            NameValueConfigurationElement header = CustomHeadersHelper.GetCustomHeaders(site, headerId.Path).FirstOrDefault(h => h.Name.Equals(headerId.Name, StringComparison.OrdinalIgnoreCase));

            if (header != null)
            {
                var section = HttpResponseHeadersHelper.GetSection(site, headerId.Path, ManagementUnit.ResolveConfigScope());

                CustomHeadersHelper.Delete(header, section);
                ManagementUnit.Current.Commit();
            }

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