Describes one or more event notifications received as an Outbound Message from Salesforce. For details about Salesforce Outbound Messages, see https://help.salesforce.com/htviewhelpdoc?id=workflow_defining_outbound_messages.htm.
        public void GetLocalName_Returns_ExpectedName(string qualified, string expected)
        {
            // Act
            string actual = SalesforceNotifications.GetLocalName(qualified);

            // Assert
            Assert.Equal(expected, actual);
        }
        public SalesforceNotificationsTests()
        {
            _doc1           = EmbeddedResource.ReadAsJXElement("Microsoft.AspNet.WebHooks.Messages.OutboundMessage1.xml");
            _notifications1 = new SalesforceNotifications(_doc1);

            _doc2           = EmbeddedResource.ReadAsJXElement("Microsoft.AspNet.WebHooks.Messages.OutboundMessage2.xml");
            _notifications2 = new SalesforceNotifications(_doc2);
        }
        public SalesforceNotificationsTests()
        {
            string msg1 = ReadResource("Microsoft.AspNet.WebHooks.Messages.OutboundMessage1.xml");
            _doc1 = XElement.Parse(msg1);
            _notifications1 = new SalesforceNotifications(_doc1);

            string msg2 = ReadResource("Microsoft.AspNet.WebHooks.Messages.OutboundMessage2.xml");
            _doc2 = XElement.Parse(msg2);
            _notifications2 = new SalesforceNotifications(_doc2);
        }
Example #4
0
        public SalesforceNotificationsTests()
        {
            string msg1 = ReadResource("Microsoft.AspNet.WebHooks.Messages.OutboundMessage1.xml");

            _doc1           = XElement.Parse(msg1);
            _notifications1 = new SalesforceNotifications(_doc1);

            string msg2 = ReadResource("Microsoft.AspNet.WebHooks.Messages.OutboundMessage2.xml");

            _doc2           = XElement.Parse(msg2);
            _notifications2 = new SalesforceNotifications(_doc2);
        }
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                var data = await ReadAsXmlAsync(request);

                var notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                var orgId  = GetShortOrgId(notifications.OrganizationId);
                var secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);

                var secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault     = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var invalidId = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(invalidId);
                }

                // Get the action
                var action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault   = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var badType = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(badType);
                }

                // Call registered handlers
                var response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response content if not already present or isn't XML. Ignore current (e.g. JSON) content.
                if (response?.Content == null || !response.Content.IsXml())
                {
                    // Ignore redirects because SOAP 1.1 doesn't mention them and they're corner cases in SOAP.
                    var statusCode = response?.StatusCode ?? HttpStatusCode.OK;
                    if (statusCode >= (HttpStatusCode)200 && statusCode < (HttpStatusCode)300)
                    {
                        var success = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                        response = GetXmlResponse(request, statusCode, success);
                    }
                    else
                    {
                        // Move failure information into a SOAP fault response. Fault contains code soapenv:Client and
                        // that must be transmitted with HTTP status 400, Bad Request according to SOAP 1.2 (mixing
                        // that sensible choice into this SOAP 1.1 implementation).
                        var resource    = ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml");
                        var faultString = string.Format(
                            CultureInfo.CurrentCulture,
                            SalesforceReceiverResources.Receiver_HandlerFailed,
                            statusCode,
                            response.ReasonPhrase);

                        var failure = string.Format(CultureInfo.InvariantCulture, resource, faultString);
                        response = GetXmlResponse(request, HttpStatusCode.BadRequest, failure);
                    }
                }

                return(response);
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
        /// <inheritdoc />
        public override async Task<HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                XElement data = await ReadAsXmlAsync(request);
                SalesforceNotifications notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                string orgId = GetShortOrgId(notifications.OrganizationId);
                string secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);
                string secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage invalidId = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return invalidId;
                }

                // Get the action
                string action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage badType = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return badType;
                }

                // Call registered handlers
                HttpResponseMessage response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response if not already present
                if (response == null || response.Content == null || !response.Content.IsXml())
                {
                    response = request.CreateResponse();
                    string ack = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                    response.Content = new StringContent(ack, Encoding.UTF8, "application/xml");
                }
                return response;
            }
            else
            {
                return CreateBadMethodResponse(request);
            }
        }
Example #7
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                var data = await ReadAsXmlAsync(request);

                var notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                var orgId  = GetShortOrgId(notifications.OrganizationId);
                var secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);

                var secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault     = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var invalidId = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(invalidId);
                }

                // Get the action
                var action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    var message = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(message);
                    var fault   = string.Format(CultureInfo.InvariantCulture, ReadResource("Microsoft.AspNet.WebHooks.Messages.FaultResponse.xml"), message);
                    var badType = GetXmlResponse(request, HttpStatusCode.BadRequest, fault);
                    return(badType);
                }

                // Call registered handlers
                var response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response if not already present
                if (response == null || response.Content == null || !response.Content.IsXml())
                {
                    var success = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                    response = GetXmlResponse(request, HttpStatusCode.OK, success);
                }
                return(response);
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }
Example #8
0
        /// <inheritdoc />
        public override async Task <HttpResponseMessage> ReceiveAsync(string id, HttpRequestContext context, HttpRequestMessage request)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.Method == HttpMethod.Post)
            {
                EnsureSecureConnection(request);

                // Read the request entity body
                XElement data = await ReadAsXmlAsync(request);

                SalesforceNotifications notifications = new SalesforceNotifications(data);

                // Ensure that the organization ID matches the expected value.
                string orgId  = GetShortOrgId(notifications.OrganizationId);
                string secret = await GetReceiverConfig(request, ReceiverConfigName, id, 15, 18);

                string secretKey = GetShortOrgId(secret);
                if (!WebHookReceiver.SecretEqual(orgId, secretKey))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadValue, "OrganizationId");
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage invalidId = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return(invalidId);
                }

                // Get the action
                string action = notifications.ActionId;
                if (string.IsNullOrEmpty(action))
                {
                    string msg = string.Format(CultureInfo.CurrentCulture, SalesforceReceiverResources.Receiver_BadBody, "ActionId");
                    context.Configuration.DependencyResolver.GetLogger().Error(msg);
                    HttpResponseMessage badType = request.CreateErrorResponse(HttpStatusCode.BadRequest, msg);
                    return(badType);
                }

                // Call registered handlers
                HttpResponseMessage response = await ExecuteWebHookAsync(id, context, request, new string[] { action }, notifications);

                // Add SOAP response if not already present
                if (response == null || response.Content == null || !response.Content.IsXml())
                {
                    response = request.CreateResponse();
                    string ack = ReadResource("Microsoft.AspNet.WebHooks.Messages.NotificationResponse.xml");
                    response.Content = new StringContent(ack, Encoding.UTF8, "application/xml");
                }
                return(response);
            }
            else
            {
                return(CreateBadMethodResponse(request));
            }
        }