コード例 #1
0
        async Task <IWopiDiscoveryDocument> IWopiDiscoveryDocumentRepository.GetAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var clientDiscoveryDocumentUrl = _wopiConfiguration.ClientDiscoveryDocumentUrl;

            Debug.Assert(clientDiscoveryDocumentUrl is not null);

            var httpClient = _httpClientFactory.CreateClient("wopi-discovery-document");

            using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, clientDiscoveryDocumentUrl);

            var xmlMediaTypes = new[] { "application/xml", "text/xml" };

            var accepts = xmlMediaTypes.Aggregate(string.Empty, (acc, n) => string.Concat(acc, n, ", "))[0..^ 2];

            httpRequestMessage.Headers.Add("Accept", accepts);

            try
            {
                using var response = await httpClient.SendAsync(httpRequestMessage, cancellationToken);

                if (!response.IsSuccessStatusCode)
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                var httpContent = response.Content;

                if (httpContent is null)
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                using var strm = await httpContent.ReadAsStreamAsync(cancellationToken);

                var xml = await XDocument.LoadAsync(strm, LoadOptions.None, cancellationToken);

                if (WopiDiscoveryDocument.IsXmlDocumentSupported(xml))
                {
                    return(new WopiDiscoveryDocument(clientDiscoveryDocumentUrl, xml, _logger));
                }
            }
            catch (HttpRequestException ex)
            {
                _logger?.LogError(ex, "Failed to connect to the WOPI Client to download the discovery document");
            }

            return(WopiDiscoveryDocument.Empty);
        }
        async Task <IWopiDiscoveryDocument> IWopiDiscoveryDocumentRepository.GetAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var ClientDiscoveryDocumentUrl = _wopiConfiguration.ClientDiscoveryDocumentUrl;

            if (string.IsNullOrWhiteSpace(ClientDiscoveryDocumentUrl))
            {
                return(WopiDiscoveryDocument.Empty);
            }
            if (!Uri.IsWellFormedUriString(ClientDiscoveryDocumentUrl, UriKind.Absolute))
            {
                return(WopiDiscoveryDocument.Empty);
            }

            var discoveryDocumentUrl = new Uri(ClientDiscoveryDocumentUrl, UriKind.Absolute);

            var httpClient = _httpClientFactory.CreateClient("wopi-discovery-document");

            using var request = new HttpRequestMessage(HttpMethod.Get, discoveryDocumentUrl);

            var xmlMediaTypes = new[] { "application/xml", "text/xml" };

            var accepts = xmlMediaTypes.Aggregate(string.Empty, (acc, n) => string.Concat(acc, n, ", "))[0..^ 2];

            request.Headers.Add("Accept", accepts);

            try
            {
                using var response = await httpClient.SendAsync(request, cancellationToken);

                if (!response.IsSuccessStatusCode)
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                var contentType = response.Content?.Headers.ContentType.MediaType.Trim();

                if (string.IsNullOrWhiteSpace(contentType))
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                if (!accepts.Contains(contentType, StringComparison.OrdinalIgnoreCase))
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                var httpContent = response.Content;

                if (httpContent is null)
                {
                    return(WopiDiscoveryDocument.Empty);
                }

                using var strm = await httpContent.ReadAsStreamAsync();

                var xml = await XDocument.LoadAsync(strm, LoadOptions.None, cancellationToken);

                if (WopiDiscoveryDocument.IsXmlDocumentSupported(xml))
                {
                    return(new WopiDiscoveryDocument(discoveryDocumentUrl, xml, _logger));
                }
            }
            catch (HttpRequestException ex)
            {
                _logger?.LogError(ex, "Failed to connect to the WOPI Client to download the discovery document");
            }

            return(WopiDiscoveryDocument.Empty);
        }