Пример #1
0
        /// <summary>
        /// Creates a new <see cref="NotificationType"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the response</param>
        /// <returns><see cref="NotificationType"/></returns>
        public static NotificationType FromHeaders(HeaderCollection headers)
        {
            string   name        = headers.GetHeaderStringValue(Header.NOTIFICATION_NAME, true);
            string   displayName = headers.GetHeaderStringValue(Header.NOTIFICATION_DISPLAY_NAME, false);
            Resource icon        = headers.GetHeaderResourceValue(Header.NOTIFICATION_ICON, false);
            bool     enabled     = headers.GetHeaderBooleanValue(Header.NOTIFICATION_ENABLED, false);

            NotificationType nt = new NotificationType(name, displayName, icon, enabled);

            SetCustomAttributesFromHeaders(nt, headers);    // NOTE: dont call SetInheritedAttributesFromHeaders because we want to ignore the common attributes
            return(nt);
        }
Пример #2
0
        /// <summary>
        /// Creates a new <see cref="Notification"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the object</param>
        /// <returns><see cref="Notification"/></returns>
        public static Notification FromHeaders(HeaderCollection headers)
        {
            string appName = headers.GetHeaderStringValue(Header.APPLICATION_NAME, true);
            string name    = headers.GetHeaderStringValue(Header.NOTIFICATION_NAME, true);
            string id      = headers.GetHeaderStringValue(Header.NOTIFICATION_ID, false);
            string title   = headers.GetHeaderStringValue(Header.NOTIFICATION_TITLE, true);
            string text    = headers.GetHeaderStringValue(Header.NOTIFICATION_TEXT, false);

            if (text == null)
            {
                text = String.Empty;
            }
            string   coalescingID = headers.GetHeaderStringValue(Header.NOTIFICATION_COALESCING_ID, false);
            Resource icon         = headers.GetHeaderResourceValue(Header.NOTIFICATION_ICON, false);
            bool     sticky       = headers.GetHeaderBooleanValue(Header.NOTIFICATION_STICKY, false);
            string   p            = headers.GetHeaderStringValue(Header.NOTIFICATION_PRIORITY, false);
            Priority priority     = Growl.Connector.Priority.Normal;

            if (p != null)
            {
                int  pval = 0;
                bool pok  = int.TryParse(p, out pval);
                if (pok && Enum.IsDefined(typeof(Priority), pval))
                {
                    priority = (Priority)pval;
                }
            }

            Notification notification = new Notification(appName, name, id, title, text, icon, sticky, priority, coalescingID);

            SetInhertiedAttributesFromHeaders(notification, headers);
            return(notification);
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="CallbackDataBase"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the object</param>
        /// <returns><see cref="CallbackDataBase"/></returns>
        public static CallbackDataBase FromHeaders(HeaderCollection headers)
        {
            string data = headers.GetHeaderStringValue(Header.NOTIFICATION_CALLBACK_CONTEXT, true);
            string type = headers.GetHeaderResourceValue(Header.NOTIFICATION_CALLBACK_CONTEXT_TYPE, true);

            CallbackDataBase context = new CallbackDataBase(data, type);

            return(context);
        }
        /// <summary>
        /// Creates a new <see cref="Error"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the object</param>
        /// <returns><see cref="Error"/></returns>
        public static Error FromHeaders(HeaderCollection headers)
        {
            int    errorCode   = headers.GetHeaderIntValue(Header.ERROR_CODE, true);
            string description = headers.GetHeaderStringValue(Header.ERROR_DESCRIPTION, false);

            Error error = new Error(errorCode, description);

            SetInhertiedAttributesFromHeaders(error, headers);
            return(error);
        }
Пример #5
0
        /// <summary>
        /// Creates a new <see cref="Response"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the response</param>
        /// <returns><see cref="Response"/></returns>
        public new static Response FromHeaders(HeaderCollection headers)
        {
            int    errorCode   = headers.GetHeaderIntValue(Header.ERROR_CODE, true);
            string description = headers.GetHeaderStringValue(Header.ERROR_DESCRIPTION, false);

            Response response = new Response(errorCode, description);

            SetInhertiedAttributesFromHeaders(response, headers);
            return(response);
        }
Пример #6
0
        /// <summary>
        /// Creates a new <see cref="Application"/> from a list of headers
        /// </summary>
        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the object</param>
        /// <returns><see cref="Application"/></returns>
        public static Application FromHeaders(HeaderCollection headers)
        {
            string   name = headers.GetHeaderStringValue(Header.APPLICATION_NAME, true);
            Resource icon = headers.GetHeaderResourceValue(Header.APPLICATION_ICON, false);

            Application app = new Application(name);

            app.Icon = icon;

            SetInhertiedAttributesFromHeaders(app, headers);

            return(app);
        }
Пример #7
0
        /// <summary>

        /// Creates a new <see cref="CallbackData"/> from a list of headers

        /// </summary>

        /// <param name="headers">The <see cref="HeaderCollection"/> used to populate the object</param>

        /// <returns><see cref="CallbackData"/></returns>

        public new static CallbackData FromHeaders(HeaderCollection headers)

        {
            try

            {
                CallbackDataBase baseObj = CallbackDataBase.FromHeaders(headers);

                CallbackResult result = CallbackResult.TIMEDOUT;



                string resultString = headers.GetHeaderStringValue(Header.NOTIFICATION_CALLBACK_RESULT, true);

                if (!String.IsNullOrEmpty(resultString))
                {
                    result = (CallbackResult)Enum.Parse(typeof(CallbackResult), resultString, true);
                }



                string notificationID = headers.GetHeaderStringValue(Header.NOTIFICATION_ID, false);



                CallbackData context = new CallbackData(baseObj.Data, baseObj.Type, result, notificationID);



                return(context);
            }

            catch

            {
                return(null);
            }
        }
        /// <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);
        }