/// <summary>Process the operation get ws binding token described by response.</summary>
        /// <param name="context">          [in,out] The context.</param>
        /// <param name="response">         [in,out] The response.</param>
        /// <param name="previousComponent">The previous component.</param>
        internal static void ProcessOperationGetWsBindingToken(
            ref HttpContext context,
            ref HttpResponseMessage response,
            string previousComponent)
        {
            List <string> ids = new List <string>();

            if (previousComponent != "Subscription")
            {
                ids.Add(previousComponent);
            }
            else
            {
                try
                {
                    using (TextReader reader = new StreamReader(context.Request.Body))
                    {
                        string requestContent = reader.ReadToEndAsync().Result;

                        Parameters opParams = _fhirParser.Parse <Parameters>(requestContent);

                        foreach (Parameters.ParameterComponent param in opParams.Parameter)
                        {
                            if (param.Name == "ids")
                            {
                                ids.Add((param.Value as Id).ToString());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.StatusCode = HttpStatusCode.BadRequest;
                    response.Content    = new StringContent("Caught exception: " + ex.Message);
                    return;
                }
            }

            foreach (string id in ids)
            {
                if (!SubscriptionManagerR4.Exists(id))
                {
                    response.StatusCode = HttpStatusCode.BadRequest;
                    response.Content    = new StringContent($"Invalid subscription id: {id}");
                    return;
                }
            }

            SubscriptionWsBindingToken token = SubscriptionWsBindingToken.GetTokenR4(ids);

            WebsocketManager.RegisterToken(token);

            Parameters parameters = new Parameters();

            parameters.Add("token", new FhirString(token.Token.ToString()));
            parameters.Add("expiration", new FhirDateTime(new DateTimeOffset(token.ExpiresAt)));
            parameters.Add("subscriptions", new FhirString(string.Join(',', ids)));

            ProcessorUtils.SerializeR4(ref response, parameters);
        }
        /// <summary>Registers the token described by token.</summary>
        /// <exception cref="ArgumentNullException">Thrown when one or more required arguments are null.</exception>
        /// <param name="token">The token.</param>
        public static void RegisterToken(SubscriptionWsBindingToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            _instance._guidTokenDict.Add(token.Token, token);
        }
        /// <summary>Expires and Removes the token described by tokenGuid.</summary>
        /// <param name="tokenGuid">Unique identifier for the token.</param>
        public static void ExpireToken(Guid tokenGuid)
        {
            if (!_instance._guidTokenDict.ContainsKey(tokenGuid))
            {
                return;
            }

            Guid clientGuid = _instance._guidTokenDict[tokenGuid].BoundClient;

            if (clientGuid == Guid.Empty)
            {
                _instance._guidTokenDict.Remove(tokenGuid);
                return;
            }

            if (!_instance._guidInfoDict.ContainsKey(clientGuid))
            {
                _instance._guidTokenDict.Remove(tokenGuid);
                return;
            }

            SubscriptionWsBindingToken token      = _instance._guidTokenDict[tokenGuid];
            WebsocketClientInformation clientInfo = _instance._guidInfoDict[clientGuid];

            // if the client only has one token (this one), deactivate everything
            if (clientInfo.BoundTokenGuids.Count == 1)
            {
                foreach (string subscriptionId in token.SubscriptionIds)
                {
                    RemoveSubscriptionFromClient(subscriptionId, clientGuid);
                }
            }

            // TODO: for now, just assume it has been replaced with a newer replica
            _instance._guidTokenDict.Remove(tokenGuid);
            _instance._guidInfoDict[clientGuid].BoundTokenGuids.Remove(tokenGuid);

            return;
        }