protected virtual SyndicationItem ReadItem(XmlReader reader, SyndicationFeed feed) { Atom10ItemFormatter formatter = new Atom10ItemFormatter(); formatter.ReadFrom(reader); return(formatter.Item); }
public static async Task <TSyndicationItem> LoadAsync <TSyndicationItem>(XmlReader reader) where TSyndicationItem : SyndicationItem, new() { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } Atom10ItemFormatter <TSyndicationItem> atomSerializer = new Atom10ItemFormatter <TSyndicationItem>(); if (atomSerializer.CanRead(reader)) { atomSerializer.ReadFrom(reader); return(atomSerializer.Item as TSyndicationItem); } Rss20ItemFormatter <TSyndicationItem> rssSerializer = new Rss20ItemFormatter <TSyndicationItem>(); if (rssSerializer.CanRead(reader)) { await rssSerializer.ReadFromAsync(reader).ConfigureAwait(false); return(rssSerializer.Item as TSyndicationItem); } throw new XmlException(SR.Format(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI)); }
public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders != null && contentHeaders.ContentLength == null) { return GetDefaultValueForType(type); } try { using (readStream) { using (var reader = XmlReader.Create(readStream)) { var formatter = new Atom10ItemFormatter(); formatter.ReadFrom(reader); var command = Activator.CreateInstance(type); ((IPublicationCommand)command).ReadSyndicationItem(formatter.Item); return command; } } } catch (Exception e) { if (formatterLogger == null) { throw; } formatterLogger.LogError(String.Empty, e); return GetDefaultValueForType(type); } }
internal static TSyndicationItem LoadItem <TSyndicationItem> (XmlReader reader) where TSyndicationItem : SyndicationItem, new() { switch (DetectVersion(reader, ReaderKind.Item)) { case SyndicationVersions.Atom10: Atom10ItemFormatter af = new Atom10ItemFormatter <TSyndicationItem> (); af.ReadFrom(reader); return((TSyndicationItem)af.Item); case SyndicationVersions.Rss20: default: // anything else are rejected by DetectVersion Rss20ItemFormatter rf = new Rss20ItemFormatter <TSyndicationItem> (); rf.ReadFrom(reader); return((TSyndicationItem)rf.Item); } }
public override object OnReadFromStream(Type type, Stream stream, HttpContentHeaders contentHeaders) { stream.Seek(0, SeekOrigin.Begin); if (type.Equals(typeof(SyndicationItem))) { var entryFormatter = new Atom10ItemFormatter(); entryFormatter.ReadFrom(XmlReader.Create(stream)); return entryFormatter.Item; } if (type.Equals(typeof(SyndicationFeed))) { var feedFormatter = new Atom10FeedFormatter(); feedFormatter.ReadFrom(XmlReader.Create(stream)); return feedFormatter.Feed; } throw new InvalidOperationException("Expected to be called with type SyndicationItem or SyndicationFeed."); }
public void FunctionalTest() { var configuration = HttpHostConfiguration.Create() .SetResourceFactory((type, instanceContext, request) => new RoomResource(Maze.NewInstance(), Monsters.NullEncounters()), (instanceContext, obj) => { }); // Workaround for serialization issue in Preview 4. // Must clear default XML formatter from Formatters before adding Atom formatter. var hostConfiguration = (HttpHostConfiguration) configuration; hostConfiguration.OperationHandlerFactory.Formatters.Clear(); hostConfiguration.OperationHandlerFactory.Formatters.Insert(0, AtomMediaType.Formatter); using (var host = new HttpConfigurableServiceHost(typeof (RoomResource), configuration, new Uri("http://" + Environment.MachineName + ":8081/rooms/"))) { host.Open(); var entryFormatter = new Atom10ItemFormatter(); var client = AtomClient.CreateDefault(); using (var firstResponse = client.Get("http://" + Environment.MachineName + ":8081/rooms/1")) { entryFormatter.ReadFrom(XmlReader.Create(firstResponse.Content.ContentReadStream)); } var firstRoom = entryFormatter.Item; var northLink = firstRoom.Links.First(l => l.RelationshipType.Equals("north")); var northUri = new Uri(firstRoom.BaseUri, northLink.Uri); using (var secondResponse = client.Get(northUri)) { entryFormatter.ReadFrom(XmlReader.Create(secondResponse.Content.ContentReadStream)); } var nextRoom = entryFormatter.Item; //See Maze class for layout of the maze. Room 4 is north of room 1. Assert.AreEqual(Maze.NewInstance().Get(4).Description, nextRoom.Summary.Text); host.Close(); } }
public static TSyndicationItem Load <TSyndicationItem>(XmlReader reader) where TSyndicationItem : SyndicationItem, new() { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } Atom10ItemFormatter <TSyndicationItem> formatter = new Atom10ItemFormatter <TSyndicationItem>(); if (formatter.CanRead(reader)) { formatter.ReadFrom(reader); return(formatter.Item as TSyndicationItem); } Rss20ItemFormatter <TSyndicationItem> formatter2 = new Rss20ItemFormatter <TSyndicationItem>(); if (!formatter2.CanRead(reader)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnknownItemXml", new object[] { reader.LocalName, reader.NamespaceURI }))); } formatter2.ReadFrom(reader); return(formatter2.Item as TSyndicationItem); }
public static TSyndicationItem Load <TSyndicationItem>(XmlReader reader) where TSyndicationItem : SyndicationItem, new() { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); } Atom10ItemFormatter <TSyndicationItem> atomSerializer = new Atom10ItemFormatter <TSyndicationItem>(); if (atomSerializer.CanRead(reader)) { atomSerializer.ReadFrom(reader); return(atomSerializer.Item as TSyndicationItem); } Rss20ItemFormatter <TSyndicationItem> rssSerializer = new Rss20ItemFormatter <TSyndicationItem>(); if (rssSerializer.CanRead(reader)) { rssSerializer.ReadFrom(reader); return(rssSerializer.Item as TSyndicationItem); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI))); }
// It is not rejected. Though I think it is .NET bug. public void ReadFrom_EmptyDate () { Atom10ItemFormatter f = new Atom10ItemFormatter (); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>")); }
protected virtual SyndicationItem ReadItem(XmlReader reader, SyndicationFeed feed) { Atom10ItemFormatter formatter = new Atom10ItemFormatter (); formatter.ReadFrom (reader); return formatter.Item; }
protected static HttpStatusCode SendContact( ConstantContactCredential credential, Uri uri, String method, Contact contact) { var request = WebRequest.Create(uri) as HttpWebRequest; if (request == null) { throw new WebException("Failed to create WebRequest"); } request.Credentials = credential; request.Headers.Add("Authorization", String.Format("Basic {0}", Convert.ToBase64String(Encoding.ASCII.GetBytes(credential.Password)))); request.Method = method; request.ContentType = "application/atom+xml"; // "application/x-www-form-urlencoded"; var contactItem = new SyndicationItem { Id = (contact.Id != null) ? contact.Id.ToString() : "data:,none", Content = new ContactContent(contact) }; contactItem.Authors.Add(new SyndicationPerson(typeof(ContactsCollection).FullName)); contactItem.Summary = new TextSyndicationContent("Contact"); var atomFormatter = new Atom10ItemFormatter(contactItem); using (var memoryStream = new MemoryStream()) { var writerSettings = new XmlWriterSettings { Indent = true, IndentChars = " ", OmitXmlDeclaration = true, Encoding = new UTF8Encoding(false), }; var xmlWriter = XmlWriter.Create(memoryStream, writerSettings); if (xmlWriter == null) throw new XmlException("Failed to create XmlWriter"); atomFormatter.WriteTo(xmlWriter); xmlWriter.Flush(); memoryStream.Seek(0, SeekOrigin.Begin); byte[] data = memoryStream.ToArray(); memoryStream.Close(); Debug.WriteLine(Encoding.UTF8.GetString(data)); // Set the content length in the request headers request.ContentLength = data.Length; // Write data using (var postStream = request.GetRequestStream()) { if (data.Length > int.MaxValue) { throw new InvalidDataException( String.Format("Contact content length exceeds {0} bytes", int.MaxValue)); } postStream.Write(data, 0, data.Length); } } // Get response using (var webResponse = request.GetResponse() as HttpWebResponse) { if (webResponse == null) { throw new WebException("Failed to get HttpWebResponse"); } // Get the response stream using (var reader = new StreamReader(webResponse.GetResponseStream())) { var response = reader.ReadToEnd(); Debug.WriteLine(response); } return webResponse.StatusCode; } }
/// <summary> /// Update the album by entry /// </summary> /// <param name="albumEntry"></param> /// <param name="access_token"></param> /// <returns></returns> public bool UpdateAlbum(SyndicationItem albumEntry, string access_token) { //get the album id string albumID = string.Empty; SyndicationElementExtension ext = albumEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("id")).First(); albumID = ext.GetReader().ReadInnerXml(); //create the URI string updateURI = string.Format("{0}{1}" , GoogleUtilityConstants.PICASA_ENTRY_BASE_URI_NO_HTTPS , string.Format(GoogleUtilityConstants.PICASA_ALBUMID_URI, albumID)); //create the content body string var output = new StringBuilder(); var formatter = new Atom10ItemFormatter(albumEntry); using (var writer = XmlWriter.Create(output)) { formatter.WriteTo(writer); } //create the requestor GoogleTCPRequestor tcpr = new GoogleTCPRequestor(Requestor.Operation.PUT , Requestor.HTTPFormat.FORMAT_1_1 , Requestor.ContentType.ATOM_XML , GoogleUtilityConstants.GOOGLE_PICASA_HOST , updateURI , output.ToString() , access_token , true); //get the output string response = tcpr.SubmitRequest(true, Requestor.ResponseTerminatorAction.READ_TO_END); return response.Contains("HTTP/1.1 200 OK"); }
/// <summary> /// Update a photo metadata by atom entry. /// </summary> /// <param name="photoEntry">The entry containing the modified information</param> /// <param name="access_token">user access token</param> /// <returns>true on success else false</returns> public bool UpdatePhotoMetadataByEntry(SyndicationItem photoEntry, string access_token) { // PUT https://picasaweb.google.com/data/entry/api/user/default/albumid/albumID/photoid/photoID string photoID = string.Empty; string albumID = string.Empty; //get the photoid and albumid from the photoentry' SyndicationElementExtension ext = null; ext = photoEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("id")).First(); photoID = ext.GetReader().ReadInnerXml(); ext = photoEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("albumid")).First(); albumID = ext.GetReader().ReadInnerXml(); string updateURI = string.Format("{0}{1}{2}" , GoogleUtilityConstants.PICASA_ENTRY_BASE_URI_NO_HTTPS , GetAlbumURI(albumID) , GetPhotoURI(photoID)); var output = new StringBuilder(); var formatter = new Atom10ItemFormatter(photoEntry); using (var writer = XmlWriter.Create(output)) { formatter.WriteTo(writer); } //var contentAsBytes = Encoding.ASCII.GetBytes(output.ToString()); GoogleTCPRequestor tcpr = new GoogleTCPRequestor(Requestor.Operation.PUT , Requestor.HTTPFormat.FORMAT_1_1 , Requestor.ContentType.ATOM_XML , GoogleUtilityConstants.GOOGLE_PICASA_HOST , updateURI , output.ToString() , access_token , true); //submit the request: return tcpr.SubmitRequest(true, false, Requestor.ResponseTerminatorAction.CONTAINS_HTTP_1_1_200_OK); }
public void Version () { Atom10ItemFormatter f = new Atom10ItemFormatter (); Assert.AreEqual ("Atom10", f.Version, "#1"); }
private void WriteAtomEntry(IPublication publication, Stream writeStream) { var entry = publication.Syndicate(); var formatter = new Atom10ItemFormatter(entry); using (var writer = XmlWriter.Create(writeStream)) { formatter.WriteTo(writer); } }
public void ReadFrom_Id () { Atom10ItemFormatter f = new Atom10ItemFormatter (); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><id>urn:myid</id></entry>")); Assert.IsNotNull (f.Item, "#1"); Assert.AreEqual ("urn:myid", f.Item.Id, "#2"); }
public void ReadFrom_Link () { Atom10ItemFormatter f = new Atom10ItemFormatter (); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><link href='urn:foo' rel='enclosure' length='50' type='text/html' wcf='wtf'><extended /></link></entry>")); Assert.AreEqual (1, f.Item.Links.Count, "#1"); SyndicationLink link = f.Item.Links [0]; Assert.AreEqual (50, link.Length, "#2"); Assert.AreEqual ("urn:foo", link.Uri.ToString (), "#3"); Assert.AreEqual ("text/html", link.MediaType, "#4"); Assert.AreEqual ("enclosure", link.RelationshipType, "#5"); Assert.AreEqual (1, link.AttributeExtensions.Count, "#6"); Assert.AreEqual (1, link.ElementExtensions.Count, "#7"); }
public void ReadFrom_WrongDate () { Atom10ItemFormatter f = new Atom10ItemFormatter (); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><published>Sat, 01 Jan 2000 00:00:00 Z</pubDate></entry>")); }
public void ReadFrom_Extension () { Atom10ItemFormatter f = new Atom10ItemFormatter (); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><ext>external</ext></entry>")); Assert.IsNotNull (f.Item, "#1"); Assert.AreEqual (1, f.Item.ElementExtensions.Count, "#2"); }
public override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context) { if (type.Equals(typeof(SyndicationItem))) { using (var writer = XmlWriter.Create(stream, WriterSettings)) { var itemFormatter = new Atom10ItemFormatter((SyndicationItem)value); itemFormatter.WriteTo(writer); writer.Flush(); } } else if (type.Equals(typeof(SyndicationFeed))) { using (var writer = XmlWriter.Create(stream, WriterSettings)) { var feedFormatter = new Atom10FeedFormatter((SyndicationFeed)value); feedFormatter.WriteTo(writer); writer.Flush(); } } else { throw new InvalidOperationException("Expected to be called with type SyndicationItem or SyndicationFeed."); } }
public void ReadXml_TitleOnly () { Atom10ItemFormatter f = new Atom10ItemFormatter (); ((IXmlSerializable) f).ReadXml (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); Assert.IsNotNull (f.Item.Title, "#1"); Assert.AreEqual ("test", f.Item.Title.Text, "#2"); ((IXmlSerializable) f).ReadXml (CreateReader ("<dummy><title>test</title></dummy>")); // it is ok }
public void ReadFrom1 () { Atom10ItemFormatter f = new Atom10ItemFormatter (); Assert.IsNull (f.Item, "#1"); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); SyndicationItem item1 = f.Item; Assert.IsNotNull (f.Item.Title, "#2"); Assert.AreEqual ("test", f.Item.Title.Text, "#3"); f.ReadFrom (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); Assert.IsFalse (object.ReferenceEquals (item1, f.Item), "#4"); }
public void CanRead () { Atom10ItemFormatter f = new Atom10ItemFormatter (); Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#1"); Assert.IsTrue (f.CanRead (CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#2"); Assert.IsFalse (f.CanRead (CreateReader ("<entry>")), "#3"); Assert.IsFalse (f.CanRead (CreateReader ("<item>")), "#4"); Assert.IsFalse (f.CanRead (CreateReader ("<hoge xmlns='http://www.w3.org/2005/Atom'>")), "#5"); XmlReader r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'></entry>"); r.Read (); // element r.Read (); // endelement Assert.IsFalse (f.CanRead (r), "#6"); r = CreateReader ("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"); r.Read (); // item r.Read (); // title Assert.IsFalse (f.CanRead (r), "#7"); }
private void WriteAtomMediaEntry(IPublicationMedia publication, Stream writeStream) { SyndicationItem mediaEntry = publication.Syndicate(); Atom10ItemFormatter formatter = new Atom10ItemFormatter(mediaEntry); using (XmlWriter writer = XmlWriter.Create(writeStream)) { formatter.WriteTo(writer); } }