コード例 #1
0
        public object Patch([FromBody] dynamic model, string id)
        {
            var outboundRuleId = new OutboundRuleId(id);

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

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

            OutboundRulesSection section = OutboundRulesHelper.GetSection(site, outboundRuleId.Path);
            OutboundRule         rule    = (OutboundRule)section.Rules.FirstOrDefault(r => r.Name.Equals(outboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

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

            OutboundRulesHelper.UpdateRule(model, rule, section);

            ManagementUnit.Current.Commit();

            dynamic updatedRule = OutboundRulesHelper.RuleToJsonModel(rule, site, outboundRuleId.Path, Context.Request.GetFields(), true);

            if (updatedRule.id != id)
            {
                return(LocationChanged(OutboundRulesHelper.GetRuleLocation(updatedRule.id), updatedRule));
            }

            return(updatedRule);
        }
コード例 #2
0
        public object Get(string id)
        {
            var outboundRuleId = new OutboundRuleId(id);

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

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

            OutboundRule rule = (OutboundRule)OutboundRulesHelper.GetSection(site, outboundRuleId.Path).Rules.FirstOrDefault(r => r.Name.Equals(outboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

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

            return(OutboundRulesHelper.RuleToJsonModel(rule, site, outboundRuleId.Path, Context.Request.GetFields()));
        }
コード例 #3
0
        public void Delete(string id)
        {
            OutboundRule rule           = null;
            var          outboundRuleId = new OutboundRuleId(id);

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

            if (outboundRuleId.SiteId == null || site != null)
            {
                rule = (OutboundRule)OutboundRulesHelper.GetSection(site, outboundRuleId.Path).Rules.FirstOrDefault(r => r.Name.Equals(outboundRuleId.Name, StringComparison.OrdinalIgnoreCase));
            }

            if (rule != null)
            {
                var section = OutboundRulesHelper.GetSection(site, outboundRuleId.Path, ManagementUnit.ResolveConfigScope());

                OutboundRulesHelper.DeleteRule(rule, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
コード例 #4
0
        public static object RuleToJsonModel(OutboundRule rule, Site site, string path, Fields fields = null, bool full = true)
        {
            if (rule == null)
            {
                return(null);
            }

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

            var outboundRuleId = new OutboundRuleId(site?.Id, path, rule.Name);
            var section        = GetSection(site, path);
            OutboundRuleMatchType matchType = GetMatchType(rule);

            dynamic obj = new ExpandoObject();

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

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = outboundRuleId.Uuid;
            }

            //
            // priority
            if (fields.Exists("priority"))
            {
                obj.priority = GetSection(site, path).Rules.IndexOf(rule);
            }

            // precondition
            if (fields.Exists("precondition"))
            {
                var precondition = section.PreConditions.FirstOrDefault(pc => pc.Name.Equals(rule.PreCondition, StringComparison.OrdinalIgnoreCase));
                obj.precondition = PreConditionToJsonModelRef(precondition, site, path, fields.Filter("precondition"));
            }

            // match_type
            if (fields.Exists("match_type"))
            {
                obj.match_type = OutboundMatchTypeHelper.ToJsonModel(matchType);
            }

            // server_variable
            if (fields.Exists("server_variable") && matchType == OutboundRuleMatchType.ServerVariable)
            {
                obj.server_variable = string.IsNullOrEmpty(rule.Match.ServerVariable) ? null : rule.Match.ServerVariable;
            }

            // tag_filters
            if (fields.Exists("tag_filters") && matchType == OutboundRuleMatchType.Response)
            {
                obj.tag_filters = CreateTagsModel(rule.Match.FilterByTags);

                TagsElement customTags = rule.Match.FilterByTags.HasFlag(FilterByTags.CustomTags) ?
                                         section.Tags.FirstOrDefault(t => t.Name.Equals(rule.Match.CustomTags, StringComparison.OrdinalIgnoreCase)) :
                                         null;

                obj.tag_filters.custom = customTags == null ? null : TagsToJsonModelRef(customTags, site, path, fields.Filter("tag_filters.custom"));
            }

            //
            // pattern
            if (fields.Exists("pattern"))
            {
                obj.pattern = rule.Match.Pattern;
            }

            //
            // pattern_syntax
            if (fields.Exists("pattern_syntax"))
            {
                obj.pattern_syntax = PatternSyntaxHelper.ToJsonModel(rule.PatternSyntax);
            }

            //
            // ignore_case
            if (fields.Exists("ignore_case"))
            {
                obj.ignore_case = rule.Match.IgnoreCase;
            }

            //
            // negate
            if (fields.Exists("negate"))
            {
                obj.negate = rule.Match.Negate;
            }

            //
            // stop_processing
            if (fields.Exists("stop_processing"))
            {
                obj.stop_processing = rule.StopProcessing;
            }

            //
            // enabled
            if (fields.Exists("enabled"))
            {
                obj.enabled = rule.Action.Type == OutboundActionType.Rewrite ? true : false;
            }

            //
            // rewrite_value
            if (fields.Exists("rewrite_value"))
            {
                obj.rewrite_value = rule.Action.RewriteValue;
            }

            //
            // replace_server_variable
            if (fields.Exists("replace_server_variable") && matchType == OutboundRuleMatchType.ServerVariable)
            {
                obj.replace_server_variable = rule.Action.ReplaceServerVariable;
            }

            //
            // condition_match_constraints
            if (fields.Exists("condition_match_constraints"))
            {
                obj.condition_match_constraints = LogicalGroupingHelper.ToJsonModel(rule.Conditions.LogicalGrouping);
            }

            //
            // track_all_captures
            if (fields.Exists("track_all_captures"))
            {
                obj.track_all_captures = rule.Conditions.TrackAllCaptures;
            }

            //
            // conditions
            if (fields.Exists("conditions"))
            {
                obj.conditions = rule.Conditions.Select(c => new {
                    input       = c.Input,
                    pattern     = c.Pattern,
                    negate      = c.Negate,
                    ignore_case = c.IgnoreCase,
                    match_type  = MatchTypeHelper.ToJsonModel(c.MatchType)
                });
            }

            //
            // url_rewrite
            if (fields.Exists("url_rewrite"))
            {
                obj.url_rewrite = RewriteHelper.ToJsonModelRef(site, path, fields.Filter("url_rewrite"));
            }

            return(Core.Environment.Hal.Apply(Defines.OutboundRulesResource.Guid, obj, full));
        }