private Bundle.BundleEntryComponent newEntry(Bundle.HTTPVerb method)
        {
            var newEntry = new Bundle.BundleEntryComponent();

            newEntry.Request        = new Bundle.BundleEntryRequestComponent();
            newEntry.Request.Method = method;

            return(newEntry);
        }
Exemple #2
0
 private static void ConnectResource(Interaction interaction, Bundle.BundleEntryComponent entry)
 {
     if (interaction.HasResource())
     {
         entry.Resource = interaction.Resource;
         interaction.Key.ApplyTo(entry.Resource);
         entry.FullUrl = interaction.Key.ToUriString();
     }
 }
        public static Bundle.BundleEntryComponent TranslateToSparseEntry(this Interaction interaction)
        {
            var entry = new Bundle.BundleEntryComponent();

            if (interaction.HasResource())
            {
                entry.Resource = interaction.Resource;
                interaction.Key.ApplyTo(entry.Resource);
            }
            return(entry);
        }
        public static void Append(this Bundle bundle, Bundle.HTTPVerb method, Resource resource)
        {
            Bundle.BundleEntryComponent entry = CreateEntryForResource(resource);

            if (entry.Request == null)
            {
                entry.Request = new Bundle.BundleEntryRequestComponent();
            }
            entry.Request.Method = method;
            bundle.Entry.Add(entry);
        }
Exemple #5
0
        public static void Append(this Bundle bundle, Bundle.HTTPVerb method, Resource resource)
        {
            var entry = new Bundle.BundleEntryComponent();

            entry.Resource = resource;
            entry.Base     = bundle.Base;
            if (entry.Transaction == null)
            {
                entry.Transaction = new Bundle.BundleEntryTransactionComponent();
            }
            entry.Transaction.Method = method;
            bundle.Entry.Add(entry);
        }
 public static Key ExtractKey(this ILocalhost localhost, Bundle.BundleEntryComponent entry)
 {
     if (entry.Transaction != null && entry.Transaction.Url != null)
     {
         return(localhost.UriToKey(entry.Transaction.Url));
     }
     else if (entry.Resource != null)
     {
         return(entry.Resource.ExtractKey());
     }
     else
     {
         return(null);
     }
 }
        public static Interaction ToInteraction(this ILocalhost localhost, Bundle.BundleEntryComponent bundleEntry)
        {
            Key key = localhost.ExtractKey(bundleEntry);

            Bundle.HTTPVerb method = localhost.ExtrapolateMethod(bundleEntry, key);

            if (key != null)
            {
                return(Interaction.Create(method, key, bundleEntry.Resource));
            }
            else
            {
                return(Interaction.Create(method, bundleEntry.Resource));
            }
        }
Exemple #8
0
        public static Bundle.BundleEntryComponent ToTransactionEntry(this Interaction interaction)
        {
            var entry = new Bundle.BundleEntryComponent();

            if (entry.Request == null)
            {
                entry.Request = new Bundle.BundleEntryRequestComponent();
            }
            entry.Request.Method = interaction.Method;
            entry.Request.Url    = interaction.Key.ToUri().ToString();

            ConnectResource(interaction, entry);

            return(entry);
        }
        internal static Bundle.BundleEntryComponent ToBundleEntry(this HttpWebResponse response)
        {
            var result = new Bundle.BundleEntryComponent();

            result.TransactionResponse        = new Bundle.BundleEntryTransactionResponseComponent();
            result.TransactionResponse.Status = response.StatusCode.ToString();
            result.TransactionResponse.SetHeaders(response.Headers);

            var contentType  = getContentType(response);
            var charEncoding = getCharacterEncoding(response);

            result.TransactionResponse.Location = response.Headers[HttpUtil.LOCATION] ?? response.Headers[HttpUtil.CONTENTLOCATION];

#if (PORTABLE45 || NETCOREAPP1_1)
            if (!String.IsNullOrEmpty(response.Headers[HttpUtil.LASTMODIFIED]))
            {
                result.TransactionResponse.LastModified = DateTimeOffset.Parse(response.Headers[HttpUtil.LASTMODIFIED]);
            }
#else
            result.TransactionResponse.LastModified = response.LastModified;
#endif
            result.TransactionResponse.Etag = getETag(response);

            var body = readBody(response);

            if (body != null)
            {
                result.TransactionResponse.SetBody(body);

                if (IsBinaryResponse(response.ResponseUri.OriginalString))
                {
                    result.Resource = makeBinaryResource(body, contentType);
                }
                else
                {
                    var bodyText = decodeBody(body, charEncoding);
                    var resource = parseResource(bodyText, contentType);

                    result.Resource = resource;
                    if (result.TransactionResponse.Location != null)
                    {
                        result.Resource.ResourceBase = new ResourceIdentity(result.TransactionResponse.Location).BaseUri;
                    }
                }
            }

            return(result);
        }
