• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / botbuilder-dotnet / 363877

10 Aug 2023 08:52PM UTC coverage: 79.092% (+0.1%) from 78.979%
363877

Pull #6655

CI-PR build

web-flow
Merge 94ad1d11f into fdaed8b69
Pull Request #6655: Implementation of Teams batch APIs

26094 of 32992 relevant lines covered (79.09%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.65
/libraries/integration/Microsoft.Bot.Builder.Integration.AspNet.Core/BotMessageHandlerBase.cs
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
using System;
5
using System.IO;
6
using System.Net;
7
using System.Text;
8
using System.Threading;
9
using System.Threading.Tasks;
10
using Microsoft.AspNetCore.Http;
11
using Microsoft.Extensions.DependencyInjection;
12
using Microsoft.Net.Http.Headers;
13
using Newtonsoft.Json;
14

15
namespace Microsoft.Bot.Builder.Integration.AspNet.Core.Handlers
16
{
17
    /// <summary>
18
    /// Abstract base class for a bot message handler.
19
    /// </summary>
20
    /// <remarks>
21
    /// BotFrameworkAdapter is still supported but the recommended adapter is `CloudAdapter`.
22
    /// </remarks>
23
    public abstract class BotMessageHandlerBase
24
    {
25
        /// <summary>
26
        /// A <see cref="JsonSerializer"/> for use when serializing bot messages.
27
        /// </summary>
28
        public static readonly JsonSerializer BotMessageSerializer = JsonSerializer.Create(MessageSerializerSettings.Create());
×
29

30
        /// <summary>
31
        /// Initializes a new instance of the <see cref="BotMessageHandlerBase"/> class.
32
        /// </summary>
33
        public BotMessageHandlerBase()
1✔
34
        {
35
        }
1✔
36

37
        /// <summary>
38
        /// Handles common behavior for handling requests, including checking valid request method and content type.
39
        /// Processes the request using the registered adapter and bot and writes the result to the response on the <see cref="HttpContext"/>.
40
        /// </summary>
41
        /// <param name="httpContext">The <see cref="HttpContext"/>.</param>
42
        /// <returns>A Task that represents the work to be executed.</returns>
43
        public async Task HandleAsync(HttpContext httpContext)
44
        {
45
            var request = httpContext.Request;
×
46
            var response = httpContext.Response;
×
47

48
            if (request.Method != HttpMethods.Post)
×
49
            {
50
                response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
×
51

52
                return;
×
53
            }
54

55
            if (request.ContentLength == 0)
×
56
            {
57
                response.StatusCode = (int)HttpStatusCode.BadRequest;
×
58

59
                return;
×
60
            }
61

62
            if (!MediaTypeHeaderValue.TryParse(request.ContentType, out var mediaTypeHeaderValue)
×
63
                    ||
×
64
                mediaTypeHeaderValue.MediaType != "application/json")
×
65
            {
66
                response.StatusCode = (int)HttpStatusCode.NotAcceptable;
×
67

68
                return;
×
69
            }
70

71
            var requestServices = httpContext.RequestServices;
×
72
            var adapter = requestServices.GetRequiredService<IAdapterIntegration>();
×
73
            var bot = requestServices.GetRequiredService<IBot>();
×
74

75
            try
76
            {
77
                var invokeResponse = await ProcessMessageRequestAsync(
×
78
                    request,
×
79
                    adapter,
×
80
                    bot.OnTurnAsync,
×
81
                    default(CancellationToken)).ConfigureAwait(false);
×
82

83
                if (invokeResponse == null)
×
84
                {
85
                    response.StatusCode = (int)HttpStatusCode.OK;
×
86
                }
87
                else
88
                {
89
                    response.StatusCode = invokeResponse.Status;
×
90

91
                    if (invokeResponse.Body != null)
×
92
                    {
93
                        response.ContentType = "application/json";
×
94
                        using (var memoryStream = new MemoryStream())
×
95
                        {
96
                            using (var writer = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
×
97
                            {
98
                                using (var jsonWriter = new JsonTextWriter(writer))
×
99
                                {
100
                                    BotMessageSerializer.Serialize(jsonWriter, invokeResponse.Body);
×
101
                                }
×
102
                            }
×
103

104
                            memoryStream.Seek(0, SeekOrigin.Begin);
×
105
                            await memoryStream.CopyToAsync(response.Body).ConfigureAwait(false);
×
106
                        }
×
107
                    }
108
                }
109
            }
×
110
            catch (UnauthorizedAccessException)
×
111
            {
112
                response.StatusCode = (int)HttpStatusCode.Forbidden;
×
113
            }
×
114
        }
×
115

116
        /// <summary>
117
        /// Abstract method to process the incoming request using the registered adapter and bot and
118
        /// to return an <see cref="InvokeResponse"/>.
119
        /// </summary>
120
        /// <param name="request">A <see cref="HttpRequest"/>.</param>
121
        /// <param name="adapter">An instance of <see cref="IAdapterIntegration"/>.</param>
122
        /// <param name="botCallbackHandler">An instance of <see cref="BotCallbackHandler"/>.</param>
123
        /// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
124
        /// <returns>An <see cref="InvokeResponse"/> returned from the adapter.</returns>
125
        protected abstract Task<InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, IAdapterIntegration adapter, BotCallbackHandler botCallbackHandler, CancellationToken cancellationToken);
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc