-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMetadataFeature.cs
More file actions
250 lines (214 loc) · 8.32 KB
/
Copy pathMetadataFeature.cs
File metadata and controls
250 lines (214 loc) · 8.32 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#if NETSTANDARD2_0
using ServiceStack.Host;
#else
using System.Web;
#endif
using System;
using System.Collections.Generic;
using ServiceStack.Host.Handlers;
using ServiceStack.Metadata;
using ServiceStack.NativeTypes;
namespace ServiceStack
{
public class MetadataFeature : IPlugin, Model.IHasStringId
{
public string Id { get; set; } = Plugins.Metadata;
public string PluginLinksTitle { get; set; }
public Dictionary<string, string> PluginLinks { get; set; }
public string DebugLinksTitle { get; set; }
public Dictionary<string, string> DebugLinks { get; set; }
public Action<IndexOperationsControl> IndexPageFilter { get; set; }
public Action<OperationControl> DetailPageFilter { get; set; }
public List<Action<AppMetadata>> AppMetadataFilters { get; } = new List<Action<AppMetadata>>();
public bool ShowResponseStatusInMetadataPages { get; set; }
/// <summary>
/// Export built-in Types so they're available from /metadata/app
/// </summary>
public List<Type> ExportTypes { get; } = new List<Type> {
};
public Dictionary<Type, string[]> ServiceRoutes { get; set; } = new Dictionary<Type, string[]> {
{ typeof(MetadataAppService), new[]
{
"/" + "metadata".Localize() + "/" + "app".Localize(),
} },
{ typeof(MetadataNavService), new[] {
"/" + "metadata".Localize() + "/" + "nav".Localize(),
"/" + "metadata".Localize() + "/" + "nav".Localize() + "/{Name}",
} },
};
public bool EnableNav
{
get => ServiceRoutes.ContainsKey(typeof(MetadataNavService));
set
{
if (!value)
ServiceRoutes.Remove(typeof(MetadataNavService));
}
}
public bool EnableAppMetadata
{
get => ServiceRoutes.ContainsKey(typeof(MetadataAppService));
set
{
if (!value)
ServiceRoutes.Remove(typeof(MetadataAppService));
}
}
public MetadataFeature()
{
PluginLinksTitle = "Plugin Links:";
PluginLinks = new Dictionary<string, string>();
DebugLinksTitle = "Debug Info:";
DebugLinks = new Dictionary<string, string> {
{"operations/metadata", "Operations Metadata"},
};
}
public void Register(IAppHost appHost)
{
appHost.CatchAllHandlers.Add(ProcessRequest);
if (EnableNav)
{
ViewUtils.Load(appHost.AppSettings);
}
appHost.RegisterServices(ServiceRoutes);
}
public virtual IHttpHandler ProcessRequest(string httpMethod, string pathInfo, string filePath)
{
var pathParts = pathInfo.TrimStart('/').Split('/');
if (pathParts.Length == 0) return null;
return GetHandlerForPathParts(pathParts);
}
private IHttpHandler GetHandlerForPathParts(string[] pathParts)
{
var pathController = pathParts[0].ToLowerInvariant();
if (pathParts.Length == 1)
{
if (pathController == "metadata")
return new IndexMetadataHandler();
return null;
}
var pathAction = pathParts[1].ToLowerInvariant();
#if !NETSTANDARD2_0
if (pathAction == "wsdl")
{
if (pathController == "soap11")
return new Soap11WsdlMetadataHandler();
if (pathController == "soap12")
return new Soap12WsdlMetadataHandler();
}
#endif
if (pathAction != "metadata") return null;
switch (pathController)
{
case "json":
return new JsonMetadataHandler();
case "xml":
return new XmlMetadataHandler();
case "jsv":
return new JsvMetadataHandler();
#if !NETSTANDARD2_0
case "soap11":
return new Soap11MetadataHandler();
case "soap12":
return new Soap12MetadataHandler();
#endif
case "operations":
return new CustomResponseHandler((httpReq, httpRes) =>
HostContext.AppHost.HasAccessToMetadata(httpReq, httpRes)
? HostContext.Metadata.GetOperationDtos()
: null, "Operations");
default:
if (HostContext.ContentTypes
.ContentTypeFormats.TryGetValue(pathController, out var contentType))
{
var format = ContentFormat.GetContentFormat(contentType);
return new CustomMetadataHandler(contentType, format);
}
break;
}
return null;
}
}
[DefaultRequest(typeof(GetNavItems))]
[Restrict(VisibilityTo = RequestAttributes.None)]
public class MetadataNavService : Service
{
public object Get(GetNavItems request)
{
return request.Name != null
? new GetNavItemsResponse {
BaseUrl = Request.GetBaseUrl(),
Results = ViewUtils.NavItemsMap.TryGetValue(request.Name, out var navItems)
? navItems
: TypeConstants<NavItem>.EmptyList,
}
: new GetNavItemsResponse {
BaseUrl = Request.GetBaseUrl(),
Results = ViewUtils.NavItems,
NavItemsMap = ViewUtils.NavItemsMap,
};
}
}
[DefaultRequest(typeof(MetadataApp))]
[Restrict(VisibilityTo = RequestAttributes.None)]
public class MetadataAppService : Service
{
public INativeTypesMetadata NativeTypesMetadata { get; set; }
public AppMetadata Any(MetadataApp request)
{
var feature = HostContext.AssertPlugin<MetadataFeature>();
var typesConfig = NativeTypesMetadata.GetConfig(new TypesMetadata());
feature.ExportTypes.Each(x => typesConfig.ExportTypes.Add(x));
var metadataTypes = NativeTypesMetadata.GetMetadataTypes(Request, typesConfig);
metadataTypes.Config = null;
var appHost = HostContext.AssertAppHost();
var response = new AppMetadata {
App = appHost.Config.AppInfo ?? new AppInfo(),
ContentTypeFormats = appHost.ContentTypes.ContentTypeFormats,
Plugins = new PluginInfo {
Loaded = appHost.GetMetadataPluginIds(),
},
CustomPlugins = new Dictionary<string, CustomPlugin>(),
Api = metadataTypes,
};
if (response.App.BaseUrl == null)
response.App.BaseUrl = Request.GetBaseUrl();
if (response.App.ServiceName == null)
response.App.ServiceName = appHost.ServiceName;
foreach (var fn in feature.AppMetadataFilters)
{
fn(response);
}
return response;
}
}
public static class MetadataFeatureExtensions
{
public static MetadataFeature AddPluginLink(this MetadataFeature metadata, string href, string title)
{
if (metadata != null)
{
metadata.PluginLinks[href] = title;
}
return metadata;
}
public static MetadataFeature RemovePluginLink(this MetadataFeature metadata, string href)
{
metadata.PluginLinks.Remove(href);
return metadata;
}
public static MetadataFeature AddDebugLink(this MetadataFeature metadata, string href, string title)
{
if (metadata != null)
{
metadata.DebugLinks[href] = title;
}
return metadata;
}
public static MetadataFeature RemoveDebugLink(this MetadataFeature metadata, string href)
{
metadata.DebugLinks.Remove(href);
return metadata;
}
}
}