Пример #1
0
        public static HeaderLimit UpdateHeaderLimit(HeaderLimit headerLimit, dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (headerLimit == null)
            {
                throw new ApiArgumentException("headerLimit");
            }
            string header = DynamicHelper.Value(model.header);

            if (header == string.Empty)
            {
                throw new ApiArgumentException("header");
            }

            try {
                headerLimit.Header    = DynamicHelper.Value(model.header) ?? headerLimit.Header;
                headerLimit.SizeLimit = DynamicHelper.To(model.size_limit, 0, uint.MaxValue) ?? headerLimit.SizeLimit;
            }
            catch (FileLoadException e) {
                throw new LockedException(RequestFilteringGlobals.RequestFilteringSectionName, e);
            }

            return(headerLimit);
        }
        public object Patch(string id, [FromBody] dynamic model)
        {
            HeaderLimitId headerLimitId = new HeaderLimitId(id);

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

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

            string      configPath  = model == null ? null : ManagementUnit.ResolveConfigScope(model);
            HeaderLimit headerLimit = HeaderLimitsHelper.GetHeaderLimits(site, headerLimitId.Path, configPath)
                                      .FirstOrDefault(h => h.Header.ToString().Equals(headerLimitId.Header));

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

            headerLimit = HeaderLimitsHelper.UpdateHeaderLimit(headerLimit, model);

            ManagementUnit.Current.Commit();

            dynamic head = HeaderLimitsHelper.ToJsonModel(headerLimit, site, headerLimitId.Path);

            if (head.id != id)
            {
                return(LocationChanged(HeaderLimitsHelper.GetLocation(head.id), head));
            }

            return(head);
        }
Пример #3
0
        public static void AddHeaderLimit(HeaderLimit headerLimit, RequestFilteringSection section)
        {
            if (headerLimit == null)
            {
                throw new ArgumentNullException("headerLimit");
            }
            if (headerLimit.Header == null)
            {
                throw new ArgumentNullException("headerLimit.Header");
            }

            HeaderLimitCollection collection = section.RequestLimits.HeaderLimits;

            if (collection.Any(h => h.Header.Equals(headerLimit.Header)))
            {
                throw new AlreadyExistsException("headerLimit");
            }

            try {
                collection.Add(headerLimit);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Пример #4
0
        public static void DeleteHeaderLimit(HeaderLimit headerLimit, RequestFilteringSection section)
        {
            if (headerLimit == null)
            {
                return;
            }

            HeaderLimitCollection collection = section.RequestLimits.HeaderLimits;

            // To utilize the remove functionality we must pull the element directly from the collection
            headerLimit = collection.FirstOrDefault(h => h.Header.Equals(headerLimit.Header));

            if (headerLimit != null)
            {
                try {
                    collection.Remove(headerLimit);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
Пример #5
0
        public static object ToJsonModelRef(HeaderLimit headerLimit, Site site, string path)
        {
            if (headerLimit == null)
            {
                return(null);
            }

            HeaderLimitId id = new HeaderLimitId(site?.Id, path, headerLimit.Header);

            var obj = new {
                header     = headerLimit.Header,
                id         = id.Uuid,
                size_limit = headerLimit.SizeLimit
            };

            return(Core.Environment.Hal.Apply(Defines.HeaderLimitsResource.Guid, obj, false));
        }
Пример #6
0
        internal static object ToJsonModel(HeaderLimit headerLimit, Site site, string path)
        {
            if (headerLimit == null)
            {
                return(null);
            }

            HeaderLimitId id = new HeaderLimitId(site?.Id, path, headerLimit.Header);

            var obj = new {
                header            = headerLimit.Header,
                id                = id.Uuid,
                size_limit        = headerLimit.SizeLimit,
                request_filtering = RequestFilteringHelper.ToJsonModelRef(site, path)
            };

            return(Core.Environment.Hal.Apply(Defines.HeaderLimitsResource.Guid, obj));
        }
        public object Post([FromBody] dynamic model)
        {
            HeaderLimit        headerLimit = null;
            Site               site        = null;
            RequestFilteringId reqId       = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_filtering == null)
            {
                throw new ApiArgumentException("request_filtering");
            }
            if (!(model.request_filtering is JObject))
            {
                throw new ApiArgumentException(String.Empty, "request_filtering");
            }
            string reqUuid = DynamicHelper.Value(model.request_filtering.id);

            if (reqUuid == null)
            {
                throw new ApiArgumentException("request_filtering.id");
            }

            reqId = new RequestFilteringId(reqUuid);

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

            string configPath = ManagementUnit.ResolveConfigScope(model);
            RequestFilteringSection section = RequestFilteringHelper.GetRequestFilteringSection(site, reqId.Path, configPath);

            headerLimit = HeaderLimitsHelper.CreateHeaderLimit(model, section);

            HeaderLimitsHelper.AddHeaderLimit(headerLimit, section);

            ManagementUnit.Current.Commit();

            dynamic header_limit = HeaderLimitsHelper.ToJsonModel(headerLimit, site, reqId.Path);

            return(Created(HeaderLimitsHelper.GetLocation(header_limit.id), header_limit));
        }
        public object Get(string id)
        {
            HeaderLimitId headerId = new HeaderLimitId(id);

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

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

            HeaderLimit headerLimit = HeaderLimitsHelper.GetHeaderLimits(site, headerId.Path).FirstOrDefault(h => h.Header.Equals(headerId.Header));

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

            return(HeaderLimitsHelper.ToJsonModel(headerLimit, site, headerId.Path));
        }
Пример #9
0
        public static HeaderLimit CreateHeaderLimit(dynamic model, RequestFilteringSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string header = DynamicHelper.Value(model.header);

            if (string.IsNullOrEmpty(header))
            {
                throw new ApiArgumentException("header");
            }

            HeaderLimit headerLimit = section.RequestLimits.HeaderLimits.CreateElement();

            headerLimit.Header = header;

            UpdateHeaderLimit(headerLimit, model);

            return(headerLimit);
        }
        public void Delete(string id)
        {
            HeaderLimitId headerId = new HeaderLimitId(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;
            }

            HeaderLimit headerLimit = HeaderLimitsHelper.GetHeaderLimits(site, headerId.Path).Where(h => h.Header.ToString().Equals(headerId.Header)).FirstOrDefault();

            if (headerLimit != null)
            {
                var section = RequestFilteringHelper.GetRequestFilteringSection(site, headerId.Path, ManagementUnit.ResolveConfigScope());

                HeaderLimitsHelper.DeleteHeaderLimit(headerLimit, section);
                ManagementUnit.Current.Commit();
            }

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