diff options
Diffstat (limited to 'OpenSim/Framework/Servers')
-rw-r--r-- | OpenSim/Framework/Servers/RestObjectPoster.cs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/RestObjectPoster.cs b/OpenSim/Framework/Servers/RestObjectPoster.cs index b77cbcc..07095f3 100644 --- a/OpenSim/Framework/Servers/RestObjectPoster.cs +++ b/OpenSim/Framework/Servers/RestObjectPoster.cs | |||
@@ -8,8 +8,11 @@ using System.Xml.Serialization; | |||
8 | 8 | ||
9 | namespace OpenSim.Framework.Servers | 9 | namespace OpenSim.Framework.Servers |
10 | { | 10 | { |
11 | public delegate void ReturnResponse<T>(T reponse); | ||
12 | |||
11 | public class RestObjectPoster | 13 | public class RestObjectPoster |
12 | { | 14 | { |
15 | |||
13 | public static void BeginPostObject<TRequest>(string requestUrl, TRequest obj) | 16 | public static void BeginPostObject<TRequest>(string requestUrl, TRequest obj) |
14 | { | 17 | { |
15 | Type type = typeof(TRequest); | 18 | Type type = typeof(TRequest); |
@@ -46,4 +49,93 @@ namespace OpenSim.Framework.Servers | |||
46 | } | 49 | } |
47 | } | 50 | } |
48 | } | 51 | } |
52 | |||
53 | public class RestObjectPosterResponse<TResponse> | ||
54 | { | ||
55 | public ReturnResponse<TResponse> ReturnResponseVal; | ||
56 | |||
57 | public void BeginPostObject<TRequest>(string requestUrl, TRequest obj) | ||
58 | { | ||
59 | Type type = typeof(TRequest); | ||
60 | |||
61 | WebRequest request = WebRequest.Create(requestUrl); | ||
62 | request.Method = "POST"; | ||
63 | request.ContentType = "text/xml"; | ||
64 | |||
65 | MemoryStream buffer = new MemoryStream(); | ||
66 | |||
67 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
68 | settings.Encoding = Encoding.UTF8; | ||
69 | |||
70 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
71 | { | ||
72 | XmlSerializer serializer = new XmlSerializer(type); | ||
73 | serializer.Serialize(writer, obj); | ||
74 | writer.Flush(); | ||
75 | } | ||
76 | |||
77 | int length = (int)buffer.Length; | ||
78 | request.ContentLength = length; | ||
79 | |||
80 | Stream requestStream = request.GetRequestStream(); | ||
81 | requestStream.Write(buffer.ToArray(), 0, length); | ||
82 | IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); | ||
83 | } | ||
84 | |||
85 | private void AsyncCallback(IAsyncResult result) | ||
86 | { | ||
87 | WebRequest request = (WebRequest)result.AsyncState; | ||
88 | using (WebResponse resp = request.EndGetResponse(result)) | ||
89 | { | ||
90 | TResponse deserial; | ||
91 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
92 | deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); | ||
93 | |||
94 | if (deserial != null && ReturnResponseVal != null) | ||
95 | { | ||
96 | ReturnResponseVal(deserial); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | public class SyncRestObjectPoster | ||
103 | { | ||
104 | |||
105 | public static TResponse BeginPostObject<TRequest, TResponse>(string requestUrl, TRequest obj) | ||
106 | { | ||
107 | Type type = typeof(TRequest); | ||
108 | |||
109 | WebRequest request = WebRequest.Create(requestUrl); | ||
110 | request.Method = "POST"; | ||
111 | request.ContentType = "text/xml"; | ||
112 | |||
113 | MemoryStream buffer = new MemoryStream(); | ||
114 | |||
115 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
116 | settings.Encoding = Encoding.UTF8; | ||
117 | |||
118 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
119 | { | ||
120 | XmlSerializer serializer = new XmlSerializer(type); | ||
121 | serializer.Serialize(writer, obj); | ||
122 | writer.Flush(); | ||
123 | } | ||
124 | |||
125 | int length = (int)buffer.Length; | ||
126 | request.ContentLength = length; | ||
127 | |||
128 | Stream requestStream = request.GetRequestStream(); | ||
129 | requestStream.Write(buffer.ToArray(), 0, length); | ||
130 | TResponse deserial = default(TResponse); | ||
131 | using (WebResponse resp = request.GetResponse()) | ||
132 | { | ||
133 | |||
134 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
135 | deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); | ||
136 | } | ||
137 | return deserial; | ||
138 | } | ||
139 | |||
140 | } | ||
49 | } \ No newline at end of file | 141 | } \ No newline at end of file |