forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataFeature.cs
More file actions
140 lines (117 loc) · 4.41 KB
/
Copy pathMetadataFeature.cs
File metadata and controls
140 lines (117 loc) · 4.41 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
using System;
using System.Collections.Generic;
using System.Web;
using ServiceStack.Host.Handlers;
using ServiceStack.Metadata;
namespace ServiceStack
{
public class MetadataFeature : IPlugin
{
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 bool ShowResponseStatusInMetadataPages { get; set; }
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);
}
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:
string contentType;
if (HostContext.ContentTypes
.ContentTypeFormats.TryGetValue(pathController, out contentType))
{
var format = ContentFormat.GetContentFormat(contentType);
return new CustomMetadataHandler(contentType, format);
}
break;
}
return null;
}
}
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;
}
}
}