Ejemplo n.º 1
0
 public void AtomNewImageTest()
 {
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/DBlog/AtomImage.aspx");
     request.Method = "POST";
     request.ContentType = "image/jpg";
     string usernamePassword = string.Format("Administrator:");
     string basicAuthorization = string.Format("Basic {0}", Convert.ToBase64String(
         Encoding.ASCII.GetBytes(usernamePassword)));
     request.Headers.Add("Authorization", basicAuthorization);
     byte[] postData = ThumbnailBitmap.GetBitmapDataFromText("x", 72, 100, 150);
     request.ContentLength = postData.Length;
     request.GetRequestStream().Write(postData, 0, postData.Length);
     request.GetRequestStream().Close();
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     string responseLocation = response.Headers["Location"];
     Assert.IsNotEmpty(responseLocation);
     Console.WriteLine(responseLocation);
     // atom entry returned
     AtomEntry atomEntry = new AtomEntry();
     atomEntry.Load(response.GetResponseStream());
     Console.WriteLine(atomEntry.Id.Uri);
     int id = int.Parse(atomEntry.Id.Uri.ToString().Substring(atomEntry.Id.Uri.ToString().LastIndexOf("/") + 1));
     Assert.IsTrue(id > 0);
     Console.WriteLine(string.Format("Id: {0}", id));
     Blog.DeleteImage(Ticket, id);
 }
Ejemplo n.º 2
0
    private void CreateOrUpdatePost(object sender, EventArgs e)
    {
        SessionManager.BasicAuth();

        if (!SessionManager.IsAdministrator)
        {
            throw new ManagedLogin.AccessDeniedException();
        }

        AtomEntry atomEntry = new AtomEntry();
        atomEntry.Load(Request.InputStream);

        TransitPost post = (RequestId > 0)
            ? SessionManager.BlogService.GetPostById(SessionManager.Ticket, RequestId)
            : new TransitPost();

        post.Title = atomEntry.Title.Content;

        List<TransitTopic> topics = new List<TransitTopic>();
        foreach (AtomCategory category in atomEntry.Categories)
        {
            TransitTopic topic = SessionManager.BlogService.GetTopicByName(SessionManager.Ticket, category.Term);
            if (topic == null)
            {
                topic = new TransitTopic();
                topic.Name = category.Term;
            }
            topics.Add(topic);
        }

        post.Topics = topics.ToArray();
        post.Body = atomEntry.Content.Content;
        post.Publish = true;
        post.Display = true;
        post.Sticky = false;
        post.Export = false;

        if (atomEntry.PublishedOn != DateTime.MinValue)
            post.Created = atomEntry.PublishedOn;
        if (atomEntry.UpdatedOn != DateTime.MinValue)
            post.Modified = atomEntry.UpdatedOn;

        post.Id = SessionManager.BlogService.CreateOrUpdatePost(SessionManager.Ticket, post);

        Response.ContentType = "application/atom+xml;type=entry;charset=\"utf-8\"";
        Response.StatusCode = 201;
        Response.StatusDescription = "Created";
        string location = string.Format("{0}AtomPost.aspx?id={1}", SessionManager.WebsiteUrl, post.Id);
        Response.Headers.Add("Location", location);
        Response.Headers.Add("Content-Location", location);
        Response.Headers.Add("ETag", string.Format("\"{0}\"", Guid.NewGuid().ToString()));

        atomEntry.Id = new AtomId(new Uri(string.Format("{0}Post/{1}", SessionManager.WebsiteUrl, post.Id)));
        atomEntry.Links.Add(new AtomLink(new Uri(string.Format("{0}AtomPost.aspx?id={1}", SessionManager.WebsiteUrl, post.Id))));
        atomEntry.Links.Add(new AtomLink(new Uri(string.Format("{0}AtomPost.aspx?id={1}", SessionManager.WebsiteUrl, post.Id)), "edit"));
        AtomLink atomEntryUri = new AtomLink(new Uri(string.Format("{0}{1}", SessionManager.WebsiteUrl, post.LinkUri)), "alternate");
        atomEntryUri.ContentType = "text/html";
        atomEntry.Links.Add(atomEntryUri);
        atomEntry.Save(Response.OutputStream);

        Response.End();
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new <see cref="AtomEntry"/> instance using the specified <see cref="Uri"/>, <see cref="ICredentials"/>, <see cref="IWebProxy"/>, and <see cref="SyndicationResourceLoadSettings"/> object.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param>
        /// <returns>An <see cref="AtomEntry"/> object loaded using the <paramref name="source"/> data.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the entry remains empty.</exception>
        public static AtomEntry Create(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            AtomEntry syndicationResource = new AtomEntry();

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Create new instance using supplied parameters
            //------------------------------------------------------------
            syndicationResource.Load(source, options, settings);

            return syndicationResource;
        }
        /// <summary>
        /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param>
        /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param>
        /// <returns>
        ///     An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>. 
        ///     If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception>
        private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(stream, "stream");

            //------------------------------------------------------------
            //	Create syndication resource based on content format
            //------------------------------------------------------------
            if (format == SyndicationContentFormat.Apml)
            {
                ApmlDocument document   = new ApmlDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Atom)
            {
                XPathDocument document      = new XPathDocument(stream);
                XPathNavigator navigator    = document.CreateNavigator();
                navigator.MoveToRoot();
                navigator.MoveToChild(XPathNodeType.Element);

                if(String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomEntry entry     = new AtomEntry();
                    entry.Load(navigator);
                    return entry;
                }
                else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomFeed feed       = new AtomFeed();
                    feed.Load(navigator);
                    return feed;
                }
                else
                {
                    return null;
                }
            }
            else if (format == SyndicationContentFormat.BlogML)
            {
                BlogMLDocument document = new BlogMLDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Opml)
            {
                OpmlDocument document   = new OpmlDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Rsd)
            {
                RsdDocument document    = new RsdDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Rss)
            {
                RssFeed feed            = new RssFeed();
                feed.Load(stream);
                return feed;
            }
            else
            {
                return null;
            }
        }
