예제 #1
1
 public static OperationOutcome ForMessage(string message, OperationOutcome.IssueType code, OperationOutcome.IssueSeverity severity = IssueSeverity.Error)
 {
     return new OperationOutcome() {
               Issue = new List<OperationOutcome.IssueComponent>()
                     { new OperationOutcome.IssueComponent() 
                             { Severity = severity, Diagnostics = message } 
                     } };
 }
        private HttpResponseMessage InternalCreateHttpResponseMessage(HttpResponseException exception, HttpRequestMessage request)
        {
            if (exception == null)
                return null;

            OperationOutcome outcome =  new OperationOutcome().AddError(exception.Response.ReasonPhrase);
            return request.CreateResponse(exception.Response.StatusCode, outcome);
        }
        private HttpResponseMessage InternalCreateHttpResponseMessage(Exception exception, HttpRequestMessage request)
        {
            if (exception == null)
                return null;

            OperationOutcome outcome = new OperationOutcome().AddAllInnerErrors(exception);
            return request.CreateResponse(HttpStatusCode.InternalServerError, outcome);
        }
예제 #4
0
        public static OperationOutcome ForException(Exception e, OperationOutcome.IssueType type, OperationOutcome.IssueSeverity severity = IssueSeverity.Error)
        {
            var result = OperationOutcome.ForMessage(e.Message, type, severity);
            var ie = e.InnerException;

            while(ie != null)
            {
                result.Issue.Add(new IssueComponent { Diagnostics = ie.Message, Severity = IssueSeverity.Information });
                ie = ie.InnerException;
            }

            return result;
        }
예제 #5
0
        OperationOutcome CreateOutcome(Exception exception)
        {
            OperationOutcome outcome = new OperationOutcome().Init();
            Exception e = exception;
            do
            {
                outcome.Error(e);
                e = e.InnerException;
            }
            while (e != null);

            return outcome;
        }
        public void TestCardinality()
        {
            OperationOutcome oo = new OperationOutcome();
            validateErrorOrFail(oo,true);

            oo.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();
            validateErrorOrFail(oo,true);

            var issue = new OperationOutcome.OperationOutcomeIssueComponent();

            oo.Issue.Add(issue);
            validateErrorOrFail(oo,true);

            issue.Severity = OperationOutcome.IssueSeverity.Information;
            FhirValidator.Validate(oo, true);
        }
예제 #7
0
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var response =  await base.SendAsync(request, cancellationToken);

            if (!response.IsSuccessStatusCode)
            {
                ObjectContent content = response.Content as ObjectContent;
                if (content != null && content.ObjectType == typeof (HttpError))
                {
                    OperationOutcome outcome = new OperationOutcome().AddError(response.ReasonPhrase);
                    return request.CreateResponse(response.StatusCode, outcome);
                }
            }
            return response;
        }
예제 #8
0
        public override void OnException(HttpActionExecutedContext context)
        {
            HttpResponseMessage response;

            if (context.Exception is SparkException)
            {
                var e = (SparkException)context.Exception;
                var outcome = e.Outcome == null ? CreateOutcome(e) : e.Outcome;
                response = context.Request.CreateResponse(e.StatusCode, outcome);
            }
            else if (context.Exception is HttpResponseException)
            {
                var e = (HttpResponseException)context.Exception;
                var outcome = new OperationOutcome().AddError(e.Response.ReasonPhrase);
                response = context.Request.CreateResponse(e.Response.StatusCode, outcome);
            }
            else
            {
                response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, CreateOutcome(context.Exception));
            }

            throw new HttpResponseException(response);
        }
예제 #9
0
 public static OperationOutcome ForException(Exception e, OperationOutcome.IssueSeverity severity = IssueSeverity.Error)
 {
     return ForException(e, IssueType.Processing, severity);
 }
예제 #10
0
        /// <inheritdoc />
        async protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
#if CommonLogging
            if (log.IsDebugEnabled)
                log.Debug("Begin");
