aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/RestDeserialiseHandler.cs
blob: 28084e2917ac0a7997ba75d9651fe11932ae953c (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
39
40
41
using System;
using System.Collections.Generic;
using System.Text;
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 );
            }
        }
    }
}