Ejemplo n.º 1
0
        public static void AddMap(RewriteMap map, RewriteMapsSection section)
        {
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            if (map.Name == null)
            {
                throw new ArgumentNullException("map.Name");
            }

            if (section.RewriteMaps.Any(m => m.Name.Equals(map.Name)))
            {
                throw new AlreadyExistsException("map");
            }

            try {
                section.RewriteMaps.Add(map);
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Ejemplo n.º 2
0
        public object Patch([FromBody] dynamic model, string id)
        {
            var rewriteMapId = new RewriteMapId(id);

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

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

            RewriteMapsSection section = RewriteMapsHelper.GetSection(site, rewriteMapId.Path);
            RewriteMap         map     = section.RewriteMaps.FirstOrDefault(r => r.Name.Equals(rewriteMapId.Name, StringComparison.OrdinalIgnoreCase));

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

            RewriteMapsHelper.UpdateMap(model, map, section);

            ManagementUnit.Current.Commit();

            dynamic updatedMap = RewriteMapsHelper.MapToJsonModel(map, site, rewriteMapId.Path, Context.Request.GetFields(), true);

            if (updatedMap.id != id)
            {
                return(LocationChanged(RewriteMapsHelper.GetMapLocation(updatedMap.id), updatedMap));
            }

            return(updatedMap);
        }
Ejemplo n.º 3
0
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteId parentId = RewriteHelper.GetRewriteIdFromBody(model);

            if (parentId == null)
            {
                throw new ApiArgumentException("url_rewrite");
            }

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

            string             configPath = ManagementUnit.ResolveConfigScope(model);
            RewriteMapsSection section    = RewriteMapsHelper.GetSection(site, parentId.Path, configPath);

            RewriteMap map = RewriteMapsHelper.CreateMap(model, section);

            RewriteMapsHelper.AddMap(map, section);

            ManagementUnit.Current.Commit();

            dynamic r = RewriteMapsHelper.MapToJsonModel(map, site, parentId.Path, Context.Request.GetFields(), true);

            return(Created(RewriteMapsHelper.GetMapLocation(r.id), r));
        }
Ejemplo n.º 4
0
        public static RewriteMap CreateMap(dynamic model, RewriteMapsSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            if (string.IsNullOrEmpty(DynamicHelper.Value(model.name)))
            {
                throw new ApiArgumentException("name");
            }

            RewriteMap map = section.RewriteMaps.CreateElement();

            SetMap(model, map, section);

            return(map);
        }
Ejemplo n.º 5
0
        public static void DeleteMap(RewriteMap map, RewriteMapsSection section)
        {
            if (map == null)
            {
                return;
            }

            map = section.RewriteMaps.FirstOrDefault(r => r.Name.Equals(map.Name));

            if (map != null)
            {
                try {
                    section.RewriteMaps.Remove(map);
                }
                catch (FileLoadException e) {
                    throw new LockedException(section.SectionPath, e);
                }
                catch (DirectoryNotFoundException e) {
                    throw new ConfigScopeNotFoundException(e);
                }
            }
        }
Ejemplo n.º 6
0
        public static void UpdateSection(dynamic model, Site site, string path, string configPath = null)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteMapsSection section = GetSection(site, path, configPath);

            try {
                if (model.metadata != null)
                {
                    DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => {
                        section.OverrideMode = v;
                    });
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Ejemplo n.º 7
0
        private static void SetMap(dynamic model, RewriteMap map, RewriteMapsSection section)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            try {
                //
                // Name, check for already existing name
                string name = DynamicHelper.Value(model.name);
                if (!string.IsNullOrEmpty(name))
                {
                    if (!name.Equals(map.Name, StringComparison.OrdinalIgnoreCase) &&
                        section.RewriteMaps.Any(r => r.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
                    {
                        throw new AlreadyExistsException("name");
                    }

                    map.Name = name;
                }

                DynamicHelper.If((object)model.default_value, v => map.DefaultValue    = v);
                DynamicHelper.If <bool>((object)model.ignore_case, v => map.IgnoreCase = v);

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

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

                    map.KeyValuePairCollection.Clear();

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

                        string itemName = DynamicHelper.Value(item.name);
                        string value    = DynamicHelper.Value(item.value);

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

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

                        KeyValueElement kvp = map.KeyValuePairCollection.CreateElement();
                        kvp.Key   = itemName;
                        kvp.Value = value;

                        map.KeyValuePairCollection.Add(kvp);
                    }
                }
            }
            catch (FileLoadException e) {
                throw new LockedException(section.SectionPath, e);
            }
            catch (DirectoryNotFoundException e) {
                throw new ConfigScopeNotFoundException(e);
            }
        }
Ejemplo n.º 8
0
 public static void UpdateMap(dynamic model, RewriteMap map, RewriteMapsSection section)
 {
     SetMap(model, map, section);
 }