ExternalRepresentationUtils.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Services.Interfaces;
  36. namespace OpenSim.Framework.Serialization.External
  37. {
  38. /// <summary>
  39. /// Utilities for manipulating external representations of data structures in OpenSim
  40. /// </summary>
  41. public class ExternalRepresentationUtils
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// Populate a node with data read from xml using a dictinoary of processors
  46. /// </summary>
  47. /// <param name="nodeToFill"></param>
  48. /// <param name="processors"></param>
  49. /// <param name="xtr"></param>
  50. /// <returns>true on successful, false if there were any processing failures</returns>
  51. public static bool ExecuteReadProcessors<NodeType>(
  52. NodeType nodeToFill, Dictionary<string, Action<NodeType, XmlReader>> processors, XmlReader xtr)
  53. {
  54. return ExecuteReadProcessors(
  55. nodeToFill,
  56. processors,
  57. xtr,
  58. (o, nodeName, e) => {
  59. m_log.Debug(string.Format("[ExternalRepresentationUtils]: Error while parsing element {0} ",
  60. nodeName), e);
  61. });
  62. }
  63. /// <summary>
  64. /// Populate a node with data read from xml using a dictinoary of processors
  65. /// </summary>
  66. /// <param name="nodeToFill"></param>
  67. /// <param name="processors"></param>
  68. /// <param name="xtr"></param>
  69. /// <param name="parseExceptionAction">
  70. /// Action to take if there is a parsing problem. This will usually just be to log the exception
  71. /// </param>
  72. /// <returns>true on successful, false if there were any processing failures</returns>
  73. public static bool ExecuteReadProcessors<NodeType>(
  74. NodeType nodeToFill,
  75. Dictionary<string, Action<NodeType, XmlReader>> processors,
  76. XmlReader xtr,
  77. Action<NodeType, string, Exception> parseExceptionAction)
  78. {
  79. bool errors = false;
  80. int numErrors = 0;
  81. Stopwatch timer = new Stopwatch();
  82. timer.Start();
  83. string nodeName = string.Empty;
  84. while (xtr.NodeType != XmlNodeType.EndElement)
  85. {
  86. nodeName = xtr.Name;
  87. // m_log.DebugFormat("[ExternalRepresentationUtils]: Processing node: {0}", nodeName);
  88. Action<NodeType, XmlReader> p = null;
  89. if (processors.TryGetValue(xtr.Name, out p))
  90. {
  91. // m_log.DebugFormat("[ExternalRepresentationUtils]: Found processor for {0}", nodeName);
  92. try
  93. {
  94. p(nodeToFill, xtr);
  95. }
  96. catch (Exception e)
  97. {
  98. errors = true;
  99. parseExceptionAction(nodeToFill, nodeName, e);
  100. if (xtr.EOF)
  101. {
  102. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to unexpected end of XML");
  103. break;
  104. }
  105. if (++numErrors == 10)
  106. {
  107. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to too many parsing errors");
  108. break;
  109. }
  110. if (xtr.NodeType == XmlNodeType.EndElement)
  111. xtr.Read();
  112. }
  113. }
  114. else
  115. {
  116. // m_log.DebugFormat("[ExternalRepresentationUtils]: found unknown element \"{0}\"", nodeName);
  117. xtr.ReadOuterXml(); // ignore
  118. }
  119. if (timer.Elapsed.TotalSeconds >= 60)
  120. {
  121. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to timeout");
  122. errors = true;
  123. break;
  124. }
  125. }
  126. return errors;
  127. }
  128. /// <summary>
  129. /// Takes a XML representation of a SceneObjectPart and returns another XML representation
  130. /// with creator data added to it.
  131. /// </summary>
  132. /// <param name="xml">The SceneObjectPart represented in XML2</param>
  133. /// <param name="homeURL">The URL of the user agents service (home) for the creator</param>
  134. /// <param name="userService">The service for retrieving user account information</param>
  135. /// <param name="scopeID">The scope of the user account information (Grid ID)</param>
  136. /// <returns>The SceneObjectPart represented in XML2</returns>
  137. [Obsolete("This method is deprecated. Use RewriteSOP instead.")]
  138. public static string RewriteSOP_Old(string xml, string homeURL, IUserAccountService userService, UUID scopeID)
  139. {
  140. if (xml == string.Empty || homeURL == string.Empty || userService == null)
  141. return xml;
  142. XmlDocument doc = new XmlDocument();
  143. doc.LoadXml(xml);
  144. XmlNodeList sops = doc.GetElementsByTagName("SceneObjectPart");
  145. foreach (XmlNode sop in sops)
  146. {
  147. UserAccount creator = null;
  148. bool hasCreatorData = false;
  149. XmlNodeList nodes = sop.ChildNodes;
  150. foreach (XmlNode node in nodes)
  151. {
  152. if (node.Name == "CreatorID")
  153. {
  154. UUID uuid = UUID.Zero;
  155. UUID.TryParse(node.InnerText, out uuid);
  156. creator = userService.GetUserAccount(scopeID, uuid);
  157. }
  158. if (node.Name == "CreatorData" && node.InnerText != null && node.InnerText != string.Empty)
  159. hasCreatorData = true;
  160. //if (node.Name == "OwnerID")
  161. //{
  162. // UserAccount owner = GetUser(node.InnerText);
  163. // if (owner != null)
  164. // node.InnerText = m_ProfileServiceURL + "/" + node.InnerText + "/" + owner.FirstName + " " + owner.LastName;
  165. //}
  166. }
  167. if (!hasCreatorData && creator != null)
  168. {
  169. XmlElement creatorData = doc.CreateElement("CreatorData");
  170. creatorData.InnerText = CalcCreatorData(homeURL, creator.FirstName + " " + creator.LastName);
  171. sop.AppendChild(creatorData);
  172. }
  173. }
  174. using (StringWriter wr = new StringWriter())
  175. {
  176. doc.Save(wr);
  177. return wr.ToString();
  178. }
  179. }
  180. /// <summary>
  181. /// Takes a XML representation of a SceneObjectPart and returns another XML representation
  182. /// with creator data added to it.
  183. /// </summary>
  184. /// <param name="xml">The SceneObjectPart represented in XML2</param>
  185. /// <param name="sceneName">An identifier for the component that's calling this function</param>
  186. /// <param name="homeURL">The URL of the user agents service (home) for the creator</param>
  187. /// <param name="userService">The service for retrieving user account information</param>
  188. /// <param name="scopeID">The scope of the user account information (Grid ID)</param>
  189. /// <returns>The SceneObjectPart represented in XML2</returns>
  190. public static string RewriteSOP(string xmlData, string sceneName, string homeURL, IUserAccountService userService, UUID scopeID)
  191. {
  192. // Console.WriteLine("Input XML [{0}]", xmlData);
  193. if (xmlData == string.Empty || homeURL == string.Empty || userService == null)
  194. return xmlData;
  195. // Deal with bug introduced in Oct. 20 2014 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  196. // Fix bad assets before sending them elsewhere
  197. xmlData = SanitizeXml(xmlData);
  198. using (StringWriter sw = new StringWriter())
  199. using (XmlTextWriter writer = new XmlTextWriter(sw))
  200. using (XmlTextReader wrappedReader = new XmlTextReader(xmlData, XmlNodeType.Element, null))
  201. using (XmlReader reader = XmlReader.Create(wrappedReader, new XmlReaderSettings() { IgnoreWhitespace = true, ConformanceLevel = ConformanceLevel.Fragment}))
  202. {
  203. TransformXml(reader, writer, sceneName, homeURL, userService, scopeID);
  204. // Console.WriteLine("Output: [{0}]", sw.ToString());
  205. return sw.ToString();
  206. }
  207. }
  208. protected static void TransformXml(XmlReader reader, XmlWriter writer, string sceneName, string homeURI, IUserAccountService userAccountService, UUID scopeID)
  209. {
  210. // m_log.DebugFormat("[HG ASSET MAPPER]: Transforming XML");
  211. int sopDepth = -1;
  212. UserAccount creator = null;
  213. bool hasCreatorData = false;
  214. while (reader.Read())
  215. {
  216. // Console.WriteLine("Depth: {0}, name {1}", reader.Depth, reader.Name);
  217. switch (reader.NodeType)
  218. {
  219. case XmlNodeType.Attribute:
  220. // Console.WriteLine("FOUND ATTRIBUTE {0}", reader.Name);
  221. writer.WriteAttributeString(reader.Name, reader.Value);
  222. break;
  223. case XmlNodeType.CDATA:
  224. writer.WriteCData(reader.Value);
  225. break;
  226. case XmlNodeType.Comment:
  227. writer.WriteComment(reader.Value);
  228. break;
  229. case XmlNodeType.DocumentType:
  230. writer.WriteDocType(reader.Name, reader.Value, null, null);
  231. break;
  232. case XmlNodeType.Element:
  233. // m_log.DebugFormat("Depth {0} at element {1}", reader.Depth, reader.Name);
  234. writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
  235. if (reader.HasAttributes)
  236. {
  237. while (reader.MoveToNextAttribute())
  238. writer.WriteAttributeString(reader.Name, reader.Value);
  239. reader.MoveToElement();
  240. }
  241. if (reader.LocalName == "SceneObjectPart")
  242. {
  243. if (sopDepth < 0)
  244. {
  245. sopDepth = reader.Depth;
  246. // m_log.DebugFormat("[HG ASSET MAPPER]: Set sopDepth to {0}", sopDepth);
  247. }
  248. }
  249. else
  250. {
  251. if (sopDepth >= 0 && reader.Depth == sopDepth + 1)
  252. {
  253. if (reader.Name == "CreatorID")
  254. {
  255. reader.Read();
  256. if (reader.NodeType == XmlNodeType.Element && reader.Name == "Guid" || reader.Name == "UUID")
  257. {
  258. reader.Read();
  259. if (reader.NodeType == XmlNodeType.Text)
  260. {
  261. UUID uuid = UUID.Zero;
  262. UUID.TryParse(reader.Value, out uuid);
  263. creator = userAccountService.GetUserAccount(scopeID, uuid);
  264. writer.WriteElementString("UUID", reader.Value);
  265. reader.Read();
  266. }
  267. else
  268. {
  269. // If we unexpected run across mixed content in this node, still carry on
  270. // transforming the subtree (this replicates earlier behaviour).
  271. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  272. }
  273. }
  274. else
  275. {
  276. // If we unexpected run across mixed content in this node, still carry on
  277. // transforming the subtree (this replicates earlier behaviour).
  278. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  279. }
  280. }
  281. else if (reader.Name == "CreatorData")
  282. {
  283. reader.Read();
  284. if (reader.NodeType == XmlNodeType.Text)
  285. {
  286. hasCreatorData = true;
  287. writer.WriteString(reader.Value);
  288. }
  289. else
  290. {
  291. // If we unexpected run across mixed content in this node, still carry on
  292. // transforming the subtree (this replicates earlier behaviour).
  293. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  294. }
  295. }
  296. }
  297. }
  298. if (reader.IsEmptyElement)
  299. {
  300. // m_log.DebugFormat("[HG ASSET MAPPER]: Writing end for empty element {0}", reader.Name);
  301. writer.WriteEndElement();
  302. }
  303. break;
  304. case XmlNodeType.EndElement:
  305. // m_log.DebugFormat("Depth {0} at EndElement", reader.Depth);
  306. if (sopDepth == reader.Depth)
  307. {
  308. if (!hasCreatorData && creator != null)
  309. writer.WriteElementString(reader.Prefix, "CreatorData", reader.NamespaceURI, string.Format("{0};{1} {2}", homeURI, creator.FirstName, creator.LastName));
  310. // m_log.DebugFormat("[HG ASSET MAPPER]: Reset sopDepth");
  311. sopDepth = -1;
  312. creator = null;
  313. hasCreatorData = false;
  314. }
  315. writer.WriteEndElement();
  316. break;
  317. case XmlNodeType.EntityReference:
  318. writer.WriteEntityRef(reader.Name);
  319. break;
  320. case XmlNodeType.ProcessingInstruction:
  321. writer.WriteProcessingInstruction(reader.Name, reader.Value);
  322. break;
  323. case XmlNodeType.Text:
  324. writer.WriteString(reader.Value);
  325. break;
  326. case XmlNodeType.XmlDeclaration:
  327. // For various reasons, not all serializations have xml declarations (or consistent ones)
  328. // and as it's embedded inside a byte stream we don't need it anyway, so ignore.
  329. break;
  330. default:
  331. m_log.WarnFormat(
  332. "[HG ASSET MAPPER]: Unrecognized node {0} in asset XML transform in {1}",
  333. reader.NodeType, sceneName);
  334. break;
  335. }
  336. }
  337. }
  338. public static string CalcCreatorData(string homeURL, string name)
  339. {
  340. return homeURL + ";" + name;
  341. }
  342. internal static string CalcCreatorData(string homeURL, UUID uuid, string name)
  343. {
  344. return homeURL + "/" + uuid + ";" + name;
  345. }
  346. /// <summary>
  347. /// Sanitation for bug introduced in Oct. 20 2014 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  348. /// </summary>
  349. /// <param name="xmlData"></param>
  350. /// <returns></returns>
  351. public static string SanitizeXml(string xmlData)
  352. {
  353. if (!string.IsNullOrWhiteSpace(xmlData))
  354. {
  355. int indx = xmlData.IndexOf("xmlns:xmlns:");
  356. if(indx > 0)
  357. {
  358. int indx2 = indx + 12;
  359. while(xmlData[indx2 + 5] == ':')
  360. indx2 += 6;
  361. string bad = xmlData.Substring(indx, indx2 - indx);
  362. xmlData = xmlData.Replace(bad, "xmlns:");
  363. }
  364. }
  365. return xmlData;
  366. }
  367. }
  368. }