public void Send(object message, IDictionary<string, string> headers) { // create an envelope for the message Envelope newEnvelope = new Envelope(); newEnvelope.Headers = headers ?? new Dictionary<string, string>(); // create a message context for message processing MessageContext ctx = new MessageContext( MessageContext.Directions.Out, newEnvelope, message); // process the message this.ProcessMessage(ctx, () => { try { _envelopeSender.Send(ctx.Envelope); } catch (Exception ex) { string msg = "Failed to send an envelope."; Log.Error(msg, ex); throw new MessageException(msg, ex); } }); }
public void ProcessMessage( MessageContext context, Action continueProcessing) { Log.Debug(string.Format("Enter ProcessMessage - Direction: {0}", context.Direction)); ProcessMessage(context, _processingChain, continueProcessing); Log.Debug(string.Format("Leave ProcessMessage - Direction: {0}", context.Direction)); }
public void ProcessMessage(MessageContext context, Action continueProcessing) { string endOfStreamTopic = typeof(EndOfStream).FullName; string collectionSizeTopic = typeof(CollectionSizeNotifier).FullName; if (context.Direction == MessageContext.Directions.Out) { if (context.Envelope.GetMessageTopic() == endOfStreamTopic) { context.Envelope.SetMessageTopic(((EndOfStream)context.Message).StreamType); } if (context.Envelope.GetMessageTopic() == collectionSizeTopic) { context.Envelope.SetMessageTopic(((CollectionSizeNotifier)context.Message).CollectionType); } } continueProcessing(); }
public void Publish(object evt) { _log.Debug("enter publish to stream"); string sequence = _sequenceId.ToString(); Envelope env = StreamingEnvelopeHelper.BuildStreamingEnvelope(sequence, _position); env.SetMessageTopic(Topic); MessageContext context = new MessageContext(MessageContext.Directions.Out, env, evt); EventStreamQueueItem eventItem = new EventStreamQueueItem(context); _log.Debug("buffering event with sequenceId: " + sequence + ", position: " + _position); _queuedEvents.Enqueue(eventItem); if (_queuedEvents.Count == (_batchLimit)) { _log.Debug("flushing " + _batchLimit + " event(s) to stream."); FlushStreamBuffer(); } _position++; }
public void ProcessInbound(MessageContext context, Action continueProcessing) { bool signatureVerified = false; Envelope env = context.Envelope; // start by getting the purported sender's identity string sender = env.GetSenderIdentity(); if (string.IsNullOrEmpty(sender)) { _log.Error("An inbound event arrived with no sender identity"); } else { // use the identity to lookup their certificate X509Certificate2 senderCert = _certProvider.GetCertificateFor(sender); if (null == senderCert) { _log.Error("Sender " + sender + " does not have a public key certificate available"); } else { // get the digital signature from the headers byte[] digitalSignature = env.GetDigitalSignature(); if (0 == digitalSignature.LongLength) { _log.Error("Sender " + sender + " did not digitally sign the event"); } else { // verify that the payload hasn't been tampered with by using the // sender's public key and the digital signature on the envelope RSACryptoServiceProvider rsaProvider = senderCert.PublicKey.Key as RSACryptoServiceProvider; signatureVerified = rsaProvider.VerifyData(env.Payload, new SHA1CryptoServiceProvider(), digitalSignature); } } } if (signatureVerified) { continueProcessing(); } }
public void ProcessMessage( MessageContext context, List<IMessageProcessor> processingChain, Action continueProcessing) { // if the chain is null or empty, complete processing if ((null == processingChain) || (!processingChain.Any())) { Log.Debug(string.Format("Message processing complete. Direction: {0}", context.Direction)); continueProcessing(); return; } // get the first processor IMessageProcessor processor = processingChain.First(); // create a processing chain that no longer contains this processor List<IMessageProcessor> newChain = processingChain.Skip(1).ToList(); // let it process the message and pass its "next" processor: a method that // recursively calls this function with the current processor removed Log.Debug(string.Format("Handing message to processor: {0} - Direction: {1}", processor.GetType(), context.Direction)); processor.ProcessMessage(context, () => ProcessMessage(context, newChain, continueProcessing)); }
public void ProcessMessage(MessageContext context, Action continueProcessing) { if (MessageContext.Directions.In == context.Direction) { this.ProcessInbound(context, continueProcessing); } if (MessageContext.Directions.Out == context.Direction) { this.ProcessOutbound(context, continueProcessing); } }
public void ProcessOutbound(MessageContext context, Action continueProcessing) { if (null == _cert) { return; } Envelope env = context.Envelope; env.SetSenderIdentity(_cert.Subject); try { RSACryptoServiceProvider rsaProvider = _cert.PrivateKey as RSACryptoServiceProvider; env.SetDigitalSignature(rsaProvider.SignData(env.Payload, new SHA1CryptoServiceProvider())); continueProcessing(); } catch (Exception ex) { _log.Error("Failed to digitally sign the event", ex); throw; } }
public void ProcessMessage( MessageContext context, Action onComplete) { _messageProcessor.ProcessMessage(context, onComplete); }
public EventStreamQueueItem(MessageContext eventContext) { _eventContext = eventContext; }
public void ProcessMessage(MessageContext context, Action continueProcessing) { if (MessageContext.Directions.Out == context.Direction) { Envelope env = context.Envelope; Guid messageId = env.GetMessageId(); messageId = Guid.Equals(Guid.Empty, messageId) ? Guid.NewGuid() : messageId; env.SetMessageId(messageId.ToString()); Guid correlationId = env.GetCorrelationId(); string messageType = env.GetMessageType(); messageType = string.IsNullOrEmpty(messageType) ? this.GetMessageType(context.Message) : messageType; env.SetMessageType(messageType); string messageTopic = env.GetMessageTopic(); messageTopic = string.IsNullOrEmpty(messageTopic) ? this.GetMessageTopic(context.Message) : messageTopic; if (Guid.Empty != correlationId) { messageTopic = messageTopic + "#" + correlationId.ToString(); } env.SetMessageTopic(messageTopic); DateTime creation = env.GetCreationTime(); creation = (DateTime.MinValue == creation) ? DateTime.UtcNow : creation; env.SetCreationTime(creation); string senderIdentity = env.GetSenderIdentity(); if (string.IsNullOrEmpty(senderIdentity)) { if (!string.IsNullOrEmpty(_alternateIdentity)) { senderIdentity = _alternateIdentity; } else { //This line will raise an exception if there is no active directory server available try { senderIdentity = UserPrincipal.Current.DistinguishedName.Replace(",", ", "); } catch { senderIdentity = UserPrincipal.Current.Name; } } env.SetSenderIdentity(senderIdentity); } } // either way, continue processing continueProcessing(); }
public virtual void ProcessInbound(MessageContext context, Action continueProcessing) { bool success = false; Envelope env = context.Envelope; try { // get the type from the headers on the envelope string eventType = env.GetMessageType(); // if no type, try the topic if (string.IsNullOrEmpty(eventType)) { eventType = env.GetMessageTopic(); } // and make sure we get something if (string.IsNullOrEmpty(eventType)) { throw new Exception("Cannot deserialize an envelope that does not specify the event's topic"); } // start with a null Type Type type = null; // go through each assembly loaded into the app domain StringBuilder assembliesLoaded = new StringBuilder(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { assembliesLoaded.AppendLine("Loaded Assembly Found: " + assembly.FullName); // and see if it can get us our Type type = assembly.GetType(eventType); if (null != type) { _log.Debug("Found type " + type + " in assembly " + assembly.FullName); break; } } _log.Debug("Currently Loaded Assemblies:" + "\r\n" + assembliesLoaded.ToString()); if (null != type) // if we did get a Type, we can deserialize the event { string jsonString = new UTF8Encoding().GetString(env.Payload); //Encoding.UTF8.GetString(env.Payload); _log.Debug("Will attempt to deserialize: " + jsonString); context.Message = JsonConvert.DeserializeObject(jsonString, type, _settings); success = true; } else // otherwise, throw an exception { throw new Exception("Cannot deserialize an event of topic '" + eventType + "' because no type definition could be found for it"); } } catch (Exception ex) { _log.Error("Failed to deserialize an event", ex); } if (success) { continueProcessing(); } }
public virtual void ProcessOutbound(MessageContext context, Action continueProcessing) { object ev = context.Message; try { // first, serialize the event (make it pretty!) string json = JsonConvert.SerializeObject(ev, Formatting.Indented, _settings); _log.Debug("Serialized event: " + json); // next, convert the string into bytes using UTF-8 context.Envelope.Payload = new UTF8Encoding().GetBytes(json); } catch (Exception ex) { _log.Error("Failed to serialize an event", ex); throw; } continueProcessing(); }