Search code examples
javascriptjavams-wopi

How to use Discovery XML in WOPI?


As mentioned in this document http://wopi.readthedocs.io/en/latest/discovery.html, I was wondering if there is a way to use the Action URL Dynamically?


Solution

  • What do you mean by "dynamically"?

    When you load the discovery file, you can dynamically build action URLs by replacing placeholders such as <ui=UI_LLCC&>.

    Here's my C# code which should be easily transformable to Java:

        public async Task<string> GetFileUrlAsync(string extension, string wopiFileUrl, WopiActionEnum action, WopiUrlSettings urlSettings = null)
        {
            var combinedUrlSettings = new WopiUrlSettings(urlSettings.Merge(UrlSettings));
            var template = await WopiDiscoverer.GetUrlTemplateAsync(extension, action);
            if (!string.IsNullOrEmpty(template))
            {
                // Resolve optional parameters
                var url = Regex.Replace(template, @"<(?<name>\w*)=(?<value>\w*)&*>", m => ResolveOptionalParameter(m.Groups["name"].Value, m.Groups["value"].Value, combinedUrlSettings));
                url = url.TrimEnd('&');
    
                // Append mandatory parameters
                url += "&WOPISrc=" + Uri.EscapeDataString(wopiFileUrl);
    
                return url;
            }
            return null;
        }
    

    Note that the WopiUrlBuilder uses WopiDiscoverer that facilitates low level operations upon the discovery file.