コード例 #1
0
ファイル: OslcClient.cs プロジェクト: nenaaki/oslc4net
        /// <summary>
        /// Find the OSLC Creation Factory URL for a given OSLC resource type.  If no resource type is given, returns
        /// the default Creation Factory, 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 Creation Factory or null if not found.</returns>
        public string LookupCreationFactory(string serviceProviderUrl, string oslcDomain, string oslcResourceType)
        {
            CreationFactory defaultCreationFactory = null;
            CreationFactory firstCreationFactory   = null;

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

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

            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))
                    {
                        CreationFactory[] creationFactories = service.GetCreationFactories();
                        if (creationFactories != null && creationFactories.Length > 0)
                        {
                            firstCreationFactory = creationFactories[0];
                            foreach (CreationFactory creationFactory in creationFactories)
                            {
                                foreach (Uri resourceType in creationFactory.GetResourceTypes())
                                {
                                    //return as soon as domain + resource type are matched
                                    if (resourceType.ToString() != null && resourceType.ToString().Equals(oslcResourceType))
                                    {
                                        return(creationFactory.GetCreation().ToString());
                                    }
                                }
                                //Check if this is the default factory
                                foreach (Uri usage in creationFactory.GetUsages())
                                {
                                    if (usage.ToString() != null && usage.ToString().Equals(OSLCConstants.USAGE_DEFAULT_URI))
                                    {
                                        defaultCreationFactory = creationFactory;
                                    }
                                }
                            }
                        }
                    }
                }
            }

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

            throw new ResourceNotFoundException(serviceProviderUrl, "CreationFactory");
        }
コード例 #2
0
        /// <summary>
        /// Register a ServiceProvider
        /// </summary>
        /// <param name="serviceProviderToRegister"></param>
        /// <returns></returns>
        public Uri registerServiceProvider(ServiceProvider serviceProviderToRegister)
        {
            Uri typeServiceProviderURI = new Uri(OslcConstants.TYPE_SERVICE_PROVIDER);
            Uri oslcUsageDefault       = new Uri(OslcConstants.OSLC_USAGE_DEFAULT);

            ServiceProvider[] serviceProviders;

            // We have to first get the ServiceProvider for ServiceProviders and then find the CreationFactory for a ServiceProvider

            // We first try for a ServiceProviderCatalog
            ServiceProviderCatalog serviceProviderCatalog = getServiceProviderCatalog();

            if (serviceProviderCatalog != null)
            {
                serviceProviders = serviceProviderCatalog.GetServiceProviders();
            }
            else
            {
                // Secondly we try for a ServiceProvider which is acting as a ServiceProvider registry
                ServiceProvider serviceProvider = GetServiceProvider();

                if (serviceProvider != null)
                {
                    serviceProviders = new ServiceProvider[] { serviceProvider };
                }
                else
                {
                    throw new OslcCoreRegistrationException(serviceProviderToRegister,
                                                            (int)HttpStatusCode.NotFound,
                                                            "ServiceProviderCatalog");
                }
            }

            if (serviceProviders != null)
            {
                CreationFactory firstCreationFactory        = null;
                CreationFactory firstDefaultCreationFactory = null;

                for (int serviceProviderIndex = 0;
                     ((serviceProviderIndex < serviceProviders.Length) &&
                      (firstDefaultCreationFactory == null));
                     serviceProviderIndex++)
                {
                    ServiceProvider serviceProvider = serviceProviders[serviceProviderIndex];

                    Service[] services = serviceProvider.GetServices();

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

                            CreationFactory[] creationFactories = service.GetCreationFactories();

                            if (creationFactories != null)
                            {
                                for (int creationFactoryIndex = 0;
                                     ((creationFactoryIndex < creationFactories.Length) &&
                                      (firstDefaultCreationFactory == null));
                                     creationFactoryIndex++)
                                {
                                    CreationFactory creationFactory = creationFactories[creationFactoryIndex];

                                    Uri[] resourceTypes = creationFactory.GetResourceTypes();

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

                                            if (typeServiceProviderURI.Equals(resourceType))
                                            {
                                                if (firstCreationFactory == null)
                                                {
                                                    firstCreationFactory = creationFactory;
                                                }

                                                Uri[] usages = creationFactory.GetUsages();

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

                                                    if (oslcUsageDefault.Equals(usage))
                                                    {
                                                        firstDefaultCreationFactory = creationFactory;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (firstCreationFactory != null)
                {
                    CreationFactory creationFactory = firstDefaultCreationFactory != null ? firstDefaultCreationFactory : firstCreationFactory;

                    Uri creation = creationFactory.GetCreation();

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

                    HttpResponseMessage clientResponse = oslcRestClient.AddOslcResourceReturnClientResponse(serviceProviderToRegister);

                    HttpStatusCode statusCode = clientResponse.StatusCode;

                    if (statusCode != HttpStatusCode.Created)
                    {
                        throw new OslcCoreRegistrationException(serviceProviderToRegister,
                                                                (int)statusCode,
                                                                clientResponse.ReasonPhrase);
                    }

                    return(clientResponse.Headers.Location);
                }
            }

            throw new OslcCoreRegistrationException(serviceProviderToRegister,
                                                    (int)HttpStatusCode.NotFound,
                                                    "CreationFactory");
        }