From 7592a033df4f81ba985e1d6f57ad0ef488dc5745 Mon Sep 17 00:00:00 2001
From: Diva Canto
Date: Tue, 12 Apr 2011 20:24:28 -0700
Subject: Moved 3 request handlers from OpenSim.Framework.Servers.HttpServer up
to OpenSim.Framework -- just pasted them in WebUtil. This is so that code
that uses the Service connectors don't need to include the HttpServer dll --
that was odd.
---
OpenSim/Framework/WebUtil.cs | 335 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 335 insertions(+)
(limited to 'OpenSim/Framework/WebUtil.cs')
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index 1feeeb3..64cd014 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -36,6 +36,9 @@ using System.Net.Security;
using System.Reflection;
using System.Text;
using System.Web;
+using System.Xml;
+using System.Xml.Serialization;
+
using log4net;
using OpenSim.Framework.Servers.HttpServer;
using OpenMetaverse.StructuredData;
@@ -625,4 +628,336 @@ namespace OpenSim.Framework
}
+
+ public static class AsynchronousRestObjectRequester
+ {
+ private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
+ ///
+ /// Perform an asynchronous REST request.
+ ///
+ /// GET or POST
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// Thrown if we encounter a
+ /// network issue while posting the request. You'll want to make
+ /// sure you deal with this as they're not uncommon
+ //
+ public static void MakeRequest(string verb,
+ string requestUrl, TRequest obj, Action action)
+ {
+ // m_log.DebugFormat("[ASYNC REQUEST]: Starting {0} {1}", verb, requestUrl);
+
+ Type type = typeof(TRequest);
+
+ WebRequest request = WebRequest.Create(requestUrl);
+ WebResponse response = null;
+ TResponse deserial = default(TResponse);
+ XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
+
+ request.Method = verb;
+
+ if (verb == "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;
+
+ request.BeginGetRequestStream(delegate(IAsyncResult res)
+ {
+ Stream requestStream = request.EndGetRequestStream(res);
+
+ requestStream.Write(buffer.ToArray(), 0, length);
+ requestStream.Close();
+
+ request.BeginGetResponse(delegate(IAsyncResult ar)
+ {
+ response = request.EndGetResponse(ar);
+ Stream respStream = null;
+ try
+ {
+ respStream = response.GetResponseStream();
+ deserial = (TResponse)deserializer.Deserialize(
+ respStream);
+ }
+ catch (System.InvalidOperationException)
+ {
+ }
+ finally
+ {
+ // Let's not close this
+ //buffer.Close();
+ respStream.Close();
+ response.Close();
+ }
+
+ action(deserial);
+
+ }, null);
+ }, null);
+
+
+ return;
+ }
+
+ request.BeginGetResponse(delegate(IAsyncResult res2)
+ {
+ try
+ {
+ // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
+ // documented in MSDN
+ response = request.EndGetResponse(res2);
+
+ Stream respStream = null;
+ try
+ {
+ respStream = response.GetResponseStream();
+ deserial = (TResponse)deserializer.Deserialize(respStream);
+ }
+ catch (System.InvalidOperationException)
+ {
+ }
+ finally
+ {
+ respStream.Close();
+ response.Close();
+ }
+ }
+ catch (WebException e)
+ {
+ if (e.Status == WebExceptionStatus.ProtocolError)
+ {
+ if (e.Response is HttpWebResponse)
+ {
+ HttpWebResponse httpResponse = (HttpWebResponse)e.Response;
+
+ if (httpResponse.StatusCode != HttpStatusCode.NotFound)
+ {
+ // We don't appear to be handling any other status codes, so log these feailures to that
+ // people don't spend unnecessary hours hunting phantom bugs.
+ m_log.DebugFormat(
+ "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}",
+ verb, requestUrl, httpResponse.StatusCode);
+ }
+ }
+ }
+ else
+ {
+ m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", verb, requestUrl, e.Status, e.Message);
+ }
+ }
+ catch (Exception e)
+ {
+ m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with exception {2}", verb, requestUrl, e);
+ }
+
+ // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
+
+ try
+ {
+ action(deserial);
+ }
+ catch (Exception e)
+ {
+ m_log.ErrorFormat(
+ "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}", verb, requestUrl, e);
+ }
+
+ }, null);
+ }
+ }
+
+ public static class SynchronousRestFormsRequester
+ {
+ private static readonly ILog m_log =
+ LogManager.GetLogger(
+ MethodBase.GetCurrentMethod().DeclaringType);
+
+ ///
+ /// Perform a synchronous REST request.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// Thrown if we encounter a network issue while posting
+ /// the request. You'll want to make sure you deal with this as they're not uncommon
+ public static string MakeRequest(string verb, string requestUrl, string obj)
+ {
+ WebRequest request = WebRequest.Create(requestUrl);
+ request.Method = verb;
+ string respstring = String.Empty;
+
+ using (MemoryStream buffer = new MemoryStream())
+ {
+ if ((verb == "POST") || (verb == "PUT"))
+ {
+ request.ContentType = "text/www-form-urlencoded";
+
+ int length = 0;
+ using (StreamWriter writer = new StreamWriter(buffer))
+ {
+ writer.Write(obj);
+ writer.Flush();
+ }
+
+ length = (int)obj.Length;
+ request.ContentLength = length;
+
+ Stream requestStream = null;
+ try
+ {
+ requestStream = request.GetRequestStream();
+ requestStream.Write(buffer.ToArray(), 0, length);
+ }
+ catch (Exception e)
+ {
+ m_log.DebugFormat("[FORMS]: exception occured on sending request to {0}: " + e.ToString(), requestUrl);
+ }
+ finally
+ {
+ if (requestStream != null)
+ requestStream.Close();
+ }
+ }
+
+ try
+ {
+ using (WebResponse resp = request.GetResponse())
+ {
+ if (resp.ContentLength != 0)
+ {
+ Stream respStream = null;
+ try
+ {
+ respStream = resp.GetResponseStream();
+ using (StreamReader reader = new StreamReader(respStream))
+ {
+ respstring = reader.ReadToEnd();
+ }
+ }
+ catch (Exception e)
+ {
+ m_log.DebugFormat("[FORMS]: exception occured on receiving reply " + e.ToString());
+ }
+ finally
+ {
+ if (respStream != null)
+ respStream.Close();
+ }
+ }
+ }
+ }
+ catch (System.InvalidOperationException)
+ {
+ // This is what happens when there is invalid XML
+ m_log.DebugFormat("[FORMS]: InvalidOperationException on receiving request");
+ }
+ }
+ return respstring;
+ }
+ }
+
+ public class SynchronousRestObjectPoster
+ {
+ [Obsolete]
+ public static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj)
+ {
+ return SynchronousRestObjectRequester.MakeRequest(verb, requestUrl, obj);
+ }
+ }
+
+ public class SynchronousRestObjectRequester
+ {
+ ///
+ /// Perform a synchronous REST request.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ /// Thrown if we encounter a network issue while posting
+ /// the request. You'll want to make sure you deal with this as they're not uncommon
+ public static TResponse MakeRequest(string verb, string requestUrl, TRequest obj)
+ {
+ Type type = typeof(TRequest);
+ TResponse deserial = default(TResponse);
+
+ WebRequest request = WebRequest.Create(requestUrl);
+ request.Method = verb;
+
+ if ((verb == "POST") || (verb == "PUT"))
+ {
+ 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 = null;
+ try
+ {
+ requestStream = request.GetRequestStream();
+ requestStream.Write(buffer.ToArray(), 0, length);
+ }
+ catch (Exception)
+ {
+ return deserial;
+ }
+ finally
+ {
+ if (requestStream != null)
+ requestStream.Close();
+ }
+ }
+
+ try
+ {
+ using (WebResponse resp = request.GetResponse())
+ {
+ if (resp.ContentLength > 0)
+ {
+ Stream respStream = resp.GetResponseStream();
+ XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
+ deserial = (TResponse)deserializer.Deserialize(respStream);
+ respStream.Close();
+ }
+ }
+ }
+ catch (System.InvalidOperationException)
+ {
+ // This is what happens when there is invalid XML
+ }
+ return deserial;
+ }
+ }
}
--
cgit v1.1