1
0

ServerUtils.cs 10 KB

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