This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpResponseFilter.cs
More file actions
295 lines (240 loc) · 10 KB
/
HttpResponseFilter.cs
File metadata and controls
295 lines (240 loc) · 10 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SimpleStack.Extensions;
using SimpleStack.Interfaces;
namespace SimpleStack
{
/// <summary>
/// Manage Content Serialization/Deserialization
/// </summary>
internal class HttpResponseFilter : IContentTypeFilter
{
private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
public static HttpResponseFilter Instance = new HttpResponseFilter();
public Dictionary<string, ResponseSerializerDelegate> ContentTypeResponseSerializers = new Dictionary<string, ResponseSerializerDelegate>();
public Dictionary<string, StreamDeserializerDelegate> ContentTypeDeserializers = new Dictionary<string, StreamDeserializerDelegate>();
public Dictionary<string, StreamSerializerDelegate> ContentTypeSerializers = new Dictionary<string, StreamSerializerDelegate>();
public HttpResponseFilter()
{
ContentTypeFormats = new Dictionary<string, List<string>>();
}
public void ClearCustomFilters()
{
ContentTypeFormats = new Dictionary<string, List<string>>();
ContentTypeSerializers = new Dictionary<string, StreamSerializerDelegate>();
ContentTypeDeserializers = new Dictionary<string, StreamDeserializerDelegate>();
}
public IDictionary<string, List<string>> ContentTypeFormats { get; set; }
public void Register(IContentTypeSerializer serializer)
{
if(serializer == null)
throw new ArgumentNullException("serializer");
for (int i = 0; i < serializer.ContentTypes.Length; i++)
{
string[] parts = serializer.ContentTypes[i].Split('/');
string format = parts[parts.Length - 1];
if (!ContentTypeFormats.ContainsKey(format))
{
ContentTypeFormats.Add(format,new List<string>());
}
ContentTypeFormats[format].Add(serializer.ContentTypes[i]);
ContentTypeSerializers[serializer.ContentTypes[i]] = serializer.GetStreamSerializer();
ContentTypeDeserializers[serializer.ContentTypes[i]] = serializer.GetStreamDeserializer();
}
}
//public void Register(string contentType,
// StreamSerializerDelegate streamSerializer,
// StreamDeserializerDelegate streamDeserializer)
//{
// if (contentType.IsNullOrEmpty())
// throw new ArgumentNullException("contentType");
// string[] parts = contentType.Split('/');
// string format = parts[parts.Length - 1];
// ContentTypeFormats[format] = contentType;
// ContentTypeSerializers[contentType] = streamSerializer;
// ContentTypeDeserializers[contentType] = streamDeserializer;
//}
//public void Register(string contentType,
// ResponseSerializerDelegate responseSerializer,
// StreamDeserializerDelegate streamDeserializer)
//{
// if (contentType.IsNullOrEmpty())
// throw new ArgumentNullException("contentType");
// string[] parts = contentType.Split('/');
// string format = parts[parts.Length - 1];
// ContentTypeFormats[format] = contentType;
// ContentTypeResponseSerializers[contentType] = responseSerializer;
// ContentTypeDeserializers[contentType] = streamDeserializer;
//}
public byte[] SerializeToBytes(IRequestContext requestContext, object response)
{
string contentType = requestContext.ResponseContentType;
StreamSerializerDelegate responseStreamWriter;
if (ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
{
using (var ms = new MemoryStream())
{
responseStreamWriter(requestContext, response, ms);
ms.Position = 0;
return ms.ToArray();
}
}
ResponseSerializerDelegate responseWriter;
if (ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
using (var ms = new MemoryStream())
{
var httpRes = new HttpResponseStreamWrapper(ms);
responseWriter(requestContext, response, httpRes);
ms.Position = 0;
return ms.ToArray();
}
}
//EndpointAttributes contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
//switch (contentTypeAttr)
//{
// case EndpointAttributes.Xml:
// return XmlSerializer.SerializeToString(response).ToUtf8Bytes();
// case EndpointAttributes.Json:
// return JsonDataContractSerializer.Instance.SerializeToString(response).ToUtf8Bytes();
// case EndpointAttributes.Jsv:
// return TypeSerializer.SerializeToString(response).ToUtf8Bytes();
//}
throw new NotSupportedException("ContentType not supported: " + contentType);
}
public string SerializeToString(IRequestContext requestContext, object response)
{
string contentType = requestContext.ResponseContentType;
StreamSerializerDelegate responseStreamWriter;
if (ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
{
using (var ms = new MemoryStream())
{
responseStreamWriter(requestContext, response, ms);
ms.Position = 0;
string result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd();
return result;
}
}
ResponseSerializerDelegate responseWriter;
if (ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
using (var ms = new MemoryStream())
{
var httpRes = new HttpResponseStreamWrapper(ms)
{
KeepOpen = true, //Don't let view engines close the OutputStream
};
responseWriter(requestContext, response, httpRes);
byte[] bytes = ms.ToArray();
string result = bytes.FromUtf8Bytes();
httpRes.ForceClose(); //Manually close the OutputStream
return result;
}
}
//EndpointAttributes contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
//switch (contentTypeAttr)
//{
// case EndpointAttributes.Xml:
// return XmlSerializer.SerializeToString(response);
// case EndpointAttributes.Json:
// return JsonDataContractSerializer.Instance.SerializeToString(response);
// case EndpointAttributes.Jsv:
// return TypeSerializer.SerializeToString(response);
//}
throw new NotSupportedException("ContentType not supported: " + contentType);
}
public void SerializeToStream(IRequestContext requestContext, object response, Stream responseStream)
{
string contentType = requestContext.ResponseContentType;
ResponseSerializerDelegate serializer = GetResponseSerializer(contentType);
if (serializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
var httpRes = new HttpResponseStreamWrapper(responseStream);
serializer(requestContext, response, httpRes);
}
public void SerializeToResponse(IRequestContext requestContext, object response, IHttpResponse httpResponse)
{
string contentType = requestContext.ResponseContentType;
ResponseSerializerDelegate serializer = GetResponseSerializer(contentType);
if (serializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
serializer(requestContext, response, httpResponse);
}
public ResponseSerializerDelegate GetResponseSerializer(string contentType)
{
ResponseSerializerDelegate responseWriter;
if (ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
return responseWriter;
}
StreamSerializerDelegate serializer = GetStreamSerializer(contentType);
if (serializer == null) return null;
return (httpReq, dto, httpRes) => serializer(httpReq, dto, httpRes.OutputStream);
}
public object DeserializeFromString(string contentType, Type type, string request)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] content = Encoding.UTF8.GetBytes(request);
ms.Write(content,0,content.Length);
ms.Seek(0, SeekOrigin.Begin);
return DeserializeFromStream(contentType, type, ms);
}
}
public object DeserializeFromStream(string contentType, Type type, Stream fromStream)
{
StreamDeserializerDelegate deserializer = GetStreamDeserializer(contentType);
if (deserializer == null)
throw new NotSupportedException("ContentType not supported: " + contentType);
return deserializer(type, fromStream);
}
public StreamDeserializerDelegate GetStreamDeserializer(string contentType)
{
StreamDeserializerDelegate streamReader;
string realContentType = contentType.Split(';')[0].Trim();
if (ContentTypeDeserializers.TryGetValue(realContentType, out streamReader))
{
return streamReader;
}
//EndpointAttributes contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
//switch (contentTypeAttr)
//{
// case EndpointAttributes.Xml:
// return XmlSerializer.DeserializeFromStream;
// case EndpointAttributes.Json:
// return JsonDataContractDeserializer.Instance.DeserializeFromStream;
// case EndpointAttributes.Jsv:
// return TypeSerializer.DeserializeFromStream;
//}
return null;
}
private StreamSerializerDelegate GetStreamSerializer(string contentType)
{
StreamSerializerDelegate responseWriter;
if (ContentTypeSerializers.TryGetValue(contentType, out responseWriter) ||
ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
{
return responseWriter;
}
//EndpointAttributes contentTypeAttr = ContentType.GetEndpointAttributes(contentType);
//switch (contentTypeAttr)
//{
// case EndpointAttributes.Xml:
// return (r, o, s) => XmlSerializer.SerializeToStream(o, s);
// case EndpointAttributes.Json:
// return (r, o, s) => JsonDataContractSerializer.Instance.SerializeToStream(o, s);
// case EndpointAttributes.Jsv:
// return (r, o, s) => TypeSerializer.SerializeToStream(o, s);
//}
return null;
}
}
}