#endif

            // Decompress the request content, if needed.
            if (request.Content != null && request.Content.Headers.ContentEncoding.Count > 0)
            {
#if CommonLogging
                if (log.IsDebugEnabled)
                    log.Debug("Decompressing request");
#endif
                var encoding = request.Content.Headers.ContentEncoding.First();
                Func<HttpContent, HttpContent> decompressor;
                if (!decompressors.TryGetValue(encoding, out decompressor))
                {
                    var outcome = new OperationOutcome
                    {
                        Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>()
                        {
                            new OperationOutcome.OperationOutcomeIssueComponent
                            {
                                Details = string.Format("The Content-Encoding '{0}' is not supported.", encoding),
                                Severity = OperationOutcome.IssueSeverity.Error,
                            }
                        }
                    };
                    throw new HttpResponseException(request.CreateResponse(HttpStatusCode.BadRequest, outcome));
                }
                request.Content = decompressor(request.Content);
            }

            // Wait for the response.
            var response = await base.SendAsync(request, cancellationToken);

#if CommonLogging
            if (log.IsDebugEnabled)
                log.Debug("Got response");
#endif

            // Is the media type blacklisted; because compression does not help?
            if (response == null
                || response.Content == null
                || response.Content.Headers.ContentType == null
                || mediaTypeBlacklist.Any(s => response.Content.Headers.ContentType.MediaType.StartsWith(s)))
                return response;

            // If the client has requested compression and the compression algorithm is known, 
            // then compress the response.
            if (request.Headers.AcceptEncoding != null)
            {
                var compressor = request.Headers.AcceptEncoding
                    .Where(e => !e.Quality.HasValue || e.Quality != 0)
                    .Where(e => compressors.ContainsKey(e.Value))
                    .OrderByDescending(e => e.Quality ?? 1.0)
                    .Select(e => compressors[e.Value])
                    .FirstOrDefault();
                if (compressor != null)
                {
                    response.Content = compressor(response.Content);
                }
            }

#if CommonLogging
            if (log.IsDebugEnabled)
                log.Debug("Compressing response");
#endif
            return response;
        }
예제 #11
0
 public static OperationOutcome ForMessage(string message, OperationOutcome.IssueSeverity severity = IssueSeverity.Error)
 {
     return ForMessage(message, IssueType.Processing, severity);
 }
예제 #12
0
 public static FhirResponse WithError(HttpStatusCode code, string message, params object[] args)
 {
     OperationOutcome outcome = new OperationOutcome();
     outcome.AddError(string.Format(message, args));
     return new FhirResponse(code, outcome);
 }
예제 #13
0
        public void TestCardinality()
        {
            OperationOutcome oo = new OperationOutcome();
            validateErrorOrFail(oo,true);

            oo.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();
            validateErrorOrFail(oo,true);

            var issue = new OperationOutcome.OperationOutcomeIssueComponent();

            oo.Issue.Add(issue);
            validateErrorOrFail(oo,true);

            issue.Severity = OperationOutcome.IssueSeverity.Information;
            validateErrorOrFail(oo, true);

            issue.Code = OperationOutcome.IssueType.Forbidden;

            DotNetAttributeValidation.Validate(oo, true);
        }
 public FhirOperationException(string message, OperationOutcome outcome)
     : base(message)
 {
     Outcome = outcome;
 }
 public FhirOperationException(string message, OperationOutcome outcome, Exception inner)
     : base(message, inner)
 {
     Outcome = outcome;
 }
예제 #16
0
 public SparkException(HttpStatusCode statuscode, OperationOutcome outcome, string message = null)
     : this(statuscode, message)
 {
     this.Outcome = outcome;
 }
예제 #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FhirOperationException"/> class with a specified error message.
 /// </summary>
 /// <param name="message">The message that describes the error.</param>
 /// <param name="status">The http status code associated with the message</param>
 /// <param name="outcome">The outcome of the operation <see cref="OperationOutcome"/>.</param>
 public FhirOperationException(string message, HttpStatusCode status, OperationOutcome outcome)
     : base(message)
 {
     Outcome = outcome;
     Status = status;
 }
