示例#1
0
        /*private string DiscoverPostApiUrl(string baseUrl, string blogPath)
         * {
         *
         * }*/

        /// <summary>
        /// Verifies the user credentials and determines whether SharePoint is configure to use HTTP or MetaWeblog authentication
        /// </summary>
        /// <param name="postApiUrl"></param>
        /// <param name="blogCredentials"></param>
        /// <param name="credentials"></param>
        /// <returns></returns>
        private static bool VerifyCredentialsAndDetectAuthScheme(string postApiUrl, IBlogCredentials blogCredentials, IBlogCredentialsAccessor credentials)
        {
            BlogClientAttribute blogClientAttr = (BlogClientAttribute)typeof(SharePointClient).GetCustomAttributes(typeof(BlogClientAttribute), false)[0];
            SharePointClient    client         = (SharePointClient)BlogClientManager.CreateClient(blogClientAttr.TypeName, postApiUrl, credentials);

            return(SharePointClient.VerifyCredentialsAndDetectAuthScheme(blogCredentials, client));
        }
        public static bool BlogIdIsValid(string id)
        {
            BlogSettings blogSettings = null;

            try
            {
                blogSettings = BlogSettings.ForBlogId(id);
                if (!blogSettings.IsValid)
                {
                    return(false);
                }
                return(BlogClientManager.IsValidClientType(blogSettings.ClientType));
            }
            catch (ArgumentException)
            {
                Trace.WriteLine("Default blog has invalid client type, ignoring blog.");
                return(false);
            }
            finally
            {
                if (blogSettings != null)
                {
                    blogSettings.Dispose();
                }
            }
        }
示例#3
0
 public static WinInetCredentialsContext GetCredentialsContext(string blogId, string url)
 {
     using (BlogSettings blogSettings = BlogSettings.ForBlogId(blogId))
     {
         IBlogSettingsAccessor settings = blogSettings as IBlogSettingsAccessor;
         return(GetCredentialsContext(BlogClientManager.CreateClient(settings), settings.Credentials, url));
     }
 }
