From 80609c2b1572d9b946675db486fc46e327171cfa Mon Sep 17 00:00:00 2001 From: MW Date: Sun, 2 Dec 2007 13:59:15 +0000 Subject: Some refactoring , mainly on Inventory code. --- OpenSim/Framework/Servers/RestObjectPoster.cs | 93 ---------------------- .../Framework/Servers/RestObjectPosterResponse.cs | 60 ++++++++++++++ .../Servers/SynchronousRestObjectPoster.cs | 48 +++++++++++ 3 files changed, 108 insertions(+), 93 deletions(-) create mode 100644 OpenSim/Framework/Servers/RestObjectPosterResponse.cs create mode 100644 OpenSim/Framework/Servers/SynchronousRestObjectPoster.cs (limited to 'OpenSim/Framework/Servers') diff --git a/OpenSim/Framework/Servers/RestObjectPoster.cs b/OpenSim/Framework/Servers/RestObjectPoster.cs index 07095f3..9bcfcca 100644 --- a/OpenSim/Framework/Servers/RestObjectPoster.cs +++ b/OpenSim/Framework/Servers/RestObjectPoster.cs @@ -5,14 +5,10 @@ using System.Text; using System.Xml; using System.Xml.Serialization; - namespace OpenSim.Framework.Servers { - public delegate void ReturnResponse(T reponse); - public class RestObjectPoster { - public static void BeginPostObject(string requestUrl, TRequest obj) { Type type = typeof(TRequest); @@ -49,93 +45,4 @@ namespace OpenSim.Framework.Servers } } } - - public class RestObjectPosterResponse - { - public ReturnResponse ReturnResponseVal; - - public void BeginPostObject(string requestUrl, TRequest obj) - { - Type type = typeof(TRequest); - - WebRequest request = WebRequest.Create(requestUrl); - request.Method = "POST"; - request.ContentType = "text/xml"; - - MemoryStream buffer = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; - - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); - } - - int length = (int)buffer.Length; - request.ContentLength = length; - - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); - } - - private void AsyncCallback(IAsyncResult result) - { - WebRequest request = (WebRequest)result.AsyncState; - using (WebResponse resp = request.EndGetResponse(result)) - { - TResponse deserial; - XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); - deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); - - if (deserial != null && ReturnResponseVal != null) - { - ReturnResponseVal(deserial); - } - } - } - } - - public class SyncRestObjectPoster - { - - public static TResponse BeginPostObject(string requestUrl, TRequest obj) - { - Type type = typeof(TRequest); - - WebRequest request = WebRequest.Create(requestUrl); - request.Method = "POST"; - request.ContentType = "text/xml"; - - MemoryStream buffer = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; - - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); - } - - int length = (int)buffer.Length; - request.ContentLength = length; - - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - TResponse deserial = default(TResponse); - using (WebResponse resp = request.GetResponse()) - { - - XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); - deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); - } - return deserial; - } - - } } \ No newline at end of file diff --git a/OpenSim/Framework/Servers/RestObjectPosterResponse.cs b/OpenSim/Framework/Servers/RestObjectPosterResponse.cs new file mode 100644 index 0000000..eeae285 --- /dev/null +++ b/OpenSim/Framework/Servers/RestObjectPosterResponse.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Xml; +using System.Xml.Serialization; + +namespace OpenSim.Framework.Servers +{ + public delegate void ReturnResponse(T reponse); + + public class RestObjectPosterResponse + { + public ReturnResponse ResponseCallback; + + public void BeginPostObject(string requestUrl, TRequest obj) + { + Type type = typeof(TRequest); + + WebRequest request = WebRequest.Create(requestUrl); + request.Method = "POST"; + request.ContentType = "text/xml"; + + MemoryStream buffer = new MemoryStream(); + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } + + int length = (int)buffer.Length; + request.ContentLength = length; + + Stream requestStream = request.GetRequestStream(); + requestStream.Write(buffer.ToArray(), 0, length); + IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); + } + + private void AsyncCallback(IAsyncResult result) + { + WebRequest request = (WebRequest)result.AsyncState; + using (WebResponse resp = request.EndGetResponse(result)) + { + TResponse deserial; + XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); + deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); + + if (deserial != null && ResponseCallback != null) + { + ResponseCallback(deserial); + } + } + } + } +} diff --git a/OpenSim/Framework/Servers/SynchronousRestObjectPoster.cs b/OpenSim/Framework/Servers/SynchronousRestObjectPoster.cs new file mode 100644 index 0000000..dde4c02 --- /dev/null +++ b/OpenSim/Framework/Servers/SynchronousRestObjectPoster.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Xml; +using System.Xml.Serialization; + +namespace OpenSim.Framework.Servers +{ + public class SynchronousRestObjectPoster + { + public static TResponse BeginPostObject(string requestUrl, TRequest obj) + { + Type type = typeof(TRequest); + + WebRequest request = WebRequest.Create(requestUrl); + request.Method = "POST"; + request.ContentType = "text/xml"; + + MemoryStream buffer = new MemoryStream(); + + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } + + int length = (int)buffer.Length; + request.ContentLength = length; + + Stream requestStream = request.GetRequestStream(); + requestStream.Write(buffer.ToArray(), 0, length); + TResponse deserial = default(TResponse); + using (WebResponse resp = request.GetResponse()) + { + + XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); + deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); + } + return deserial; + } + + } +} -- cgit v1.1