/// <summary>
        /// Returns the document stream for the given file name in specified library.
        /// </summary>
        /// <param name="filename">The document to find and stream.</param>
        /// <param name="libraryName">The folder name of the document location.</param>
        /// <returns> A stream containing the contents of the document. </returns>
        public async Task <byte[]> GetDocument(string filename, string libraryName)
        {
            _logger.LogInformation($"[{nameof(GetDocument)}] - Attempting to connect to SharePoint location.");

            string fileRelativeUrl = $"{_spConfig.RelativeSiteURL}/{libraryName}/{filename}";

            try
            {
                var file = _clientContext.Web.GetFileByServerRelativeUrl(fileRelativeUrl);
                _clientContext.Load(file);
                if (file is null)
                {
                    _logger.LogError($"[{nameof(GetDocument)}] - File not found: {fileRelativeUrl}");
                    return(HandleFileNotFoundExceptionWithTestPdf(new DocumentNotFoundException($"[{nameof(GetDocument)}] - File not found: {fileRelativeUrl}")));
                }

                ClientResult <Stream> stream = file.OpenBinaryStream();
                await _clientContext.ExecuteQueryAsync();

                if (stream.Value is null)
                {
                    _logger.LogError($"[{nameof(GetDocument)}] - File not stream from location: {fileRelativeUrl}");
                    return(HandleFileNotFoundExceptionWithTestPdf(new DocumentNotFoundException($"[{nameof(GetDocument)}] - File not found: {fileRelativeUrl}")));
                }

                _logger.LogInformation($"[{nameof(GetDocument)}] - File stream location: {fileRelativeUrl} completed.");
                return(stream.Value.ToByteArray());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"[{nameof(GetDocument)}] - The contract pdf file is not accessible. File: {fileRelativeUrl}");
                return(HandleFileNotFoundExceptionWithTestPdf(new DocumentNotAccessibleException("The contract pdf file is not accessible.", ex)));
            }
        }
Пример #2
0
        /// <inheritdoc/>
        public async Task <string> AcquireSPTokenAsync()
        {
            Uri site = new Uri($"{_spConfig.ApiBaseAddress}{_spConfig.RelativeSiteURL}");
            var cleanAccessRequestContent = "grant_type=client_credentials" +
                                            $"&resource={_spConfig.Resource}/{site.DnsSafeHost}@{_spConfig.TenantId}" +
                                            $"&client_id={_spConfig.ClientId}@{_spConfig.TenantId}";

            var body = cleanAccessRequestContent +
                       $"&client_secret={HttpUtility.UrlEncode(_spConfig.ClientSecret)}";

            using (var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"))
            {
                var sharepointAadEndpoint = $"{_spConfig.Authority}{_spConfig.TenantId}/tokens/OAuth/2";
                _logger.LogInformation($"[{nameof(AcquireSPTokenAsync)}] - Attempting to get token from the SharePoint account access control [{sharepointAadEndpoint}] with access request [{cleanAccessRequestContent}]. For site: {site.AbsoluteUri}");

                var result = await _httpClient.PostAsync(sharepointAadEndpoint, stringContent);

                var resultContent = await result.Content.ReadAsStringAsync();

                if (!result.IsSuccessStatusCode)
                {
                    _logger.LogError($"[{nameof(AcquireSPTokenAsync)}] - Failed to get access token with status code [{result.StatusCode}] and reponse message [{resultContent}]");
                    throw new SPTokenAcquisitionFailureException($"Access token cannot be acquired for the SharePoint AAD Auth. Failed with error: [{resultContent}]");
                }

                var    tokenResult = JsonSerializer.Deserialize <JsonElement>(resultContent);
                string token       = string.Empty;

                try
                {
                    token = tokenResult.GetProperty("access_token").GetString();
                }
                catch (KeyNotFoundException ex)
                {
                    _logger.LogError(ex, $"[{nameof(AcquireSPTokenAsync)}] - Access token cannot be acquired for the SharePoint AAD Auth. Site: {site.AbsoluteUri}");
                    throw new SPTokenAcquisitionFailureException("Access token cannot be acquired for the SharePoint AAD Auth.", ex);
                }

                if (string.IsNullOrEmpty(token))
                {
                    throw new SPTokenAcquisitionFailureException($"Access token cannot be acquired from token result [{resultContent}] for the SharePoint AAD Auth.");
                }

                _logger.LogInformation($"[{nameof(AcquireSPTokenAsync)}] - Successfully acquired the SharePoint token from the SharePoint account access control. Site: {site.AbsoluteUri}");
                return(token);
            }
        }
Пример #3
0
        private async Task SaveFailedState(IMessageSession session, Message message, SessionWorkflowState state, Exception ex, string reason)
        {
            state.FailedMessageId     = message.MessageId;
            state.IsFaulted           = true;
            state.FailedMessageReason = $"{reason} with error [{ex.Message}]";
            await _stateManager.SetWorkflowStateAsync(session, state);

            var logMessage = $"[{nameof(ContractEventSessionManager)}.{nameof(ProcessSessionMessageAsync)}] - {reason} saving workflow state as faulted with failed message : {message.MessageId} for session: {session.SessionId}.";

            if (ex is ContractEventExpectationFailedException)
            {
                _logger.LogError(ex, logMessage);
            }
            else
            {
                _logger.LogWarning(logMessage);
            }
        }