blob: adf5c3fc6425319033b5cd1582de9c39e409148f (
plain)
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
|
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace OpenSim.Framework.Servers
{
public delegate TResponse RestDeserialiseMethod<TRequest, TResponse>(TRequest request);
public class RestDeserialisehandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler
where TRequest : new()
{
private RestDeserialiseMethod<TRequest, TResponse> m_method;
public RestDeserialisehandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method)
: base(httpMethod, path)
{
m_method = method;
}
public void Handle(string path, Stream request, Stream responseStream)
{
TRequest deserial;
using (XmlTextReader xmlReader = new XmlTextReader(request))
{
XmlSerializer deserializer = new XmlSerializer(typeof (TRequest));
deserial = (TRequest) deserializer.Deserialize(xmlReader);
}
TResponse response = m_method(deserial);
using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream))
{
XmlSerializer serializer = new XmlSerializer(typeof (TResponse));
serializer.Serialize(xmlWriter, response);
}
}
}
}
|