-
Notifications
You must be signed in to change notification settings - Fork 146
/
LinkRewritingFilter.cs
115 lines (97 loc) · 3.96 KB
/
LinkRewritingFilter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using BeautifulRestApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Routing;
namespace BeautifulRestApi.Infrastructure
{
public sealed class LinkRewritingFilter : IAsyncResultFilter
{
private readonly IUrlHelperFactory _urlHelperFactory;
public LinkRewritingFilter(IUrlHelperFactory urlHelperFactory)
{
_urlHelperFactory = urlHelperFactory;
}
public async Task OnResultExecutionAsync(
ResultExecutingContext context,
ResultExecutionDelegate next)
{
var asObjectResult = context.Result as ObjectResult;
var shouldSkip = asObjectResult?.Value == null || asObjectResult.StatusCode != (int)HttpStatusCode.OK;
if (shouldSkip)
{
await next();
return;
}
var rewriter = new LinkRewriter(_urlHelperFactory.GetUrlHelper(context));
RewriteAllLinks(asObjectResult.Value, rewriter);
await next();
}
private static void RewriteAllLinks(object model, LinkRewriter rewriter)
{
if (model == null) return;
var allProperties = model
.GetType().GetTypeInfo()
.GetAllProperties()
.Where(p => p.CanRead)
.ToArray();
var linkProperties = allProperties
.Where(p => p.CanWrite && typeof(Link).IsAssignableFrom(p.PropertyType));
foreach (var linkProperty in linkProperties)
{
var rewritten = rewriter.Rewrite(linkProperty.GetValue(model) as RouteLink);
if (rewritten == null) continue;
linkProperty.SetValue(model, rewritten);
// Special handling of the hidden Self property
if (linkProperty.Name == nameof(Resource.Self))
{
allProperties.SingleOrDefault(p => p.Name == nameof(Resource.Href))
?.SetValue(model, rewritten.Href);
allProperties.SingleOrDefault(p => p.Name == nameof(Resource.Method))
?.SetValue(model, rewritten.Method);
allProperties.SingleOrDefault(p => p.Name == nameof(Resource.Relations))
?.SetValue(model, rewritten.Relations);
}
}
var arrayProperties = allProperties.Where(p => p.PropertyType.IsArray);
RewriteLinksInArrays(arrayProperties, model, rewriter);
var objectProperties = allProperties.Except(linkProperties).Except(arrayProperties);
RewriteLinksInNestedObjects(objectProperties, model, rewriter);
}
private static void RewriteLinksInNestedObjects(
IEnumerable<PropertyInfo> objectProperties,
object obj,
LinkRewriter rewriter)
{
foreach (var objectProperty in objectProperties)
{
var shouldSkip = objectProperty.PropertyType == typeof(string);
if (shouldSkip) continue;
var typeInfo = objectProperty.PropertyType.GetTypeInfo();
if (typeInfo.IsClass)
{
RewriteAllLinks(objectProperty.GetValue(obj), rewriter);
}
}
}
private static void RewriteLinksInArrays(
IEnumerable<PropertyInfo> arrayProperties,
object obj,
LinkRewriter rewriter)
{
foreach (var arrayProperty in arrayProperties)
{
var array = arrayProperty.GetValue(obj) as Array ?? new Array[0];
foreach (var element in array)
{
RewriteAllLinks(element, rewriter);
}
}
}
}
}