Inheritance: IAtomPubClient, ISyndicationClient
Exemple #1
0
        static public AtomPubClient GetClient()
        {
            AtomPubClient client;

            client = new AtomPubClient();
            client.BypassCacheOnRetrieve = true;

            if (!String.IsNullOrEmpty(user) && !String.IsNullOrEmpty(password))
            {
                client.ServerCredential = new PasswordCredential()
                {
                    UserName = user,
                    Password = password
                };
            }
            else 
            {
                client.ServerCredential = null;
            }

            return client;
        }
        public async Task<List<Resource>> GetResources(Uri collectionUri, ResourceType type)
        {
            var entries = DataCache.Instance.Lookup<SyndicationFeed>(collectionUri.AbsoluteUri);
            if (entries == null)
            {
                AtomPubClient client = new AtomPubClient();
                entries = await client.RetrieveFeedAsync(collectionUri);
                DataCache.Instance.Cache(collectionUri.AbsoluteUri, entries);                    
            }

            var resources = new List<Resource>();
            var resourceIndex = new Dictionary<string, Resource>();
            foreach (var entry in entries.Items)
            {
                var resource = new Resource(this, type);
                resource.Identity = new Uri(entry.Id);
                resource.Title = entry.Title.Text;
                resources.Add(resource);
            }

            // check for a link to more entries            
            var nextLink = entries.Links.FirstOrDefault(l => l.Relationship.Equals("next"));
            if (nextLink != null)
            {
                var nextResourceType = new ResourceType(this);
                nextResourceType.Identity = new Uri("http://www.brightstardb.com/databrowser/core/next");

                var nextResource = new Resource(this, nextResourceType);
                nextResource.Identity = nextLink.Uri;
                nextResource.Title = "More...";

                resources.Add(nextResource);
            }

            return resources;
        }
        public async Task<List<Property>> GetResourceProperties(Resource resource)
        {
            var client = new AtomPubClient();
            var res = DataCache.Instance.Lookup<SyndicationItem>(resource.Identity.AbsoluteUri);
            if (res == null)
            {
                res = await client.RetrieveResourceAsync(resource.Identity);
                DataCache.Instance.Cache(resource.Identity.AbsoluteUri, res);
            }
         
            // process simple properties
            var properties = new List<Property>();

            if (res.Content.Type.Equals("application/xml"))
            {
                var xml = res.Content.Xml;
                foreach (var cn in xml.FirstChild.ChildNodes)
                {
                    try
                    {
                        if (cn.FirstChild == null || cn.FirstChild.NodeValue == null) continue;
                        var val = cn.FirstChild.NodeValue.ToString();
                        if (string.IsNullOrEmpty(val)) continue;
                        var property = new Property(this, null);
                        property.PropertyName = cn.LocalName.ToString();
                        property.PropertyValue = val;
                        property.IsLiteral = true;
                        if (IsPropertyVisible(resource.Type.Identity.ToString(), property.PropertyName)) properties.Add(property);
                    }
                    catch (Exception ex)
                    {
                        var msg = ex.Message;
                    }
                }
            }
            else
            {
                var propertyElements = res.ElementExtensions;
                foreach (var item in propertyElements)
                {
                    if (item.NodeName.ToLower().Equals("properties"))
                    {
                        foreach (var prop in item.ElementExtensions)
                        {
                            if (string.IsNullOrEmpty(prop.NodeValue)) continue;
                            if (prop.ElementExtensions.Count > 0)
                            {
                                foreach (var innerprop in prop.ElementExtensions)
                                {
                                    if (string.IsNullOrEmpty(innerprop.NodeValue)) continue;
                                    var property = new Property(this, null)
                                    {
                                        PropertyName = prop.NodeName + innerprop.NodeName,
                                        PropertyValue = innerprop.NodeValue,
                                        IsLiteral = true
                                    };
                                    properties.Add(property);
                                }
                            }
                            else
                            {
                                var property = new Property(this, null)
                                                   {
                                                       PropertyName = prop.NodeName,
                                                       PropertyValue = prop.NodeValue,
                                                       IsLiteral = true
                                                   };
                                properties.Add(property);
                            }
                        }
                    }
                }
            }
            
            // process link properties
            var links = res.Links.Where(l => l.Relationship.StartsWith(OdataRelationshipRelTypePrefix));
            foreach (var syndicationLink in links)
            {
                // property name
                var propertyName = syndicationLink.Relationship.Substring(OdataRelationshipRelTypePrefix.Length);

                // need to check if we need to load an entry or a feed
                if (syndicationLink.MediaType.ToLower().Contains("type=entry"))
                {
                    try
                    {
                        var relatedResource = await client.RetrieveResourceAsync(syndicationLink.Uri);
                        var property = new Property(this, null)
                                           {
                                               PropertyName = propertyName,
                                               PropertyValue = syndicationLink.Uri.ToString(),
                                               PropertyValueName = relatedResource.Title.Text
                                           };
                        properties.Add(property);
                    }
                    catch (Exception)
                    {
                        // sometimes we get bad data from odata services.
                    }
                }
                else
                {
                    try
                    {
                        AtomPubClient relatedResourceClient = new AtomPubClient();
                        // always ask for four and show three, if we get four then we can have a show all property.
                        var entries = await client.RetrieveFeedAsync(new Uri(syndicationLink.Uri.ToString() + "?$top=4")); 

                        foreach (var entry in entries.Items.Take(3))
                        {
                            var property = new Property(this, null);
                            property.PropertyName = propertyName;
                            property.PropertyValue = entry.EditUri.AbsoluteUri;
                            property.PropertyValueName = entry.Title.Text;
                            properties.Add(property);
                        }

                        if (entries.Items.Count > 3)
                        {
                            var property = new Property(this, null);
                            property.PropertyName = "";
                            property.IsCollectionProperty = true;
                            property.PropertyValue = syndicationLink.Uri.ToString();
                            property.PropertyValueName = "See all related " + propertyName;
                            properties.Add(property);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return properties;
        }
        public async Task<List<ResourceType>> GetResourceTypes()
        {
            try
            {
                // read service document
                var client = new AtomPubClient();
                var serviceDoc = await client.RetrieveServiceDocumentAsync(new Uri(_endpoint));

                // get workspace 0
                var workspace = serviceDoc.Workspaces[0];

                // get collections list
                var resourceTypes = new List<ResourceType>();
                foreach (var collection in workspace.Collections)
                {
                    var rt = new ResourceType(this);
                    rt.Identity = collection.Uri;
                    rt.Title = collection.Title.Text;

                    // check for type annotations
                    var collectionName = collection.NodeValue;
                    if (collectionName != null)
                    {
                        // get type name from collection name
                        string typeName;
                        if (!_collectionToTypeMappings.TryGetValue(collectionName, out typeName))
                        {
                            continue;
                        }

                        // get type annotations
                        var annotations = _annotations.Where(a => a.Target.Equals(typeName)).ToList();
                        if (annotations.Count > 0)
                        {
                            // see if we should hide the type
                            var hideAnnotation = annotations.FirstOrDefault(a => a.Property.Equals(Annotation.Hide));
                            if (hideAnnotation != null && hideAnnotation.Value.Equals("True"))
                            {
                                continue;                                
                            }

                            // try and get a display name
                            var displayNameAnnotation = annotations.FirstOrDefault(a => a.Property.Equals(Annotation.DisplayName));
                            if (displayNameAnnotation != null)
                            {
                                rt.Title = displayNameAnnotation.Value;
                            }
                            
                            // try and get an image
                            var imageAnnotation = annotations.FirstOrDefault(a => a.Property.Equals(Annotation.ThumbnailUrl));
                            if (imageAnnotation != null && Uri.IsWellFormedUriString(imageAnnotation.Value, UriKind.Absolute))
                            {
                                rt.Image = new Uri(imageAnnotation.Value);
                            }
                        }
                    }

                    resourceTypes.Add(rt);
                }

                return resourceTypes;
            } catch(Exception ex)
            {
                return new List<ResourceType>();
            }
        }