forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMetadataHandler.cs
More file actions
303 lines (258 loc) · 12.3 KB
/
Copy pathBaseMetadataHandler.cs
File metadata and controls
303 lines (258 loc) · 12.3 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using ServiceStack.Host;
using ServiceStack.Support.WebHost;
using ServiceStack.Web;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStack.Metadata
{
public abstract class BaseMetadataHandler : HttpHandlerBase
{
public abstract Format Format { get; }
public string ContentType { get; set; }
public string ContentFormat { get; set; }
public override void Execute(HttpContextBase context)
{
var writer = new HtmlTextWriter(context.Response.Output);
context.Response.ContentType = "text/html";
var request = context.ToRequest();
ProcessOperations(writer, request, request.Response);
}
public override void ProcessRequest(IRequest httpReq, IResponse httpRes, string operationName)
{
if (HostContext.ApplyCustomHandlerRequestFilters(httpReq, httpRes))
return;
using (var sw = new StreamWriter(httpRes.OutputStream))
{
var writer = new HtmlTextWriter(sw);
httpRes.ContentType = "text/html";
ProcessOperations(writer, httpReq, httpRes);
}
httpRes.EndHttpHandlerRequest(skipHeaders:true);
}
public virtual string CreateResponse(Type type)
{
if (type == typeof(string))
return "(string)";
if (type == typeof(byte[]))
return "(byte[])";
if (type == typeof(Stream))
return "(Stream)";
if (type == typeof(HttpWebResponse))
return "(HttpWebResponse)";
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
type = type.GetGenericArguments()[0];
return CreateMessage(type);
}
protected virtual void ProcessOperations(HtmlTextWriter writer, IRequest httpReq, IResponse httpRes)
{
var operationName = httpReq.QueryString["op"];
if (!AssertAccess(httpReq, httpRes, operationName)) return;
ContentFormat = ServiceStack.ContentFormat.GetContentFormat(Format);
var metadata = HostContext.Metadata;
if (operationName != null)
{
var allTypes = metadata.GetAllOperationTypes();
//var operationType = allTypes.Single(x => x.Name == operationName);
var operationType = allTypes.Single(x => x.GetOperationName() == operationName);
var op = metadata.GetOperation(operationType);
var requestMessage = CreateResponse(operationType);
string responseMessage = null;
var responseType = metadata.GetResponseTypeByRequest(operationType);
if (responseType != null)
{
responseMessage = CreateResponse(responseType);
}
var isSoap = Format == Format.Soap11 || Format == Format.Soap12;
var sb = new StringBuilder();
var description = operationType.GetDescription();
if (!description.IsNullOrEmpty())
{
sb.AppendFormat("<h3 id='desc'>{0}</div>", ConvertToHtml(description));
}
if (op.RequiresAuthentication)
{
sb.AppendLine("<table class='authentication'>" +
"<caption><b>Requires Authentication</b><i class='auth' style='display:inline-block;margin:0 0 -4px 5px;'></i></caption>");
sb.Append("<tr>");
if (!op.RequiredRoles.IsEmpty())
{
var plural = op.RequiredRoles.Count > 1 ? "s" : "";
sb.Append("<td>Required role{0}:</td><td>{1}</td>".Fmt(plural, string.Join(", ", op.RequiredRoles)));
}
if (!op.RequiresAnyRole.IsEmpty())
{
var plural = op.RequiresAnyRole.Count > 1 ? "Requires any of the roles" : "Requires the role";
sb.Append("<td>{0}:</td><td>{1}</td>".Fmt(plural, string.Join(", ", op.RequiresAnyRole)));
}
if (!op.RequiredPermissions.IsEmpty())
{
var plural = op.RequiredPermissions.Count > 1 ? "s" : "";
sb.Append("<td>Required permission{0}:</td><td>{1}</td>".Fmt(plural, string.Join(", ", op.RequiredPermissions)));
}
if (!op.RequiresAnyPermission.IsEmpty())
{
var plural = op.RequiresAnyPermission.Count > 1 ? "Requires any of the permissions" : "Requires the permission";
sb.Append("<td>{0}:</td><td>{1}</td>".Fmt(plural, string.Join(", ", op.RequiresAnyPermission)));
}
sb.Append("</tr>");
sb.Append("</table>");
}
if (op.Routes.Count > 0)
{
sb.Append("<table class='routes'>");
if (!isSoap)
{
sb.Append("<caption>The following routes are available for this service:</caption>");
}
sb.Append("<tbody>");
foreach (var route in op.Routes)
{
if (isSoap && !(route.AllowsAllVerbs || route.AllowedVerbs.Contains(HttpMethods.Post)))
continue;
sb.Append("<tr>");
var verbs = route.AllowsAllVerbs ? "All Verbs" : route.AllowedVerbs;
if (!isSoap)
{
var path = "/" + PathUtils.CombinePaths(HostContext.Config.HandlerFactoryPath, route.Path);
sb.AppendFormat("<th>{0}</th>", verbs);
sb.AppendFormat("<th>{0}</th>", path);
}
sb.AppendFormat("<td>{0}</td>", route.Summary);
sb.AppendFormat("<td><i>{0}</i></td>", route.Notes);
sb.Append("</tr>");
}
sb.Append("<tbody>");
sb.Append("</tbody>");
sb.Append("</table>");
}
var metadataTypes = metadata.GetMetadataTypesForOperation(httpReq, op);
metadataTypes.Each(x => AppendType(sb, op, x));
sb.Append(@"<div class=""call-info"">");
var overrideExtCopy = HostContext.Config.AllowRouteContentTypeExtensions
? " the <b>.{0}</b> suffix or ".Fmt(ContentFormat)
: "";
sb.AppendFormat(@"<p>To override the Content-type in your clients, use the HTTP <b>Accept</b> Header, append {1} <b>?format={0}</b></p>", ContentFormat, overrideExtCopy);
if (ContentFormat == "json")
{
sb.Append("<p>To embed the response in a <b>jsonp</b> callback, append <b>?callback=myCallback</b></p>");
}
sb.Append("</div>");
RenderOperation(writer, httpReq, operationName, requestMessage, responseMessage, sb.ToString());
return;
}
RenderOperations(writer, httpReq, metadata);
}
private void AppendType(StringBuilder sb, Operation op, MetadataType metadataType)
{
if (metadataType.Properties.IsEmpty()) return;
sb.Append("<table class='params'>");
sb.Append("<caption><b>{0}</b> Parameters:</caption>".Fmt(ConvertToHtml(metadataType.DisplayType ?? metadataType.Name)));
sb.Append("<thead><tr>");
sb.Append("<th>Name</th>");
sb.Append("<th>Parameter</th>");
sb.Append("<th>Data Type</th>");
sb.Append("<th>Required</th>");
sb.Append("<th>Description</th>");
sb.Append("</tr></thead>");
sb.Append("<tbody>");
foreach (var p in metadataType.Properties)
{
sb.Append("<tr>");
sb.AppendFormat("<td>{0}</td>", ConvertToHtml(p.Name));
sb.AppendFormat("<td>{0}</td>", p.GetParamType(metadataType, op));
sb.AppendFormat("<td>{0}</td>", ConvertToHtml(p.DisplayType ?? p.Type));
sb.AppendFormat("<td>{0}</td>", p.IsRequired.GetValueOrDefault() ? "Yes" : "No");
var desc = p.Description;
if (!p.AllowableValues.IsEmpty())
{
desc += "<h4>Allowable Values</h4>";
desc += "<ul>";
p.AllowableValues.Each(x => desc += "<li>{0}</li>".Fmt(x));
desc += "</ul>";
}
if (p.AllowableMin != null)
{
desc += "<h4>Valid Range: {0} - {1}</h4>".Fmt(p.AllowableMin, p.AllowableMax);
}
sb.AppendFormat("<td>{0}</td>", desc);
sb.Append("</tr>");
}
sb.Append("</tbody>");
sb.Append("</table>");
}
protected void RenderOperations(HtmlTextWriter writer, IRequest httpReq, ServiceMetadata metadata)
{
var defaultPage = new IndexOperationsControl
{
Request = httpReq,
MetadataConfig = HostContext.MetadataPagesConfig,
Title = HostContext.ServiceName,
Xsds = XsdTypes.Xsds,
XsdServiceTypesIndex = 1,
OperationNames = metadata.GetOperationNamesForMetadata(httpReq),
};
var metadataFeature = HostContext.GetPlugin<MetadataFeature>();
if (metadataFeature != null && metadataFeature.IndexPageFilter != null)
{
metadataFeature.IndexPageFilter(defaultPage);
}
defaultPage.RenderControl(writer);
}
private string ConvertToHtml(string text)
{
return text.Replace("<", "<")
.Replace(">", ">")
.Replace("\n", "<br />\n");
}
protected bool AssertAccess(IRequest httpReq, IResponse httpRes, string operationName)
{
var appHost = HostContext.AppHost;
if (!appHost.HasAccessToMetadata(httpReq, httpRes)) return false;
if (operationName == null) return true; //For non-operation pages we don't need to check further permissions
if (!appHost.Config.EnableAccessRestrictions) return true;
if (!appHost.MetadataPagesConfig.IsVisible(httpReq, Format, operationName))
{
appHost.HandleErrorResponse(httpReq, httpRes, HttpStatusCode.Forbidden, "Service Not Available");
return false;
}
return true;
}
protected abstract string CreateMessage(Type dtoType);
protected virtual void RenderOperation(HtmlTextWriter writer, IRequest httpReq, string operationName,
string requestMessage, string responseMessage, string metadataHtml)
{
var operationControl = new OperationControl
{
HttpRequest = httpReq,
MetadataConfig = HostContext.Config.ServiceEndpointsMetadataConfig,
Title = HostContext.ServiceName,
Format = this.Format,
OperationName = operationName,
HostName = httpReq.GetUrlHostName(),
RequestMessage = requestMessage,
ResponseMessage = responseMessage,
MetadataHtml = metadataHtml,
};
if (!this.ContentType.IsNullOrEmpty())
{
operationControl.ContentType = this.ContentType;
}
if (!this.ContentFormat.IsNullOrEmpty())
{
operationControl.ContentFormat = this.ContentFormat;
}
var metadataFeature = HostContext.GetPlugin<MetadataFeature>();
if (metadataFeature != null && metadataFeature.DetailPageFilter != null)
{
metadataFeature.DetailPageFilter(operationControl);
}
operationControl.Render(writer);
}
}
}