aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/RestDeserialiseHandler.cs
blob: cb500cabec696cbbe574e75e4ffe72d210f34f07 (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 string RestDeserialiseMethod<TRequest>(TRequest request);

    public class RestDeserialisehandler<TRequest> : BaseStreamHandler
        where TRequest : new()
    {
        private RestDeserialiseMethod<TRequest> m_method;

        public RestDeserialisehandler(string httpMethod, string path, RestDeserialiseMethod<TRequest> method)
            : base(httpMethod, path)
        {
            m_method = method;
        }

        public override byte[] Handle(string path, Stream request)
        {
            Type type = typeof(TRequest);

            TRequest deserial= default(TRequest);
            using (XmlTextReader xreader = new XmlTextReader(request))
            {
                XmlSerializer serializer = new XmlSerializer(type);
                deserial = (TRequest)serializer.Deserialize(xreader);
            }

            string response = m_method(deserial);

            Encoding encoding = new UTF8Encoding(false);
            return encoding.GetBytes(response);

        }
    }
}