protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, 
            CancellationToken cancellationToken) {

            UrlHelper url = request.GetUrlHelper();
            ServiceDocument doc = new ServiceDocument();
            Workspace ws = new Workspace() {

                Title = new TextSyndicationContent("My Site"),
                BaseUri = new Uri(request.RequestUri.GetLeftPart(UriPartial.Authority))
            };

            ws.Collections.Add(GetPostsResourceCollectionInfo(url));
            ws.Collections.Add(GetMediaResourceCollectionInfo(url));
            doc.Workspaces.Add(ws);

            HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);
            var formatter = new AtomPub10ServiceDocumentFormatter(doc);

            var stream = new MemoryStream();
            using (var writer = XmlWriter.Create(stream)) {
                formatter.WriteTo(writer);
            }

            stream.Position = 0;
            var content = new StreamContent(stream);
            response.Content = content;
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/atomsvc+xml");

            return Task.FromResult(response);
        }
 protected static ResourceCollectionInfo CreateCollection(Workspace workspace)
 {
     if (workspace == null)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     return workspace.CreateResourceCollection();
 }
 internal static void LoadElementExtensions(XmlBuffer buffer, XmlDictionaryWriter writer, Workspace workspace)
 {
     if (workspace == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     Atom10FeedFormatter.CloseBuffer(buffer, writer);
     workspace.LoadElementExtensions(buffer);
 }
        public HttpResponseMessage Get()
        {
            var doc = new ServiceDocument();
            var ws = new Workspace
            {
                Title = new TextSyndicationContent("My Site"),
                BaseUri = new Uri(Request.RequestUri.GetLeftPart(UriPartial.Authority))
            };

            var posts = new ResourceCollectionInfo("Blog",
                new Uri(Url.Link("DefaultApi", new { controller = "posts" })));

            posts.Accepts.Add("application/atom+xml;type=entry");

            // For WLW to work we need to include format in the categories URI.
            // Hoping to provide a better solution than this.
            var categoriesUri = new Uri(Url.Link("DefaultApi", new { controller = "tags", format = "atomcat" }));
            var categories = new ReferencedCategoriesDocument(categoriesUri);
            posts.Categories.Add(categories);

            ws.Collections.Add(posts);

            doc.Workspaces.Add(ws);

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            var formatter = new AtomPub10ServiceDocumentFormatter(doc);

            var stream = new MemoryStream();
            using (var writer = XmlWriter.Create(stream))
            {
                formatter.WriteTo(writer);
            }

            stream.Position = 0;
            var content = new StreamContent(stream);
            response.Content = content;
            response.Content.Headers.ContentType =
                new MediaTypeHeaderValue("application/atomsvc+xml");

            return response;
        }
Example #5
0
		//=============== Workspace service

		internal static AtomPub10ServiceDocumentFormatter BuildWorkspace(string repositoryId)
		{
			//var xmlns = new XmlSerializerNamespaces();
			//xmlns.Add("app", Workspace.APPNAMESPACE);
			//xmlns.Add("atom", Workspace.ATOMNAMESPACE);
			//xmlns.Add("cmis", Workspace.CMISNAMESPACE);

			var baseUri = GetBaseUri();

			var workspace = new Workspace("Main Repository", GetResourceCollections(baseUri, repositoryId));
			var repInfo =  new RepositoryInfo
			{
				Id = repositoryId,
				Name = "MainRep",
				Relationship = enumRepositoryRelationship.self,
				Description = "Main Repository",
				VendorName = "Sense/Net Ltd.",
				ProductName = "SenseNet Content Repository Prototype",
				ProductVersion = "0.01",
				RootFolderId = "2",
				Capabilities = new RepositoryCapabilities
				{
					Multifiling = false,
					Unfiling = true,
					VersionSpecificFiling = false,
					PWCUpdateable = false,
					AllVersionsSearchable = false,
					Join = enumCapabilityJoin.nojoin,
					FullText = enumCapabilityFullText.none
				},
				CmisVersionsSupported = "0.5"
			};
			workspace.ElementExtensions.Add(repInfo, new XmlSerializer(typeof(RepositoryInfo)));

			var serviceDoc = new ServiceDocument(new Workspace[] { workspace });
			var formatter = new AtomPub10ServiceDocumentFormatter(serviceDoc);
			return formatter;
		}
        public HttpResponseMessage GetServiceDoc(HttpRequestMessage request)
        {
            string baseUrl = request.BaseUrl("servicedoc");

            ServiceDocument doc = new ServiceDocument();
            var postCollection = new ResourceCollectionInfo()
            {
                Title = new TextSyndicationContent("Posts"),
                Link = new Uri(string.Format("{0}/posts", baseUrl))
            };
            postCollection.Accepts.Add("application/atom+xml;type=entry");

            var wspace = new Workspace() { Title = new TextSyndicationContent("The Blog") };
            wspace.Collections.Add(postCollection);

            doc.Workspaces.Add(wspace);

            return new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content = new ObjectContent(typeof(AtomPub10ServiceDocumentFormatter), doc.GetFormatter())
            };
        }
        private Workspace ReadWorkspace(XmlReader reader, ServiceDocument document)
        {
            Workspace result = CreateWorkspace(document);

            result.BaseUri = document.BaseUri;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        result.BaseUri = FeedUtils.CombineXmlBase(result.BaseUri, reader.Value);
                    }
                    else
                    {
                        string ns   = reader.NamespaceURI;
                        string name = reader.LocalName;
                        if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                        {
                            continue;
                        }

                        string val = reader.Value;
                        if (!TryParseAttribute(name, ns, val, result, Version))
                        {
                            result.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }

            XmlBuffer           buffer    = null;
            XmlDictionaryWriter extWriter = null;

            reader.ReadStartElement();
            try
            {
                while (reader.IsStartElement())
                {
                    if (reader.IsStartElement(Atom10Constants.TitleTag, Atom10Constants.Atom10Namespace))
                    {
                        result.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/atom:title[@type]", preserveAttributeExtensions: true);
                    }
                    else if (reader.IsStartElement(App10Constants.Collection, App10Constants.Namespace))
                    {
                        result.Collections.Add(ReadCollection(reader, result));
                    }
                    else if (!TryParseElement(reader, result, Version))
                    {
                        SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, _maxExtensionSize);
                    }
                }
                LoadElementExtensions(buffer, extWriter, result);
            }
            finally
            {
                extWriter?.Close();
            }

            reader.ReadEndElement();
            return(result);
        }
 protected static void WriteElementExtensions(XmlWriter writer, Workspace workspace, string version)
 {
     workspace.WriteElementExtensions (writer, version);
 }
 protected static bool TryParseAttribute(string name, string ns, string value, Workspace workspace, string version)
 {
     return workspace.TryParseAttribute (name, ns, value, version);
 }
 private ResourceCollectionInfo ReadCollection(XmlReader reader, Workspace workspace)
 {
     CreateInlineCategoriesDelegate inlineCategoriesFactory = null;
     CreateReferencedCategoriesDelegate referencedCategoriesFactory = null;
     ResourceCollectionInfo result = ServiceDocumentFormatter.CreateCollection(workspace);
     result.BaseUri = workspace.BaseUri;
     if (reader.HasAttributes)
     {
         while (reader.MoveToNextAttribute())
         {
             if ((reader.LocalName == "base") && (reader.NamespaceURI == "http://www.w3.org/XML/1998/namespace"))
             {
                 result.BaseUri = FeedUtils.CombineXmlBase(result.BaseUri, reader.Value);
             }
             else
             {
                 if ((reader.LocalName == "href") && (reader.NamespaceURI == string.Empty))
                 {
                     result.Link = new Uri(reader.Value, UriKind.RelativeOrAbsolute);
                     continue;
                 }
                 string namespaceURI = reader.NamespaceURI;
                 string localName = reader.LocalName;
                 if (!FeedUtils.IsXmlns(localName, namespaceURI) && !FeedUtils.IsXmlSchemaType(localName, namespaceURI))
                 {
                     string str3 = reader.Value;
                     if (!ServiceDocumentFormatter.TryParseAttribute(localName, namespaceURI, str3, result, this.Version))
                     {
                         if (this.preserveAttributeExtensions)
                         {
                             result.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                             continue;
                         }
                         SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                     }
                 }
             }
         }
     }
     XmlBuffer buffer = null;
     XmlDictionaryWriter extWriter = null;
     reader.ReadStartElement();
     try
     {
         while (reader.IsStartElement())
         {
             if (reader.IsStartElement("title", "http://www.w3.org/2005/Atom"))
             {
                 result.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/app:collection/atom:title[@type]", this.preserveAttributeExtensions);
             }
             else
             {
                 if (reader.IsStartElement("categories", "http://www.w3.org/2007/app"))
                 {
                     if (inlineCategoriesFactory == null)
                     {
                         inlineCategoriesFactory = () => ServiceDocumentFormatter.CreateInlineCategories(result);
                     }
                     if (referencedCategoriesFactory == null)
                     {
                         referencedCategoriesFactory = () => ServiceDocumentFormatter.CreateReferencedCategories(result);
                     }
                     result.Categories.Add(ReadCategories(reader, result.BaseUri, inlineCategoriesFactory, referencedCategoriesFactory, this.Version, this.preserveElementExtensions, this.preserveAttributeExtensions, this.maxExtensionSize));
                     continue;
                 }
                 if (reader.IsStartElement("accept", "http://www.w3.org/2007/app"))
                 {
                     result.Accepts.Add(reader.ReadElementString());
                 }
                 else if (!ServiceDocumentFormatter.TryParseElement(reader, result, this.Version))
                 {
                     if (this.preserveElementExtensions)
                     {
                         SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, this.maxExtensionSize);
                         continue;
                     }
                     SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                     reader.Skip();
                 }
             }
         }
         ServiceDocumentFormatter.LoadElementExtensions(buffer, extWriter, result);
     }
     finally
     {
         if (extWriter != null)
         {
             extWriter.Close();
         }
     }
     reader.ReadEndElement();
     return result;
 }
 protected static bool TryParseElement(XmlReader reader, Workspace workspace, string version)
 {
     if (workspace == null)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     return workspace.TryParseElement(reader, version);
 }
 protected static void LoadElementExtensions(XmlReader reader, Workspace workspace, int maxExtensionSize)
 {
     if (workspace == null)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     workspace.LoadElementExtensions(reader, maxExtensionSize);
 }
        public HttpResponseMessage GetServiceDoc(HttpRequestMessage request)
        {
            ServiceDocument doc = new ServiceDocument { BaseUri = new Uri(this.serviceURI) };
            List<ResourceCollectionInfo> resources = new List<ResourceCollectionInfo>();

            ResourceCollectionInfo blogsCollection = new ResourceCollectionInfo("Blogs", new Uri(String.Format("{0}/blogs", this.serviceURI)));
            blogsCollection.Accepts.Add("application/atom+xml;type=feed");
            resources.Add(blogsCollection);

            Workspace main = new Workspace("RESTBlogsService", resources);
            doc.Workspaces.Add(main);

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
                                               {
                                                   Content = new ObjectContent(typeof(AtomPub10ServiceDocumentFormatter), doc.GetFormatter())
                                               };

            return response;
        }
 void WriteWorkspace(XmlWriter writer, Workspace workspace, Uri baseUri)
 {
     writer.WriteStartElement(App10Constants.Prefix, App10Constants.Workspace, App10Constants.Namespace);
     Uri baseUriToWrite = FeedUtils.GetBaseUriToWrite(baseUri, workspace.BaseUri);
     if (baseUriToWrite != null)
     {
         baseUri = workspace.BaseUri;
         WriteXmlBase(writer, baseUriToWrite);
     }
     WriteAttributeExtensions(writer, workspace, this.Version);
     if (workspace.Title != null)
     {
         workspace.Title.WriteTo(writer, Atom10Constants.TitleTag, Atom10Constants.Atom10Namespace);
     }
     for (int i = 0; i < workspace.Collections.Count; ++i)
     {
         WriteCollection(writer, workspace.Collections[i], baseUri);
     }
     WriteElementExtensions(writer, workspace, this.Version);
     writer.WriteEndElement();
 }
        ResourceCollectionInfo ReadCollection(XmlReader reader, Workspace workspace)
        {
            ResourceCollectionInfo result = CreateCollection(workspace);
            result.BaseUri = workspace.BaseUri;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        result.BaseUri = FeedUtils.CombineXmlBase(result.BaseUri, reader.Value);
                    }
                    else if (reader.LocalName == App10Constants.Href && reader.NamespaceURI == string.Empty)
                    {
                        result.Link = new Uri(reader.Value, UriKind.RelativeOrAbsolute);
                    }
                    else
                    {
                        string ns = reader.NamespaceURI;
                        string name = reader.LocalName;
                        if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                        {
                            continue;
                        }
                        string val = reader.Value;
                        if (!TryParseAttribute(name, ns, val, result, this.Version))
                        {
                            if (this.preserveAttributeExtensions)
                            {
                                result.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                            }
                            else
                            {
                                SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            }
                        }
                    }
                }
            }

            XmlBuffer buffer = null;
            XmlDictionaryWriter extWriter = null;

            reader.ReadStartElement();
            try
            {
                while (reader.IsStartElement())
                {
                    if (reader.IsStartElement(Atom10Constants.TitleTag, Atom10Constants.Atom10Namespace))
                    {
                        result.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/app:collection/atom:title[@type]", this.preserveAttributeExtensions);
                    }
                    else if (reader.IsStartElement(App10Constants.Categories, App10Constants.Namespace))
                    {
                        result.Categories.Add(ReadCategories(reader,
                            result.BaseUri,
                            delegate()
                            {
                                return CreateInlineCategories(result);
                            },

                            delegate()
                            {
                                return CreateReferencedCategories(result);
                            },
                            this.Version,
                            this.preserveElementExtensions,
                            this.preserveAttributeExtensions,
                            this.maxExtensionSize));
                    }
                    else if (reader.IsStartElement(App10Constants.Accept, App10Constants.Namespace))
                    {
                        result.Accepts.Add(reader.ReadElementString());
                    }
                    else if (!TryParseElement(reader, result, this.Version))
                    {
                        if (this.preserveElementExtensions)
                        {
                            SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, this.maxExtensionSize);
                        }
                        else
                        {
                            SyndicationFeedFormatter.TraceSyndicationElementIgnoredOnRead(reader);
                            reader.Skip();
                        }
                    }
                }
                LoadElementExtensions(buffer, extWriter, result);
            }
            finally
            {
                if (extWriter != null)
                {
                    extWriter.Close();
                }
            }
            reader.ReadEndElement();
            return result;
        }
 protected static void LoadElementExtensions(System.Xml.XmlReader reader, Workspace workspace, int maxExtensionSize)
 {
 }
        public HttpResponseMessage<ServiceDocument> ServiceDocumentation()
        {

            //TODO: change this to specific
            var serviceDocument = new ServiceDocument()
                                      {
                                          BaseUri = new Uri(this.ServiceURI + "/blogs")
                                      };

            var collection = new List<ResourceCollectionInfo>();

            foreach (var blog in _blogService.GetAll())
            {
                var resourceCollectionInfo = new ResourceCollectionInfo(blog.Title,
                                                                    new Uri(this.ServiceURI + "/blogs/" + blog.Id + "/posts"));

                resourceCollectionInfo.Accepts.Add("application/atom+xml;type=entry");
                collection.Add(resourceCollectionInfo);
            }

            var workspace = new Workspace("Feed", collection);
            serviceDocument.Workspaces.Add(workspace);

            return new HttpResponseMessage<ServiceDocument>(serviceDocument, HttpStatusCode.OK);
        }
		public void WriteTo2 ()
		{
			var s = new ServiceDocument ();
			var ws = new Workspace ("test title", null);
			var rc = new ResourceCollectionInfo ("test resource", new Uri ("urn:foo"));
			rc.Accepts.Add ("application/atom+xml;type=entry");
			ws.Collections.Add (rc);
			s.Workspaces.Add (ws);
			var a = new AtomPub10ServiceDocumentFormatter (s);
			Assert.AreEqual ("http://www.w3.org/2007/app", a.Version, "#1");
			Assert.IsTrue (a.CanRead (XmlReader.Create (new StringReader (app2))), "#2");
			var sw = new StringWriter ();
			using (var xw = XmlWriter.Create (sw, settings))
				a.WriteTo (xw);
			Assert.AreEqual (app2, sw.ToString (), "#3");
		}
 protected static bool TryParseAttribute(string name, string ns, string value, Workspace workspace, string version)
 {
     if (workspace == null)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     return workspace.TryParseAttribute(name, ns, value, version);
 }
 protected static void WriteAttributeExtensions(XmlWriter writer, Workspace workspace, string version)
 {
     if (workspace == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     workspace.WriteAttributeExtensions(writer, version);
 }
 protected static void WriteElementExtensions(XmlWriter writer, Workspace workspace, string version)
 {
     if (workspace == null)
     {
         throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("workspace");
     }
     workspace.WriteElementExtensions(writer, version);
 }
 protected static bool TryParseAttribute(string name, string ns, string value, Workspace workspace, string version)
 {
   return default(bool);
 }
 private void WriteWorkspace(XmlWriter writer, Workspace workspace, Uri baseUri)
 {
     writer.WriteStartElement("app", "workspace", "http://www.w3.org/2007/app");
     Uri baseUriToWrite = FeedUtils.GetBaseUriToWrite(baseUri, workspace.BaseUri);
     if (baseUriToWrite != null)
     {
         baseUri = workspace.BaseUri;
         WriteXmlBase(writer, baseUriToWrite);
     }
     ServiceDocumentFormatter.WriteAttributeExtensions(writer, workspace, this.Version);
     if (workspace.Title != null)
     {
         workspace.Title.WriteTo(writer, "title", "http://www.w3.org/2005/Atom");
     }
     for (int i = 0; i < workspace.Collections.Count; i++)
     {
         this.WriteCollection(writer, workspace.Collections[i], baseUri);
     }
     ServiceDocumentFormatter.WriteElementExtensions(writer, workspace, this.Version);
     writer.WriteEndElement();
 }
 protected static bool TryParseElement(System.Xml.XmlReader reader, Workspace workspace, string version)
 {
   return default(bool);
 }
 //[MonoTODO ("Use maxExtensionSize somewhere")]
 protected static void LoadElementExtensions(XmlReader reader, Workspace workspace, int maxExtensionSize)
 {
     workspace.ElementExtensions.Add (reader);
 }
 protected static void WriteElementExtensions(System.Xml.XmlWriter writer, Workspace workspace, string version)
 {
 }
 protected static bool TryParseElement(XmlReader reader, Workspace workspace, string version)
 {
     return workspace.TryParseElement (reader, version);
 }
 protected static ResourceCollectionInfo CreateCollection(Workspace workspace)
 {
   return default(ResourceCollectionInfo);
 }
 protected static ResourceCollectionInfo CreateCollection(Workspace workspace)
 {
     return workspace.CreateResourceCollection ();
 }
        private ResourceCollectionInfo ReadCollection(XmlReader reader, Workspace workspace)
        {
            ResourceCollectionInfo result = CreateCollection(workspace);

            result.BaseUri = workspace.BaseUri;
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    if (reader.LocalName == "base" && reader.NamespaceURI == Atom10FeedFormatter.XmlNs)
                    {
                        result.BaseUri = FeedUtils.CombineXmlBase(result.BaseUri, reader.Value);
                    }
                    else if (reader.LocalName == App10Constants.Href && reader.NamespaceURI == string.Empty)
                    {
                        result.Link = new Uri(reader.Value, UriKind.RelativeOrAbsolute);
                    }
                    else
                    {
                        string ns   = reader.NamespaceURI;
                        string name = reader.LocalName;
                        if (FeedUtils.IsXmlns(name, ns) || FeedUtils.IsXmlSchemaType(name, ns))
                        {
                            continue;
                        }

                        string val = reader.Value;
                        if (!TryParseAttribute(name, ns, val, result, Version))
                        {
                            result.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }

            XmlBuffer           buffer    = null;
            XmlDictionaryWriter extWriter = null;

            reader.ReadStartElement();
            try
            {
                while (reader.IsStartElement())
                {
                    if (reader.IsStartElement(Atom10Constants.TitleTag, Atom10Constants.Atom10Namespace))
                    {
                        result.Title = Atom10FeedFormatter.ReadTextContentFrom(reader, "//app:service/app:workspace/app:collection/atom:title[@type]", preserveAttributeExtensions: true);
                    }
                    else if (reader.IsStartElement(App10Constants.Categories, App10Constants.Namespace))
                    {
                        result.Categories.Add(ReadCategories(reader,
                                                             result.BaseUri,
                                                             () => CreateInlineCategories(result),
                                                             () => CreateReferencedCategories(result),
                                                             Version,
                                                             _maxExtensionSize));
                    }
                    else if (reader.IsStartElement(App10Constants.Accept, App10Constants.Namespace))
                    {
                        result.Accepts.Add(reader.ReadElementString());
                    }
                    else if (!TryParseElement(reader, result, Version))
                    {
                        SyndicationFeedFormatter.CreateBufferIfRequiredAndWriteNode(ref buffer, ref extWriter, reader, _maxExtensionSize);
                    }
                }
                LoadElementExtensions(buffer, extWriter, result);
            }
            finally
            {
                extWriter?.Close();
            }

            reader.ReadEndElement();
            return(result);
        }