Пример #1
0
        private static void AssignRuleFromModel(dynamic model, InboundRule rule, InboundRulesSection section, AllowedServerVariablesSection serverVariablesSection)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            //
            // Name, check for already existing name
            string name = DynamicHelper.Value(model.name);

            if (!string.IsNullOrEmpty(name))
            {
                if (!name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase) &&
                    section.InboundRules.Any(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new AlreadyExistsException("name");
                }

                rule.Name = name;
            }

            DynamicHelper.If((object)model.pattern, v => rule.Match.Pattern = v);
            DynamicHelper.If <bool>((object)model.ignore_case, v => rule.Match.IgnoreCase   = v);
            DynamicHelper.If <bool>((object)model.negate, v => rule.Match.Negate            = v);
            DynamicHelper.If <bool>((object)model.stop_processing, v => rule.StopProcessing = v);
            DynamicHelper.If((object)model.pattern_syntax, v => rule.PatternSyntax          = PatternSyntaxHelper.FromJsonModel(v));

            //
            // Action
            dynamic action = model.action;

            if (action != null)
            {
                if (!(action is JObject))
                {
                    throw new ApiArgumentException("action", ApiArgumentException.EXPECTED_OBJECT);
                }

                DynamicHelper.If((object)action.type, v => rule.Action.Type = ActionTypeHelper.FromJsonModel(v));
                DynamicHelper.If((object)action.url, v => rule.Action.Url   = v);
                DynamicHelper.If <bool>((object)action.append_query_string, v => rule.Action.AppendQueryString = v);
                DynamicHelper.If <bool>((object)action.log_rewritten_url, v => rule.Action.LogRewrittenUrl     = v);
                DynamicHelper.If <long>((object)action.status_code, v => rule.Action.StatusCode        = v);
                DynamicHelper.If <long>((object)action.sub_status_code, v => rule.Action.SubStatusCode = v);
                DynamicHelper.If((object)action.description, v => rule.Action.StatusDescription        = v);
                DynamicHelper.If((object)action.reason, v => rule.Action.StatusReason = v);
                DynamicHelper.If <RedirectType>((object)action.redirect_type, v => rule.Action.RedirectType = v);
            }

            //
            // Server variables
            if (model.server_variables != null)
            {
                IEnumerable <dynamic> serverVariables = model.server_variables as IEnumerable <dynamic>;

                if (serverVariables == null)
                {
                    throw new ApiArgumentException("server_variables", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.ServerVariableAssignments.Clear();

                foreach (dynamic serverVariable in serverVariables)
                {
                    if (!(serverVariable is JObject))
                    {
                        throw new ApiArgumentException("server_variables.item");
                    }

                    string svName    = DynamicHelper.Value(serverVariable.name);
                    string svValue   = DynamicHelper.Value(serverVariable.value);
                    bool   svReplace = DynamicHelper.To <bool>(serverVariable.replace) ?? false;

                    if (string.IsNullOrEmpty(svName))
                    {
                        throw new ApiArgumentException("server_variables.item.name", "Required");
                    }

                    if (string.IsNullOrEmpty(svValue))
                    {
                        throw new ApiArgumentException("server_variables.item.value", "Required");
                    }

                    var svAssignment = rule.ServerVariableAssignments.CreateElement();
                    svAssignment.Name    = svName;
                    svAssignment.Value   = svValue;
                    svAssignment.Replace = svReplace;

                    AddAllowedServerVariable(serverVariablesSection, svAssignment.Name);

                    rule.ServerVariableAssignments.Add(svAssignment);
                }
            }

            DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
            DynamicHelper.If <bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures  = v);

            //
            // Conditions
            if (model.conditions != null)
            {
                IEnumerable <dynamic> conditions = model.conditions as IEnumerable <dynamic>;

                if (conditions == null)
                {
                    throw new ApiArgumentException("conditions", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.Conditions.Clear();

                foreach (dynamic condition in conditions)
                {
                    if (!(condition is JObject))
                    {
                        throw new ApiArgumentException("conditions.item");
                    }

                    string input        = DynamicHelper.Value(condition.input);
                    string rawMatchType = DynamicHelper.Value(condition.match_type);

                    if (string.IsNullOrEmpty(input))
                    {
                        throw new ApiArgumentException("conditions.item.input", "Required");
                    }

                    if (string.IsNullOrEmpty(rawMatchType))
                    {
                        throw new ApiArgumentException("conditions.item.match_type", "Required");
                    }

                    MatchType matchType = MatchTypeHelper.FromJsonModel(rawMatchType);

                    var con = rule.Conditions.CreateElement();
                    con.Input      = input;
                    con.MatchType  = matchType;
                    con.Pattern    = DynamicHelper.Value(condition.pattern);
                    con.Negate     = DynamicHelper.To <bool>(condition.negate);
                    con.IgnoreCase = DynamicHelper.To <bool>(condition.ignore_case);

                    rule.Conditions.Add(con);
                }
            }

            if (rule.Schema.HasAttribute(InboundRule.ResponseCacheDirectiveAttribute))
            {
                DynamicHelper.If((object)model.response_cache_directive, v => rule.ResponseCacheDirective = ResponseCacheDirectiveHelper.FromJsonModel(v));
            }

            //
            // Check set to valid state
            if ((rule.Action.Type == ActionType.Redirect || rule.Action.Type == ActionType.Rewrite) && string.IsNullOrEmpty(rule.Action.Url))
            {
                throw new ApiArgumentException("action.url");
            }

            UpdatePriority(model, rule, section);
        }
Пример #2
0
        public static object RuleToJsonModel(InboundRule rule, Site site, string path, Fields fields = null, bool full = true)
        {
            if (rule == null)
            {
                return(null);
            }

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

            var inboundRuleId = new InboundRuleId(site?.Id, path, rule.Name);

            dynamic obj = new ExpandoObject();

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

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

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

            //
            // 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;
            }

            //
            // response_cache_directive
            if (fields.Exists("response_cache_directive") && rule.Schema.HasAttribute(InboundRule.ResponseCacheDirectiveAttribute))
            {
                obj.response_cache_directive = ResponseCacheDirectiveHelper.ToJsonModel(rule.ResponseCacheDirective);
            }

            //
            // 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;
            }

            //
            // action
            if (fields.Exists("action"))
            {
                obj.action = new ExpandoObject();
                dynamic action = obj.action;

                action.type = ActionTypeHelper.ToJsonModel(rule.Action.Type);
                action.url  = rule.Action.Url;
                action.append_query_string = rule.Action.AppendQueryString;
                action.log_rewritten_url   = rule.Action.LogRewrittenUrl;

                if (rule.Action.Type == ActionType.Redirect)
                {
                    action.redirect_type = Enum.GetName(typeof(RedirectType), rule.Action.RedirectType).ToLowerInvariant();
                }

                if (rule.Action.Type == ActionType.CustomResponse)
                {
                    action.status_code     = rule.Action.StatusCode;
                    action.sub_status_code = rule.Action.SubStatusCode;
                    action.description     = rule.Action.StatusDescription;
                    action.reason          = rule.Action.StatusReason;
                }
            }

            //
            // server_variables
            if (fields.Exists("server_variables"))
            {
                obj.server_variables = rule.ServerVariableAssignments.Select(s => new {
                    name    = s.Name,
                    value   = s.Value,
                    replace = s.Replace
                });
            }

            //
            // 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.InboundRulesResource.Guid, obj));
        }
        private static void SetPreCondition(dynamic model, PreCondition precondition, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

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

                    precondition.Name = name;
                }

                //
                // Match (Logical Grouping)
                DynamicHelper.If((object)model.match, v => {
                    switch (v.ToLowerInvariant())
                    {
                    case "all":
                        precondition.LogicalGrouping = LogicalGrouping.MatchAll;
                        break;

                    case "any":
                        precondition.LogicalGrouping = LogicalGrouping.MatchAny;
                        break;

                    default:
                        throw new ApiArgumentException("match");
                    }
                });

                // Pattern Syntax
                DynamicHelper.If((object)model.pattern_syntax, v => precondition.PatternSyntax = PatternSyntaxHelper.FromJsonModel(v));

                //
                // requirements
                if (model.requirements != null)
                {
                    IEnumerable <dynamic> requirements = model.requirements as IEnumerable <dynamic>;

                    if (requirements == null)
                    {
                        throw new ApiArgumentException("requirements", ApiArgumentException.EXPECTED_ARRAY);
                    }

                    precondition.Conditions.Clear();

                    foreach (dynamic requirement in requirements)
                    {
                        if (!(requirement is JObject))
                        {
                            throw new ApiArgumentException("requirements.item");
                        }

                        string input = DynamicHelper.Value(requirement.input);

                        if (string.IsNullOrEmpty(input))
                        {
                            throw new ApiArgumentException("requirements.item.input", "Required");
                        }

                        var req = precondition.Conditions.CreateElement();
                        req.Input      = input;
                        req.Pattern    = DynamicHelper.Value(requirement.pattern);
                        req.Negate     = DynamicHelper.To <bool>(requirement.negate);
                        req.IgnoreCase = DynamicHelper.To <bool>(requirement.ignore_case);

                        // Schema only specifies pattern match type for outbound rules
                        req.MatchType = PreConditionMatchType.Pattern;

                        precondition.Conditions.Add(req);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
        private static void AssignRuleFromModel(dynamic model, OutboundRule rule, OutboundRulesSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            //
            // Name, check for already existing name
            string name = DynamicHelper.Value(model.name);

            if (!string.IsNullOrEmpty(name))
            {
                if (!name.Equals(rule.Name, StringComparison.OrdinalIgnoreCase) &&
                    section.Rules.Any(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                {
                    throw new AlreadyExistsException("name");
                }

                rule.Name = name;
            }

            DynamicHelper.If((object)model.pattern, v => rule.Match.Pattern                 = v);
            DynamicHelper.If <bool>((object)model.enabled, v => rule.Action.Type            = v ? OutboundActionType.Rewrite : OutboundActionType.None);
            DynamicHelper.If((object)model.rewrite_value, v => rule.Action.RewriteValue     = v);
            DynamicHelper.If <bool>((object)model.ignore_case, v => rule.Match.IgnoreCase   = v);
            DynamicHelper.If <bool>((object)model.negate, v => rule.Match.Negate            = v);
            DynamicHelper.If <bool>((object)model.stop_processing, v => rule.StopProcessing = v);
            DynamicHelper.If((object)model.pattern_syntax, v => rule.PatternSyntax          = PatternSyntaxHelper.FromJsonModel(v));

            //
            // Server Variable
            DynamicHelper.If((object)model.server_variable, v => rule.Match.ServerVariable = v);
            DynamicHelper.If <bool>((object)model.replace_server_variable, v => rule.Action.ReplaceServerVariable = v);

            //
            // Html Tags
            dynamic tagFilters = null;
            dynamic customTags = null;

            if (model.tag_filters != null)
            {
                tagFilters = model.tag_filters;

                if (!(tagFilters is JObject))
                {
                    throw new ApiArgumentException("tag_filters", ApiArgumentException.EXPECTED_OBJECT);
                }

                customTags = tagFilters.custom;

                // Clear custom tags
                rule.Match.CustomTags    = null;
                rule.Match.FilterByTags &= ~FilterByTags.CustomTags;
            }

            // Set standard tags
            if (tagFilters != null)
            {
                FilterByTags ruleTags = rule.Match.FilterByTags;

                DynamicHelper.If <bool>((object)tagFilters.a, v => SetTagFlag(ref ruleTags, FilterByTags.A, v));
                DynamicHelper.If <bool>((object)tagFilters.area, v => SetTagFlag(ref ruleTags, FilterByTags.Area, v));
                DynamicHelper.If <bool>((object)tagFilters.@base, v => SetTagFlag(ref ruleTags, FilterByTags.Base, v));
                DynamicHelper.If <bool>((object)tagFilters.form, v => SetTagFlag(ref ruleTags, FilterByTags.Form, v));
                DynamicHelper.If <bool>((object)tagFilters.frame, v => SetTagFlag(ref ruleTags, FilterByTags.Frame, v));
                DynamicHelper.If <bool>((object)tagFilters.head, v => SetTagFlag(ref ruleTags, FilterByTags.Head, v));
                DynamicHelper.If <bool>((object)tagFilters.iframe, v => SetTagFlag(ref ruleTags, FilterByTags.IFrame, v));
                DynamicHelper.If <bool>((object)tagFilters.img, v => SetTagFlag(ref ruleTags, FilterByTags.Img, v));
                DynamicHelper.If <bool>((object)tagFilters.input, v => SetTagFlag(ref ruleTags, FilterByTags.Input, v));
                DynamicHelper.If <bool>((object)tagFilters.link, v => SetTagFlag(ref ruleTags, FilterByTags.Link, v));
                DynamicHelper.If <bool>((object)tagFilters.script, v => SetTagFlag(ref ruleTags, FilterByTags.Script, v));

                rule.Match.FilterByTags = ruleTags;
            }

            // Set custom tags
            if (customTags != null)
            {
                if (!(customTags is JObject))
                {
                    throw new ApiArgumentException("tags.custom", ApiArgumentException.EXPECTED_OBJECT);
                }

                string ctId = DynamicHelper.Value(customTags.id);

                if (string.IsNullOrEmpty(ctId))
                {
                    throw new ArgumentException("tags.custom.id", "required");
                }

                TagsElement targetCustomTags = section.Tags.FirstOrDefault(t => t.Name.Equals(new CustomTagsId(ctId).Name, StringComparison.OrdinalIgnoreCase));

                if (targetCustomTags == null)
                {
                    throw new NotFoundException("tags.custom");
                }

                rule.Match.FilterByTags |= FilterByTags.CustomTags;
                rule.Match.CustomTags    = targetCustomTags.Name;
            }

            if (model.precondition != null)
            {
                dynamic precondition = model.precondition;

                if (!(precondition is JObject))
                {
                    throw new ApiArgumentException("precondition", ApiArgumentException.EXPECTED_OBJECT);
                }

                string id = DynamicHelper.Value(precondition.id);

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

                PreConditionId preconditionId = new PreConditionId(id);

                PreCondition pc = section.PreConditions.FirstOrDefault(p => p.Name.Equals(preconditionId.Name, StringComparison.OrdinalIgnoreCase));

                if (pc == null)
                {
                    throw new NotFoundException("precondition.id");
                }

                rule.PreCondition = pc.Name;
            }

            DynamicHelper.If((object)model.condition_match_constraints, v => rule.Conditions.LogicalGrouping = LogicalGroupingHelper.FromJsonModel(v));
            DynamicHelper.If <bool>((object)model.track_all_captures, v => rule.Conditions.TrackAllCaptures  = v);

            //
            // Conditions
            if (model.conditions != null)
            {
                IEnumerable <dynamic> conditions = model.conditions as IEnumerable <dynamic>;

                if (conditions == null)
                {
                    throw new ApiArgumentException("conditions", ApiArgumentException.EXPECTED_ARRAY);
                }

                rule.Conditions.Clear();

                foreach (dynamic condition in conditions)
                {
                    if (!(condition is JObject))
                    {
                        throw new ApiArgumentException("server_variables.item");
                    }

                    string input = DynamicHelper.Value(condition.input);

                    if (string.IsNullOrEmpty(input))
                    {
                        throw new ApiArgumentException("conditions.item.input", "Required");
                    }

                    var con = rule.Conditions.CreateElement();
                    con.Input      = input;
                    con.Pattern    = DynamicHelper.Value(condition.pattern);
                    con.Negate     = DynamicHelper.To <bool>(condition.negate);
                    con.IgnoreCase = DynamicHelper.To <bool>(condition.ignore_case);

                    // Schema only specifies pattern match type for outbound rules
                    con.MatchType = MatchType.Pattern;

                    rule.Conditions.Add(con);
                }
            }

            //
            // Set match type
            string type = DynamicHelper.Value(model.match_type);
            OutboundRuleMatchType matchType = string.IsNullOrEmpty(type) ? GetMatchType(rule) : OutboundMatchTypeHelper.FromJsonModel(type);

            if (matchType == OutboundRuleMatchType.Response)
            {
                rule.Match.ServerVariable = null;
            }
            else
            {
                rule.Match.FilterByTags = FilterByTags.None;
            }

            UpdatePriority(model, rule, section);
        }
        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));
        }
        public static object PreConditionToJsonModel(PreCondition precondition, Site site, string path, Fields fields = null, bool full = true)
        {
            if (precondition == null)
            {
                return(null);
            }

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

            dynamic obj = new ExpandoObject();

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

            //
            // id
            if (fields.Exists("id"))
            {
                obj.id = new PreConditionId(site?.Id, path, precondition.Name).Uuid;
            }

            //
            // match
            if (fields.Exists("match"))
            {
                obj.match = precondition.LogicalGrouping == LogicalGrouping.MatchAll ? "all" : "any";
            }

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

            //
            // requirements
            if (fields.Exists("requirements"))
            {
                obj.requirements = precondition.Conditions.Select(c => new {
                    input       = c.Input,
                    pattern     = c.Pattern,
                    negate      = c.Negate,
                    ignore_case = c.IgnoreCase
                });
            }

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

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