Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <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);
                }
            }
        }
Example #4
0
        /// <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 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 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);
        }
Example #7
0
        /// <summary>
        /// Converts the object to a list of headers
        /// </summary>
        /// <returns><see cref="HeaderCollection"/></returns>
        public HeaderCollection ToHeaders()
        {
            HeaderCollection headers = new HeaderCollection();

            if (headers != null)
            {
                foreach (KeyValuePair <string, string> item in this)
                {
                    Header dataHeader = new DataHeader(item.Key, item.Value);
                    headers.AddHeader(dataHeader);
                }
            }

            return(headers);
        }
        /// <summary>
        /// Creates a <see cref="HeaderCollection"/> from a message
        /// </summary>
        /// <param name="message">The message to parse</param>
        /// <returns><see cref="HeaderCollection"/></returns>
        public static HeaderCollection FromMessage(string message)
        {
            HeaderCollection headers = new HeaderCollection();

            string[] lines = message.Split('\r', '\n');
            foreach (string line in lines)
            {
                Header header = Header.ParseHeader(line);
                if (header != null)
                {
                    headers.AddHeader(header);
                }
            }

            return(headers);
        }
Example #9
0
        /// <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);
        }
        /// <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);
        }