Example #1
0
        /// <summary>
        /// Do special Blogger-specific detection logic.  We want to
        /// use the Blogger Atom endpoints specified in the HTML, not
        /// the Blogger endpoint in the RSD.
        /// </summary>
        private async Task <bool> AttemptBloggerDetection(string homepageUrl, string html)
        {
            html = html ?? "";
            BloggerDetectionHelper bloggerDetectionHelper = new BloggerDetectionHelper(homepageUrl, html);

            if (!bloggerDetectionHelper.IsBlogger())
            {
                return(false);
            }

            const string  BLOGGER_ATOM_PROVIDER_ID = "B6F817C3-9D39-45c1-A634-EAC792B8A635";
            IBlogProvider bloggerProvider          = BlogProviderManager.FindProvider(BLOGGER_ATOM_PROVIDER_ID);

            if (bloggerProvider == null)
            {
                //Trace.Fail("Couldn't retrieve Blogger provider");
                return(false);
            }

            _providerId  = bloggerProvider.Id;
            _serviceName = bloggerProvider.Name;
            _clientType  = bloggerProvider.ClientType;
            _postApiUrl  = "http://www.blogger.com/feeds/default/blogs";

            BlogAccountDetector blogAccountDetector = new BlogAccountDetector(bloggerProvider.ClientType, "http://www.blogger.com", _credentials);

            if (await blogAccountDetector.ValidateServiceAsync())
            {
                _usersBlogs = blogAccountDetector.UsersBlogs;
                foreach (BlogInfo blog in _usersBlogs)
                {
                    string blogHomepageUrl = blog.HomepageUrl;
                    if (NormalizeBloggerHomepageUrl(blogHomepageUrl) == NormalizeBloggerHomepageUrl(homepageUrl))
                    {
                        _hostBlogId = blog.Id;
                        _postApiUrl = blog.Id;
                        _blogName   = blog.Name;
                        return(true);
                    }
                }

                // We didn't find the specific blog, but we'll prompt the user with the list of blogs
                return(true);
            }
            else
            {
                AuthenticationErrorOccurred = blogAccountDetector.Exception is BlogClientAuthenticationException;
                ReportErrorAndFail(blogAccountDetector.ErrorMessageType, blogAccountDetector.ErrorMessageParams);
                return(false);
            }
        }
        public static IBlogClient CreateClient(string clientType, string postApiUrl, IBlogCredentialsAccessor credentials, string providerId, IDictionary optionOverrides, IDictionary userOptionOverrides, IDictionary homepageOptionOverrides)
        {
            // create blog client reflecting the settings
            IBlogClient blogClient = CreateClient(clientType, postApiUrl, credentials);

            // if there is a provider associated with the client then use it to override options
            // as necessary for this provider
            IBlogProvider provider = BlogProviderManager.FindProvider(providerId);

            if (provider != null)
            {
                IBlogClientOptions providerOptions = provider.ConstructBlogOptions(blogClient.Options);
                blogClient.OverrideOptions(providerOptions);
            }

            if (homepageOptionOverrides != null)
            {
                OptionOverrideReader homepageOptionsReader = new OptionOverrideReader(homepageOptionOverrides);
                IBlogClientOptions   homepageOptions       = BlogClientOptions.ApplyOptionOverrides(new OptionReader(homepageOptionsReader.Read), blogClient.Options, true);
                blogClient.OverrideOptions(homepageOptions);
            }

            // if there are manifest overrides then apply them
            if (optionOverrides != null)
            {
                OptionOverrideReader manifestOptionsReader = new OptionOverrideReader(optionOverrides);
                IBlogClientOptions   manifestOptions       = BlogClientOptions.ApplyOptionOverrides(new OptionReader(manifestOptionsReader.Read), blogClient.Options, true);
                blogClient.OverrideOptions(manifestOptions);
            }

            // if there are user overrides then apply them
            if (userOptionOverrides != null)
            {
                OptionOverrideReader userOptionsReader = new OptionOverrideReader(userOptionOverrides);
                IBlogClientOptions   userOptions       = BlogClientOptions.ApplyOptionOverrides(new OptionReader(userOptionsReader.Read), blogClient.Options, true);
                blogClient.OverrideOptions(userOptions);
            }



            // return the blog client
            return(blogClient);
        }
Example #3
0
        public void DisplayException(IWin32Window owner, Exception ex)
        {
            // display a custom display message for exceptions that have one
            // registered, otherwise display the generic error form
            if (ex is BlogClientProviderException)
            {
                IBlogProvider provider = BlogProviderManager.FindProvider(_settings.ProviderId);
                if (provider != null)
                {
                    BlogClientProviderException pe = ex as BlogClientProviderException;
                    MessageId messageId            = provider.DisplayMessageForProviderError(pe.ErrorCode, pe.ErrorString);
                    if (messageId != MessageId.None)
                    {
                        DisplayMessage.Show(messageId, owner);
                        return;
                    }
                }
            }
            else if (ex is WebException)
            {
                WebException    we   = (WebException)ex;
                HttpWebResponse resp = we.Response as HttpWebResponse;
                if (resp != null)
                {
                    string friendlyError = HttpRequestHelper.GetFriendlyErrorMessage(we);
                    Trace.WriteLine("Server response body:\r\n" + friendlyError);
                    ex = new BlogClientHttpErrorException(
                        UrlHelper.SafeToAbsoluteUri(resp.ResponseUri),
                        friendlyError,
                        we);
                }
                else
                {
                    DisplayMessage msg = new DisplayMessage(MessageId.ErrorConnecting);
                    ex = new BlogClientException(msg.Title, msg.Text);
                }
                HttpRequestHelper.LogException(we);
            }

            // no custom message, use default UI
            DisplayableExceptionDisplayForm.Show(owner, ex);
        }
