public BloomLinkArgs(string url)
        {
            if (!url.StartsWith(kBloomUrlPrefix))
            {
                throw new ArgumentException(String.Format("unrecognized BloomLinkArgs URL string: {0}", url));
            }
            // I think we can't use the standard HttpUtility because we are trying to stick to the .NET 4.0 Client profile
            var queryData = url.Substring(kBloomUrlPrefix.Length);
            var qparams   = queryData.Split('&');
            var parts     = qparams[0].Split('=');

            if (parts.Length != 2 || parts[0] != kOrderFile)
            {
                throw new ArgumentException(String.Format("badly formed BloomLinkArgs URL string: {0}", url));
            }
            OrderUrl = HttpUtilityFromMono.UrlDecode(parts[1]);
            if (qparams.Length > 1 && qparams[1].StartsWith("title="))
            {
                Title = HttpUtilityFromMono.UrlDecode(qparams[1].Substring("title=".Length));
            }
            else
            {
                // Make up a title from the book order. This should be obsolete once all instances of Bloom Library
                // are supplying titles.
                var indexOfSlash = OrderUrl.LastIndexOf('/');
                var bookOrder    = OrderUrl.Substring(indexOfSlash + 1);
                Title = Path.GetFileNameWithoutExtension(bookOrder);
            }
        }
