public object Create(object data, bool updateIfExists)
        {
            if (data is PubSubChannelDefinition definition)
            {
                try
                {
                    PubSubChannelDefinition retVal = this.m_manager.RegisterChannel(definition.Name, definition.DispatcherFactoryId, new Uri(definition.Endpoint), definition.Settings.ToDictionary(o => o.Name, o => o.Value));

                    return(retVal);
                }
                catch (Exception e)
                {
                    this.m_tracer.TraceError("Error creating channel - {0}", e);
                    throw new Exception(this.m_localizationService.GetString("error.rest.ami.errorCreatingChannel"), e);
                }
            }
            else
            {
                this.m_tracer.TraceError("Body must be of type PubSubChannelDefinition");
                throw new ArgumentException(this.m_localizationService.GetString("error.rest.ami.bodyMustBePubSubChannel"));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Register the specified channel
        /// </summary>
        public PubSubChannelDefinition RegisterChannel(string name, string dispatcherFactoryId, Uri endpoint, IDictionary <string, string> settings)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            else if (String.IsNullOrEmpty(dispatcherFactoryId))
            {
                var dispatchFactory = DispatcherFactoryUtil.FindDispatcherFactoryByUri(endpoint);
                if (dispatchFactory == null)
                {
                    throw new InvalidOperationException("Cannot find dispatcher factory for scheme!");
                }
                dispatcherFactoryId = dispatchFactory.Id;
            }
            else if (endpoint == null)
            {
                throw new ArgumentNullException(nameof(endpoint));
            }

            // Validate state
            this.m_policyEnforcementService.Demand(PermissionPolicyIdentifiers.CreatePubSubSubscription);

            var channel = new PubSubChannelDefinition()
            {
                Name = name,
                DispatcherFactoryId = dispatcherFactoryId,
                Endpoint            = endpoint.ToString(),
                IsActive            = false,
                Settings            = settings.Select(o => new PubSubChannelSetting()
                {
                    Name = o.Key, Value = o.Value
                }).ToList()
            };

            using (var conn = this.m_configuration.Provider.GetWriteConnection())
            {
                try
                {
                    conn.Open();
                    using (var tx = conn.BeginTransaction())
                    {
                        var dbChannel = this.m_mapper.MapModelInstance <PubSubChannelDefinition, DbChannel>(channel);
                        dbChannel.Endpoint = channel.Endpoint.ToString();

                        // Get the authorship
                        var se = this.m_securityRepository.GetSecurityEntity(AuthenticationContext.Current.Principal);
                        if (se == null)
                        {
                            throw new KeyNotFoundException($"Unable to determine structure data for {AuthenticationContext.Current.Principal.Identity.Name}");
                        }

                        dbChannel.CreatedByKey = se.Key.Value;
                        dbChannel.CreationTime = DateTimeOffset.Now;
                        dbChannel = conn.Insert(dbChannel);

                        // Insert settings
                        foreach (var itm in channel.Settings)
                        {
                            conn.Insert(new DbChannelSetting()
                            {
                                ChannelKey = dbChannel.Key.Value,
                                Name       = itm.Name,
                                Value      = itm.Value
                            });
                        }

                        tx.Commit();
                        channel = this.MapInstance(conn, dbChannel);

                        this.m_cache?.Add(channel);
                        return(channel);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception($"Error creating channel {channel}", e);
                }
            }
        }