Example #1
0
        /// <summary>
        /// Send a Bundle to a path on the server
        /// </summary>
        /// <param name="bundle">The contents of the Bundle to be sent</param>
        /// <param name="path">A path on the server to send the Bundle to</param>
        /// <returns>True if the bundle was successfully delivered, false otherwise</returns>
        /// <remarks>This method differs from Batch, in that it can be used to deliver a Bundle
        /// at the endpoint for messages, documents or binaries, instead of the batched update
        /// REST endpoint.</remarks>
        public bool DeliverBundle(Bundle bundle, string path)
        {
            if (Endpoint == null)
            {
                throw new InvalidOperationException("Endpoint must be provided using either the Endpoint property or the FhirClient constructor");
            }

            byte[] data;
            string contentType = ContentType.BuildContentType(PreferredFormat, false);

            if (PreferredFormat == ContentType.ResourceFormat.Json)
            {
                data = FhirSerializer.SerializeBundleToJsonBytes(bundle);
            }
            else if (PreferredFormat == ContentType.ResourceFormat.Xml)
            {
                data = FhirSerializer.SerializeBundleToXmlBytes(bundle);
            }
            else
            {
                throw new ArgumentException("Cannot encode a batch into format " + PreferredFormat.ToString());
            }

            var req = createRequest(Endpoint, true);

            req.Method      = "POST";
            req.ContentType = contentType;
            prepareRequest(req, data);

            return(doRequest(req, HttpStatusCode.OK, () => true));
        }
Example #2
0
        public void TestSigning()
        {
            Bundle b = new Bundle();

            b.Title       = "Updates to resource 233";
            b.Id          = new Uri("urn:uuid:0d0dcca9-23b9-4149-8619-65002224c3");
            b.LastUpdated = new DateTimeOffset(2012, 11, 2, 14, 17, 21, TimeSpan.Zero);
            b.AuthorName  = "Ewout Kramer";

            ResourceEntry <Patient> p = new ResourceEntry <Patient>();

            p.Id            = new ResourceIdentity("http://test.com/fhir/Patient/233");
            p.Resource      = new Patient();
            p.Resource.Name = new List <HumanName> {
                HumanName.ForFamily("Kramer").WithGiven("Ewout")
            };
            b.Entries.Add(p);

            var certificate = getCertificate();

            var bundleData   = FhirSerializer.SerializeBundleToXmlBytes(b);
            var bundleXml    = Encoding.UTF8.GetString(bundleData);
            var bundleSigned = XmlSignatureHelper.Sign(bundleXml, certificate);

            _signedXml = bundleSigned;

            using (var response = postBundle(bundleSigned))
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    TestResult.Fail("Server refused POSTing signed document at /");
                }
            }
        }
Example #3
0
        private HttpWebRequest prepareRequest(string method, Uri uri, object data, IEnumerable <Tag> tags, bool expectBundleResponse)
        {
            byte[] body = null;

            Uri api = uri;

            if (UseFormatParam)
            {
                api = api.AddParam(HttpUtil.RESTPARAM_FORMAT, ContentType.BuildFormatParam(PreferredFormat));
            }

            var req = initializeRequest(api, method);

            if (!UseFormatParam)
            {
                req.Accept = ContentType.BuildContentType(PreferredFormat, forBundle: expectBundleResponse);
            }

            if (data is Binary)
            {
                var bin = (Binary)data;
                body            = bin.Content;
                req.ContentType = bin.ContentType;
            }
            else if (data is Resource)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeResourceToXmlBytes((Resource)data) :
                       FhirSerializer.SerializeResourceToJsonBytes((Resource)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, false);
            }
            else if (data is Bundle)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeBundleToXmlBytes((Bundle)data) :
                       FhirSerializer.SerializeBundleToJsonBytes((Bundle)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, true);
            }
            else if (data is TagList)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeTagListToXmlBytes((TagList)data) :
                       FhirSerializer.SerializeTagListToJsonBytes((TagList)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, false);
            }

            if (tags != null)
            {
                req.Headers[HttpUtil.CATEGORY] = HttpUtil.BuildCategoryHeader(tags);
            }

            if (body != null)
            {
                writeBody(req, body);
            }
            return(req);
        }
