Example #1
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);
        }
Example #2
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));
        }
 public static object MapToJsonModelRef(RewriteMap map, Site site, string path, Fields fields = null)
 {
     if (fields == null || !fields.HasFields)
     {
         return(MapToJsonModel(map, site, path, RewriteMapRefFields, false));
     }
     else
     {
         return(MapToJsonModel(map, site, path, fields, false));
     }
 }
        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);
        }
Example #5
0
        public object Get(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());
            }

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

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

            return(RewriteMapsHelper.MapToJsonModel(map, site, rewriteMapId.Path, Context.Request.GetFields()));
        }
        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);
                }
            }
        }
Example #7
0
        public void Delete(string id)
        {
            RewriteMap map          = null;
            var        rewriteMapId = new RewriteMapId(id);

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

            if (rewriteMapId.SiteId == null || site != null)
            {
                map = RewriteMapsHelper.GetSection(site, rewriteMapId.Path).RewriteMaps.FirstOrDefault(m => m.Name.Equals(rewriteMapId.Name, StringComparison.OrdinalIgnoreCase));
            }

            if (map != null)
            {
                var section = RewriteMapsHelper.GetSection(site, rewriteMapId.Path, ManagementUnit.ResolveConfigScope());

                RewriteMapsHelper.DeleteMap(map, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
        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);
            }
        }
 public static void UpdateMap(dynamic model, RewriteMap map, RewriteMapsSection section)
 {
     SetMap(model, map, section);
 }
        public static object MapToJsonModel(RewriteMap map, Site site, string path, Fields fields = null, bool full = true)
        {
            if (map == null)
            {
                return(null);
            }

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

            var rewriteMapId = new RewriteMapId(site?.Id, path, map.Name);
            var section      = GetSection(site, path);

            dynamic obj = new ExpandoObject();

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

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

            //
            // default_value
            if (fields.Exists("default_value"))
            {
                obj.default_value = map.DefaultValue;
            }

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

            //
            // mappings
            if (fields.Exists("mappings"))
            {
                obj.mappings = map.KeyValuePairCollection.Select(kvp => new {
                    name  = kvp.Key,
                    value = kvp.Value
                });
            }

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

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