public void SaveDomain(RedirectDomain domain, int userId)
        {
            // Some input validation
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }
            if (String.IsNullOrWhiteSpace(domain.InboundDomain))
            {
                throw new PropertyNotSetException("domain.InboundDomain");
            }
            if (String.IsNullOrWhiteSpace(domain.OutboundDomain))
            {
                throw new PropertyNotSetException("domain.OutboundDomain");
            }

            // Check whether another domain matches the inbound parameters
            RedirectDomain existing = GetDomainByInbound(domain.InboundProtocol, domain.InboundDomain, domain.InboundPort);

            if (existing != null && existing.Id != domain.Id)
            {
                throw new DomainsException("A domain with the same inbound parameters already exists.");
            }

            // Update the timestamp for when the domain was updated
            domain.Updated = DateTime.UtcNow;

            // Update the domain in the database
            Database.Update(domain);

            // Update the domain in the caches across all domains
            DistributedCache.Instance.Refresh(DomainsCacheRefresher.CacheRefresherId, domain.Id);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Refreshes the domain with the specified <paramref name="domainId"/> in the database.
        /// </summary>
        /// <param name="domainId">The ID of the domain.</param>
        public void RefreshDomainInCache(int domainId)
        {
            LogHelper.Info <DomainsRepository>("RefreshDomainInCache - > " + domainId);

            // Remove the domain from the cache (eg. if the inbound values have changed)
            RemoveDomainFromCache(domainId);

            // Get the domain from the service
            RedirectDomain domain = Service.GetDomainById(domainId);

            // Return now if the domain wasn't found in the database
            if (domain == null)
            {
                return;
            }

            // Rebuild the entire cache if not already loaded
            if (!HasCache)
            {
                RebuildCache();
                return;
            }

            // Add/set the domain in the dictionary
            _lookup[GetDomainKey(domain)] = domain;
        }
 /// <summary>
 /// Deletes the specified <paramref name="domain"/>.
 /// </summary>
 /// <param name="domain">The domain to be deleted.</param>
 /// <param name="user">The user responsible for the action.</param>
 public void DeleteDomain(RedirectDomain domain, IUser user)
 {
     if (user == null)
     {
         throw new ArgumentNullException("user");
     }
     DeleteDomain(domain, user.Id);
 }
        public RedirectDomain AddDomain(RedirectProtocol inboundProtocol, string inboundDomain, int inboundPort, RedirectProtocol outboundProtocol, string outboundDomain, int outboundPort, int userId = 0)
        {
            if (String.IsNullOrWhiteSpace(inboundDomain))
            {
                throw new ArgumentNullException("inboundDomain");
            }
            if (String.IsNullOrWhiteSpace(outboundDomain))
            {
                throw new ArgumentNullException("outboundDomain");
            }

            if (!SchemaHelper.TableExist(RedirectDomain.TableName))
            {
                SchemaHelper.CreateTable <RedirectDomain>(false);
            }

            if (GetDomainByInbound(inboundProtocol, inboundDomain, inboundPort) != null)
            {
                throw new DomainsException("A domain with the specified inbound parameters already exists.");
            }

            // Initialize the new domain and populate the properties
            RedirectDomain row = new RedirectDomain {
                UniqueId         = Guid.NewGuid(),
                InboundProtocol  = inboundProtocol,
                InboundDomain    = inboundDomain.Trim().ToLower(),
                InboundPort      = inboundPort,
                OutboundProtocol = outboundProtocol,
                OutboundDomain   = outboundDomain.Trim().ToLower(),
                OutboundPort     = outboundPort,
                StatusCode       = HttpStatusCode.MovedPermanently,
                Created          = DateTime.UtcNow,
                Updated          = DateTime.UtcNow
            };

            // Attempt to add the redirect to the database
            try {
                Database.Insert(row);
            } catch (Exception ex) {
                LogHelper.Error <DomainsRepository>("Unable to insert domain into the database", ex);
                throw new Exception("Unable to insert domain into the database", ex);
            }

            // Get an updated reference to the created domain
            RedirectDomain domain = GetDomainById(row.UniqueId);

            // Update the domain in the caches across all domains
            DistributedCache.Instance.Refresh(DomainsCacheRefresher.CacheRefresherId, domain.Id);

            return(domain);
        }
        /// <summary>
        /// Deletes the specified <paramref name="domain"/>.
        /// </summary>
        /// <param name="domain">The domain to be deleted.</param>
        /// <param name="userId">The ID of the user responsible for the action.</param>
        public void DeleteDomain(RedirectDomain domain, int userId = 0)
        {
            // Some input validation
            if (domain == null)
            {
                throw new ArgumentNullException("domain");
            }

            // Remove the domain from the database
            Database.Delete(domain);

            // Remove the domain from the distributed cache (removing doesn't support GUIDs, so we use the numeric ID instead)
            DistributedCache.Instance.Remove(DomainsCacheRefresher.CacheRefresherId, domain.Id);
        }
Exemplo n.º 6
0
        public bool TryGetDomain(Uri url, out RedirectDomain domain)
        {
            // Parse the inbound protocol
            RedirectProtocol protocol;

            switch (url.Scheme)
            {
            case "http":
                protocol = RedirectProtocol.Http;
                break;

            case "https":
                protocol = RedirectProtocol.Https;
                break;

            default:
                domain = null;
                return(false);
            }

            // Use the method overload for looking up the domain in the cache
            return(TryGetDomain(protocol, url.Host, url.Port, out domain));
        }
Exemplo n.º 7
0
 public bool TryGetDomain(RedirectProtocol protocol, string domainName, int portNumber, out RedirectDomain domain)
 {
     if (!HasCache)
     {
         RebuildCache();
     }
     return(_lookup.TryGetValue(GetDomainKey(protocol, domainName, portNumber), out domain));
 }
Exemplo n.º 8
0
        public bool TryGetDomain(RedirectProtocol protocol, string domainName, out RedirectDomain domain)
        {
            int portNumber = protocol == RedirectProtocol.Http ? 80 : 443;

            return(TryGetDomain(protocol, domainName, portNumber, out domain));
        }
Exemplo n.º 9
0
 private string GetDomainKey(RedirectDomain domain)
 {
     return((domain.InboundProtocol + "__" + domain.InboundDomain + "__" + domain.InboundPort).ToLower());
 }