forked from NTTLimitedRD/PowerShell.REST.API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericActionSelector.cs
92 lines (80 loc) · 3.42 KB
/
GenericActionSelector.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
namespace DynamicPowerShellApi
{
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Controllers;
using Controllers;
/// <summary>
/// An action selector to force our generic PowerShell invoker.
/// </summary>
public class GenericActionSelector
: IHttpActionSelector
{
/// <summary>
/// The current configuration from the http server.
/// </summary>
private readonly HttpConfiguration _currentConfiguration;
/// <summary>
/// Gets the action descriptor.
/// </summary>
private HttpActionDescriptor ActionDescriptor
{
get
{
return new ReflectedHttpActionDescriptor(
new HttpControllerDescriptor(_currentConfiguration, "generic", typeof(GenericController)),
typeof(GenericController).GetMethod("ProcessRequestAsync"));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="GenericActionSelector" /> class.
/// </summary>
/// <param name="configuration">The configuration of the http channel.</param>
public GenericActionSelector(HttpConfiguration configuration)
{
_currentConfiguration = configuration;
}
/// <summary>
/// Selects the action for the controller.
/// </summary>
/// <param name="controllerContext">The context of the controller.</param>
/// <returns>
/// The action for the controller.
/// </returns>
public HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
{
// if the user is requesting the server status..
if (controllerContext.Request.RequestUri.AbsolutePath == Constants.StatusUrlPath)
return new ReflectedHttpActionDescriptor(
new HttpControllerDescriptor(_currentConfiguration, "generic", typeof(GenericController)),
typeof(GenericController).GetMethod("Status"));
if (controllerContext.Request.RequestUri.AbsolutePath == Constants.JobListPath)
return new ReflectedHttpActionDescriptor(
new HttpControllerDescriptor(_currentConfiguration, "generic", typeof(GenericController)),
typeof(GenericController).GetMethod("AllJobStatus"));
if (controllerContext.Request.RequestUri.AbsolutePath == Constants.GetJobPath)
return new ReflectedHttpActionDescriptor(
new HttpControllerDescriptor(_currentConfiguration, "generic", typeof(GenericController)),
typeof(GenericController).GetMethod("GetJob"));
// Always give the same action
return ActionDescriptor;
}
/// <summary>
/// Returns a map, keyed by action string, of all <see cref="T:System.Web.Http.Controllers.HttpActionDescriptor" /> that the selector can select. This is primarily called by <see cref="T:System.Web.Http.Description.IApiExplorer" /> to discover all the possible actions in the controller.
/// </summary>
/// <param name="controllerDescriptor">The controller descriptor.</param>
/// <returns>
/// A map of <see cref="T:System.Web.Http.Controllers.HttpActionDescriptor" /> that the selector can select, or null if the selector does not have a well-defined mapping of <see cref="T:System.Web.Http.Controllers.HttpActionDescriptor" />.
/// </returns>
public ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor)
{
// Exercised only by ASP.NET Web API’s API explorer feature
List<HttpActionDescriptor> descriptors = new List<HttpActionDescriptor> { ActionDescriptor };
ILookup<string, HttpActionDescriptor> result = descriptors.ToLookup(
p => "generic",
p => p);
return result;
}
}
}