Exemple #10
0
        public Bundle.BundleEntryComponent Execute(Bundle.BundleEntryComponent interaction)
        {
            if (interaction == null)
            {
                throw Error.ArgumentNull("interaction");
            }

            LastResult = doRequest(interaction);
            var            status = LastResult.Response.Status;
            HttpStatusCode statusCode;

            if (!Enum.TryParse <HttpStatusCode>(status, out statusCode))
            {
                // If the status code is unable to be parsed, then report
                // in internal server error
                statusCode = HttpStatusCode.InternalServerError;
            }

            if (status.StartsWith("2"))      // 2xx codes - success
            {
                return(LastResult);
            }
            else if (status.StartsWith("3") || status.StartsWith("1"))      // 3xx codes - we don't handle them, unless the .NET API did it for us
            {
                throw Error.NotSupported("Server returned a status code '{0}', which is not supported by the FhirClient".FormatWith(status));
            }
            else if (status.StartsWith("4") || status.StartsWith("5"))      // 4xx/5xx codes - client or server error.
            {
                var message = String.Format("Operation was unsuccessful, and returned status {0}.", status);

                var outcome = LastResult.Resource as OperationOutcome;
                if (outcome != null)
                {
                    throw new FhirOperationException(message + " OperationOutcome: " + outcome.ToString(), statusCode, outcome);
                }
                else
                {
                    throw new FhirOperationException(message, statusCode);
                }
            }
            else
            {
                throw Error.NotSupported("Server returned an illegal http status code '{0}', which is not defined by the Http standard".FormatWith(status));
            }
        }
        public static Bundle.BundleEntryComponent ToTransactionEntry(this Interaction interaction)
        {
            var entry = new Bundle.BundleEntryComponent();

            if (entry.Transaction == null)
            {
                entry.Transaction = new Bundle.BundleEntryTransactionComponent();
            }
            entry.Transaction.Method = interaction.Method;
            entry.Transaction.Url    = interaction.Key.ToUri().ToString();

            if (interaction.HasResource())
            {
                entry.Resource = interaction.Resource;
                interaction.Key.ApplyTo(entry.Resource);
            }

            return(entry);
        }
Exemple #12
0
        private Bundle.BundleEntryComponent doRequest(Bundle.BundleEntryComponent interaction)
        {
            byte[] outBody;
            var    request = interaction.ToHttpRequest(Prefer, PreferredFormat, UseFormatParameter, out outBody);

#if !PORTABLE45
            request.Timeout = Timeout;
#endif

            LastRequest = request;
            if (BeforeRequest != null)
            {
                BeforeRequest(request, outBody);
            }

            // Make sure the HttpResponse gets disposed!
            // using (HttpWebResponse webResponse = (HttpWebResponse)await request.GetResponseAsync(new TimeSpan(0, 0, 0, 0, Timeout)))
            using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponseNoEx())
            {
                try
                {
                    //Read body before we call the hook, so the hook cannot read the body before we do
                    var inBody = readBody(webResponse);

                    LastResponse = webResponse;
                    if (AfterResponse != null)
                    {
                        AfterResponse(webResponse, inBody);
                    }

                    // Do this call after AfterResponse, so this will be called, even if exceptions are thrown by ToBundleEntry()
                    return(webResponse.ToBundleEntry(inBody));
                }
                catch (AggregateException ae)
                {
                    //EK: This code looks weird. Is this correct?
                    if (ae.GetBaseException() is WebException)
                    {
                    }
                    throw ae.GetBaseException();
                }
            }
        }
