ServerUtils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, object> ParseQueryString(string query)
  147. {
  148. Dictionary<string, object> result = new Dictionary<string, object>();
  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. if (name.EndsWith("[]"))
  162. {
  163. if (result.ContainsKey(name))
  164. {
  165. if (!(result[name] is List<string>))
  166. continue;
  167. List<string> l = (List<string>)result[name];
  168. l.Add(value);
  169. }
  170. else
  171. {
  172. List<string> newList = new List<string>();
  173. newList.Add(value);
  174. result[name] = newList;
  175. }
  176. }
  177. else
  178. {
  179. if (!result.ContainsKey(name))
  180. result[name] = value;
  181. }
  182. }
  183. return result;
  184. }
  185. public static string BuildQueryString(Dictionary<string, object> data)
  186. {
  187. string qstring = String.Empty;
  188. string part;
  189. foreach (KeyValuePair<string, object> kvp in data)
  190. {
  191. if (kvp.Value is List<string>)
  192. {
  193. List<string> l = (List<String>)kvp.Value;
  194. foreach (string s in l)
  195. {
  196. part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
  197. "[]=" + System.Web.HttpUtility.UrlEncode(s);
  198. if (qstring != String.Empty)
  199. qstring += "&";
  200. qstring += part;
  201. }
  202. }
  203. else
  204. {
  205. if (kvp.Value.ToString() != String.Empty)
  206. {
  207. part = System.Web.HttpUtility.UrlEncode(kvp.Key) +
  208. "=" + System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
  209. }
  210. else
  211. {
  212. part = System.Web.HttpUtility.UrlEncode(kvp.Key);
  213. }
  214. if (qstring != String.Empty)
  215. qstring += "&";
  216. qstring += part;
  217. }
  218. }
  219. return qstring;
  220. }
  221. public static string BuildXmlResponse(Dictionary<string, object> data)
  222. {
  223. XmlDocument doc = new XmlDocument();
  224. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  225. "", "");
  226. doc.AppendChild(xmlnode);
  227. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  228. "");
  229. doc.AppendChild(rootElement);
  230. BuildXmlData(rootElement, data);
  231. return doc.InnerXml;
  232. }
  233. private static void BuildXmlData(XmlElement parent, Dictionary<string, object> data)
  234. {
  235. foreach (KeyValuePair<string, object> kvp in data)
  236. {
  237. XmlElement elem = parent.OwnerDocument.CreateElement("",
  238. kvp.Key, "");
  239. if (kvp.Value is Dictionary<string, object>)
  240. {
  241. XmlAttribute type = parent.OwnerDocument.CreateAttribute("",
  242. "type", "");
  243. type.Value = "List";
  244. elem.Attributes.Append(type);
  245. BuildXmlData(elem, (Dictionary<string, object>)kvp.Value);
  246. }
  247. else
  248. {
  249. elem.AppendChild(parent.OwnerDocument.CreateTextNode(
  250. kvp.Value.ToString()));
  251. }
  252. parent.AppendChild(elem);
  253. }
  254. }
  255. public static Dictionary<string, object> ParseXmlResponse(string data)
  256. {
  257. //m_log.DebugFormat("[XXX]: received xml string: {0}", data);
  258. Dictionary<string, object> ret = new Dictionary<string, object>();
  259. XmlDocument doc = new XmlDocument();
  260. doc.LoadXml(data);
  261. XmlNodeList rootL = doc.GetElementsByTagName("ServerResponse");
  262. if (rootL.Count != 1)
  263. return ret;
  264. XmlNode rootNode = rootL[0];
  265. ret = ParseElement(rootNode);
  266. return ret;
  267. }
  268. private static Dictionary<string, object> ParseElement(XmlNode element)
  269. {
  270. Dictionary<string, object> ret = new Dictionary<string, object>();
  271. XmlNodeList partL = element.ChildNodes;
  272. foreach (XmlNode part in partL)
  273. {
  274. XmlNode type = part.Attributes.GetNamedItem("type");
  275. if (type == null || type.Value != "List")
  276. {
  277. ret[part.Name] = part.InnerText;
  278. }
  279. else
  280. {
  281. ret[part.Name] = ParseElement(part);
  282. }
  283. }
  284. return ret;
  285. }
  286. }
  287. }