Exemplo n.º 1
0
        internal Yield CreateSubscriptionSet(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            XDoc subscriptionSet = request.ToDocument();
            Tuplet <PubSubSubscriptionSet, bool> set = _dispatcher.RegisterSet(subscriptionSet);
            XUri         locationUri = Self.At("subscribers", set.Item1.Location).Uri.AsPublicUri();
            DreamMessage msg         = null;

            if (set.Item2)
            {
                // existing subs cause a Conflict with ContentLocation of the sub
                msg = DreamMessage.Conflict("The specified owner already has a registered subscription set");
                msg.Headers.ContentLocation = locationUri;
            }
            else
            {
                // new subs cause a Created with Location of the sub, plus XDoc containing the location
                XDoc responseDoc = new XDoc("subscription-set")
                                   .Elem("uri.location", locationUri)
                                   .Elem("access-key", set.Item1.AccessKey);
                msg = DreamMessage.Created(locationUri, responseDoc);
                msg.Headers.Location = locationUri.With("access-key", set.Item1.AccessKey);
            }
            response.Return(msg);
            yield break;
        }
Exemplo n.º 2
0
        internal Yield PostEntries(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            if (request.ToDocument().Name != "entry")
            {
                throw new DreamBadRequestException("invalid format");
            }

            // prepare entry
            XAtomEntry entry  = new XAtomEntry(request.ToDocument());
            int        number = System.Threading.Interlocked.Increment(ref _counter);
            XUri       link   = Self.At(number.ToString());

            entry.Id = link;
            entry.AddLink(link, XAtomBase.LinkRelation.Edit, null, 0, null);

            // update feed
            XAtomFeed feed = _feed;

            if (feed != null)
            {
                lock (feed) {
                    feed.Add(entry);
                }
            }
            else
            {
                throw new DreamBadRequestException("not initialized");
            }

            // schedule entry deletion
            double seconds = context.GetParam <double>("ttl", _defaultTTL);

            if (seconds > 0)
            {
                TimerFactory.New(TimeSpan.FromSeconds(seconds), AutoDeletePost, number, TaskEnv.Clone());
            }

            // return updated entry
            response.Return(DreamMessage.Created(link, entry));
            yield break;
        }