/// <summary> /// Called when a GNTP request is successfully parsed and no errors are found. /// </summary> /// <param name="request">The <see cref="GNTPRequest"/>.</param> protected void OnMessageParsed(GNTPRequest request) { if (this.MessageParsed != null) { this.MessageParsed(request); } }
/// <summary> /// Handles the requestReader's <see cref="GNTPRequestReader.MessageParsed"/> event /// </summary> /// <param name="request">The parsed <see cref="GNTPRequest"/></param> void requestReader_MessageParsed(GNTPRequest request) { this.request = request; OnMessageParsed(this.socket); }
/// <summary> /// Called when the request is successfully parsed. /// </summary> private void OnMessageParsed() { // handle callback context CallbackContext context = null; if (!String.IsNullOrEmpty(this.callbackData) && !String.IsNullOrEmpty(this.callbackDataType) && String.IsNullOrEmpty(this.callbackUrl)) { context = new CallbackContext(this.callbackData, this.callbackDataType); } else if (!String.IsNullOrEmpty(this.callbackUrl)) { context = new CallbackContext(this.callbackUrl); } if (this.MessageParsed != null) { GNTPRequest request = new GNTPRequest(this.version, this.directive, this.key, this.headers, this.applicationName, this.notificationsToBeRegistered, context); this.MessageParsed(request); } else { // no handler - return some kind of error? (this should never really happen) OnError(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } }
/// <summary> /// Handles the parser's <see cref="GNTPParser.MessageParsed"/> event /// </summary> /// <param name="request">The parsed <see cref="GNTPRequest"/></param> void parser_MessageParsed(GNTPRequest request) { CleanUp(); this.DecryptedData = parser.DecryptedRequest; this.OnMessageParsed(request); }
/// <summary> /// Writes back the GNTP response to the requesting application /// </summary> /// <param name="cbInfo">The <see cref="CallbackInfo"/> associated with the response</param> /// <param name="response">The <see cref="Response"/> to be written back</param> public void WriteResponse(CallbackInfo cbInfo, Response response) { if (!cbInfo.AlreadyResponded) { cbInfo.AlreadyResponded = true; MessageHandler mh = cbInfo.MessageHandler; GNTPRequest request = mh.Request; ResponseType responseType = ResponseType.ERROR; if (response != null) { if (response.IsCallback) { responseType = ResponseType.CALLBACK; } else if (response.IsOK) { responseType = ResponseType.OK; } } else { response = new Response(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } if (cbInfo.AdditionalInfo != null) { foreach (KeyValuePair <string, string> item in cbInfo.AdditionalInfo) { response.CustomTextAttributes.Add(item.Key, item.Value); } } AddServerHeaders(response); MessageBuilder mb = new MessageBuilder(responseType); HeaderCollection responseHeaders = response.ToHeaders(); foreach (Header header in responseHeaders) { mb.AddHeader(header); } // return any application-specific data headers that were received RequestData rd = RequestData.FromHeaders(request.Headers); AddRequestData(mb, rd); mh.WriteResponse(mb, true); } }
/// <summary> /// Called when the request is successfully parsed. /// </summary> private void OnMessageParsed() { // handle callback context CallbackContext context = null; if (!String.IsNullOrEmpty(this.callbackData) && !String.IsNullOrEmpty(this.callbackDataType) && String.IsNullOrEmpty(this.callbackUrl)) context = new CallbackContext(this.callbackData, this.callbackDataType); else if (!String.IsNullOrEmpty(this.callbackUrl)) context = new CallbackContext(this.callbackUrl); if (this.MessageParsed != null) { GNTPRequest request = new GNTPRequest(this.version, this.directive, this.key, this.headers, this.applicationName, this.notificationsToBeRegistered, context); this.MessageParsed(request); } else { // no handler - return some kind of error? (this should never really happen) OnError(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } }
/// <summary> /// Handles the parsed message after it is received /// </summary> /// <param name="obj">The <see cref="MessageHandler"/> object that parsed the message</param> private void HandleParsedMessage(object obj) { MessageHandler mh = (MessageHandler)obj; GNTPRequest request = mh.Request; try { Response response = null; switch (request.Directive) { case RequestType.REGISTER: Application application = Application.FromHeaders(request.Headers); List <NotificationType> notificationTypes = new List <NotificationType>(); for (int i = 0; i < request.NotificationsToBeRegistered.Count; i++) { HeaderCollection headers = request.NotificationsToBeRegistered[i]; notificationTypes.Add(NotificationType.FromHeaders(headers)); } response = this.OnRegisterReceived(application, notificationTypes, mh.RequestInfo); break; case RequestType.NOTIFY: Notification notification = Notification.FromHeaders(request.Headers); mh.CallbackInfo.NotificationID = notification.ID; response = this.OnNotifyReceived(notification, mh.CallbackInfo, mh.RequestInfo); break; case RequestType.SUBSCRIBE: Subscriber subscriber = Subscriber.FromHeaders(request.Headers); subscriber.IPAddress = mh.Socket.RemoteAddress.ToString(); subscriber.Key = new SubscriberKey(request.Key, subscriber.ID, request.Key.HashAlgorithm, request.Key.EncryptionAlgorithm); response = this.OnSubscribeReceived(subscriber, mh.RequestInfo); break; } ResponseType responseType = ResponseType.ERROR; if (response != null && response.IsOK) { responseType = ResponseType.OK; response.InResponseTo = request.Directive.ToString(); } // no response if (response == null) { response = new Response(ErrorCode.INTERNAL_SERVER_ERROR, ErrorDescription.INTERNAL_SERVER_ERROR); } AddServerHeaders(response); MessageBuilder mb = new MessageBuilder(responseType); HeaderCollection responseHeaders = response.ToHeaders(); foreach (Header header in responseHeaders) { mb.AddHeader(header); } // return any application-specific data headers that were received RequestData rd = RequestData.FromHeaders(request.Headers); AddRequestData(mb, rd); bool requestComplete = !mh.CallbackInfo.ShouldKeepConnectionOpen(); mh.WriteResponse(mb, requestComplete); } catch (GrowlException gEx) { mh.WriteError(gEx.ErrorCode, gEx.Message, gEx.AdditionalInfo); } catch (Exception ex) { mh.WriteError(ErrorCode.INTERNAL_SERVER_ERROR, ex.Message); } }