private string GetConvertDataResult(ConvertDataRequest convertRequest, ITemplateProvider templateProvider, CancellationToken cancellationToken)
        {
            var converter = _converterMap.GetValueOrDefault(convertRequest.InputDataType);

            if (converter == null)
            {
                // This case should never happen.
                _logger.LogInformation("Invalid input data type for conversion.");
                throw new RequestNotValidException("Invalid input data type for conversion.");
            }

            try
            {
                return(converter.Convert(convertRequest.InputData, convertRequest.RootTemplate, templateProvider, cancellationToken));
            }
            catch (FhirConverterException convertException)
            {
                if (convertException.FhirConverterErrorCode == FhirConverterErrorCode.TimeoutError)
                {
                    _logger.LogError(convertException.InnerException, "Convert data operation timed out.");
                    throw new ConvertDataTimeoutException(Resources.ConvertDataOperationTimeout, convertException.InnerException);
                }

                _logger.LogInformation(convertException, "Convert data failed.");
                throw new ConvertDataFailedException(string.Format(Resources.ConvertDataFailed, convertException.Message), convertException);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unhandled exception: convert data process failed.");
                throw new ConvertDataUnhandledException(string.Format(Resources.ConvertDataFailed, ex.Message), ex);
            }
        }
Exemple #2
0
        public async Task <ConvertDataResponse> Process(ConvertDataRequest convertRequest, CancellationToken cancellationToken)
        {
            var templateCollection = await _convertDataTemplateProvider.GetTemplateCollectionAsync(convertRequest, cancellationToken);

            var result = GetConvertDataResult(convertRequest, new Hl7v2TemplateProvider(templateCollection), cancellationToken);

            return(new ConvertDataResponse(result));
        }
Exemple #3
0
        public async Task <ConvertDataResponse> Process(ConvertDataRequest convertRequest, CancellationToken cancellationToken)
        {
            var templateCollection = await _convertDataTemplateProvider.GetTemplateCollectionAsync(convertRequest, cancellationToken);

            ITemplateProvider templateProvider = GetTemplateProvider(convertRequest.InputDataType, templateCollection);

            if (templateProvider == null)
            {
                // This case should never happen.
                _logger.LogError("Invalid input data type for conversion.");
                throw new RequestNotValidException("Invalid input data type for conversion.");
            }

            var result = GetConvertDataResult(convertRequest, templateProvider, cancellationToken);

            return(new ConvertDataResponse(result));
        }
        public async Task <IActionResult> ConvertData([FromBody] Parameters inputParams)
        {
            CheckIfConvertDataIsEnabled();

            ValidateParams(inputParams);

            string inputData = ReadStringParameter(inputParams, ConvertDataProperties.InputData);
            string templateCollectionReference = ReadStringParameter(inputParams, ConvertDataProperties.TemplateCollectionReference);
            string rootTemplate = ReadStringParameter(inputParams, ConvertDataProperties.RootTemplate);

            Liquid.Converter.Models.DataType inputDataType = ReadEnumParameter <Liquid.Converter.Models.DataType>(inputParams, ConvertDataProperties.InputDataType);

            // Validate template reference format.
            if (!ImageInfo.IsValidImageReference(templateCollectionReference))
            {
                _logger.LogInformation("Templates collection reference format is invalid.");
                throw new RequestNotValidException(string.Format(Resources.InvalidTemplateCollectionReference, templateCollectionReference));
            }

            // Validate if template has been configured.
            bool   isDefaultTemplateReference = ImageInfo.IsDefaultTemplateImageReference(templateCollectionReference);
            string registryServer             = ExtractRegistryServer(templateCollectionReference);

            if (isDefaultTemplateReference)
            {
                CheckInputDataTypeAndDefaultTemplateImageReferenceConsistent(inputDataType, templateCollectionReference);
            }
            else
            {
                CheckIfCustomTemplateIsConfigured(registryServer, templateCollectionReference);
            }

            var convertDataRequest       = new ConvertDataRequest(inputData, inputDataType, registryServer, isDefaultTemplateReference, templateCollectionReference, rootTemplate);
            ConvertDataResponse response = await _mediator.Send(convertDataRequest, cancellationToken : default);

            return(new ContentResult
            {
                Content = response.Resource,
                ContentType = "text/plain",
            });
        }
        /// <summary>
        /// Fetch template collection from container registry or built-in archive.
        /// </summary>
        /// <param name="request">The convert data request which contains template reference.</param>
        /// <param name="cancellationToken">Cancellation token to cancel the fetch operation.</param>
        /// <returns>Template collection.</returns>
        public async Task <List <Dictionary <string, Template> > > GetTemplateCollectionAsync(ConvertDataRequest request, CancellationToken cancellationToken)
        {
            // We have embedded a default template collection in the templatemanagement package.
            // If the template collection is the default reference, we don't need to retrieve token.
            var accessToken = string.Empty;

            if (!request.IsDefaultTemplateReference)
            {
                _logger.LogInformation("Using a custom template collection for data conversion.");

                async Task <string> TokenEntryFactory(ICacheEntry entry)
                {
                    var token = await _containerRegistryTokenProvider.GetTokenAsync(request.RegistryServer, cancellationToken);

                    entry.Size = token.Length;
                    entry.AbsoluteExpiration = GetTokenAbsoluteExpiration(token);
                    return(token);
                }

                accessToken = await _cache.GetOrCreateAsync(GetCacheKey(request.RegistryServer), TokenEntryFactory);
            }
            else
            {
                _logger.LogInformation("Using the default template collection for data conversion.");
            }

            try
            {
                var provider = _templateCollectionProviderFactory.CreateTemplateCollectionProvider(request.TemplateCollectionReference, accessToken);
                return(await provider.GetTemplateCollectionAsync(cancellationToken));
            }
            catch (ContainerRegistryAuthenticationException authEx)
            {
                // Remove token from cache when authentication failed.
                _cache.Remove(GetCacheKey(request.RegistryServer));

                _logger.LogError(authEx, "Failed to access container registry.");
                throw new ContainerRegistryNotAuthorizedException(string.Format(Resources.ContainerRegistryNotAuthorized, request.RegistryServer), authEx);
            }
            catch (ImageFetchException fetchEx)
            {
                _logger.LogError(fetchEx, "Failed to fetch template image.");
                throw new FetchTemplateCollectionFailedException(string.Format(Resources.FetchTemplateCollectionFailed, fetchEx.Message), fetchEx);
            }
            catch (TemplateManagementException templateEx)
            {
                _logger.LogError(templateEx, "Template collection is invalid.");
                throw new TemplateCollectionErrorException(string.Format(Resources.FetchTemplateCollectionFailed, templateEx.Message), templateEx);
            }
            catch (Exception unhandledEx)
            {
                _logger.LogError(unhandledEx, "Unhandled exception: failed to get template collection.");
                throw new FetchTemplateCollectionFailedException(string.Format(Resources.FetchTemplateCollectionFailed, unhandledEx.Message), unhandledEx);
            }
        }