Example #1
0
        /// <summary>
        /// Find the OSLC Query Capability URL for a given OSLC resource type.  If no resource type is given, returns
        /// the default Query Capability, if it exists.
        /// </summary>
        /// <param name="serviceProviderUrl"></param>
        /// <param name="oslcDomain"></param>
        /// <param name="oslcResourceType">the resource type of the desired query capability.   This may differ from the OSLC artifact type.</param>
        /// <returns>URL of requested Query Capablility or null if not found.</returns>
        public string LookupQueryCapability(string serviceProviderUrl, string oslcDomain, string oslcResourceType)
        {
            QueryCapability defaultQueryCapability = null;
            QueryCapability firstQueryCapability   = null;

            HttpResponseMessage response = GetResource(serviceProviderUrl, OSLCConstants.CT_RDF);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ResourceNotFoundException(serviceProviderUrl, "QueryCapability");
            }

            ServiceProvider serviceProvider = response.Content.ReadAsAsync <ServiceProvider>(formatters).Result;

            if (serviceProvider != null)
            {
                foreach (Service service in serviceProvider.GetServices())
                {
                    Uri domain = service.GetDomain();
                    if (domain != null && domain.ToString().Equals(oslcDomain))
                    {
                        QueryCapability[] queryCapabilities = service.GetQueryCapabilities();
                        if (queryCapabilities != null && queryCapabilities.Length > 0)
                        {
                            firstQueryCapability = queryCapabilities[0];
                            foreach (QueryCapability queryCapability in service.GetQueryCapabilities())
                            {
                                foreach (Uri resourceType in queryCapability.GetResourceTypes())
                                {
                                    //return as soon as domain + resource type are matched
                                    if (resourceType.ToString() != null && resourceType.ToString().Equals(oslcResourceType))
                                    {
                                        return(queryCapability.GetQueryBase().OriginalString);
                                    }
                                }
                                //Check if this is the default capability
                                foreach (Uri usage in queryCapability.GetUsages())
                                {
                                    if (usage.ToString() != null && usage.ToString().Equals(OSLCConstants.USAGE_DEFAULT_URI))
                                    {
                                        defaultQueryCapability = queryCapability;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //If we reached this point, there was no resource type match
            if (defaultQueryCapability != null)
            {
                //return default, if present
                return(defaultQueryCapability.GetQueryBase().ToString());
            }
            else if (firstQueryCapability != null && firstQueryCapability.GetResourceTypes().Length == 0)
            {
                //return the first for the domain, if present
                return(firstQueryCapability.GetQueryBase().ToString());
            }

            throw new ResourceNotFoundException(serviceProviderUrl, "QueryCapability");
        }
        /// <summary>
        /// Return the registered ServiceProvider's.
        /// </summary>
        /// <returns></returns>
        public ServiceProvider[] GetServiceProviders()
        {
            // We first try for a ServiceProviderCatalog
            ServiceProviderCatalog serviceProviderCatalog = getServiceProviderCatalog();

            if (serviceProviderCatalog != null)
            {
                return(serviceProviderCatalog.GetServiceProviders());
            }

            // Secondly we try for a ServiceProvider which is acting as a ServiceProvider registry
            ServiceProvider serviceProvider = GetServiceProvider();

            if (serviceProvider != null)
            {
                Service[] services = serviceProvider.GetServices();

                if (services != null)
                {
                    QueryCapability firstQueryCapability        = null;
                    QueryCapability firstDefaultQueryCapability = null;

                    for (int serviceIndex = 0;
                         ((serviceIndex < services.Length) &&
                          (firstDefaultQueryCapability == null));
                         serviceIndex++)
                    {
                        Service service = services[serviceIndex];

                        QueryCapability[] queryCapabilities = service.GetQueryCapabilities();

                        if (queryCapabilities != null)
                        {
                            for (int queryCapabilityIndex = 0;
                                 ((queryCapabilityIndex < queryCapabilities.Length) &&
                                  (firstDefaultQueryCapability == null));
                                 queryCapabilityIndex++)
                            {
                                QueryCapability queryCapability = queryCapabilities[queryCapabilityIndex];

                                Uri[] resourceTypes = queryCapability.GetResourceTypes();

                                if (resourceTypes != null)
                                {
                                    for (int resourceTypeIndex = 0;
                                         ((resourceTypeIndex < resourceTypes.Length) &&
                                          (firstDefaultQueryCapability == null));
                                         resourceTypeIndex++)
                                    {
                                        Uri resourceType = resourceTypes[resourceTypeIndex];

                                        if (OslcConstants.TYPE_SERVICE_PROVIDER.Equals(resourceType.ToString()))
                                        {
                                            if (firstQueryCapability == null)
                                            {
                                                firstQueryCapability = queryCapability;
                                            }

                                            Uri[] usages = queryCapability.GetUsages();

                                            for (int usageIndex = 0;
                                                 ((usageIndex < usages.Length) &&
                                                  (firstDefaultQueryCapability == null));
                                                 usageIndex++)
                                            {
                                                Uri usage = usages[usageIndex];

                                                if (OslcConstants.OSLC_USAGE_DEFAULT.Equals(usage.ToString()))
                                                {
                                                    firstDefaultQueryCapability = queryCapability;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (firstQueryCapability != null)
                    {
                        QueryCapability queryCapability = firstDefaultQueryCapability != null ? firstDefaultQueryCapability : firstQueryCapability;

                        Uri queryBase = queryCapability.GetQueryBase();

                        // Foundation Registry Services requires the query string of oslc.select=* in order to flesh out the ServiceProviders
                        string query = queryBase.ToString() + "?oslc.select=*";

                        OslcRestClient oslcRestClient = new OslcRestClient(client.GetFormatters(),
                                                                           query);

                        return(oslcRestClient.GetOslcResources <ServiceProvider>());
                    }
                }
            }

            return(null);
        }