public bool ValidateService()
        {
            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 = client.GetUsersBlogs();

                    // 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);
            }
        }
Ejemplo n.º 2
0
        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());
                    }
                }
            }
        }
Ejemplo n.º 3
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);
        }