示例#2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Internal method for translating the specified text.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        protected override string InternalTranslate(string srcText)
        {
            var text     = HttpUtilityFromMono.UrlPathEncode(srcText);
            var ver      = HttpUtilityFromMono.UrlEncode("1.0");
            var langPair = HttpUtilityFromMono.UrlEncode(string.Format("{0}|{1}", m_srcCultureId, m_tgtCultureId));
            var encodedRequestUrlFragment = string.Format("?v={0}&q={1}&langpair={2}", ver, text, langPair);

            try
            {
                var request  = WebRequest.Create(kServiceUrl + encodedRequestUrlFragment);
                var response = request.GetResponse();

                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var json = reader.ReadLine();
                    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                    {
                        var ser         = new DataContractJsonSerializer(typeof(JSONResponse));
                        var translation = ser.ReadObject(ms) as JSONResponse;
                        reader.Close();
                        return(translation.responseData.translatedText);
                    }
                }
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
示例#3
0
        /// <summary>
        /// Builds a context descriptor with the given parameters that will provide a url link to the correct area in FLEx.
        /// </summary>
        internal static ContextDescriptor GenerateContextDescriptor(string filePath, string guid, string label)
        {
            var appId         = "FLEx";
            var directory     = Path.GetDirectoryName(filePath);
            var lastDirectory = Path.GetFileName(directory);

            if (lastDirectory == "Scripture")
            {
                appId = "TE";
            }
            // figure out here which we need.
            var fwAppArgs = new FwAppArgs(appId, "current", "", "default", guid);

            // Add the "label" information which the Chorus Notes browser extracts to identify the object in the UI.
            // This is just for a label and we can't have & or = in the value. So replace them if they occur.
            fwAppArgs.AddProperty("label", label.Replace("&", " and ").Replace("=", " equals "));
            // The FwUrl has all the query part encoded.
            // Chorus needs it unencoded so it can extract the label.
            var fwUrl      = fwAppArgs.ToString();
            var hostLength = fwUrl.IndexOf("?", StringComparison.Ordinal);
            var host       = fwUrl.Substring(0, hostLength);
            var query      = HttpUtilityFromMono.UrlDecode(fwUrl.Substring(hostLength + 1));
            var url        = host + "?" + query;

            return(new ContextDescriptor(label, url));
        }
示例#4
0
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Use the explicit type tag to parse parameters into the right type
 /// </summary>
 /// <param name="value">The value.</param>
 /// ------------------------------------------------------------------------------------
 protected object Decode(string value)
 {
     if (value.IndexOf("bool:") > -1)
     {
         value = value.Substring(5);
         return(bool.Parse(value));
     }
     return(HttpUtilityFromMono.UrlDecode(value));
 }
示例#5
0
 public virtual void InitFromUri(string url)
 {
     SetServerLabelFromUrl(url);
     Password    = HttpUtilityFromMono.UrlDecode(UrlHelper.GetPassword(url));
     AccountName = HttpUtilityFromMono.UrlDecode(UrlHelper.GetUserName(url));
     ProjectId   = HttpUtilityFromMono.UrlDecode(UrlHelper.GetPathAfterHost(url));
     CustomUrl   = UrlHelper.GetPathOnly(url);
     //CustomUrlSelected = true;
 }
示例#6
0
        public virtual string GetSyncUri(ILfProject project)
        {
            var settings = MainClass.Container.Resolve <LfMergeSettings>();

            if (!string.IsNullOrEmpty(settings.LanguageDepotRepoUri))
            {
                return(settings.LanguageDepotRepoUri);
            }

            var uriBldr = new UriBuilder(project.LanguageDepotProjectUri)
            {
                UserName = Username,
                Password = Password,
                Path     = HttpUtilityFromMono.UrlEncode(project.LanguageDepotProject.Identifier)
            };

            return(uriBldr.Uri.ToString());
        }
示例#7
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Get a URL corresponding to this link
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents this instance.
        /// </returns>
        /// ------------------------------------------------------------------------------------
        public override string ToString()
        {
            UriBuilder uriBuilder = new UriBuilder(kSilScheme, kLocalHost);

            uriBuilder.Path = kLink;
            StringBuilder query = new StringBuilder();

            AddProperties(query);

            foreach (Property property in PropertyTableEntries)
            {
                query.AppendFormat("&{0}={1}", property.Name, Encode(property.Value));
            }

            //make it safe to represent as a url string (e.g., convert spaces)
            uriBuilder.Query = HttpUtilityFromMono.UrlEncode(query.ToString());

            return(uriBuilder.Uri.AbsoluteUri);
        }
示例#8
0
        private void JumpToFlexObject(string url)
        {
            //// Flex expects the query to be UrlEncoded (I think so it can be used as a command line argument).
            var hostLength = url.IndexOf("?", StringComparison.InvariantCulture);

            if (hostLength < 0)
            {
                return;                 // can't do it, not a valid FLEx url.
            }
            var host = url.Substring(0, hostLength);
            // This should be fairly safe for a lift URL, since it won't have the "database=current" string in the query.
            // A lift URL will be something like:
            //		lift://foo.lift?type=entry&id=someguid&label=formforentry
            var originalQuery = url.Substring(hostLength + 1).Replace("database=current", "database=" + ProjectName);
            var query         = HttpUtilityFromMono.UrlEncode(originalQuery);

            // Instead of closing the conflict viewer we now need to fire this event to notify
            // the FLExConnectionHelper that we have a URL to jump to.
            if (JumpUrlChanged != null)
            {
                JumpUrlChanged(this, new JumpEventArgs(host + "?" + query));
            }
        }
示例#9
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Cleans up a string after it has returned from being translated by Google. The
        /// paramCount provides the number of parameters that were found in the pretranslated
        /// string.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        protected virtual string PostTranslateProcess(int paramCount, string text)
        {
            if (text == null)
            {
                return(null);
            }

            text = HttpUtilityFromMono.HtmlDecode(text);

            if (paramCount > 0)
            {
                // Because Google translate replaces the braces in string parameters,
                // with parentheses, we need to restore the braces. This process will
                // be a problem only when the original string contains numbers surrounded
                // by parentheses. However, I think those cases are far less likely.
                for (int i = 0; i < paramCount; i++)
                {
                    text = text.Replace("(" + i + ")", "{" + i + "}");
                }
            }

            text = text.Replace("(-999) ", Environment.NewLine);
            return(text.Trim());
        }
示例#10
0
        /// <summary>
        /// API to query the SLDR for an LDML file and save it locally in the SLDR cache and specified directories
        /// </summary>
        /// <param name="destinationPath">Destination path to save the requested LDML file</param>
        /// <param name="languageTag">Current IETF language tag</param>
        /// <param name="topLevelElements">List of top level element names to request. SLDR will always publish identity, so it doesn't need to be requested.
        /// If null, the entire LDML file will be requested.</param>
        /// <param name="filename">Saved filename</param>
        /// <returns>Enum status SldrStatus if file could be retrieved and the source</returns>
        public static SldrStatus GetLdmlFile(string destinationPath, string languageTag, IEnumerable <string> topLevelElements, out string filename)
        {
            CheckInitialized();

            if (String.IsNullOrEmpty(destinationPath))
            {
                throw new ArgumentException("destinationPath");
            }
            if (!Directory.Exists(destinationPath))
            {
                throw new DirectoryNotFoundException("destinationPath");
            }
            if (String.IsNullOrEmpty(languageTag) || (!IetfLanguageTag.IsValid(languageTag)))
            {
                throw new ArgumentException("ietfLanguageTag");
            }
            if (topLevelElements == null)
            {
                throw new ArgumentNullException("topLevelElements");
            }

            string sldrLanguageTag = IetfLanguageTag.Canonicalize(languageTag);
            SldrLanguageTagInfo langTagInfo;

            if (LanguageTags.TryGet(sldrLanguageTag, out langTagInfo))
            {
                sldrLanguageTag = langTagInfo.SldrLanguageTag;
            }
            string[] topLevelElementsArray = topLevelElements.ToArray();

            using (_sldrCacheMutex.Lock())
            {
                var status = SldrStatus.NotFound;
                CreateSldrCacheDirectory();
                string sldrCacheFilePath;
                bool   redirected;
                do
                {
                    string revid, uid = "", tempString;
                    if (destinationPath == SldrCachePath)
                    {
                        filename = string.Format("{0}.{1}", sldrLanguageTag, LdmlExtension);
                    }
                    else
                    {
                        filename = string.Format("{0}.{1}", languageTag, LdmlExtension);
                        // Check if LDML file already exists in destination and read revid and uid
                        if (!ReadSilIdentity(Path.Combine(destinationPath, filename), out tempString, out uid))
                        {
                            uid = DefaultUserId;
                        }
                    }

                    // If languageTag contains fonipa, don't bother trying to access the SLDR
                    if (sldrLanguageTag.Contains(WellKnownSubtags.IpaVariant) || sldrLanguageTag.Contains(WellKnownSubtags.AudioScript))
                    {
                        return(SldrStatus.NotFound);
                    }

                    sldrCacheFilePath = Path.Combine(SldrCachePath, !string.IsNullOrEmpty(uid) && uid != DefaultUserId ? string.Format("{0}-{1}.{2}", sldrLanguageTag, uid, LdmlExtension)
                                                : string.Format("{0}.{1}", sldrLanguageTag, LdmlExtension));
                    // Read revid from cache file
                    ReadSilIdentity(sldrCacheFilePath, out revid, out tempString);

                    // Concatenate parameters for url string
                    string requestedElements = string.Empty;
                    if (topLevelElementsArray.Length > 0)
                    {
                        requestedElements = string.Format("&inc[]={0}", string.Join("&inc[]=", topLevelElementsArray));
                    }
                    string requestedUserId = !string.IsNullOrEmpty(uid) ? string.Format("&uid={0}", uid) : string.Empty;
                    string requestedRevid  = !string.IsNullOrEmpty(revid) ? string.Format("&revid={0}", revid) : string.Empty;
                    string url             = string.Format("{0}{1}?ext={2}&flatten=1{3}{4}{5}",
                                                           SldrRepository, sldrLanguageTag, LdmlExtension,
                                                           requestedElements, requestedUserId, requestedRevid);

                    string tempFilePath = sldrCacheFilePath + "." + TmpExtension;

                    // Using WebRequest instead of WebClient so we have access to disable AllowAutoRedirect
                    var webRequest = (HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(url));
                    webRequest.AllowAutoRedirect = false;
                    webRequest.UserAgent         = UserAgent;
                    webRequest.Timeout           = 10000;

                    try
                    {
                        if (_offlineMode)
                        {
                            throw new WebException("Test mode: SLDR offline so accessing cache", WebExceptionStatus.ConnectFailure);
                        }

                        // Check the response header to see if the requested LDML file got redirected
                        using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
                        {
                            if (webResponse.StatusCode == HttpStatusCode.NotModified)
                            {
                                // Report status that file is the most current from SLDR
                                status     = SldrStatus.FromSldr;
                                redirected = false;
                            }
                            else if (webResponse.StatusCode == HttpStatusCode.MovedPermanently)
                            {
                                // Extract ietfLanguageTag from the response header
                                var parsedresponse = HttpUtilityFromMono.ParseQueryString(webResponse.Headers["Location"]);
                                sldrLanguageTag = parsedresponse.Get("ws_id").Split('?')[0];
                                redirected      = true;
                            }
                            else
                            {
                                // Download the LDML file to a temp file in case the transfer gets interrupted
                                using (Stream responseStream = webResponse.GetResponseStream())
                                    using (var fs = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.Write))
                                    {
                                        var buff = new byte[102400];
                                        int c;
                                        while ((c = responseStream.Read(buff, 0, buff.Length)) > 0)
                                        {
                                            fs.Write(buff, 0, c);
                                            fs.Flush();
                                        }
                                    }

                                status            = SldrStatus.FromSldr;
                                sldrCacheFilePath = MoveTmpToCache(tempFilePath, uid);
                                redirected        = false;
                            }
                        }
                    }
                    catch (WebException we)
                    {
                        // Return from 404 error
                        var errorResponse = (HttpWebResponse)we.Response;
                        if ((we.Status == WebExceptionStatus.ProtocolError) && (errorResponse.StatusCode == HttpStatusCode.NotFound))
                        {
                            return(SldrStatus.NotFound);
                        }

                        string sldrCacheFilename;
                        // Download failed so check SLDR cache
                        if (!string.IsNullOrEmpty(uid) && (uid != DefaultUserId))
                        {
                            sldrCacheFilename = string.Format("{0}-{1}.{2}", sldrLanguageTag, uid, LdmlExtension);
                        }
                        else
                        {
                            sldrCacheFilename = string.Format("{0}.{1}", sldrLanguageTag, LdmlExtension);
                        }
                        sldrCacheFilePath = Path.Combine(SldrCachePath, sldrCacheFilename);
                        if (File.Exists(sldrCacheFilePath))
                        {
                            status = SldrStatus.FromCache;
                        }
                        else
                        {
                            return(SldrStatus.UnableToConnectToSldr);
                        }
                        redirected = false;
                    }
                    finally
                    {
                        if (File.Exists(tempFilePath))
                        {
                            File.Delete(tempFilePath);
                        }
                    }
                } while (redirected);

                if (destinationPath != SldrCachePath)
                {
                    // Copy from Cache to destination (w/o uid in filename), overwriting whatever used to be there
                    File.Copy(sldrCacheFilePath, Path.Combine(destinationPath, filename), true);
                }

                return(status);
            }
        }
