Exemplo n.º 1
0
        public static EntryElement ToEntry(EventRecord evnt, string userHostName)
        {
            if (evnt == null || userHostName == null)
                return null;

            var entry = new EntryElement();

            entry.SetTitle(String.Format("{0} #{1}", evnt.EventStreamId, evnt.EventNumber));

            entry.SetId(HostName.Combine(userHostName, "/streams/{0}/{1}", evnt.EventStreamId, evnt.EventNumber));
            entry.SetUpdated(evnt.TimeStamp);

            entry.SetAuthor(AtomSpecs.Author);
            entry.SetSummary(String.Format("Entry #{0}", evnt.EventNumber));

            entry.AddLink(HostName.Combine(userHostName, "/streams/{0}/{1}", evnt.EventStreamId, evnt.EventNumber), "edit", null);

            entry.AddLink(
                HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=text", evnt.EventStreamId, evnt.EventNumber),
                null,
                ContentType.PlainText);
            entry.AddLink(
                HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=json", evnt.EventStreamId, evnt.EventNumber),
                "alternate",
                ContentType.Json);
            entry.AddLink(
                HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=xml", evnt.EventStreamId, evnt.EventNumber),
                "alternate",
                ContentType.Xml);

            return entry;
        }
Exemplo n.º 2
0
 public void AddEntry(EntryElement entry)
 {
     Ensure.NotNull(entry, "entry");
     Entries.Add(entry);
 }
Exemplo n.º 3
0
 private static void SetEntryProperties(string stream, int eventNumber, DateTime timestamp, Uri requestedUrl, EntryElement entry)
 {
     var escapedStreamId = Uri.EscapeDataString(stream);
     entry.SetTitle(eventNumber + "@" + stream);
     entry.SetId(HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, eventNumber));
     entry.SetUpdated(timestamp);
     entry.SetAuthor(AtomSpecs.Author);
     entry.AddLink("edit",
         HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, eventNumber));
     entry.AddLink("alternate",
         HostName.Combine(requestedUrl, "/streams/{0}/{1}", escapedStreamId, eventNumber));
 }
Exemplo n.º 4
0
        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.Body)
                    {
                        try
                        {
                            richEntry.MetaData = Helper.UTF8NoBom.GetString(evnt.Metadata);
                            richEntry.IsMetaData = richEntry.MetaData.IsNotEmptyString();
                            // next step may fail, so we have already assigned body
                            if (embedContent >= EmbedLevel.PrettyBody)
                            {
                                richEntry.MetaData = FormatJson(richEntry.MetaData);
                            }
                            if (string.IsNullOrEmpty(richEntry.MetaData))
                            {
                                richEntry.MetaData = null;
                            }
                        }
                        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
                                if (embedContent >= EmbedLevel.PrettyBody)
                                {
                                    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 eventLoc = GetLinkData(Encoding.UTF8.GetString(link.Data));
                SetEntryProperties(eventLoc.Item1, eventLoc.Item2, link.TimeStamp, requestedUrl, entry);
                entry.SetSummary("$>");
            }
            return entry;
        }
Exemplo n.º 5
0
        public void have_all_fields_filled()
        {
            var writer = XmlWriter.Create(new MemoryStream());

            var entry = new EntryElement();
            Assert.Throws<AtomSpecificationViolationException>(() => entry.WriteXml(writer));

            entry.SetTitle("test #0");
            Assert.Throws<AtomSpecificationViolationException>(() => entry.WriteXml(writer));

            entry.SetId("guid");
            Assert.Throws<AtomSpecificationViolationException>(() => entry.WriteXml(writer));

            entry.SetUpdated(DateTime.UtcNow);
            Assert.Throws<AtomSpecificationViolationException>(() => entry.WriteXml(writer));

            entry.SetAuthor(AtomSpecs.Author);
            Assert.Throws<AtomSpecificationViolationException>(() => entry.WriteXml(writer));

            entry.SetSummary("Entry #0");

            Assert.DoesNotThrow(() => entry.WriteXml(writer));
            writer.Close();
        }
Exemplo n.º 6
0
        public static EntryElement ToEntry(ResolvedEvent eventLinkPair, string userHostName, EmbedLevel embedContent)
        {
            if (eventLinkPair.Event == null || userHostName == null)
                return null;

            var evnt = eventLinkPair.Event;

            EntryElement entry;
            if (embedContent > EmbedLevel.None)
            {
                var richEntry = new RichEntryElement();
                entry = richEntry;

                richEntry.EventType = evnt.EventType;
                richEntry.EventNumber = evnt.EventNumber;
                richEntry.StreamId = evnt.EventStreamId;
                richEntry.PositionEventNumber = (eventLinkPair.Link ?? eventLinkPair.Event).EventNumber;
                richEntry.PositionStreamId = (eventLinkPair.Link ?? eventLinkPair.Event).EventStreamId;
                richEntry.IsJson = (evnt.Flags & PrepareFlags.IsJson) != 0;
                if (embedContent >= EmbedLevel.Body)
                {
                    if (richEntry.IsJson)
                    {
                        if (embedContent >= EmbedLevel.PrettyBody)
                            try
                            {
                                richEntry.Data = Encoding.UTF8.GetString(evnt.Data);
                                // next step may fail, so we have already assigned body
                                richEntry.Data = FormatJson(Encoding.UTF8.GetString(evnt.Data));
                            }
                            catch
                            {
                                // ignore - we tried
                            }
                        else 
                            richEntry.Data = Encoding.UTF8.GetString(evnt.Data);
                    }
                    else if (embedContent >= EmbedLevel.TryHarder)
                    {
                        try
                        {
                            richEntry.Data = Encoding.UTF8.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 = Encoding.UTF8.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 = Encoding.UTF8.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(userHostName, "/streams/{0}/{1}", escapedStreamId, evnt.EventNumber));
            entry.SetUpdated(evnt.TimeStamp);
            entry.SetAuthor(AtomSpecs.Author);
            entry.SetSummary(evnt.EventType);

            entry.AddLink("edit", HostName.Combine(userHostName, "/streams/{0}/{1}", escapedStreamId, evnt.EventNumber));
            entry.AddLink(null, HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=text", escapedStreamId, evnt.EventNumber), ContentType.PlainText);
            entry.AddLink("alternate", HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=json", escapedStreamId, evnt.EventNumber), ContentType.Json);
            entry.AddLink("alternate", HostName.Combine(userHostName, "/streams/{0}/event/{1}?format=xml", escapedStreamId, evnt.EventNumber), ContentType.Xml);

            return entry;
        }
Exemplo n.º 7
0
 public void AddEntry(EntryElement entry)
 {
     Ensure.NotNull(entry, "entry");
     Entries.Add(entry);
 }
Exemplo n.º 8
0
        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;
        }