public static string EventEntry(HttpResponseFormatterArgs entity, Message message, EmbedLevel embed) { var msg = message as ClientMessage.ReadEventCompleted; if (msg == null || msg.Result != ReadEventResult.Success || msg.Record.Event == null) { return(entity.ResponseCodec.To(new {})); } switch (entity.ResponseCodec.ContentType) { case ContentType.Atom: case ContentType.AtomJson: case ContentType.Html: return(entity.ResponseCodec.To(Convert.ToEntry(msg.Record, entity.RequestedUrl, embed, singleEntry: true))); default: return(AutoEventConverter.SmartFormat(msg.Record, entity.ResponseCodec)); } }
public static string ReadEventCompleted(HttpEntity entity, Message message) { Debug.Assert(message.GetType() == typeof(ClientMessage.ReadEventCompleted)); var completed = message as ClientMessage.ReadEventCompleted; if (completed != null) { switch (completed.Result) { case SingleReadResult.Success: return(AutoEventConverter.SmartFormat(completed, entity.ResponseCodec)); case SingleReadResult.NotFound: case SingleReadResult.NoStream: case SingleReadResult.StreamDeleted: return(string.Empty); default: throw new ArgumentOutOfRangeException(); } } return(string.Empty); }
public static EntryElement ToEntry(ResolvedEvent eventLinkPair, Uri requestedUrl, EmbedLevel embedContent, bool singleEntry = false) { if (requestedUrl == null) { return(null); } var evnt = eventLinkPair.Event; var link = eventLinkPair.Link; EntryElement entry; if (embedContent > EmbedLevel.Content && evnt != null) { var richEntry = new RichEntryElement(); entry = richEntry; richEntry.EventId = evnt.EventId; richEntry.EventType = evnt.EventType; richEntry.EventNumber = evnt.EventNumber; richEntry.StreamId = evnt.EventStreamId; richEntry.PositionEventNumber = eventLinkPair.OriginalEvent.EventNumber; richEntry.PositionStreamId = eventLinkPair.OriginalEvent.EventStreamId; richEntry.IsJson = (evnt.Flags & PrepareFlags.IsJson) != 0; if (embedContent >= EmbedLevel.Body && eventLinkPair.Event != null) { if (richEntry.IsJson) { if (embedContent >= EmbedLevel.PrettyBody) { try { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); // next step may fail, so we have already assigned body richEntry.Data = FormatJson(Helper.UTF8NoBom.GetString(evnt.Data)); } catch { // ignore - we tried } } else { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); } } else if (embedContent >= EmbedLevel.TryHarder) { try { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); // next step may fail, so we have already assigned body richEntry.Data = FormatJson(richEntry.Data); // it is json if successed richEntry.IsJson = true; } catch { // ignore - we tried } } // metadata if (embedContent >= EmbedLevel.PrettyBody) { try { richEntry.MetaData = Helper.UTF8NoBom.GetString(evnt.Metadata); richEntry.IsMetaData = richEntry.MetaData.IsNotEmptyString(); // next step may fail, so we have already assigned body richEntry.MetaData = FormatJson(richEntry.MetaData); } catch { // ignore - we tried } var lnk = eventLinkPair.Link; if (lnk != null) { try { richEntry.LinkMetaData = Helper.UTF8NoBom.GetString(lnk.Metadata); richEntry.IsLinkMetaData = richEntry.LinkMetaData.IsNotEmptyString(); // next step may fail, so we have already assigned body richEntry.LinkMetaData = FormatJson(richEntry.LinkMetaData); } catch { // ignore - we tried } } } } } else { entry = new EntryElement(); } if (evnt != null && link == null) { SetEntryProperties(evnt.EventStreamId, evnt.EventNumber, evnt.TimeStamp, requestedUrl, entry); entry.SetSummary(evnt.EventType); if ((singleEntry || embedContent == EmbedLevel.Content) && ((evnt.Flags & PrepareFlags.IsJson) != 0)) { entry.SetContent(AutoEventConverter.CreateDataDto(eventLinkPair)); } } else if (link != null) { var pieces = Encoding.UTF8.GetString(link.Data).Split('@'); if (pieces.Length != 2) { throw new Exception("link not in proper format."); } var eventNumber = int.Parse(pieces[0]); var streamId = pieces[1]; SetEntryProperties(streamId, eventNumber, link.TimeStamp, requestedUrl, entry); entry.SetSummary("$>"); } return(entry); }
public static EntryElement ToEntry(ResolvedEvent eventLinkPair, Uri requestedUrl, EmbedLevel embedContent, bool singleEntry = false) { if (eventLinkPair.Event == null || requestedUrl == null) { return(null); } var evnt = eventLinkPair.Event; EntryElement entry; if (embedContent > EmbedLevel.Content) { var richEntry = new RichEntryElement(); entry = richEntry; richEntry.EventType = evnt.EventType; richEntry.EventNumber = evnt.EventNumber; richEntry.StreamId = evnt.EventStreamId; richEntry.PositionEventNumber = eventLinkPair.OriginalEvent.EventNumber; richEntry.PositionStreamId = eventLinkPair.OriginalEvent.EventStreamId; richEntry.IsJson = (evnt.Flags & PrepareFlags.IsJson) != 0; if (embedContent >= EmbedLevel.Body) { if (richEntry.IsJson) { if (embedContent >= EmbedLevel.PrettyBody) { try { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); // next step may fail, so we have already assigned body richEntry.Data = FormatJson(Helper.UTF8NoBom.GetString(evnt.Data)); } catch { // ignore - we tried } } else { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); } } else if (embedContent >= EmbedLevel.TryHarder) { try { richEntry.Data = Helper.UTF8NoBom.GetString(evnt.Data); // next step may fail, so we have already assigned body richEntry.Data = FormatJson(richEntry.Data); // it is json if successed richEntry.IsJson = true; } catch { // ignore - we tried } } // metadata if (embedContent >= EmbedLevel.PrettyBody) { try { richEntry.MetaData = Helper.UTF8NoBom.GetString(evnt.Metadata); richEntry.IsMetaData = richEntry.MetaData.IsNotEmptyString(); // next step may fail, so we have already assigned body richEntry.MetaData = FormatJson(richEntry.MetaData); } catch { // ignore - we tried } var lnk = eventLinkPair.Link; if (lnk != null) { try { richEntry.LinkMetaData = Helper.UTF8NoBom.GetString(lnk.Metadata); richEntry.IsLinkMetaData = richEntry.LinkMetaData.IsNotEmptyString(); // next step may fail, so we have already assigned body richEntry.LinkMetaData = FormatJson(richEntry.LinkMetaData); } catch { // ignore - we tried } } } } } else { entry = new EntryElement(); } var escapedStreamId = Uri.EscapeDataString(evnt.EventStreamId); entry.SetTitle(evnt.EventNumber + "@" + evnt.EventStreamId); entry.SetId(HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, evnt.EventNumber)); entry.SetUpdated(evnt.TimeStamp); entry.SetAuthor(AtomSpecs.Author); entry.SetSummary(evnt.EventType); if ((singleEntry || embedContent == EmbedLevel.Content) && ((evnt.Flags & PrepareFlags.IsJson) != 0)) { entry.SetContent(AutoEventConverter.CreateDataDto(eventLinkPair)); } entry.AddLink("edit", HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, evnt.EventNumber)); entry.AddLink("alternate", HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, evnt.EventNumber)); return(entry); }