/
MessageRedirectsController.cs
213 lines (171 loc) · 7.55 KB
/
MessageRedirectsController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
namespace ServiceControl.MessageRedirects.Api
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Contracts.MessageRedirects;
using Infrastructure.DomainEvents;
using Infrastructure.WebApi;
using MessageFailures.InternalMessages;
using Microsoft.AspNetCore.Mvc;
using NServiceBus;
using ServiceControl.Persistence.Infrastructure;
using ServiceControl.Persistence.MessageRedirects;
using DeterministicGuid = Infrastructure.DeterministicGuid;
[ApiController]
[Route("api")]
public class MessageRedirectsController(
IMessageSession session,
IMessageRedirectsDataStore store,
IDomainEvents events)
: ControllerBase
{
[Route("redirects")]
[HttpPost]
public async Task<IActionResult> NewRedirects(MessageRedirectRequest request)
{
if (string.IsNullOrWhiteSpace(request.FromPhysicalAddress) || string.IsNullOrWhiteSpace(request.ToPhysicalAddress))
{
return BadRequest();
}
var messageRedirect = new MessageRedirect
{
FromPhysicalAddress = request.FromPhysicalAddress,
ToPhysicalAddress = request.ToPhysicalAddress,
LastModifiedTicks = DateTime.UtcNow.Ticks
};
var collection = await store.GetOrCreate();
var existing = collection[messageRedirect.MessageRedirectId];
if (existing != null)
{
if (existing.ToPhysicalAddress == messageRedirect.ToPhysicalAddress)
{
return StatusCode((int)HttpStatusCode.Created, messageRedirect);
}
// Setting the ReasonPhrase is no longer supported in HTTP/2. We could be using application/problem+json instead
// but that would require a lot of changes in the client code. For now, we are using the X-Particular-Reason header
Response.Headers["X-Particular-Reason"] = "Duplicate";
return StatusCode((int)HttpStatusCode.Conflict, existing);
}
var dependents = collection.Redirects.Where(r => r.ToPhysicalAddress == request.FromPhysicalAddress).ToList();
if (dependents.Any())
{
// Setting the ReasonPhrase is no longer supported in HTTP/2. We could be using application/problem+json instead
// but that would require a lot of changes in the client code. For now, we are using the X-Particular-Reason header
Response.Headers["X-Particular-Reason"] = "Dependents";
return StatusCode((int)HttpStatusCode.Conflict, dependents);
}
collection.Redirects.Add(messageRedirect);
await store.Save(collection);
await events.Raise(new MessageRedirectCreated
{
MessageRedirectId = messageRedirect.MessageRedirectId,
FromPhysicalAddress = messageRedirect.FromPhysicalAddress,
ToPhysicalAddress = messageRedirect.ToPhysicalAddress
});
if (request.RetryExisting)
{
await session.SendLocal(new RetryPendingMessages
{
QueueAddress = messageRedirect.FromPhysicalAddress,
PeriodFrom = DateTime.MinValue,
PeriodTo = DateTime.UtcNow
});
}
// not using Created here because that would be turned by the HttpNoContentOutputFormatter into a 204
return StatusCode((int)HttpStatusCode.Created, messageRedirect);
}
[Route("redirects/{messageRedirectId:guid}")]
[HttpPut]
public async Task<IActionResult> UpdateRedirect(Guid messageRedirectId, MessageRedirectRequest request)
{
if (string.IsNullOrWhiteSpace(request.ToPhysicalAddress))
{
return BadRequest();
}
var redirects = await store.GetOrCreate();
var messageRedirect = redirects[messageRedirectId];
if (messageRedirect == null)
{
return NotFound();
}
var toMessageRedirectId = DeterministicGuid.MakeId(request.ToPhysicalAddress);
if (redirects[toMessageRedirectId] != null)
{
return Conflict();
}
var messageRedirectChanged = new MessageRedirectChanged
{
MessageRedirectId = messageRedirectId,
PreviousToPhysicalAddress = messageRedirect.ToPhysicalAddress,
FromPhysicalAddress = messageRedirect.FromPhysicalAddress,
ToPhysicalAddress = messageRedirect.ToPhysicalAddress = request.ToPhysicalAddress
};
messageRedirect.LastModifiedTicks = DateTime.UtcNow.Ticks;
await store.Save(redirects);
await events.Raise(messageRedirectChanged);
return NoContent();
}
[Route("redirects/{messageRedirectId:guid}")]
[HttpDelete]
public async Task<IActionResult> DeleteRedirect(Guid messageRedirectId)
{
var redirects = await store.GetOrCreate();
var messageRedirect = redirects[messageRedirectId];
if (messageRedirect == null)
{
return NoContent();
}
redirects.Redirects.Remove(messageRedirect);
await store.Save(redirects);
await events.Raise(new MessageRedirectRemoved
{
MessageRedirectId = messageRedirectId,
FromPhysicalAddress = messageRedirect.FromPhysicalAddress,
ToPhysicalAddress = messageRedirect.ToPhysicalAddress
});
return NoContent();
}
[Route("redirect")]
[HttpHead]
public async Task CountRedirects()
{
var redirects = await store.GetOrCreate();
Response.WithEtag(redirects.ETag);
Response.WithTotalCount(redirects.Redirects.Count);
}
[Route("redirects")]
[HttpGet]
public async Task<IEnumerable<RedirectsQueryResult>> Redirects(string sort, string direction, [FromQuery] PagingInfo pagingInfo)
{
var redirects = await store.GetOrCreate();
var queryResult = redirects
.Sort(sort, direction)
.Paging(pagingInfo)
.Select(r => new RedirectsQueryResult
(
r.MessageRedirectId,
r.FromPhysicalAddress,
r.ToPhysicalAddress,
new DateTime(r.LastModifiedTicks)
));
Response.WithEtag(redirects.ETag);
Response.WithPagingLinksAndTotalCount(pagingInfo, redirects.Redirects.Count);
return queryResult;
}
public record RedirectsQueryResult(Guid MessageRedirectId, string FromPhysicalAddress, string ToPhysicalAddress, DateTime LastModified);
// Input models from the API are for some odd reasons not snake cased
public class MessageRedirectRequest
{
[JsonPropertyName("fromphysicaladdress")]
public string FromPhysicalAddress { get; set; }
[JsonPropertyName("tophysicaladdress")]
public string ToPhysicalAddress { get; set; }
[JsonPropertyName("retryexisting")]
public bool RetryExisting { get; set; }
}
}
}