Example #4
0
        public void SetBody(Bundle bundle, ResourceFormat format)
        {
            if (bundle == null)
            {
                throw Error.ArgumentNull("bundle");
            }

            _body = format == ResourceFormat.Xml ?
                    FhirSerializer.SerializeBundleToXmlBytes(bundle, summary: false) :
                    FhirSerializer.SerializeBundleToJsonBytes(bundle, summary: false);

            _contentType = ContentType.BuildContentType(format, forBundle: true);
        }
Example #5
0
        public void AvoidBOMUse()
        {
            Bundle b = new Bundle();

            var data = FhirSerializer.SerializeBundleToJsonBytes(b);

            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            data = FhirSerializer.SerializeBundleToXmlBytes(b);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            Patient p = new Patient();

            data = FhirSerializer.SerializeResourceToJsonBytes(p);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            data = FhirSerializer.SerializeResourceToXmlBytes(p);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);
        }
Example #6
0
        public void TestSigning()
        {
            Bundle b = new Bundle();

            b.Title       = "Updates to resource 233";
            b.Id          = new Uri("urn:uuid:0d0dcca9-23b9-4149-8619-65002224c3");
            b.LastUpdated = new DateTimeOffset(2012, 11, 2, 14, 17, 21, TimeSpan.Zero);
            b.AuthorName  = "Ewout Kramer";

            ResourceEntry <Patient> p = new ResourceEntry <Patient>();

            p.Id            = new ResourceIdentity("http://test.com/fhir/Patient/233");
            p.Resource      = new Patient();
            p.Resource.Name = new List <HumanName> {
                HumanName.ForFamily("Kramer").WithGiven("Ewout")
            };
            b.Entries.Add(p);

            var myAssembly = typeof(TestXmlSignature).Assembly;
            var stream     = myAssembly.GetManifestResourceStream("Spark.Tests.spark.pfx");

            var data = new byte[stream.Length];

            stream.Read(data, 0, (int)stream.Length);
            var certificate = new X509Certificate2(data);

            var bundleData = FhirSerializer.SerializeBundleToXmlBytes(b);
            var bundleXml  = Encoding.UTF8.GetString(bundleData);

            var bundleSigned = XmlSignatureHelper.Sign(bundleXml, certificate);

            Assert.IsTrue(XmlSignatureHelper.IsSigned(bundleSigned));
            Assert.IsTrue(XmlSignatureHelper.VerifySignature(bundleSigned));

            var changedBundle = bundleSigned.Replace("<name>Ewout", "<name>Ewald");

            Assert.AreEqual(bundleSigned.Length, changedBundle.Length);

            Assert.IsFalse(XmlSignatureHelper.VerifySignature(changedBundle));
        }
Example #7
0
        public void SerializationDoesNotEmitXmlHeader()
        {
            Bundle b      = createTestBundle();
            var    actual = FhirSerializer.SerializeBundleToXml(b);

            Assert.IsFalse(actual.StartsWith("<?xml"));

            var data = FhirSerializer.SerializeBundleToXmlBytes(b);

            actual = System.Text.Encoding.UTF8.GetString(data);
            Assert.IsFalse(actual.StartsWith("<?xml"));


            Patient p = new Patient();

            actual = FhirSerializer.SerializeResourceToXml(p);
            Assert.IsFalse(actual.StartsWith("<?xml"));

            data   = FhirSerializer.SerializeResourceToXmlBytes(p);
            actual = System.Text.Encoding.UTF8.GetString(data);
            Assert.IsFalse(actual.StartsWith("<?xml"));
        }