public object Patch([FromBody] dynamic model, string id)
        {
            var inboundRuleId = new InboundRuleId(id);

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

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

            InboundRulesSection section = InboundRulesHelper.GetSection(site, inboundRuleId.Path);
            InboundRule         rule    = (InboundRule)section.InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

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

            InboundRulesHelper.UpdateRule(model, rule, site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope(model));

            ManagementUnit.Current.Commit();

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

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

            return(updatedRule);
        }
        public object Get(string id)
        {
            var inboundRuleId = new InboundRuleId(id);

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

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

            InboundRule rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

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

            return(InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields()));
        }
        public void Delete(string id)
        {
            InboundRule rule          = null;
            var         inboundRuleId = new InboundRuleId(id);

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

            if (inboundRuleId.SiteId == null || site != null)
            {
                rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));
            }

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

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

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
Exemple #4
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));
        }