ServerUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. using System;
  28. using System.IO;
  29. using System.Reflection;
  30. using System.Xml;
  31. using System.Xml.Serialization;
  32. using System.Text;
  33. using System.Collections.Generic;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenMetaverse;
  37. namespace OpenSim.Server.Base
  38. {
  39. public static class ServerUtils
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public static byte[] SerializeResult(XmlSerializer xs, object data)
  43. {
  44. MemoryStream ms = new MemoryStream();
  45. XmlTextWriter xw = new XmlTextWriter(ms, Util.UTF8);
  46. xw.Formatting = Formatting.Indented;
  47. xs.Serialize(xw, data);
  48. xw.Flush();
  49. ms.Seek(0, SeekOrigin.Begin);
  50. byte[] ret = ms.GetBuffer();
  51. Array.Resize(ref ret, (int)ms.Length);
  52. return ret;
  53. }
  54. /// <summary>
  55. /// Load a plugin from a dll with the given class or interface
  56. /// </summary>
  57. /// <param name="dllName"></param>
  58. /// <param name="args">The arguments which control which constructor is invoked on the plugin</param>
  59. /// <returns></returns>
  60. public static T LoadPlugin<T>(string dllName, Object[] args) where T:class
  61. {
  62. string[] parts = dllName.Split(new char[] {':'});
  63. dllName = parts[0];
  64. string className = String.Empty;
  65. if (parts.Length > 1)
  66. className = parts[1];
  67. return LoadPlugin<T>(dllName, className, args);
  68. }
  69. /// <summary>
  70. /// Load a plugin from a dll with the given class or interface
  71. /// </summary>
  72. /// <param name="dllName"></param>
  73. /// <param name="className"></param>
  74. /// <param name="args">The arguments which control which constructor is invoked on the plugin</param>
  75. /// <returns></returns>
  76. public static T LoadPlugin<T>(string dllName, string className, Object[] args) where T:class
  77. {
  78. string interfaceName = typeof(T).ToString();
  79. try
  80. {
  81. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  82. foreach (Type pluginType in pluginAssembly.GetTypes())
  83. {
  84. if (pluginType.IsPublic)
  85. {
  86. if (className != String.Empty
  87. && pluginType.ToString() != pluginType.Namespace + "." + className)
  88. continue;
  89. Type typeInterface = pluginType.GetInterface(interfaceName, true);
  90. if (typeInterface != null)
  91. {
  92. T plug = null;
  93. try
  94. {
  95. plug = (T)Activator.CreateInstance(pluginType,
  96. args);
  97. }
  98. catch (Exception e)
  99. {
  100. if (!(e is System.MissingMethodException))
  101. m_log.ErrorFormat("Error loading plugin from {0}, exception {1}", dllName, e.InnerException);
  102. return null;
  103. }
  104. return plug;
  105. }
  106. }
  107. }
  108. return null;
  109. }
  110. catch (Exception e)
  111. {
  112. m_log.Error(string.Format("Error loading plugin from {0}", dllName), e);
  113. return null;
  114. }
  115. }
  116. public static Dictionary<string, object> ParseQueryString(string query)
  117. {
  118. Dictionary<string, object> result = new Dictionary<string, object>();
  119. string[] terms = query.Split(new char[] {'&'});
  120. if (terms.Length == 0)
  121. return result;
  122. foreach (string t in terms)
  123. {
  124. string[] elems = t.Split(new char[] {'='});
  125. if (elems.Length == 0)
  126. continue;
  127. string name = System.Web.HttpUtility.UrlDecode(elems[0]);
  128. string value = String.Empty;
  129. if (elems.Length > 1)
  130. value = System.Web.HttpUtility.UrlDecode(elems[1]);
  131. if (name.EndsWith("[]"))
  132. {
  133. string cleanName = name.Substring(0, name.Length - 2);
  134. if (result.ContainsKey(cleanName))
  135. {
  136. if (!(result[cleanName] is List<string>))
  137. continue;
  138. List<string> l = (List<string>)result[cleanName];
  139. l.Add(value);
  140. }
  141. else
  142. {
  143. List<string> newList = new List<string>();
  144. newList.Add(value);
  145. result[cleanName] = newList;
  146. }
  147. }
  148. else
  149. {
  150. if (!result.ContainsKey(name))
  151. result[name] = value;
  152. }
  153. }
  154. return result;
  155. }
  156. public static string BuildQueryString(Dictionary<string, object> data)
  157. {
  158. string qstring = String.Empty;
  159. string part;
  160. foreach (KeyValuePair<string, object> kvp in data)
  161. {
  162. if (kvp.Value is List<string>)
  163. {
  164. List<string> l = (List<String>)kvp.Value;
  165. foreach (string s in l)
  166. {
  167. part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
  168. "[]=" + System.Web.HttpUtility.UrlEncode(s);
  169. if (qstring != String.Empty)
  170. qstring += "&";
  171. qstring += part;
  172. }
  173. }
  174. else
  175. {
  176. if (kvp.Value.ToString() != String.Empty)
  177. {
  178. part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
  179. "=" + System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
  180. }
  181. else
  182. {
  183. part = System.Web.HttpUtility.UrlEncode(kvp.Key);
  184. }
  185. if (qstring != String.Empty)
  186. qstring += "&";
  187. qstring += part;
  188. }
  189. }
  190. return qstring;
  191. }
  192. public static string BuildXmlResponse(Dictionary<string, object> data)
  193. {
  194. XmlDocument doc = new XmlDocument();
  195. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  196. "", "");
  197. doc.AppendChild(xmlnode);
  198. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  199. "");
  200. doc.AppendChild(rootElement);
  201. BuildXmlData(rootElement, data);
  202. return doc.InnerXml;
  203. }
  204. private static void BuildXmlData(XmlElement parent, Dictionary<string, object> data)
  205. {
  206. foreach (KeyValuePair<string, object> kvp in data)
  207. {
  208. if (kvp.Value == null)
  209. continue;
  210. XmlElement elem = parent.OwnerDocument.CreateElement("",
  211. kvp.Key, "");
  212. if (kvp.Value is Dictionary<string, object>)
  213. {
  214. XmlAttribute type = parent.OwnerDocument.CreateAttribute("",
  215. "type", "");
  216. type.Value = "List";
  217. elem.Attributes.Append(type);
  218. BuildXmlData(elem, (Dictionary<string, object>)kvp.Value);
  219. }
  220. else
  221. {
  222. elem.AppendChild(parent.OwnerDocument.CreateTextNode(
  223. kvp.Value.ToString()));
  224. }
  225. parent.AppendChild(elem);
  226. }
  227. }
  228. public static Dictionary<string, object> ParseXmlResponse(string data)
  229. {
  230. //m_log.DebugFormat("[XXX]: received xml string: {0}", data);
  231. Dictionary<string, object> ret = new Dictionary<string, object>();
  232. XmlDocument doc = new XmlDocument();
  233. doc.LoadXml(data);
  234. XmlNodeList rootL = doc.GetElementsByTagName("ServerResponse");
  235. if (rootL.Count != 1)
  236. return ret;
  237. XmlNode rootNode = rootL[0];
  238. ret = ParseElement(rootNode);
  239. return ret;
  240. }
  241. private static Dictionary<string, object> ParseElement(XmlNode element)
  242. {
  243. Dictionary<string, object> ret = new Dictionary<string, object>();
  244. XmlNodeList partL = element.ChildNodes;
  245. foreach (XmlNode part in partL)
  246. {
  247. XmlNode type = part.Attributes.GetNamedItem("type");
  248. if (type == null || type.Value != "List")
  249. {
  250. ret[part.Name] = part.InnerText;
  251. }
  252. else
  253. {
  254. ret[part.Name] = ParseElement(part);
  255. }
  256. }
  257. return ret;
  258. }
  259. }
  260. }