-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRestBootstrapper.cs
More file actions
94 lines (90 loc) · 4.01 KB
/
Copy pathRestBootstrapper.cs
File metadata and controls
94 lines (90 loc) · 4.01 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
using System;
using System.Collections.Generic;
using System.Linq;
using Nancy.Bootstrapper;
using Nancy.Diagnostics;
using Nancy.ModelBinding;
using Nancy.Responses.Negotiation;
using Nancy.Rest.Module.Filters.Serializers.Json;
using Nancy.Rest.Module.Filters.Serializers.Xml;
namespace Nancy.Rest.Module
{
public class RestBootstrapper : DefaultNancyBootstrapper
{
//We Need to skip this assembly from the available modules.
private ModuleRegistration[] _modules;
protected override IEnumerable<ModuleRegistration> Modules
{
get
{
return
_modules
??
(_modules = AppDomainAssemblyTypeScanner
.TypesOf<INancyModule>(ScanMode.ExcludeNancy)
.NotOfType<DiagnosticModule>()
.Where(a=>a.Name!=typeof(RestModule).Name).Select(t => new ModuleRegistration(t))
.ToArray());
}
}
private IEnumerable<Type> _bodySerializers;
protected override IEnumerable<Type> BodyDeserializers
{
get
{
if (_bodySerializers == null)
{
List<Type> serializers = AppDomainAssemblyTypeScanner.TypesOf<IBodyDeserializer>(ScanMode.ExcludeNancy).ToList();
RemoveProcessors(serializers).ForEach(a => serializers.Remove(a));
serializers.Add(typeof(JsonFilteredBodyDeserializer));
serializers.Add(typeof(XmlFilteredBodyDeserializer));
_bodySerializers = serializers;
}
return _bodySerializers;
}
}
//TODO find a dynamic way of remove originals, instead of asking for ContentType, so it can be extensible to other protocols.
//Remove json and xml serializer, add our filter-able serializers.
private NancyInternalConfiguration _configuration;
protected override NancyInternalConfiguration InternalConfiguration
{
get
{
if (_configuration == null)
{
_configuration = base.InternalConfiguration ?? NancyInternalConfiguration.Default;
RemoveProcessors(_configuration.Serializers).ForEach(a=> _configuration.Serializers.Remove(a));
_configuration.Serializers.Add(typeof(JsonFilteredSerializer));
_configuration.Serializers.Add(typeof(XmlFilteredSerializer));
_configuration.ResponseProcessors.Remove(typeof(JsonProcessor));
_configuration.ResponseProcessors.Remove(typeof(XmlProcessor));
_configuration.ResponseProcessors.Add(typeof(JsonProcessor));
_configuration.ResponseProcessors.Add(typeof(XmlProcessor));
}
return _configuration;
}
}
//TODO find a dynamic way of remove originals, instead of asking for ContentType, so it can be extensible to other protocols.
private List<Type> RemoveProcessors(IEnumerable<Type> types)
{
List<Type> toremove=new List<Type>();
foreach (Type t in types)
{
object o = Activator.CreateInstance(t);
ISerializer serializer = o as ISerializer;
if (serializer != null)
{
if (serializer.CanSerialize("application/json") || serializer.CanSerialize("text/xml"))
toremove.Add(t);
}
IBodyDeserializer deserializer = o as IBodyDeserializer;
if (deserializer != null)
{
if (deserializer.CanDeserialize("application/json",null) || deserializer.CanDeserialize("text/xml",null))
toremove.Add(t);
}
}
return toremove;
}
}
}