2013-11-07 17:16:22 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
2013-12-02 17:20:50 +11:00
|
|
|
|
using System.Net.Http.Headers;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
|
using System.Web.Http.ModelBinding;
|
2016-06-27 18:27:49 +02:00
|
|
|
|
using System.Web.Http.Results;
|
2015-02-20 14:17:28 +01:00
|
|
|
|
using Microsoft.Owin;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
using Umbraco.Core;
|
2015-11-09 18:34:14 +01:00
|
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
2013-11-07 17:16:22 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.WebApi
|
|
|
|
|
|
{
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
public static class HttpRequestMessageExtensions
|
|
|
|
|
|
{
|
2015-02-20 14:17:28 +01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Borrowed from the latest Microsoft.AspNet.WebApi.Owin package which we cannot use because of a later webapi dependency
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
internal static Attempt<IOwinContext> TryGetOwinContext(this HttpRequestMessage request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var httpContext = request.TryGetHttpContext();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
return httpContext
|
|
|
|
|
|
? Attempt.Succeed(httpContext.Result.GetOwinContext())
|
2015-02-20 14:17:28 +01:00
|
|
|
|
: Attempt<IOwinContext>.Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// <summary>
|
2014-03-25 20:25:24 -07:00
|
|
|
|
/// Tries to retrieve the current HttpContext if one exists.
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static Attempt<HttpContextBase> TryGetHttpContext(this HttpRequestMessage request)
|
|
|
|
|
|
{
|
|
|
|
|
|
object context;
|
|
|
|
|
|
if (request.Properties.TryGetValue("MS_HttpContext", out context))
|
|
|
|
|
|
{
|
|
|
|
|
|
var httpContext = context as HttpContextBase;
|
|
|
|
|
|
if (httpContext != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Attempt.Succeed(httpContext);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (HttpContext.Current != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Attempt<HttpContextBase>.Succeed(new HttpContextWrapper(HttpContext.Current));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Attempt<HttpContextBase>.Fail();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a 403 (Forbidden) response indicating that hte current user doesn't have access to the resource
|
|
|
|
|
|
/// requested or the action it needs to take.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This is different from a 401 which indicates that the user is not logged in.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public static HttpResponseMessage CreateUserNoAccessResponse(this HttpRequestMessage request)
|
|
|
|
|
|
{
|
|
|
|
|
|
return request.CreateResponse(HttpStatusCode.Forbidden);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a 400 response message indicating that a validation error occurred
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateValidationErrorResponse<T>(this HttpRequestMessage request, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = request.CreateResponse(HttpStatusCode.BadRequest, value);
|
|
|
|
|
|
msg.Headers.Add("X-Status-Reason", "Validation failed");
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a 400 response message indicating that a validation error occurred
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateValidationErrorResponse(this HttpRequestMessage request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = request.CreateResponse(HttpStatusCode.BadRequest);
|
|
|
|
|
|
msg.Headers.Add("X-Status-Reason", "Validation failed");
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a 400 response message indicating that a validation error occurred
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="errorMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateValidationErrorResponse(this HttpRequestMessage request, string errorMessage)
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage);
|
|
|
|
|
|
msg.Headers.Add("X-Status-Reason", "Validation failed");
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-09 18:34:14 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates an error response with notifications in the result to be displayed in the UI
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="errorMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateNotificationValidationErrorResponse(this HttpRequestMessage request, string errorMessage)
|
|
|
|
|
|
{
|
|
|
|
|
|
var notificationModel = new SimpleNotificationModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Message = errorMessage
|
|
|
|
|
|
};
|
|
|
|
|
|
notificationModel.AddErrorNotification(errorMessage, string.Empty);
|
|
|
|
|
|
return request.CreateValidationErrorResponse(notificationModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-27 18:27:49 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a succressful response with notifications in the result to be displayed in the UI
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="successMessage"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateNotificationSuccessResponse(this HttpRequestMessage request, string successMessage)
|
|
|
|
|
|
{
|
|
|
|
|
|
var notificationModel = new SimpleNotificationModel
|
|
|
|
|
|
{
|
|
|
|
|
|
Message = successMessage
|
|
|
|
|
|
};
|
|
|
|
|
|
notificationModel.AddSuccessNotification(successMessage, string.Empty);
|
|
|
|
|
|
return request.CreateResponse(HttpStatusCode.OK, notificationModel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-07 17:16:22 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a 400 response message indicating that a validation error occurred
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <param name="modelState"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static HttpResponseMessage CreateValidationErrorResponse(this HttpRequestMessage request, ModelStateDictionary modelState)
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = request.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
|
|
|
|
|
|
msg.Headers.Add("X-Status-Reason", "Validation failed");
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|