public object AddRedirect(int rootNodeId, string url, string linkMode, int linkId, string linkUrl, string linkName = null)
        {
            try {
                RedirectLinkItem redirect = RedirectLinkItem.Parse(new JObject {
                    { "id", linkId },
                    { "name", linkName + "" },
                    { "url", linkUrl },
                    { "mode", linkMode }
                });

                return(Repository.AddRedirect(rootNodeId, url, redirect));
            } catch (RedirectsException ex) {
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }
        public object EditRedirect(int rootNodeId, string redirectId, string url, string linkMode, int linkId, string linkUrl, string linkName = null)
        {
            try {
                // Get the redirect from the database
                RedirectItem redirect = Repository.GetRedirectById(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                if (String.IsNullOrWhiteSpace(url))
                {
                    throw new RedirectsException("You must specify a URL for the redirect.");
                }
                if (String.IsNullOrWhiteSpace(linkUrl))
                {
                    throw new RedirectsException("You must specify a destination link for the redirect.");
                }
                if (String.IsNullOrWhiteSpace(linkMode))
                {
                    throw new RedirectsException("You must specify a destination link for the redirect.");
                }

                // Initialize a new link picker item
                RedirectLinkItem link = RedirectLinkItem.Parse(new JObject {
                    { "id", linkId },
                    { "name", linkName + "" },
                    { "url", linkUrl },
                    { "mode", linkMode }
                });

                string[] urlParts = url.Split('?');
                url = urlParts[0].TrimEnd('/');
                string query = urlParts.Length == 2 ? urlParts[1] : "";

                redirect.RootNodeId  = rootNodeId;
                redirect.Url         = url;
                redirect.QueryString = query;
                redirect.Link        = link;

                Repository.SaveRedirect(redirect);

                return(redirect);
            } catch (RedirectsException ex) {
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }
Ejemplo n.º 3
0
        public object AddRedirect(int rootNodeId, string url, string linkMode, int linkId, string linkUrl, string linkName = null, bool permanent = true, bool regex = false, bool forward = false)
        {
            try {
                // Some input validation
                if (String.IsNullOrWhiteSpace(url))
                {
                    throw new RedirectsException(Localize("redirects/errorNoUrl"));
                }
                if (String.IsNullOrWhiteSpace(linkUrl))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }
                if (String.IsNullOrWhiteSpace(linkMode))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }

                // Parse the link mode
                RedirectLinkMode mode;
                switch (linkMode)
                {
                case "content": mode = RedirectLinkMode.Content; break;

                case "media": mode = RedirectLinkMode.Media; break;

                case "url": mode = RedirectLinkMode.Url; break;

                default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode"));
                }

                // Initialize a new link item
                RedirectLinkItem destination = new RedirectLinkItem(linkId, linkName, linkUrl, mode);

                // Add the redirect
                RedirectItem redirect = Repository.AddRedirect(rootNodeId, url, destination, permanent, regex, forward);

                // Return the redirect
                return(redirect);
            } catch (RedirectsException ex) {
                // Generate the error response
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }
Ejemplo n.º 4
0
        public object EditRedirect(int rootNodeId, string redirectId, string url, string linkMode, int linkId, string linkUrl, string linkName = null, bool permanent = true, bool regex = false, bool forward = false)
        {
            try {
                // Get a reference to the redirect
                RedirectItem redirect = Repository.GetRedirectById(redirectId);
                if (redirect == null)
                {
                    throw new RedirectNotFoundException();
                }

                // Some input validation
                if (String.IsNullOrWhiteSpace(url))
                {
                    throw new RedirectsException(Localize("redirects/errorNoUrl"));
                }
                if (String.IsNullOrWhiteSpace(linkUrl))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }
                if (String.IsNullOrWhiteSpace(linkMode))
                {
                    throw new RedirectsException(Localize("redirects/errorNoDestination"));
                }

                // Parse the link mode
                RedirectLinkMode mode;
                switch (linkMode)
                {
                case "content": mode = RedirectLinkMode.Content; break;

                case "media": mode = RedirectLinkMode.Media; break;

                case "url": mode = RedirectLinkMode.Url; break;

                default: throw new RedirectsException(Localize("redirects/errorUnknownLinkMode"));
                }

                // Initialize a new link item
                RedirectLinkItem destination = new RedirectLinkItem(linkId, linkName, linkUrl, mode);

                // Split the URL and query string
                string[] urlParts = url.Split('?');
                url = urlParts[0].TrimEnd('/');
                string query = urlParts.Length == 2 ? urlParts[1] : "";

                // Update the properties of the redirect
                redirect.RootNodeId         = rootNodeId;
                redirect.Url                = url;
                redirect.QueryString        = query;
                redirect.Link               = destination;
                redirect.IsPermanent        = permanent;
                redirect.IsRegex            = regex;
                redirect.ForwardQueryString = forward;

                // Save/update the redirect
                Repository.SaveRedirect(redirect);

                // Return the redirect
                return(redirect);
            } catch (RedirectsException ex) {
                // Generate the error response
                return(Request.CreateResponse(JsonMetaResponse.GetError(HttpStatusCode.InternalServerError, ex.Message)));
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance based on the specified <see cref="JObject"/>.
 /// </summary>
 /// <param name="obj">An instance of <see cref="JObject"/> representing the redirect.</param>
 protected OutboundRedirect(JObject obj)
 {
     JObject     = obj;
     IsPermanent = obj.GetBoolean("permanent");
     Link        = obj.GetObject(obj.HasValue("items") ? "items.items[0]" : "link", RedirectLinkItem.Parse) ?? new RedirectLinkItem();
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance with an empty model.
 /// </summary>
 public OutboundRedirect()
 {
     IsPermanent = true;
     Link        = new RedirectLinkItem();
 }