diff options
Diffstat (limited to 'OpenSim/Framework/Servers/RestSessionService.cs')
-rw-r--r-- | OpenSim/Framework/Servers/RestSessionService.cs | 292 |
1 files changed, 0 insertions, 292 deletions
diff --git a/OpenSim/Framework/Servers/RestSessionService.cs b/OpenSim/Framework/Servers/RestSessionService.cs deleted file mode 100644 index 6a8b614..0000000 --- a/OpenSim/Framework/Servers/RestSessionService.cs +++ /dev/null | |||
@@ -1,292 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.IO; | ||
30 | using System.Net; | ||
31 | using System.Reflection; | ||
32 | using System.Text; | ||
33 | using System.Xml; | ||
34 | using System.Xml.Serialization; | ||
35 | using log4net; | ||
36 | using OpenSim.Framework.Servers.Interfaces; | ||
37 | |||
38 | namespace OpenSim.Framework.Servers | ||
39 | { | ||
40 | public class RestSessionObject<TRequest> | ||
41 | { | ||
42 | private string sid; | ||
43 | private string aid; | ||
44 | private TRequest request_body; | ||
45 | |||
46 | public string SessionID | ||
47 | { | ||
48 | get { return sid; } | ||
49 | set { sid = value; } | ||
50 | } | ||
51 | |||
52 | public string AvatarID | ||
53 | { | ||
54 | get { return aid; } | ||
55 | set { aid = value; } | ||
56 | } | ||
57 | |||
58 | public TRequest Body | ||
59 | { | ||
60 | get { return request_body; } | ||
61 | set { request_body = value; } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | public class SynchronousRestSessionObjectPoster<TRequest, TResponse> | ||
66 | { | ||
67 | public static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) | ||
68 | { | ||
69 | RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>(); | ||
70 | sobj.SessionID = sid; | ||
71 | sobj.AvatarID = aid; | ||
72 | sobj.Body = obj; | ||
73 | |||
74 | Type type = typeof(RestSessionObject<TRequest>); | ||
75 | |||
76 | WebRequest request = WebRequest.Create(requestUrl); | ||
77 | request.Method = verb; | ||
78 | request.ContentType = "text/xml"; | ||
79 | |||
80 | MemoryStream buffer = new MemoryStream(); | ||
81 | |||
82 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
83 | settings.Encoding = Encoding.UTF8; | ||
84 | |||
85 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
86 | { | ||
87 | XmlSerializer serializer = new XmlSerializer(type); | ||
88 | serializer.Serialize(writer, sobj); | ||
89 | writer.Flush(); | ||
90 | } | ||
91 | |||
92 | int length = (int)buffer.Length; | ||
93 | request.ContentLength = length; | ||
94 | |||
95 | Stream requestStream = request.GetRequestStream(); | ||
96 | requestStream.Write(buffer.ToArray(), 0, length); | ||
97 | TResponse deserial = default(TResponse); | ||
98 | using (WebResponse resp = request.GetResponse()) | ||
99 | { | ||
100 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
101 | deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); | ||
102 | } | ||
103 | return deserial; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | public class RestSessionObjectPosterResponse<TRequest, TResponse> | ||
108 | { | ||
109 | public ReturnResponse<TResponse> ResponseCallback; | ||
110 | |||
111 | public void BeginPostObject(string requestUrl, TRequest obj, string sid, string aid) | ||
112 | { | ||
113 | BeginPostObject("POST", requestUrl, obj, sid, aid); | ||
114 | } | ||
115 | |||
116 | public void BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) | ||
117 | { | ||
118 | RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>(); | ||
119 | sobj.SessionID = sid; | ||
120 | sobj.AvatarID = aid; | ||
121 | sobj.Body = obj; | ||
122 | |||
123 | Type type = typeof(RestSessionObject<TRequest>); | ||
124 | |||
125 | WebRequest request = WebRequest.Create(requestUrl); | ||
126 | request.Method = verb; | ||
127 | request.ContentType = "text/xml"; | ||
128 | request.Timeout = 10000; | ||
129 | |||
130 | MemoryStream buffer = new MemoryStream(); | ||
131 | |||
132 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
133 | settings.Encoding = Encoding.UTF8; | ||
134 | |||
135 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
136 | { | ||
137 | XmlSerializer serializer = new XmlSerializer(type); | ||
138 | serializer.Serialize(writer, sobj); | ||
139 | writer.Flush(); | ||
140 | } | ||
141 | |||
142 | int length = (int)buffer.Length; | ||
143 | request.ContentLength = length; | ||
144 | |||
145 | Stream requestStream = request.GetRequestStream(); | ||
146 | requestStream.Write(buffer.ToArray(), 0, length); | ||
147 | requestStream.Close(); | ||
148 | // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); | ||
149 | request.BeginGetResponse(AsyncCallback, request); | ||
150 | } | ||
151 | |||
152 | private void AsyncCallback(IAsyncResult result) | ||
153 | { | ||
154 | WebRequest request = (WebRequest)result.AsyncState; | ||
155 | using (WebResponse resp = request.EndGetResponse(result)) | ||
156 | { | ||
157 | TResponse deserial; | ||
158 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
159 | Stream stream = resp.GetResponseStream(); | ||
160 | |||
161 | // This is currently a bad debug stanza since it gobbles us the response... | ||
162 | // StreamReader reader = new StreamReader(stream); | ||
163 | // m_log.DebugFormat("[REST OBJECT POSTER RESPONSE]: Received {0}", reader.ReadToEnd()); | ||
164 | |||
165 | deserial = (TResponse)deserializer.Deserialize(stream); | ||
166 | |||
167 | if (deserial != null && ResponseCallback != null) | ||
168 | { | ||
169 | ResponseCallback(deserial); | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | |||
175 | public delegate bool CheckIdentityMethod(string sid, string aid); | ||
176 | |||
177 | public class RestDeserialiseSecureHandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler | ||
178 | where TRequest : new() | ||
179 | { | ||
180 | private static readonly ILog m_log | ||
181 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
182 | |||
183 | private RestDeserialiseMethod<TRequest, TResponse> m_method; | ||
184 | private CheckIdentityMethod m_smethod; | ||
185 | |||
186 | public RestDeserialiseSecureHandler( | ||
187 | string httpMethod, string path, | ||
188 | RestDeserialiseMethod<TRequest, TResponse> method, CheckIdentityMethod smethod) | ||
189 | : base(httpMethod, path) | ||
190 | { | ||
191 | m_smethod = smethod; | ||
192 | m_method = method; | ||
193 | } | ||
194 | |||
195 | public void Handle(string path, Stream request, Stream responseStream, | ||
196 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
197 | { | ||
198 | RestSessionObject<TRequest> deserial = default(RestSessionObject<TRequest>); | ||
199 | bool fail = false; | ||
200 | |||
201 | using (XmlTextReader xmlReader = new XmlTextReader(request)) | ||
202 | { | ||
203 | try | ||
204 | { | ||
205 | XmlSerializer deserializer = new XmlSerializer(typeof(RestSessionObject<TRequest>)); | ||
206 | deserial = (RestSessionObject<TRequest>)deserializer.Deserialize(xmlReader); | ||
207 | } | ||
208 | catch (Exception e) | ||
209 | { | ||
210 | m_log.Error("[REST]: Deserialization problem. Ignoring request. " + e); | ||
211 | fail = true; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | TResponse response = default(TResponse); | ||
216 | if (!fail && m_smethod(deserial.SessionID, deserial.AvatarID)) | ||
217 | { | ||
218 | response = m_method(deserial.Body); | ||
219 | } | ||
220 | |||
221 | using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream)) | ||
222 | { | ||
223 | XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); | ||
224 | serializer.Serialize(xmlWriter, response); | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | public delegate bool CheckTrustedSourceMethod(IPEndPoint peer); | ||
230 | |||
231 | public class RestDeserialiseTrustedHandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler | ||
232 | where TRequest : new() | ||
233 | { | ||
234 | private static readonly ILog m_log | ||
235 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
236 | |||
237 | /// <summary> | ||
238 | /// The operation to perform once trust has been established. | ||
239 | /// </summary> | ||
240 | /// <param name="httpMethod"></param> | ||
241 | /// <param name="path"></param> | ||
242 | /// <param name="method"></param> | ||
243 | /// <param name="tmethod"></param> | ||
244 | private RestDeserialiseMethod<TRequest, TResponse> m_method; | ||
245 | |||
246 | /// <summary> | ||
247 | /// The method used to check whether a request is trusted. | ||
248 | /// </summary> | ||
249 | private CheckTrustedSourceMethod m_tmethod; | ||
250 | |||
251 | public RestDeserialiseTrustedHandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method, CheckTrustedSourceMethod tmethod) | ||
252 | : base(httpMethod, path) | ||
253 | { | ||
254 | m_tmethod = tmethod; | ||
255 | m_method = method; | ||
256 | } | ||
257 | |||
258 | public void Handle(string path, Stream request, Stream responseStream, | ||
259 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
260 | { | ||
261 | TRequest deserial = default(TRequest); | ||
262 | bool fail = false; | ||
263 | |||
264 | using (XmlTextReader xmlReader = new XmlTextReader(request)) | ||
265 | { | ||
266 | try | ||
267 | { | ||
268 | XmlSerializer deserializer = new XmlSerializer(typeof(TRequest)); | ||
269 | deserial = (TRequest)deserializer.Deserialize(xmlReader); | ||
270 | } | ||
271 | catch (Exception e) | ||
272 | { | ||
273 | m_log.Error("[REST]: Deserialization problem. Ignoring request. " + e); | ||
274 | fail = true; | ||
275 | } | ||
276 | } | ||
277 | |||
278 | TResponse response = default(TResponse); | ||
279 | if (!fail && m_tmethod(httpRequest.RemoteIPEndPoint)) | ||
280 | { | ||
281 | response = m_method(deserial); | ||
282 | } | ||
283 | |||
284 | using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream)) | ||
285 | { | ||
286 | XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); | ||
287 | serializer.Serialize(xmlWriter, response); | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | |||
292 | } | ||