public void WithBase() { var id = new ResourceIdentity("http://localhost/services/fhir/v012/Patient/3"); var id1 = id.WithBase("http://nu.nl/fhir"); Assert.AreEqual("http://nu.nl/fhir/Patient/3", id1.ToString()); var id2 = new ResourceIdentity("Patient/3").WithBase("http://nu.nl/fhir"); Assert.AreEqual("http://nu.nl/fhir/Patient/3", id2.ToString()); var id3 = id2.MakeRelative(); Assert.AreEqual("Patient/3", id3.ToString()); }
public HttpResponseMessage Put(string ResourceName, string id, [FromBody] Resource bodyResource) { System.Diagnostics.Trace.WriteLine("PUT: " + this.ControllerContext.Request.RequestUri.OriginalString); var buri = this.CalculateBaseURI("{ResourceName}"); if (!Id.IsValidValue(id)) { throw new FhirServerException(HttpStatusCode.BadRequest, "ID [" + id + "] is not a valid FHIR Resource ID"); } var Inputs = GetInputs(buri); bodyResource.Id = id; if (ResourceName == "AuditEvent") { // depends on the AuditEvent, if was created by the server, then it will get an unauthorized exception report // otherwise externally reported events can be updated! //throw new FhirServerException(HttpStatusCode.MethodNotAllowed, "Cannot PUT a AuditEvent, you must POST them"); } IFhirResourceServiceSTU3 model = GetResourceModel(ResourceName, GetInputs(buri)); var result = model.Create(bodyResource, null, null, null); this.ControllerContext.Request.SaveEntry(bodyResource); result.ResourceBase = new Uri(buri); var actualResource = result; ResourceIdentity ri = null; if (result is Bundle) { ri = result.ResourceIdentity(result.ResourceBase); } else if (!(result is OperationOutcome) && !string.IsNullOrEmpty(result.Id)) { ri = result.ResourceIdentity(result.ResourceBase); } // Check the prefer header if (Request.Headers.Contains("Prefer")) { string preferHeader = Request.Headers.GetValues("Prefer").FirstOrDefault(); if (preferHeader != null && preferHeader.ToLower() == "return=operationoutcome") { if (!(result is OperationOutcome)) { OperationOutcome so = new OperationOutcome() { Text = Utility.CreateNarative("Resource update") }; so.Text.Status = Narrative.NarrativeStatus.Generated; so.Issue = new List <OperationOutcome.IssueComponent> { new OperationOutcome.IssueComponent() { Severity = OperationOutcome.IssueSeverity.Information, Code = OperationOutcome.IssueType.Informational, Details = new CodeableConcept(null, null, "Update was completed") } }; result = so; } } } HttpResponseMessage returnMessage; if (bodyResource.Annotation <CreateOrUpate>() == CreateOrUpate.Create) { returnMessage = Request.CreateResponse(HttpStatusCode.Created, result); } else { returnMessage = Request.CreateResponse(HttpStatusCode.OK, result); } // Put in the "Location" header if (ri != null) { returnMessage.Headers.Add("Location", ri.OriginalString); Request.Properties.Add(Const.ResourceIdentityKey, ri.MakeRelative().OriginalString); } if (actualResource.Meta != null && !string.IsNullOrEmpty(actualResource.Meta.VersionId)) { returnMessage.Headers.Add("ETag", String.Format("W/\"{0}\"", actualResource.Meta.VersionId)); } if (actualResource.Meta != null && actualResource.Meta.LastUpdated.HasValue) { // returnMessage.Headers.CacheControl. returnMessage.Content.Headers.LastModified = actualResource.Meta.LastUpdated.Value; } // Check the prefer header if (Request.Headers.Contains("Prefer")) { string preferHeader = Request.Headers.GetValues("Prefer").FirstOrDefault(); if (preferHeader != null && preferHeader.ToLower() == "return=minimal") { returnMessage.Content = null; } } return(returnMessage); }
public HttpResponseMessage Post(string ResourceName, [FromBody] Resource bodyResource) { System.Diagnostics.Trace.WriteLine("POST: " + this.ControllerContext.Request.RequestUri.OriginalString); var buri = this.CalculateBaseURI("{ResourceName}"); if (bodyResource == null) { var oo = new OperationOutcome() { Text = Utility.CreateNarative("Validation Error"), // Contained = new List<Resource> { bodyResource } // Can't attach a null resource! }; oo.Issue = new List <OperationOutcome.IssueComponent> { new OperationOutcome.IssueComponent() { Details = new CodeableConcept(null, null, "Missing " + ResourceName + " resource POST"), Severity = OperationOutcome.IssueSeverity.Fatal } }; // return oo; return(Request.CreateResponse(HttpStatusCode.BadRequest, oo)); } if (!String.IsNullOrEmpty(bodyResource.Id)) { var oo = new OperationOutcome() { Text = Utility.CreateNarative("Validation Error"), Contained = new List <Resource> { bodyResource } }; oo.Issue = new List <OperationOutcome.IssueComponent> { new OperationOutcome.IssueComponent() { Details = new CodeableConcept(null, null, "ID must be empty for POST"), Severity = OperationOutcome.IssueSeverity.Fatal } }; // return oo; return(Request.CreateResponse(HttpStatusCode.BadRequest, oo)); } var Inputs = GetInputs(buri); IFhirResourceServiceSTU3 model = GetResourceModel(ResourceName, GetInputs(buri)); var result = model.Create(bodyResource, null, null, null); this.ControllerContext.Request.SaveEntry(bodyResource); result.ResourceBase = new Uri(buri); var actualResource = result; ResourceIdentity ri = null; if (result is Bundle) { ri = result.ResourceIdentity(result.ResourceBase); } else if (!(result is OperationOutcome) && !string.IsNullOrEmpty(result.Id)) { ri = result.ResourceIdentity(result.ResourceBase); } // Check the prefer header if (Request.Headers.Contains("Prefer")) { string preferHeader = Request.Headers.GetValues("Prefer").FirstOrDefault(); if (preferHeader != null && preferHeader.ToLower() == "return=operationoutcome") { if (!(result is OperationOutcome)) { OperationOutcome so = new OperationOutcome() { Text = Utility.CreateNarative("Resource update") }; so.Text.Status = Narrative.NarrativeStatus.Generated; so.Issue = new List <OperationOutcome.IssueComponent> { new OperationOutcome.IssueComponent() { Severity = OperationOutcome.IssueSeverity.Information, Code = OperationOutcome.IssueType.Informational, Details = new CodeableConcept(null, null, "Update was completed") } }; result = so; } } } HttpResponseMessage returnMessage; if (bodyResource.Annotation <CreateOrUpate>() == CreateOrUpate.Create) { returnMessage = Request.CreateResponse(HttpStatusCode.Created, result); } else { returnMessage = Request.CreateResponse(HttpStatusCode.OK, result); } // Put in the "Location" header if (ri != null) { returnMessage.Headers.Add("Location", ri.OriginalString); Request.Properties.Add(Const.ResourceIdentityKey, ri.MakeRelative().OriginalString); } if (actualResource.Meta != null && !string.IsNullOrEmpty(actualResource.Meta.VersionId)) { returnMessage.Headers.Add("ETag", String.Format("W/\"{0}\"", actualResource.Meta.VersionId)); } if (actualResource.Meta != null && actualResource.Meta.LastUpdated.HasValue) { returnMessage.Content.Headers.LastModified = actualResource.Meta.LastUpdated.Value; // returnMessage.Headers.Add("Last-Modified", actualResource.Meta.LastUpdated.Value.ToString("r")); } // Check the prefer header if (Request.Headers.Contains("Prefer")) { string preferHeader = Request.Headers.GetValues("Prefer").FirstOrDefault(); if (preferHeader != null && preferHeader.ToLower() == "return=minimal") { returnMessage.Content = null; } } return(returnMessage); }