2019-05-27 09:46:04 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2019-05-30 14:57:15 +00:00
|
|
|
|
using System.Security.Authentication;
|
2019-05-27 09:46:04 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2019-05-30 09:28:21 +00:00
|
|
|
|
namespace ASC.Api.Core.Middleware
|
2019-05-27 09:46:04 +00:00
|
|
|
|
{
|
|
|
|
|
public class ResponseWrapper
|
|
|
|
|
{
|
|
|
|
|
private readonly RequestDelegate next;
|
|
|
|
|
|
|
|
|
|
public ResponseWrapper(RequestDelegate next)
|
|
|
|
|
{
|
|
|
|
|
this.next = next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
|
{
|
2019-05-30 09:28:21 +00:00
|
|
|
|
Exception error = null;
|
2019-05-27 09:46:04 +00:00
|
|
|
|
var currentBody = context.Response.Body;
|
|
|
|
|
|
|
|
|
|
using var memoryStream = new MemoryStream();
|
2019-05-29 14:44:37 +00:00
|
|
|
|
|
2019-05-27 09:46:04 +00:00
|
|
|
|
context.Response.Body = memoryStream;
|
|
|
|
|
|
2019-05-30 09:28:21 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await next(context);
|
2019-05-31 10:47:47 +00:00
|
|
|
|
|
|
|
|
|
if(context.Response.StatusCode == 401)
|
|
|
|
|
{
|
|
|
|
|
error = new AuthenticationException(HttpStatusCode.Unauthorized.ToString());
|
|
|
|
|
}
|
2019-05-30 09:28:21 +00:00
|
|
|
|
}
|
2019-05-30 14:57:15 +00:00
|
|
|
|
catch(AuthenticationException exception)
|
|
|
|
|
{
|
|
|
|
|
context.Response.StatusCode = 401;
|
|
|
|
|
error = exception;
|
|
|
|
|
}
|
2019-05-30 09:28:21 +00:00
|
|
|
|
catch(Exception exception)
|
|
|
|
|
{
|
|
|
|
|
context.Response.StatusCode = 500;
|
|
|
|
|
error = exception;
|
|
|
|
|
}
|
2019-05-27 09:46:04 +00:00
|
|
|
|
|
|
|
|
|
context.Response.Body = currentBody;
|
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
ResponseParser responseParser;
|
|
|
|
|
|
|
|
|
|
switch (context.Request.RouteValues["format"])
|
|
|
|
|
{
|
|
|
|
|
case "xml":
|
|
|
|
|
responseParser = new XmlResponseParser();
|
|
|
|
|
break;
|
|
|
|
|
case "json":
|
|
|
|
|
default:
|
|
|
|
|
responseParser = new JsonResponseParser();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var readToEnd = new StreamReader(memoryStream).ReadToEnd();
|
2019-05-30 14:57:15 +00:00
|
|
|
|
await context.Response.WriteAsync(responseParser.WrapAndWrite((HttpStatusCode)context.Response.StatusCode, readToEnd, error));
|
2019-05-27 09:46:04 +00:00
|
|
|
|
}
|
2019-05-30 09:28:21 +00:00
|
|
|
|
|
2019-05-27 09:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 09:28:21 +00:00
|
|
|
|
public static class ResponseWrapperExtensions
|
2019-05-27 09:46:04 +00:00
|
|
|
|
{
|
2019-05-30 09:28:21 +00:00
|
|
|
|
public static IApplicationBuilder UseResponseWrapper(this IApplicationBuilder builder)
|
2019-05-27 09:46:04 +00:00
|
|
|
|
{
|
2019-05-30 09:28:21 +00:00
|
|
|
|
return builder.UseMiddleware<ResponseWrapper>();
|
2019-05-27 09:46:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|