This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ObjectResultExecutor.cs
134 lines (116 loc) · 4.89 KB
/
ObjectResultExecutor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
/// <summary>
/// Executes an <see cref="ObjectResult"/> to write to the response.
/// </summary>
public class ObjectResultExecutor : IActionResultExecutor<ObjectResult>
{
/// <summary>
/// Creates a new <see cref="ObjectResultExecutor"/>.
/// </summary>
/// <param name="formatterSelector">The <see cref="OutputFormatterSelector"/>.</param>
/// <param name="writerFactory">The <see cref="IHttpResponseStreamWriterFactory"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public ObjectResultExecutor(
OutputFormatterSelector formatterSelector,
IHttpResponseStreamWriterFactory writerFactory,
ILoggerFactory loggerFactory)
{
if (formatterSelector == null)
{
throw new ArgumentNullException(nameof(formatterSelector));
}
if (writerFactory == null)
{
throw new ArgumentNullException(nameof(writerFactory));
}
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
FormatterSelector = formatterSelector;
WriterFactory = writerFactory.CreateWriter;
Logger = loggerFactory.CreateLogger<ObjectResultExecutor>();
}
/// <summary>
/// Gets the <see cref="ILogger"/>.
/// </summary>
protected ILogger Logger { get; }
/// <summary>
/// Gets the <see cref="OutputFormatterSelector"/>.
/// </summary>
protected OutputFormatterSelector FormatterSelector { get; }
/// <summary>
/// Gets the writer factory delegate.
/// </summary>
protected Func<Stream, Encoding, TextWriter> WriterFactory { get; }
/// <summary>
/// Executes the <see cref="ObjectResult"/>.
/// </summary>
/// <param name="context">The <see cref="ActionContext"/> for the current request.</param>
/// <param name="result">The <see cref="ObjectResult"/>.</param>
/// <returns>
/// A <see cref="Task"/> which will complete once the <see cref="ObjectResult"/> is written to the response.
/// </returns>
public virtual Task ExecuteAsync(ActionContext context, ObjectResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
// If the user sets the content type both on the ObjectResult (example: by Produces) and Response object,
// then the one set on ObjectResult takes precedence over the Response object
if (result.ContentTypes == null || result.ContentTypes.Count == 0)
{
var responseContentType = context.HttpContext.Response.ContentType;
if (!string.IsNullOrEmpty(responseContentType))
{
if (result.ContentTypes == null)
{
result.ContentTypes = new MediaTypeCollection();
}
result.ContentTypes.Add(responseContentType);
}
}
var objectType = result.DeclaredType;
if (objectType == null || objectType == typeof(object))
{
objectType = result.Value?.GetType();
}
var formatterContext = new OutputFormatterWriteContext(
context.HttpContext,
WriterFactory,
objectType,
result.Value);
var selectedFormatter = FormatterSelector.SelectFormatter(
formatterContext,
(IList<IOutputFormatter>)result.Formatters ?? Array.Empty<IOutputFormatter>(),
result.ContentTypes);
if (selectedFormatter == null)
{
// No formatter supports this.
Logger.NoFormatter(formatterContext);
context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
return Task.CompletedTask;
}
Logger.ObjectResultExecuting(result.Value);
result.OnFormatting(context);
return selectedFormatter.WriteAsync(formatterContext);
}
}
}