예제 #1
0
        public static Site ResolveSite(dynamic model = null)
        {
            Site   site     = null;
            string scope    = null;
            string siteUuid = null;

            // Resolve from model
            if (model != null)
            {
                //
                // website.id
                if (model.website != null)
                {
                    if (!(model.website is JObject))
                    {
                        throw new ApiArgumentException("website");
                    }

                    siteUuid = DynamicHelper.Value(model.website.id);
                }

                //
                // scope
                if (model.scope != null)
                {
                    scope = DynamicHelper.Value(model.scope);
                }
            }

            var context = HttpHelper.Current;

            //
            // Resolve {site_id} from query string
            if (siteUuid == null)
            {
                siteUuid = context.Request.Query[Defines.IDENTIFIER];
            }

            if (!string.IsNullOrEmpty(siteUuid))
            {
                SiteId siteId = new SiteId(siteUuid);

                site = SiteHelper.GetSite(new SiteId(siteUuid).Id);
                if (site == null)
                {
                    throw new NotFoundException("site");
                }

                return(site);
            }


            //
            // Resolve {scope} from query string
            if (scope == null)
            {
                scope = context.Request.Query[SCOPE_KEY];
            }

            if (!string.IsNullOrEmpty(scope))
            {
                int    index    = scope.IndexOf('/');
                string siteName = index >= 0 ? scope.Substring(0, index) : scope;

                site = ManagementUnit.Current.ServerManager.Sites.FirstOrDefault(s => s.Name.Equals(siteName, StringComparison.OrdinalIgnoreCase));

                // Scope points to non existant site
                if (site == null)
                {
                    throw new ScopeNotFoundException(scope);
                }
            }

            return(site);
        }
예제 #2
0
        internal static object ToJsonModel(Site site, Fields fields = null, bool full = true)
        {
            if (site == null)
            {
                return(null);
            }

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

            dynamic obj    = new ExpandoObject();
            var     siteId = new SiteId(site.Id);

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

            //
            // id
            obj.id = siteId.Uuid;

            //
            // physical_path
            if (fields.Exists("physical_path"))
            {
                string      physicalPath = string.Empty;
                Application rootApp      = site.Applications["/"];

                if (rootApp != null && rootApp.VirtualDirectories["/"] != null)
                {
                    physicalPath = rootApp.VirtualDirectories["/"].PhysicalPath;
                }

                obj.physical_path = physicalPath;
            }

            //
            // key
            if (fields.Exists("key"))
            {
                obj.key = siteId.Id;
            }

            //
            // status
            if (fields.Exists("status"))
            {
                // Prepare state
                Status state = Status.Unknown;
                try {
                    state = StatusExtensions.FromObjectState(site.State);
                }
                catch (COMException) {
                    // Problem getting state of site. Possible reasons:
                    // 1. Site's application pool was deleted.
                    // 2. Site was just created and the status is not accessible yet.
                }
                obj.status = Enum.GetName(typeof(Status), state).ToLower();
            }

            //
            // server_auto_start
            if (fields.Exists("server_auto_start"))
            {
                obj.server_auto_start = site.ServerAutoStart;
            }

            //
            // enabled_protocols
            if (fields.Exists("enabled_protocols"))
            {
                Application rootApp = site.Applications["/"];
                obj.enabled_protocols = rootApp == null ? string.Empty : rootApp.EnabledProtocols;
            }

            //
            // limits
            if (fields.Exists("limits"))
            {
                dynamic limits = new ExpandoObject();

                limits.connection_timeout = site.Limits.ConnectionTimeout.TotalSeconds;
                limits.max_bandwidth      = site.Limits.MaxBandwidth;
                limits.max_connections    = site.Limits.MaxConnections;

                if (site.Limits.Schema.HasAttribute(MaxUrlSegmentsAttribute))
                {
                    limits.max_url_segments = site.Limits.MaxUrlSegments;
                }

                obj.limits = limits;
            }

            //
            // bindings
            if (fields.Exists("bindings"))
            {
                obj.bindings = site.Bindings.Select(b => ToJsonModel(b));
            }

            //
            // application_pool
            if (fields.Exists("application_pool"))
            {
                Application rootApp = site.Applications["/"];
                var         pool    = rootApp != null?AppPoolHelper.GetAppPool(rootApp.ApplicationPoolName) : null;

                obj.application_pool = (pool == null) ? null : AppPoolHelper.ToJsonModelRef(pool, fields.Filter("application_pool"));
            }

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