Exemple #13
0
        private Bundle.BundleEntryComponent doRequest(Bundle.BundleEntryComponent interaction)
        {
            var request = interaction.ToHttpRequest(Prefer, PreferredFormat, UseFormatParameter);

#if (!PORTABLE45 && !NETCOREAPP1_1)
            request.Timeout = Timeout;
#endif

            if (BeforeRequest != null)
            {
                BeforeRequest(request);
            }

            // Make sure the HttpResponse gets disposed!
            // using (HttpWebResponse webResponse = (HttpWebResponse)await request.GetResponseAsync(new TimeSpan(0, 0, 0, 0, Timeout)))
            using (HttpWebResponse webResponse = (HttpWebResponse)request.GetResponseNoEx())
            {
                try
                {
                    var response = webResponse.ToBundleEntry();
                    if (AfterResponse != null)
                    {
                        AfterResponse(webResponse, response.TransactionResponse, response.Resource);
                    }

                    return(response);
                }
                catch (AggregateException ae)
                {
                    //EK: This code looks weird. Is this correct?
                    if (ae.GetBaseException() is WebException)
                    {
                    }
                    throw ae.GetBaseException();
                }
            }
        }
Exemple #14
0
        public static HttpWebRequest ToHttpRequest(this Bundle.BundleEntryComponent entry,
                                                   Prefer bodyPreference, ResourceFormat format, bool useFormatParameter, out byte[] body)
        {
            System.Diagnostics.Debug.WriteLine("{0}: {1}", entry.Request.Method, entry.Request.Url);

            var interaction = entry.Request;

            body = null;

            if (entry.Resource != null && !(interaction.Method == Bundle.HTTPVerb.POST || interaction.Method == Bundle.HTTPVerb.PUT))
            {
                throw Error.InvalidOperation("Cannot have a body on an Http " + interaction.Method.ToString());
            }

            var location = new RestUrl(interaction.Url);

            if (useFormatParameter)
            {
                location.AddParam(HttpUtil.RESTPARAM_FORMAT, Hl7.Fhir.Rest.ContentType.BuildFormatParam(format));
            }

            var request = (HttpWebRequest)HttpWebRequest.Create(location.Uri);

            request.Method = interaction.Method.ToString();
            setAgent(request, ".NET FhirClient for FHIR " + Model.ModelInfo.Version);

            if (!useFormatParameter)
            {
                request.Accept = Hl7.Fhir.Rest.ContentType.BuildContentType(format, forBundle: false);
            }

            if (interaction.IfMatch != null)
            {
                request.Headers["If-Match"] = interaction.IfMatch;
            }
            if (interaction.IfNoneMatch != null)
            {
                request.Headers["If-None-Match"] = interaction.IfNoneMatch;
            }
#if PORTABLE45
            if (interaction.IfModifiedSince != null)
            {
                request.Headers["If-Modified-Since"] = interaction.IfModifiedSince.Value.UtcDateTime.ToString();
            }
#else
            if (interaction.IfModifiedSince != null)
            {
                request.IfModifiedSince = interaction.IfModifiedSince.Value.UtcDateTime;
            }
#endif
            if (interaction.IfNoneExist != null)
            {
                request.Headers["If-None-Exist"] = interaction.IfNoneExist;
            }

            if (interaction.Method == Bundle.HTTPVerb.POST || interaction.Method == Bundle.HTTPVerb.PUT)
            {
                request.Headers["Prefer"] = bodyPreference == Prefer.ReturnMinimal ? "return=minimal" : "return=representation";
            }

            if (entry.Resource != null)
            {
                setBodyAndContentType(request, entry.Resource, format, out body);
            }

            return(request);
        }
 private static Bundle.HTTPVerb ExtrapolateMethod(this ILocalhost localhost, Bundle.BundleEntryComponent entry, IKey key)
 {
     return(entry.Transaction.Method ?? DetermineMethod(localhost, key));
 }
Exemple #16
0
 private void addEntry(Bundle.BundleEntryComponent newEntry, RestUrl path)
 {
     newEntry.Request.Url = path.Uri.ToString();
     _result.Entry.Add(newEntry);
 }
 public static bool HasResource(this Bundle.BundleEntryComponent entry)
 {
     return(entry.Resource != null);
 }