/// <summary> /// Converts the object to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public HeaderCollection ToHeaders() { Header hID = new Header(Header.SUBSCRIBER_ID, this.id); Header hName = new Header(Header.SUBSCRIBER_NAME, this.name); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hID); headers.AddHeader(hName); // only pass the port if different than the standard port if (this.port != GrowlConnector.TCP_PORT) { Header hPort = new Header(Header.SUBSCRIBER_PORT, this.port.ToString()); headers.AddHeader(hPort); } this.AddInheritedAttributesToHeaders(headers); return(headers); }
/// <summary> /// Converts the Error to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public virtual HeaderCollection ToHeaders() { Header hErrorCode = new Header(Header.ERROR_CODE, this.ErrorCode.ToString()); Header hDescription = new Header(Header.ERROR_DESCRIPTION, this.ErrorDescription); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hErrorCode); headers.AddHeader(hDescription); this.AddInheritedAttributesToHeaders(headers); return headers; }
public void AddOrRemoveReservedHeaders() { var collection = new HeaderCollection(); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("message_id", "one-id")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("timestamp", 12345678L)); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("content_type", "application/json")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("content_encoding", "UTF-8")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("message_id")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("timestamp")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("content_type")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("content_encoding")); }
public void BuildBasicPropertiesByConfiguration() { const String contentType = "application/json"; const String contentEncoding = "UTF-8"; const String messageId = "one-id"; const Int64 timestamp = 123456789L; var collection = new HeaderCollection(new Dictionary <String, Object>(StringComparer.OrdinalIgnoreCase) { { "content_type", contentType }, { "content_encoding", contentEncoding } }); const String key = "foo"; const String value = "bar"; collection.AddHeader(key, value); var resolver = new Mock <IMessageTypeResolver>(); resolver.Setup(_ => _.Resolve <Foo>()).Returns(EmptyMessageBinding.Instance); var newId = new Mock <INewId>(); newId.Setup(_ => _.Next()).Returns(messageId); var dateTimeProvider = new Mock <IDateTimeProvider>(); dateTimeProvider.Setup(_ => _.UtcNow()).Returns(timestamp.ToDateTimeOffset()); var message = new OutboundMessage <Foo>(new Foo(), collection); var properties = message.BuildBasicProperties(resolver.Object, dateTimeProvider.Object, newId.Object); Assert.Equal(messageId, properties.MessageId); Assert.Equal(new AmqpTimestamp(timestamp), properties.Timestamp); Assert.Equal(contentType, properties.ContentType); Assert.Equal(contentEncoding, properties.ContentEncoding); }
public void BuildBasicProperties() { const String contentType = "application/xml"; const String contentEncoding = "UTF-16"; const String messageId = "one-id"; const Int64 timestamp = 123456789L; var collection = new HeaderCollection(new Dictionary <String, Object>(StringComparer.OrdinalIgnoreCase) { { "message_id", messageId }, { "timestamp", timestamp }, { "content_type", contentType }, { "content_encoding", contentEncoding } }); const String key = "foo"; const String value = "bar"; collection.AddHeader(key, value); var resolver = new Mock <IMessageTypeResolver>(); resolver.Setup(_ => _.Resolve <Foo>()).Returns(EmptyMessageBinding.Instance); var message = new OutboundMessage <Foo>(new Foo(), collection); var properties = message.BuildBasicProperties(resolver.Object, null, null); Assert.Equal(messageId, properties.MessageId); Assert.Equal(new AmqpTimestamp(timestamp), properties.Timestamp); Assert.Equal(contentType, properties.ContentType); Assert.Equal(contentEncoding, properties.ContentEncoding); Assert.Equal(value, collection[key]); }
/// <summary> /// Converts the object to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public HeaderCollection ToHeaders() { Header hName = new Header(Header.APPLICATION_NAME, this.name); Header hIcon = new Header(Header.APPLICATION_ICON, this.Icon); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hName); if (this.Icon != null && this.Icon.IsSet) { headers.AddHeader(hIcon); headers.AssociateBinaryData(this.Icon); } this.AddInheritedAttributesToHeaders(headers); return headers; }
public void KeyComparison() { const String key = "foo"; const String value = "some-value"; var collection = new HeaderCollection(); collection.AddHeader(key, value); Assert.Equal(value, collection[key.ToUpperInvariant()]); }
public void ContainsHeader() { const String key = "foo"; var collection = new HeaderCollection(); collection.AddHeader(key, null); Assert.True(collection.ContainsHeader(key)); }
/// <summary> /// Converts the SubscriptionResponse to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public override HeaderCollection ToHeaders() { HeaderCollection headers = new HeaderCollection(); Header hTTL = new Header(Header.SUBSCRIPTION_TTL, this.ttl.ToString()); headers.AddHeader(hTTL); HeaderCollection baseHeaders = base.ToHeaders(); headers.AddHeaders(baseHeaders); return headers; }
/// <summary> /// Converts the SubscriptionResponse to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public override HeaderCollection ToHeaders() { HeaderCollection headers = new HeaderCollection(); Header hTTL = new Header(Header.SUBSCRIPTION_TTL, this.ttl.ToString()); headers.AddHeader(hTTL); HeaderCollection baseHeaders = base.ToHeaders(); headers.AddHeaders(baseHeaders); return(headers); }
public void BuildBasicProperties() { const String contentType = "application/xml"; const String contentEncoding = "UTF-16"; const String messageId = "one-id"; const Int64 timestamp = 123456789L; const String replyExchangeName = "reply-queue-name"; const String replyRoutingKey = "reply-queue-name"; var correlationId = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1).ToString(); var directReplyConfiguration = new DirectReplyConfiguration(replyExchangeName, replyRoutingKey); var collection = new HeaderCollection(new Dictionary <String, Object>(StringComparer.OrdinalIgnoreCase) { { "message_id", messageId }, { "timestamp", timestamp }, { "content_type", contentType }, { "content_encoding", contentEncoding }, { "correlation_id", correlationId }, { "reply_configuration", directReplyConfiguration } }); const String key = "foo"; const String value = "bar"; collection.AddHeader(key, value); var resolver = new Mock <IMessageTypeResolver>(); resolver.Setup(_ => _.Resolve <Foo>()).Returns(EmptyMessageBinding.Instance); var message = new OutboundMessage <Foo>(new Foo(), collection); var properties = message.BuildBasicProperties(resolver.Object, null, null); Assert.Equal(messageId, properties.MessageId); Assert.Equal(new AmqpTimestamp(timestamp), properties.Timestamp); Assert.Equal(contentType, properties.ContentType); Assert.Equal(contentEncoding, properties.ContentEncoding); Assert.Equal(correlationId, properties.CorrelationId); Assert.Equal(directReplyConfiguration.ToString(), properties.ReplyTo); Assert.Equal(value, collection[key]); }
public void AddOrRemoveReservedHeaders() { var collection = new HeaderCollection(); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("message_id", "one-id")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("timestamp", 12345678L)); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("content_type", "application/json")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("content_encoding", "UTF-8")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("correlation_id", new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1).ToString())); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("reply_to", "reply-queue-name")); Assert.Throws <InvalidOperationException>(() => collection.AddHeader("type", "urn:message:sample")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("message_id")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("timestamp")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("content_type")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("content_encoding")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("correlation_id")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("reply_to")); Assert.Throws <InvalidOperationException>(() => collection.RemoveHeader("type")); }
/// <summary> /// When converting an <see cref="ExtensibleObject"/> to a list of headers, /// this method adds the custom attributes (both text and binary) to the /// list of headers. /// </summary> /// <param name="headers">The <see cref="HeaderCollection"/> to add the custom attributes to</param> protected void AddCustomAttributesToHeaders(HeaderCollection headers) { if (headers != null) { foreach (KeyValuePair<string, string> item in this.CustomTextAttributes) { Header customHeader = new CustomHeader(item.Key, item.Value); headers.AddHeader(customHeader); } foreach (KeyValuePair<string, Resource> item in this.CustomBinaryAttributes) { Header customHeader = new CustomHeader(item.Key, item.Value.ToString()); headers.AddHeader(customHeader); headers.AssociateBinaryData(item.Value); } } }
/// <summary> /// Parses a response message and returns the corresponding <see cref="Response"/> object /// </summary> /// <param name="message">The entire GNTP response message</param> /// <param name="headers">The <see cref="HeaderCollection"/> of parsed header values</param> /// <returns><see cref="Response"/></returns> private Response Parse(string message, out HeaderCollection headers) { ResponseType responseType = ResponseType.ERROR; Response response = null; headers = new HeaderCollection(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message); System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); using (stream) { GNTPStreamReader reader = new GNTPStreamReader(stream); using (reader) { bool isError = false; bool isFirstLine = true; while (!reader.EndOfStream) { string line = reader.ReadLine(); if (isFirstLine) { Match match = ParseGNTPHeaderLine(line); if (match.Success) { this.version = match.Groups["Version"].Value; this.directive = match.Groups["Directive"].Value; if (this.directive.StartsWith("-", StringComparison.InvariantCulture)) this.directive = this.directive.Remove(0, 1); if (version == GNTP_SUPPORTED_VERSION) { if (Enum.IsDefined(typeof(ResponseType), this.directive)) { responseType = (ResponseType)Enum.Parse(typeof(ResponseType), this.directive, false); response = new Response(); if (responseType == ResponseType.ERROR) isError = true; isFirstLine = false; } else { // invalid directive response = new Response(ErrorCode.INVALID_REQUEST, "Unrecognized response type"); break; } } else { // invalid version response = new Response(ErrorCode.UNKNOWN_PROTOCOL_VERSION, "Unsupported version"); break; } } else { // invalid message header response = new Response(ErrorCode.UNKNOWN_PROTOCOL, "Unrecognized response"); break; } } else { Header header = Header.ParseHeader(line); headers.AddHeader(header); } } if (response != null) { if (isError) { int errorCode = headers.GetHeaderIntValue(Header.ERROR_CODE, false); string errorDescription = headers.GetHeaderStringValue(Header.ERROR_DESCRIPTION, false); if (errorCode > 0 && errorDescription != null) response = new Response(errorCode, errorDescription); else response = new Response(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } else { string inResponseTo = headers.GetHeaderStringValue(Header.RESPONSE_ACTION, false); response.InResponseTo = inResponseTo; } response.SetAttributesFromHeaders(headers, (responseType == ResponseType.CALLBACK)); } else { // if we got here, that is bad. response = new Response(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } } } return response; }
/// <summary> /// When converting an <see cref="ExtensibleObject"/> to a list of headers, /// this method adds the common attributes to the list of headers. /// </summary> /// <param name="headers">The <see cref="HeaderCollection"/> to add the custom headers to</param> protected void AddCommonAttributesToHeaders(HeaderCollection headers) { if (headers != null) { //Header hRequestID = new Header("RequestID", requestID); Header hMachineName = new Header(Header.ORIGIN_MACHINE_NAME, machineName); Header hSoftwareName = new Header(Header.ORIGIN_SOFTWARE_NAME, softwareName); Header hSoftwareVersion = new Header(Header.ORIGIN_SOFTWARE_VERSION, softwareVersion); Header hPlatformName = new Header(Header.ORIGIN_PLATFORM_NAME, platformName); Header hPlatformVersion = new Header(Header.ORIGIN_PLATFORM_VERSION, platformVersion); //headers.Add(hRequestID); headers.AddHeader(hMachineName); headers.AddHeader(hSoftwareName); headers.AddHeader(hSoftwareVersion); headers.AddHeader(hPlatformName); headers.AddHeader(hPlatformVersion); } }
/// <summary> /// Converts the Response to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public override HeaderCollection ToHeaders() { HeaderCollection headers = new HeaderCollection(); if (this.IsOK && !this.IsCallback) { Header hResponseAction = new Header(Header.RESPONSE_ACTION, this.InResponseTo); headers.AddHeader(hResponseAction); } if (this.IsError) { Header hErrorCode = new Header(Header.ERROR_CODE, this.ErrorCode.ToString()); Header hDescription = new Header(Header.ERROR_DESCRIPTION, this.ErrorDescription); headers.AddHeader(hErrorCode); headers.AddHeader(hDescription); } if (this.IsCallback) { Header hNotificationID = new Header(Header.NOTIFICATION_ID, this.callbackData.NotificationID); Header hCallbackResult = new Header(Header.NOTIFICATION_CALLBACK_RESULT, Enum.GetName(typeof(CallbackResult), this.callbackData.Result)); Header hCallbackContext = new Header(Header.NOTIFICATION_CALLBACK_CONTEXT, this.callbackData.Data); Header hCallbackContextType = new Header(Header.NOTIFICATION_CALLBACK_CONTEXT_TYPE, this.callbackData.Type); //Header hCallbackTimestamp = new Header(Header.NOTIFICATION_CALLBACK_TIMESTAMP, DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")); Header hCallbackTimestamp = new Header(Header.NOTIFICATION_CALLBACK_TIMESTAMP, DateTime.UtcNow.ToString("u")); headers.AddHeader(hNotificationID); headers.AddHeader(hCallbackResult); headers.AddHeader(hCallbackContext); headers.AddHeader(hCallbackContextType); headers.AddHeader(hCallbackTimestamp); } this.AddInheritedAttributesToHeaders(headers); return headers; }
public EndpointReference AddProperty(MessageHeader messageHeader) { _properties.AddHeader(messageHeader); return(this); }
/// <summary> /// Converts the object to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public HeaderCollection ToHeaders() { Header hName = new Header(Header.NOTIFICATION_NAME, this.Name); Header hDisplayName = new Header(Header.NOTIFICATION_DISPLAY_NAME, this.DisplayName); Header hIcon = new Header(Header.NOTIFICATION_ICON, this.Icon); Header hEnabled = new Header(Header.NOTIFICATION_ENABLED, this.Enabled.ToString()); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hName); headers.AddHeader(hEnabled); if(this.displayName != null) headers.AddHeader(hDisplayName); if (this.Icon != null && this.Icon.IsSet) { headers.AddHeader(hIcon); headers.AssociateBinaryData(this.Icon); } this.AddCustomAttributesToHeaders(headers); // NOTE: dont call AddInheritedAttributesToHeaders because we want to ignore the common attributes return headers; }
/// <summary> /// Converts the object to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public HeaderCollection ToHeaders() { Header hAppName = new Header(Header.APPLICATION_NAME, this.ApplicationName); Header hName = new Header(Header.NOTIFICATION_NAME, this.Name); Header hID = new Header(Header.NOTIFICATION_ID, this.ID); Header hTitle = new Header(Header.NOTIFICATION_TITLE, this.Title); Header hText = new Header(Header.NOTIFICATION_TEXT, this.Text); Header hSticky = new Header(Header.NOTIFICATION_STICKY, this.Sticky); Header hPriority = new Header(Header.NOTIFICATION_PRIORITY, ((int) this.Priority).ToString()); Header hIcon = new Header(Header.NOTIFICATION_ICON, this.Icon); Header hCoalescingID = new Header(Header.NOTIFICATION_COALESCING_ID, this.CoalescingID); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hAppName); headers.AddHeader(hName); headers.AddHeader(hID); headers.AddHeader(hTitle); headers.AddHeader(hText); headers.AddHeader(hSticky); headers.AddHeader(hPriority); headers.AddHeader(hCoalescingID); if (this.Icon != null && this.Icon.IsSet) { headers.AddHeader(hIcon); headers.AssociateBinaryData(this.Icon); } this.AddInheritedAttributesToHeaders(headers); return headers; }
/// <summary> /// Converts the object to a list of headers /// </summary> /// <returns><see cref="HeaderCollection"/></returns> public HeaderCollection ToHeaders() { Header hID = new Header(Header.SUBSCRIBER_ID, this.id); Header hName = new Header(Header.SUBSCRIBER_NAME, this.name); HeaderCollection headers = new HeaderCollection(); headers.AddHeader(hID); headers.AddHeader(hName); // only pass the port if different than the standard port if (this.port != GrowlConnector.TCP_PORT) { Header hPort = new Header(Header.SUBSCRIBER_PORT, this.port.ToString()); headers.AddHeader(hPort); } this.AddInheritedAttributesToHeaders(headers); return headers; }