示例#4
0
 public WeblogBlogFileUploader(string destinationContext, string postContext, string localBlogId, string remoteBlogId)
     : base(destinationContext,
            postContext,
            remoteBlogId)
 {
     _blogSettings    = BlogSettings.ForBlogId(localBlogId);
     _blogClient      = BlogClientManager.CreateClient(_blogSettings);
     _blogHomepageUrl = _blogSettings.HomepageUrl;
 }
        /// <summary>
        /// SetContext using a weblog account
        /// </summary>
        public void SetContext(BlogAccount blogAccount, IBlogCredentialsAccessor credentials, string blogHomepageUrl, string blogTemplateDir, WriterEditingManifestDownloadInfo manifestDownloadInfo, bool probeForManifest, string providerId, IDictionary optionOverrides, IDictionary userOptionOverrides, IDictionary homepageOptionOverrides)
        {
            // note context set
            _contextSet = true;

            // create a blog client
            _blogAccount = blogAccount;
            _credentials = credentials;
            _blogClient  = BlogClientManager.CreateClient(blogAccount.ClientType, blogAccount.PostApiUrl, credentials, providerId, optionOverrides, userOptionOverrides, homepageOptionOverrides);

            // set other context that we've got
            _blogHomepageUrl      = blogHomepageUrl;
            _blogTemplateDir      = blogTemplateDir;
            _manifestDownloadInfo = manifestDownloadInfo;
            _probeForManifest     = probeForManifest;
        }
        public async Task <bool> ValidateServiceAsync()
        {
            try
            {
                //using (new BlogClientUIContextSilentMode()) //suppress prompting for password if an error occurs
                //{
                // get a list of the user's blogs
                IBlogClient client = BlogClientManager.CreateClient(_clientType, _postApiUrl, _credentials);
                _usersBlogs = await client.GetUsersBlogsAsync();

                // we can't continue if there are no blogs
                if (_usersBlogs.Length == 0)
                {
                    throw new NoAccountsOnServerException();
                }

                // success
                return(true);
                //}
            }
            catch (BlogClientAuthenticationException ex)
            {
                ReportError(ex, MessageId.WeblogAuthenticationError);
                return(false);
            }
            catch (BlogClientPostUrlNotFoundException ex)
            {
                ReportError(ex, MessageId.WeblogUrlNotFound, _postApiUrl);
                return(false);
            }
            catch (NoAccountsOnServerException ex)
            {
                ReportError(ex, MessageId.WeblogNoAccountsOnServer);
                return(false);
            }
            catch (BlogClientOperationCancelledException)
            {
                return(false);
            }
            catch (Exception ex)
            {
                ReportError(ex, MessageId.WeblogConnectionError, ex.Message);
                return(false);
            }
        }
        public void RunTests(Provider provider, Blog blog, XmlElement providerEl)
        {
            using (new BlogClientUIContextSilentMode()) //supress prompting for credentials
            {
                TemporaryBlogCredentials credentials = new TemporaryBlogCredentials();
                credentials.Username = blog.Username;
                credentials.Password = blog.Password;
                BlogCredentialsAccessor credentialsAccessor = new BlogCredentialsAccessor(Guid.NewGuid().ToString(), credentials);
                IBlogClient             client = BlogClientManager.CreateClient(provider.ClientType, blog.ApiUrl, credentialsAccessor);

                if (blog.BlogId == null)
                {
                    BlogInfo[] blogs = client.GetUsersBlogs();
                    if (blogs.Length == 1)
                    {
                        blog.BlogId         = blogs[0].Id;
                        credentialsAccessor = new BlogCredentialsAccessor(blog.BlogId, credentials);
                        client = BlogClientManager.CreateClient(provider.ClientType, blog.ApiUrl, credentialsAccessor);
                    }
                }

                foreach (Test test in tests)
                {
                    try
                    {
                        Console.WriteLine("Running test " + test.ToString());
                        test.DoTest(blog, client, providerEl);
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Error: Test " + test.GetType().Name + " failed for provider " + provider.Name + ":");
                        Console.Error.WriteLine(e.ToString());
                    }
                }
            }
        }
        public BlogProviderFromXml(XmlNode providerNode)
        {
            string id = NodeText(providerNode.SelectSingleNode("id"));

            if (id.Length == 0)
            {
                throw new ArgumentException("Missing Id parameter");
            }

            string name = NodeText(providerNode.SelectSingleNode("name"));

            if (name.Length == 0)
            {
                throw new ArgumentException("Missing Name parameter");
            }

            string description = NodeText(providerNode.SelectSingleNode("description"));

            string link = NodeText(providerNode.SelectSingleNode("link"));

            string clientType = NodeText(providerNode.SelectSingleNode("clientType"));

            if (clientType.Length == 0)
            {
                throw new ArgumentException("Missing ClientType parameter");
            }
            if (!BlogClientManager.IsValidClientType(clientType))
            {
                throw new ArgumentException("Invalid ClientType: " + clientType);
            }

            string postApiUrl = NodeText(providerNode.SelectSingleNode("postApiUrl"));

            if (postApiUrl.Length == 0)
            {
                throw new ArgumentException("Invalid PostApiUrl");
            }

            // visibiilty flag
            bool visible = BlogClientOptions.ReadBool(NodeText(providerNode.SelectSingleNode("visible")), true);;

            // auto-detection
            string homepageUrlPattern     = NodeText(providerNode.SelectSingleNode("homepageUrlPattern"));
            string homepageContentPattern = NodeText(providerNode.SelectSingleNode("homepageContentPattern"));
            string rsdEngineNamePattern   = NodeText(providerNode.SelectSingleNode("rsdEngineNamePattern"));
            string rsdHomepageLinkPattern = NodeText(providerNode.SelectSingleNode("rsdHomepageLinkPattern"));

            // rsd client type mappings
            ArrayList   rsdClientTypeMappings = new ArrayList();
            XmlNodeList mappingNodes          = providerNode.SelectNodes("rsdClientTypeMappings/mapping");

            foreach (XmlNode mappingNode in mappingNodes)
            {
                string rsdClientType    = NodeText(mappingNode.SelectSingleNode("@rsdClientType"));
                string writerClientType = NodeText(mappingNode.SelectSingleNode("@clientType"));
                if (rsdClientType != String.Empty && writerClientType != String.Empty)
                {
                    rsdClientTypeMappings.Add(new RsdClientTypeMapping(rsdClientType, writerClientType));
                }
            }

            // provider faults
            ArrayList   providerFaults = new ArrayList();
            XmlNodeList faultNodes     = providerNode.SelectNodes("faults/fault");

            foreach (XmlNode faultNode in faultNodes)
            {
                string codePattern   = NodeText(faultNode.SelectSingleNode("codePattern"));
                string stringPattern = NodeText(faultNode.SelectSingleNode("stringPattern"));
                string messageId     = NodeText(faultNode.SelectSingleNode("messageId"));
                if (messageId != String.Empty)
                {
                    providerFaults.Add(new ProviderFault(codePattern, stringPattern, messageId));
                }
            }

            // parse options (create generic options object to populate defaults)
            XmlNode optionsNode = providerNode.SelectSingleNode("options");

            foreach (XmlNode node in optionsNode.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    _options.Add(node.Name, node.InnerText.Trim());
                }
            }

            StringId   postApiUrlLabel           = StringId.CWSelectProviderApiUrlLabel;
            XmlElement postApiUrlDescriptionNode = providerNode.SelectSingleNode("postApiUrlLabel") as XmlElement;

            if (postApiUrlDescriptionNode != null)
            {
                try
                {
                    postApiUrlLabel = (StringId)Enum.Parse(
                        typeof(StringId),
                        "CWSelectProviderApiUrlLabel_" + postApiUrlDescriptionNode.InnerText,
                        false);
                }
                catch
                {
                    Debug.Fail("Invalid value for postApiUrlLabel");
                }
            }

            string  appid     = null;
            XmlNode appIdNode = providerNode.SelectSingleNode("appid/text()");

            if (appIdNode != null && !string.IsNullOrEmpty(appIdNode.Value))
            {
                appid = appIdNode.Value;
            }

            // initialize
            Init(id, name, description, link, clientType, postApiUrl, postApiUrlLabel, appid,
                 homepageUrlPattern, homepageContentPattern,
                 rsdClientTypeMappings.ToArray(typeof(RsdClientTypeMapping)) as RsdClientTypeMapping[],
                 rsdEngineNamePattern, rsdHomepageLinkPattern,
                 providerFaults.ToArray(typeof(ProviderFault)) as ProviderFault[],
                 visible);
        }
