opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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  
37 namespace OpenSim.Framework.Servers.HttpServer
38 {
39 public class RestSessionObject<TRequest>
40 {
41 private string sid;
42 private string aid;
43 private TRequest request_body;
44  
45 public string SessionID
46 {
47 get { return sid; }
48 set { sid = value; }
49 }
50  
51 public string AvatarID
52 {
53 get { return aid; }
54 set { aid = value; }
55 }
56  
57 public TRequest Body
58 {
59 get { return request_body; }
60 set { request_body = value; }
61 }
62 }
63  
64 public class SynchronousRestSessionObjectPoster<TRequest, TResponse>
65 {
66 public static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid)
67 {
68 RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>();
69 sobj.SessionID = sid;
70 sobj.AvatarID = aid;
71 sobj.Body = obj;
72  
73 Type type = typeof(RestSessionObject<TRequest>);
74  
75 WebRequest request = WebRequest.Create(requestUrl);
76 request.Method = verb;
77 request.ContentType = "text/xml";
78 request.Timeout = 20000;
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 buffer.Close();
98 requestStream.Close();
99  
100 TResponse deserial = default(TResponse);
101 using (WebResponse resp = request.GetResponse())
102 {
103 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
104  
105 using (Stream respStream = resp.GetResponseStream())
106 deserial = (TResponse)deserializer.Deserialize(respStream);
107 }
108  
109 return deserial;
110 }
111 }
112  
113 public class RestSessionObjectPosterResponse<TRequest, TResponse>
114 {
115 public ReturnResponse<TResponse> ResponseCallback;
116  
117 public void BeginPostObject(string requestUrl, TRequest obj, string sid, string aid)
118 {
119 BeginPostObject("POST", requestUrl, obj, sid, aid);
120 }
121  
122 public void BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid)
123 {
124 RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>();
125 sobj.SessionID = sid;
126 sobj.AvatarID = aid;
127 sobj.Body = obj;
128  
129 Type type = typeof(RestSessionObject<TRequest>);
130  
131 WebRequest request = WebRequest.Create(requestUrl);
132 request.Method = verb;
133 request.ContentType = "text/xml";
134 request.Timeout = 10000;
135  
136 MemoryStream buffer = new MemoryStream();
137  
138 XmlWriterSettings settings = new XmlWriterSettings();
139 settings.Encoding = Encoding.UTF8;
140  
141 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
142 {
143 XmlSerializer serializer = new XmlSerializer(type);
144 serializer.Serialize(writer, sobj);
145 writer.Flush();
146 }
147 buffer.Close();
148  
149 int length = (int)buffer.Length;
150 request.ContentLength = length;
151  
152 Stream requestStream = request.GetRequestStream();
153 requestStream.Write(buffer.ToArray(), 0, length);
154 requestStream.Close();
155 // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request);
156 request.BeginGetResponse(AsyncCallback, request);
157 }
158  
159 private void AsyncCallback(IAsyncResult result)
160 {
161 WebRequest request = (WebRequest)result.AsyncState;
162 using (WebResponse resp = request.EndGetResponse(result))
163 {
164 TResponse deserial;
165 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
166 Stream stream = resp.GetResponseStream();
167  
168 // This is currently a bad debug stanza since it gobbles us the response...
169 // StreamReader reader = new StreamReader(stream);
170 // m_log.DebugFormat("[REST OBJECT POSTER RESPONSE]: Received {0}", reader.ReadToEnd());
171  
172 deserial = (TResponse)deserializer.Deserialize(stream);
173 if (stream != null)
174 stream.Close();
175  
176 if (deserial != null && ResponseCallback != null)
177 {
178 ResponseCallback(deserial);
179 }
180 }
181 }
182 }
183  
184 public delegate bool CheckIdentityMethod(string sid, string aid);
185  
186 public class RestDeserialiseSecureHandler<TRequest, TResponse> : BaseOutputStreamHandler, IStreamHandler
187 where TRequest : new()
188 {
189 private static readonly ILog m_log
190 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
191  
192 private RestDeserialiseMethod<TRequest, TResponse> m_method;
193 private CheckIdentityMethod m_smethod;
194  
195 public RestDeserialiseSecureHandler(
196 string httpMethod, string path,
197 RestDeserialiseMethod<TRequest, TResponse> method, CheckIdentityMethod smethod)
198 : base(httpMethod, path)
199 {
200 m_smethod = smethod;
201 m_method = method;
202 }
203  
204 protected override void ProcessRequest(string path, Stream request, Stream responseStream,
205 IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
206 {
207 RestSessionObject<TRequest> deserial = default(RestSessionObject<TRequest>);
208 bool fail = false;
209  
210 using (XmlTextReader xmlReader = new XmlTextReader(request))
211 {
212 try
213 {
214 XmlSerializer deserializer = new XmlSerializer(typeof(RestSessionObject<TRequest>));
215 deserial = (RestSessionObject<TRequest>)deserializer.Deserialize(xmlReader);
216 }
217 catch (Exception e)
218 {
219 m_log.Error("[REST]: Deserialization problem. Ignoring request. " + e);
220 fail = true;
221 }
222 }
223  
224 TResponse response = default(TResponse);
225 if (!fail && m_smethod(deserial.SessionID, deserial.AvatarID))
226 {
227 response = m_method(deserial.Body);
228 }
229  
230 using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream))
231 {
232 XmlSerializer serializer = new XmlSerializer(typeof(TResponse));
233 serializer.Serialize(xmlWriter, response);
234 }
235 }
236 }
237  
238 public delegate bool CheckTrustedSourceMethod(IPEndPoint peer);
239  
240 public class RestDeserialiseTrustedHandler<TRequest, TResponse> : BaseOutputStreamHandler, IStreamHandler
241 where TRequest : new()
242 {
243 private static readonly ILog m_log
244 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
245  
246 /// <summary>
247 /// The operation to perform once trust has been established.
248 /// </summary>
249 private RestDeserialiseMethod<TRequest, TResponse> m_method;
250  
251 /// <summary>
252 /// The method used to check whether a request is trusted.
253 /// </summary>
254 private CheckTrustedSourceMethod m_tmethod;
255  
256 public RestDeserialiseTrustedHandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method, CheckTrustedSourceMethod tmethod)
257 : base(httpMethod, path)
258 {
259 m_tmethod = tmethod;
260 m_method = method;
261 }
262  
263 protected override void ProcessRequest(string path, Stream request, Stream responseStream,
264 IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
265 {
266 TRequest deserial = default(TRequest);
267 bool fail = false;
268  
269 using (XmlTextReader xmlReader = new XmlTextReader(request))
270 {
271 try
272 {
273 XmlSerializer deserializer = new XmlSerializer(typeof(TRequest));
274 deserial = (TRequest)deserializer.Deserialize(xmlReader);
275 }
276 catch (Exception e)
277 {
278 m_log.Error("[REST]: Deserialization problem. Ignoring request. " + e);
279 fail = true;
280 }
281 }
282  
283 TResponse response = default(TResponse);
284 if (!fail && m_tmethod(httpRequest.RemoteIPEndPoint))
285 {
286 response = m_method(deserial);
287 }
288  
289 using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream))
290 {
291 XmlSerializer serializer = new XmlSerializer(typeof(TResponse));
292 serializer.Serialize(xmlWriter, response);
293 }
294 }
295 }
296 }