Ejemplo n.º 5
0
 public void AtomNewPostTest()
 {
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/DBlog/AtomPost.aspx");
     request.Method = "POST";
     request.ContentType = "application/atom+xml;type=entry";
     string usernamePassword = string.Format("Administrator:");
     string basicAuthorization = string.Format("Basic {0}", Convert.ToBase64String(
         Encoding.ASCII.GetBytes(usernamePassword)));
     request.Headers.Add("Authorization", basicAuthorization);
     XmlDocument postXml = new XmlDocument();
     postXml.LoadXml(
         "<?xml version=\"1.0\"?>" +
         "<entry xmlns=\"http://www.w3.org/2005/Atom\">" +
           "<title>Atom-Powered Robots Run Amok</title>" +
           "<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>" +
           "<updated>2003-12-13T18:30:02Z</updated>" +
           "<author><name>John Doe</name></author>" +
           "<content>Some text.</content>" +
         "</entry>");
     byte[] postData = Encoding.UTF8.GetBytes(postXml.OuterXml);
     request.ContentLength = postData.Length;
     request.GetRequestStream().Write(postData, 0, postData.Length);
     request.GetRequestStream().Close();
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     string responseLocation = response.Headers["Location"];
     Assert.IsNotEmpty(responseLocation);
     Console.WriteLine(responseLocation);
     // atom entry returned
     AtomEntry atomEntry = new AtomEntry();
     atomEntry.Load(response.GetResponseStream());
     Console.WriteLine(atomEntry.Id.Uri);
     int id = int.Parse(atomEntry.Id.Uri.ToString().Substring(atomEntry.Id.Uri.ToString().LastIndexOf("/") + 1));
     Assert.IsTrue(id > 0);
     Console.WriteLine(string.Format("Id: {0}", id));
     Blog.DeletePost(Ticket, id);
 }
Ejemplo n.º 6
0
        //============================================================
        //    INSTANCE METHODS
        //============================================================
        /// <summary>
        /// Provides example code for the Load(IXPathNavigable) method
        /// </summary>
        public static void LoadIXPathNavigableExample()
        {
            #region Load(IXPathNavigable source)
            XPathDocument source    = new XPathDocument("http://example.org/blog/1234");

            AtomEntry entry         = new AtomEntry();
            entry.Load(source);

            if (entry.UpdatedOn >= DateTime.Today)
            {
                //  Perform some processing on the entry
            }
            #endregion
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Provides example code for the Load(XmlReader) method
        /// </summary>
        public static void LoadXmlReaderExample()
        {
            #region Load(XmlReader reader)
            AtomEntry entry = new AtomEntry();

            using (Stream stream = new FileStream("AtomEntryDocument.xml", FileMode.Open, FileAccess.Read))
            {
                XmlReaderSettings settings  = new XmlReaderSettings();
                settings.IgnoreComments     = true;
                settings.IgnoreWhitespace   = true;

                using(XmlReader reader = XmlReader.Create(stream, settings))
                {
                    entry.Load(reader);

                    if (entry.UpdatedOn >= DateTime.Today)
                    {
                        //  Perform some processing on the entry
                    }
                }
            }
            #endregion
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Provides example code for the Load(Uri, ICredentials, IWebProxy) method
        /// </summary>
        public static void LoadUriExample()
        {
            #region Load(Uri source, ICredentials credentials, IWebProxy proxy)
            AtomEntry entry = new AtomEntry();
            Uri source      = new Uri("http://example.org/blog/1234");

            entry.Load(source, CredentialCache.DefaultNetworkCredentials, null);

            if (entry.UpdatedOn >= DateTime.Today)
            {
                //  Perform some processing on the entry
            }
            #endregion
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Provides example code for the Load(Stream) method
        /// </summary>
        public static void LoadStreamExample()
        {
            #region Load(Stream stream)
            AtomEntry entry = new AtomEntry();

            using (Stream stream = new FileStream("AtomEntryDocument.xml", FileMode.Open, FileAccess.Read))
            {
                entry.Load(stream);

                if (entry.UpdatedOn >= DateTime.Today)
                {
                    //  Perform some processing on the entry
                }
            }
            #endregion
        }