示例#9
0
 /// <summary>
 /// Create a blog client based on all of the context we currently have available
 /// </summary>
 /// <returns></returns>
 private IBlogClient CreateBlogClient()
 {
     return(BlogClientManager.CreateClient(_context.ClientType, _context.PostApiUrl, _context.Credentials, _context.ProviderId, _context.OptionOverrides, _context.UserOptionOverrides, _context.HomePageOverrides));
 }
示例#10
0
        private bool AttemptGenericAtomLinkDetection(string url, string html, bool preferredOnly)
        {
            const string GENERIC_ATOM_PROVIDER_ID = "D48F1B5A-06E6-4f0f-BD76-74F34F520792";

            if (html == null)
            {
                return(false);
            }

            HtmlExtractor ex = new HtmlExtractor(html);

            if (ex
                .SeekWithin("<head>", "<body>")
                .SeekWithin("<link href rel='service' type='application/atomsvc+xml'>", "</head>")
                .Success)
            {
                IBlogProvider atomProvider = BlogProviderManager.FindProvider(GENERIC_ATOM_PROVIDER_ID);

                BeginTag bt = ex.Element as BeginTag;

                if (preferredOnly)
                {
                    string classes = bt.GetAttributeValue("class");
                    if (classes == null)
                    {
                        return(false);
                    }
                    if (!Regex.IsMatch(classes, @"\bpreferred\b"))
                    {
                        return(false);
                    }
                }

                string linkUrl = bt.GetAttributeValue("href");

                Debug.WriteLine("Atom service link detected in the blog homepage");

                _providerId  = atomProvider.Id;
                _serviceName = atomProvider.Name;
                _clientType  = atomProvider.ClientType;
                _blogName    = string.Empty;
                _postApiUrl  = GetAbsoluteUrl(url, linkUrl);

                IBlogClient client = BlogClientManager.CreateClient(atomProvider.ClientType, _postApiUrl, _credentials);
                client.VerifyCredentials();
                _usersBlogs = client.GetUsersBlogs();
                if (_usersBlogs.Length == 1)
                {
                    _hostBlogId = _usersBlogs[0].Id;
                    _blogName   = _usersBlogs[0].Name;

                    /*
                     *                  if (_usersBlogs[0].HomepageUrl != null && _usersBlogs[0].HomepageUrl.Length > 0)
                     *                      _homepageUrl = _usersBlogs[0].HomepageUrl;
                     */
                }

                // attempt to read the blog name from the homepage title
                if (_blogName == null || _blogName.Length == 0)
                {
                    HtmlExtractor ex2 = new HtmlExtractor(html);
                    if (ex2.Seek("<title>").Success)
                    {
                        _blogName = ex2.CollectTextUntil("title");
                    }
                }

                return(true);
            }
            return(false);
        }