예제 #18
0
        public static OperationOutcome ValidateEntry(ResourceEntry entry)
        {
            OperationOutcome result = new OperationOutcome();
            result.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();

            ICollection<ValidationResult> vresults = new List<ValidationResult>();

            // Phase 1, validate against low-level rules built into the FHIR datatypes

            // todo: The API no longer seems to have the FhirValidator class.
            /*
            (!FhirValidator.TryValidate(entry.Resource, vresults, recurse: true))
            {
                foreach (var vresult in vresults)
                    result.Issue.Add(createValidationResult("[.NET validation] " + vresult.ErrorMessage, vresult.MemberNames));
            }
            */

            // Phase 2, validate against the XML schema
            var xml = FhirSerializer.SerializeResourceToXml(entry.Resource);
            var doc = XDocument.Parse(xml);
            doc.Validate(SchemaCollection.ValidationSchemaSet, (source, args) => result.Issue.Add( createValidationResult("[XSD validation] " + args.Message,null) ));

            // Phase 3, validate against a profile, if present
            var profileTags = entry.GetAssertedProfiles();
            if (profileTags.Count() == 0)
            {
                // If there's no profile specified, at least compare it to the "base" profile
                string baseProfile = CoreZipArtifactSource.CORE_SPEC_PROFILE_URI_PREFIX + entry.Resource.GetCollectionName();
                profileTags = new Uri[] { new Uri(baseProfile, UriKind.Absolute) };
            }

            var artifactSource = ArtifactResolver.CreateOffline();
            var specProvider = new SpecificationProvider(artifactSource);

            foreach (var profileTag in profileTags)
            {
                var specBuilder = new SpecificationBuilder(specProvider);
                specBuilder.Add(StructureFactory.PrimitiveTypes());
                specBuilder.Add(StructureFactory.MetaTypes());
                specBuilder.Add(StructureFactory.NonFhirNamespaces());
                specBuilder.Add(profileTag.ToString());
                specBuilder.Expand();

                string path = Directory.GetCurrentDirectory();

                var spec = specBuilder.ToSpecification();
                var nav = doc.CreateNavigator();
                nav.MoveToFirstChild();

                Report report = spec.Validate(nav);
                var errors = report.Errors;
                foreach (var error in errors)
                {
                    result.Issue.Add(createValidationResult("[Profile validator] " + error.Message, null));
                }
            }

            if(result.Issue.Count == 0)
                return null;
            else
                return result;
        }
예제 #19
0
        public void TestCardinality()
        {
            OperationOutcome oo = new OperationOutcome();
            validateErrorOrFail(oo,true);

            oo.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();
            validateErrorOrFail(oo,true);

            var issue = new OperationOutcome.OperationOutcomeIssueComponent();

            oo.Issue.Add(issue);
            validateErrorOrFail(oo,true);

            issue.Severity = OperationOutcome.IssueSeverity.Information;
            validateErrorOrFail(oo, true);

            issue.Code = new CodeableConcept("http://somesystem.org/some", "bla");
            DotNetAttributeValidation.Validate(oo, true);
        }
예제 #20
0
        public static OperationOutcome AgainstSchema(Resource resource)
        {
            OperationOutcome result = new OperationOutcome();
            result.Issue = new List<OperationOutcome.OperationOutcomeIssueComponent>();

            throw new NotImplementedException();

            //ICollection<ValidationResult> vresults = new List<ValidationResult>();

            // DSTU2: validation
            // Phase 2, validate against the XML schema

            //var xml = FhirSerializer.SerializeResourceToXml(resource);
            //var doc = XDocument.Parse(xml);
        }
예제 #21
0
        private static OperationOutcome AddIssue(this OperationOutcome outcome, OperationOutcome.IssueSeverity severity, string message)
        {
            if (outcome.Issue == null) outcome.Init();

            var item = new OperationOutcome.OperationOutcomeIssueComponent();
            item.Severity = severity;
            item.Details = message;
            outcome.Issue.Add(item);
            return outcome;
        }