Created
August 31, 2012 16:35
-
-
Save benfoster/3555524 to your computer and use it in GitHub Desktop.
AtomPubMediaTypeFormatter for ASP.NET Web API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AtomPubMediaFormatter : MediaTypeFormatter | |
{ | |
private const string Atom = "application/atom+xml"; | |
public AtomPubMediaFormatter() | |
{ | |
SupportedMediaTypes.Add(new MediaTypeHeaderValue(Atom)); | |
this.AddQueryStringMapping("format", "atom", Atom); | |
} | |
public override bool CanReadType(Type type) | |
{ | |
return type.Implements<IPublicationEntry>(); | |
} | |
public override bool CanWriteType(Type type) | |
{ | |
return type.Implements<IPublication>() || type.Implements<IPublicationFeed>(); | |
} | |
public override Task<object> ReadFromStreamAsync(Type type, System.IO.Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger) | |
{ | |
if (type == null) | |
{ | |
throw new ArgumentNullException("type"); | |
} | |
if (readStream == null) | |
{ | |
throw new ArgumentNullException("type"); | |
} | |
return TaskHelpers.RunSynchronously<object>(() => | |
{ | |
HttpContentHeaders contentHeaders = content == null ? null : content.Headers; | |
// If content length is 0 then return default value for this type | |
if (contentHeaders != null && contentHeaders.ContentLength == 0) | |
{ | |
return GetDefaultValueForType(type); | |
} | |
try | |
{ | |
using (var reader = XmlReader.Create(readStream)) | |
{ | |
var formatter = new Atom10ItemFormatter(); | |
formatter.ReadFrom(reader); | |
var entry = formatter.Item.ToPublicationEntry(); | |
var item = Mapper.DynamicMap(entry, entry.GetType(), type); | |
return item; | |
} | |
} | |
catch (Exception e) | |
{ | |
if (formatterLogger == null) | |
{ | |
throw; | |
} | |
formatterLogger.LogError(String.Empty, e); | |
return GetDefaultValueForType(type); | |
} | |
}); | |
} | |
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext) | |
{ | |
return TaskHelpers.RunSynchronously(() => | |
{ | |
BuildSyndicationFeed(value, writeStream, content.Headers.ContentType.MediaType); | |
}); | |
} | |
private void BuildSyndicationFeed(object model, System.IO.Stream writeStream, string mediaType) | |
{ | |
var entries = new List<SyndicationItem>(); | |
if (model is IPublicationFeed) | |
{ | |
((IPublicationFeed)model).Items.ForEach(entry => | |
entries.Add(entry.Syndicate()) | |
); | |
} | |
else | |
{ | |
entries.Add((model as IPublication).Syndicate()); | |
} | |
// TODO set additional properties on feed | |
var feed = new SyndicationFeed | |
{ | |
Title = new TextSyndicationContent("My Feed"), | |
Items = entries | |
}; | |
using (var writer = XmlWriter.Create(writeStream)) | |
{ | |
new Atom10FeedFormatter(feed).WriteTo(writer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment