forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataTypesHandler.cs
More file actions
306 lines (265 loc) · 10.9 KB
/
MetadataTypesHandler.cs
File metadata and controls
306 lines (265 loc) · 10.9 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
304
305
306
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Web;
using ServiceStack.Common;
using ServiceStack.Common.ServiceModel;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.WebHost.Endpoints.Extensions;
using ServiceStack.WebHost.Endpoints.Support;
namespace ServiceStack
{
public class MetadataTypesHandler : HttpHandlerBase, IServiceStackHttpHandler
{
public MetadataTypesConfig Config { get; set; }
public override void Execute(HttpContext context)
{
ProcessRequest(
new HttpRequestWrapper(GetType().Name, context.Request),
new HttpResponseWrapper(context.Response),
GetType().Name);
}
public void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
{
var metadata = new MetadataTypes {
Config = Config,
};
var existingTypes = new HashSet<Type> {
typeof(ResponseStatus),
typeof(ErrorResponse),
};
var meta = EndpointHost.Metadata;
foreach (var operation in meta.Operations)
{
if (!meta.IsVisible(httpReq, operation))
continue;
metadata.Operations.Add(new MetadataOperationType {
Actions = operation.Actions,
Request = operation.RequestType.ToType(),
Response = operation.ResponseType.ToType(),
});
existingTypes.Add(operation.RequestType);
if (operation.ResponseType != null)
{
existingTypes.Add(operation.ResponseType);
}
}
foreach (var type in meta.GetAllTypes())
{
if (existingTypes.Contains(type))
continue;
metadata.Operations.Add(new MetadataOperationType {
Request = type.ToType(),
});
existingTypes.Add(type);
}
var considered = new HashSet<Type>(existingTypes);
var queue = new Queue<Type>(existingTypes);
while (queue.Count > 0)
{
var type = queue.Dequeue();
foreach (var pi in type.GetSerializableProperties())
{
if (pi.PropertyType.IsUserType())
{
if (considered.Contains(pi.PropertyType))
continue;
considered.Add(pi.PropertyType);
queue.Enqueue(pi.PropertyType);
metadata.Types.Add(pi.PropertyType.ToType());
}
}
if (type.BaseType != null
&& type.BaseType.IsUserType()
&& !considered.Contains(type.BaseType))
{
considered.Add(type.BaseType);
queue.Enqueue(type.BaseType);
metadata.Types.Add(type.BaseType.ToType());
}
}
var json = metadata.ToJson();
//httpRes.ContentType = "application/json";
//httpRes.Write(json);
//return;
httpRes.ContentType = "application/x-ssz-metatypes";
var encJson = CryptUtils.Encrypt(EndpointHostConfig.PublicKey, json, RsaKeyLengths.Bit2048);
httpRes.Write(encJson);
}
}
public static class MetadataTypeExtensions
{
public static MetadataType ToType(this Type type)
{
if (type == null) return null;
var metaType = new MetadataType {
Name = type.Name,
Namespace = type.Namespace,
GenericArgs = type.IsGenericType
? type.GetGenericArguments().Select(x => x.Name).ToArray()
: null,
Attributes = type.ToAttributes(),
Properties = type.ToProperties(),
};
if (type.BaseType != null && type.BaseType != typeof(object))
{
metaType.Inherits = type.BaseType.Name;
metaType.InheritsGenericArgs = type.BaseType.IsGenericType
? type.BaseType.GetGenericArguments().Select(x => x.Name).ToArray()
: null;
}
if (type.GetTypeWithInterfaceOf(typeof(IReturnVoid)) != null)
{
metaType.ReturnVoidMarker = true;
}
else
{
var genericMarker = type.GetTypeWithGenericTypeDefinitionOf(typeof(IReturn<>));
if (genericMarker != null)
{
metaType.ReturnMarkerGenericArgs = genericMarker.GetGenericArguments().Select(x => x.Name).ToArray();
}
}
var typeAttrs = type.GetCustomAttributes(false);
var routeAttrs = typeAttrs.OfType<RouteAttribute>().ToList();
if (routeAttrs.Count > 0)
{
metaType.Routes = routeAttrs.ConvertAll(x =>
new MetadataRoute {
Path = x.Path,
Notes = x.Notes,
Summary = x.Summary,
Verbs = x.Verbs,
});
}
var descAttr = typeAttrs.OfType<DescriptionAttribute>().FirstOrDefault();
if (descAttr != null)
{
metaType.Description = descAttr.Description;
}
var dcAttr = type.GetDataContract();
if (dcAttr != null)
{
metaType.DataContract = new MetadataDataContract {
Name = dcAttr.Name,
Namespace = dcAttr.Namespace,
};
}
return metaType;
}
public static List<MetadataAttribute> ToAttributes(this Type type)
{
return !type.IsUserType() || type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable<>))
? null
: type.GetCustomAttributes(false).ToAttributes();
}
public static List<MetadataPropertyType> ToProperties(this Type type)
{
var props = !type.IsUserType() || type.IsOrHasGenericInterfaceTypeOf(typeof(IEnumerable<>))
? null
: type.GetInstancePublicProperties().ToList().ConvertAll(x => x.ToProperty());
return props == null || props.Count == 0 ? null : props;
}
public static bool ExcludeKnownAttrsFilter(Attribute x)
{
return x.GetType() != typeof(RouteAttribute)
&& x.GetType() != typeof(DescriptionAttribute)
&& x.GetType().Name != "DataContractAttribute" //Type equality issues with Mono .NET 3.5/4
&& x.GetType().Name != "DataMemberAttribute";
}
public static List<MetadataAttribute> ToAttributes(this object[] attrs)
{
var to = attrs.OfType<Attribute>()
.Where(ExcludeKnownAttrsFilter)
.ToList().ConvertAll(x => x.ToAttribute());
return to.Count == 0 ? null : to;
}
public static List<MetadataAttribute> ToAttributes(this IEnumerable<Attribute> attrs)
{
var to = attrs
.Where(ExcludeKnownAttrsFilter)
.Select(attr => attr.ToAttribute())
.ToList();
return to.Count == 0 ? null : to;
}
public static MetadataAttribute ToAttribute(this Attribute attr)
{
var firstCtor = attr.GetType().GetConstructors().OrderBy(x => x.GetParameters().Length).FirstOrDefault();
var metaAttr = new MetadataAttribute {
Name = attr.GetType().Name,
ConstructorArgs = firstCtor != null
? firstCtor.GetParameters().ToList().ConvertAll(x => x.ToProperty())
: null,
Args = attr.NonDefaultProperties(),
};
return metaAttr;
}
public static List<MetadataPropertyType> NonDefaultProperties(this Attribute attr)
{
return attr.GetType().GetPublicProperties()
.Select(pi => pi.ToProperty(attr))
.Where(property => property.Name != "TypeId"
&& property.Value != null)
.ToList();
}
public static MetadataPropertyType ToProperty(this PropertyInfo pi, object instance = null)
{
var property = new MetadataPropertyType {
Name = pi.Name,
Attributes = pi.GetCustomAttributes(false).ToAttributes(),
Type = pi.PropertyType.Name,
DataMember = pi.GetDataMember().ToDataMember(),
GenericArgs = pi.PropertyType.IsGenericType
? pi.PropertyType.GetGenericArguments().Select(x => x.Name).ToArray()
: null,
};
if (instance != null)
{
var value = pi.GetValue(instance, null);
if (value != pi.PropertyType.GetDefaultValue())
{
property.Value = value.ToJson();
}
}
return property;
}
public static MetadataPropertyType ToProperty(this ParameterInfo pi)
{
var propertyAttrs = pi.GetCustomAttributes(false);
var property = new MetadataPropertyType {
Name = pi.Name,
Attributes = propertyAttrs.ToAttributes(),
Type = pi.ParameterType.Name,
};
var descAttr = propertyAttrs.OfType<DescriptionAttribute>().FirstOrDefault();
if (descAttr != null)
{
property.Description = descAttr.Description;
}
return property;
}
public static MetadataDataMember ToDataMember(this DataMemberAttribute attr)
{
if (attr == null) return null;
var metaAttr = new MetadataDataMember {
Name = attr.Name,
EmitDefaultValue = attr.EmitDefaultValue != true ? attr.EmitDefaultValue : (bool?)null,
Order = attr.Order >= 0 ? attr.Order : (int?)null,
IsRequired = attr.IsRequired != false ? attr.IsRequired : (bool?)null,
};
return metaAttr;
}
public static PropertyInfo[] GetInstancePublicProperties(this Type type)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
.ToArray();
}
}
}