示例#11
0
        public string DownloadFromOrderUrl(string orderUrl, string destPath)
        {
            var decoded     = HttpUtilityFromMono.UrlDecode(orderUrl);
            var bucketStart = decoded.IndexOf(_s3Client.BucketName, StringComparison.InvariantCulture);

            if (bucketStart == -1)
            {
#if DEBUG
                if (decoded.StartsWith(("BloomLibraryBooks")))
                {
                    Palaso.Reporting.ErrorReport.NotifyUserOfProblem(
                        "The book is from bloomlibrary.org, but you are running the DEBUG version of Bloom, which can only use dev.bloomlibrary.org.");
                }
                else
                {
                    throw new ApplicationException("Can't match URL of bucket of the book being downloaded, and I don't know why.");
                }
#else
                if (decoded.StartsWith(("BloomLibraryBooks-Sandbox")))
                {
                    Palaso.Reporting.ErrorReport.NotifyUserOfProblem(
                        "The book is from the testing version of the bloomlibrary, but you are running the RELEASE version of Bloom. The RELEASE build cannot use the 'dev.bloomlibrary.org' site. If you need to do that for testing purposes, set the windows Environment variable 'BloomSandbox' to 'true'.", decoded);
                }
                else
                {
                    throw new ApplicationException(string.Format("Can't match URL of bucket of the book being downloaded {0}, and I don't know why.", decoded));
                }
#endif
                return(null);
            }

            var    s3orderKey = decoded.Substring(bucketStart + _s3Client.BucketName.Length + 1);
            string url        = "unknown";
            string title      = "unknown";
            try
            {
                var metadata = BookMetaData.FromString(_s3Client.DownloadFile(s3orderKey));
                url   = metadata.DownloadSource;
                title = metadata.Title;
                if (_progressDialog != null)
                {
                    _progressDialog.Invoke((Action)(() => { _progressDialog.Progress = 1; }));
                }
                // downloading the metadata is considered step 1.
                var destinationPath = DownloadBook(metadata.DownloadSource, destPath);
                LastBookDownloadedPath = destinationPath;

                Analytics.Track("DownloadedBook-Success",
                                new Dictionary <string, string>()
                {
                    { "url", url }, { "title", title }
                });
                return(destinationPath);
            }
            catch (WebException e)
            {
                DisplayNetworkDownloadProblem(e);
                Analytics.Track("DownloadedBook-Failure",
                                new Dictionary <string, string>()
                {
                    { "url", url }, { "title", title }
                });
                Analytics.ReportException(e);
                return("");
            }
            catch (AmazonServiceException e)
            {
                DisplayNetworkDownloadProblem(e);
                Analytics.Track("DownloadedBook-Failure",
                                new Dictionary <string, string>()
                {
                    { "url", url }, { "title", title }
                });
                Analytics.ReportException(e);
                return("");
            }
            catch (Exception e)
            {
                ShellWindow.Invoke((Action)(() =>
                                            Palaso.Reporting.ErrorReport.NotifyUserOfProblem(e,
                                                                                             LocalizationManager.GetString("PublishTab.Upload.DownloadProblem",
                                                                                                                           "There was a problem downloading your book. You may need to restart Bloom or get technical help."))));
                Analytics.Track("DownloadedBook-Failure",
                                new Dictionary <string, string>()
                {
                    { "url", url }, { "title", title }
                });
                Analytics.ReportException(e);
                return("");
            }
        }
示例#12
0
        /// <summary>
        /// This method receives the HtmlDetails stored in a conflict, and returns adjusted HTML.
        /// Specifically it fixes URLs containing "database=current" to contain the real project name,
        /// replaces WS identifiers with pretty names, and replaces reference property checksums
        /// with a nice list of reference properties that have conflicts (if any).
        /// </summary>
        internal string AdjustConflictHtml(string input)
        {
            var projectNameForUrl = HttpUtilityFromMono.UrlEncode(ProjectName);

            return(FixChecksums(FixWsRuns(input)).Replace(@"&amp;database=current&amp;", @"&amp;database=" + projectNameForUrl + @"&amp;"));
        }