Example #4
0
        /// <summary>
        /// Do special Blogger-specific detection logic.  We want to
        /// use the Blogger Atom endpoints specified in the HTML, not
        /// the Blogger endpoint in the RSD.
        /// </summary>
        private bool AttemptBloggerDetection(string homepageUrl, string html)
        {
            Debug.Assert(string.IsNullOrEmpty(homepageUrl), "Google Blogger blogs don't know the homepageUrl");
            Debug.Assert(string.IsNullOrEmpty(html), "Google Blogger blogs don't know the homepageUrl");

            const string  BLOGGER_V3_PROVIDER_ID = "343F1D83-1098-43F4-AE86-93AFC7602855";
            IBlogProvider bloggerProvider        = BlogProviderManager.FindProvider(BLOGGER_V3_PROVIDER_ID);

            if (bloggerProvider == null)
            {
                Trace.Fail("Couldn't retrieve Blogger provider");
                return(false);
            }

            BlogAccountDetector blogAccountDetector = new BlogAccountDetector(bloggerProvider.ClientType, bloggerProvider.PostApiUrl, _credentials);

            if (blogAccountDetector.ValidateService())
            {
                CopySettingsFromProvider(bloggerProvider);

                _usersBlogs = blogAccountDetector.UsersBlogs;
                if (_usersBlogs.Length == 1)
                {
                    _hostBlogId  = _usersBlogs[0].Id;
                    _blogName    = _usersBlogs[0].Name;
                    _homepageUrl = _usersBlogs[0].HomepageUrl;
                }

                // If we didn't find the specific blog, we'll prompt the user with the list of blogs
                return(true);
            }
            else
            {
                AuthenticationErrorOccurred = blogAccountDetector.Exception is BlogClientAuthenticationException;
                ReportErrorAndFail(blogAccountDetector.ErrorMessageType, blogAccountDetector.ErrorMessageParams);
                return(false);
            }
        }
Example #5
0
        protected override object DetectBlogService(IProgressHost progressHost)
        {
            using (BlogClientUIContextSilentMode uiContextScope = new BlogClientUIContextSilentMode()) //suppress prompting for credentials
            {
                try
                {
                    // copy basic account info
                    IBlogProvider provider = BlogProviderManager.FindProvider("4AA58E69-8C24-40b1-BACE-3BB14237E8F9");
                    _providerId  = provider.Id;
                    _serviceName = provider.Name;
                    _clientType  = provider.ClientType;

                    //calculate the API url based on the homepage Url.
                    //  API URL Format: <blogurl>/_layouts/metaweblog.aspx
                    string homepagePath = UrlHelper.SafeToAbsoluteUri(new Uri(_homepageUrl)).Split('?')[0];
                    if (homepagePath == null)
                    {
                        homepagePath = "/";
                    }

                    //trim off any file information included in the URL (ex: /default.aspx)
                    int lastPathPartIndex = homepagePath.LastIndexOf("/", StringComparison.OrdinalIgnoreCase);
                    if (lastPathPartIndex != -1)
                    {
                        string lastPathPart = homepagePath.Substring(lastPathPartIndex);
                        if (lastPathPart.IndexOf('.') != -1)
                        {
                            homepagePath = homepagePath.Substring(0, lastPathPartIndex);
                            if (homepagePath == String.Empty)
                            {
                                homepagePath = "/";
                            }
                        }
                    }
                    if (homepagePath != "/" && homepagePath.EndsWith("/", StringComparison.OrdinalIgnoreCase)) //trim off trailing slash
                    {
                        homepagePath = homepagePath.Substring(0, homepagePath.Length - 1);
                    }

                    //Update the homepage url
                    _homepageUrl = homepagePath;

                    _postApiUrl = String.Format(CultureInfo.InvariantCulture, "{0}/_layouts/metaweblog.aspx", homepagePath);

                    if (VerifyCredentialsAndDetectAuthScheme(_postApiUrl, _blogCredentials, _credentials))
                    {
                        AuthenticationErrorOccurred = false;
                        //detect the user's blog ID.
                        if (!BlogProviderParameters.UrlContainsParameters(_postApiUrl))
                        {
                            // we detected the provider, now see if we can detect the weblog id
                            // (or at lease the list of the user's weblogs)
                            UpdateProgress(progressHost, 80, Res.Get(StringId.ProgressAnalyzingWeblogList));
                            AttemptUserBlogDetection();
                        }
                    }
                    else
                    {
                        AuthenticationErrorOccurred = true;
                    }
                }
                catch (OperationCancelledException)
                {
                    // WasCancelled == true
                }
                catch (BlogClientOperationCancelledException)
                {
                    Cancel();
                    // WasCancelled == true
                }
                catch (BlogAccountDetectorException)
                {
                    // ErrorOccurred == true
                }
                catch (BlogClientAuthenticationException)
                {
                    AuthenticationErrorOccurred = true;
                    // ErrorOccurred == true
                }
                catch (Exception ex)
                {
                    // ErrorOccurred == true
                    ReportError(MessageId.WeblogDetectionUnexpectedError, ex.Message);
                }
            }
            return(this);
        }
Example #6
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);
        }