31 using System.Reflection;
34 using System.Xml.Serialization;
37 namespace OpenSim.Framework.Servers.HttpServer
39 public class RestSessionObject<TRequest>
43 private TRequest request_body;
45 public string SessionID
51 public string AvatarID
59 get {
return request_body; }
60 set { request_body = value; }
64 public class SynchronousRestSessionObjectPoster<TRequest, TResponse>
66 public static TResponse
BeginPostObject(
string verb,
string requestUrl, TRequest obj,
string sid,
string aid)
75 WebRequest request = WebRequest.Create(requestUrl);
76 request.Method = verb;
77 request.ContentType =
"text/xml";
78 request.Timeout = 20000;
80 using (MemoryStream buffer =
new MemoryStream())
82 XmlWriterSettings settings =
new XmlWriterSettings();
83 settings.Encoding = Encoding.UTF8;
85 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
87 XmlSerializer serializer =
new XmlSerializer(type);
88 serializer.Serialize(writer, sobj);
92 int length = (int)buffer.Length;
93 request.ContentLength = length;
95 using (Stream requestStream = request.GetRequestStream())
96 requestStream.Write(buffer.ToArray(), 0, length);
99 TResponse deserial =
default(TResponse);
100 using (WebResponse resp = request.GetResponse())
102 XmlSerializer deserializer =
new XmlSerializer(typeof(TResponse));
104 using (Stream respStream = resp.GetResponseStream())
105 deserial = (TResponse)deserializer.Deserialize(respStream);
112 public class RestSessionObjectPosterResponse<TRequest, TResponse>
118 BeginPostObject(
"POST", requestUrl, obj, sid, aid);
121 public void BeginPostObject(
string verb,
string requestUrl, TRequest obj,
string sid,
string aid)
124 sobj.SessionID = sid;
130 WebRequest request = WebRequest.Create(requestUrl);
131 request.Method = verb;
132 request.ContentType =
"text/xml";
133 request.Timeout = 10000;
135 using (MemoryStream buffer =
new MemoryStream())
137 XmlWriterSettings settings =
new XmlWriterSettings();
138 settings.Encoding = Encoding.UTF8;
140 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
142 XmlSerializer serializer =
new XmlSerializer(type);
143 serializer.Serialize(writer, sobj);
147 int length = (int)buffer.Length;
148 request.ContentLength = length;
150 using (Stream requestStream = request.GetRequestStream())
151 requestStream.Write(buffer.ToArray(), 0, length);
155 request.BeginGetResponse(AsyncCallback, request);
158 private void AsyncCallback(IAsyncResult result)
160 WebRequest request = (WebRequest)result.AsyncState;
161 using (WebResponse resp = request.EndGetResponse(result))
164 XmlSerializer deserializer =
new XmlSerializer(typeof(TResponse));
165 Stream stream = resp.GetResponseStream();
171 deserial = (TResponse)deserializer.Deserialize(stream);
175 if (deserial != null && ResponseCallback != null)
177 ResponseCallback(deserial);
186 where TRequest : new()
188 private static readonly ILog m_log
189 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
195 string httpMethod,
string path,
197 : base(httpMethod, path)
203 protected override void ProcessRequest(
string path, Stream request, Stream responseStream,
209 using (XmlTextReader xmlReader =
new XmlTextReader(request))
218 m_log.Error(
"[REST]: Deserialization problem. Ignoring request. " + e);
223 TResponse response =
default(TResponse);
226 response = m_method(deserial.
Body);
229 using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream))
231 XmlSerializer serializer =
new XmlSerializer(typeof(TResponse));
232 serializer.Serialize(xmlWriter, response);
240 where TRequest : new()
242 private static readonly ILog m_log
243 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
256 : base(httpMethod, path)
262 protected override void ProcessRequest(
string path, Stream request, Stream responseStream,
265 TRequest deserial =
default(TRequest);
268 using (XmlTextReader xmlReader =
new XmlTextReader(request))
272 XmlSerializer deserializer =
new XmlSerializer(typeof(TRequest));
273 deserial = (TRequest)deserializer.Deserialize(xmlReader);
277 m_log.Error(
"[REST]: Deserialization problem. Ignoring request. " + e);
282 TResponse response =
default(TResponse);
285 response = m_method(deserial);
288 using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream))
290 XmlSerializer serializer =
new XmlSerializer(typeof(TResponse));
291 serializer.Serialize(xmlWriter, response);
delegate bool CheckIdentityMethod(string sid, string aid)
Base handler for writing to an output stream
static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid)
IPEndPoint RemoteIPEndPoint
RestDeserialiseSecureHandler(string httpMethod, string path, RestDeserialiseMethod< TRequest, TResponse > method, CheckIdentityMethod smethod)
delegate bool CheckTrustedSourceMethod(IPEndPoint peer)
ReturnResponse< TResponse > ResponseCallback
void BeginPostObject(string requestUrl, TRequest obj, string sid, string aid)
delegate TResponse RestDeserialiseMethod< TRequest, TResponse >(TRequest request)
override void ProcessRequest(string path, Stream request, Stream responseStream, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
override void ProcessRequest(string path, Stream request, Stream responseStream, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
RestDeserialiseTrustedHandler(string httpMethod, string path, RestDeserialiseMethod< TRequest, TResponse > method, CheckTrustedSourceMethod tmethod)
void BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid)