InventoryArchivePlugin.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Net;
  29. using System.IO;
  30. using System.IO.Compression;
  31. using System.Xml;
  32. using System.Reflection;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Serialization;
  36. using OpenSim.Framework.Serialization.External;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using log4net;
  40. namespace OpenSim.Grid.AssetInventoryServer.Plugins
  41. {
  42. public class InventoryArchivePlugin : IAssetInventoryServerPlugin
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private AssetInventoryServer m_server;
  46. public InventoryArchivePlugin()
  47. {
  48. }
  49. #region IPlugin implementation
  50. public void Initialise(AssetInventoryServer server)
  51. {
  52. m_server = server;
  53. m_server.HttpServer.AddStreamHandler(new GetInventoryArchive(server));
  54. m_log.Info("[INVENTORYARCHIVE]: Inventory Archive loaded.");
  55. }
  56. /// <summary>
  57. /// <para>Initialises asset interface</para>
  58. /// </summary>
  59. public void Initialise()
  60. {
  61. m_log.InfoFormat("[INVENTORYARCHIVE]: {0} cannot be default-initialized!", Name);
  62. throw new PluginNotInitialisedException(Name);
  63. }
  64. public void Dispose()
  65. {
  66. }
  67. public string Version
  68. {
  69. // TODO: this should be something meaningful and not hardcoded?
  70. get { return "0.1"; }
  71. }
  72. public string Name
  73. {
  74. get { return "InventoryArchive"; }
  75. }
  76. #endregion IPlugin implementation
  77. public class GetInventoryArchive : BaseStreamHandler
  78. {
  79. AssetInventoryServer m_server;
  80. //public GetInventoryArchive(AssetInventoryServer server) : base("GET", @"^/inventoryarchive/")
  81. public GetInventoryArchive(AssetInventoryServer server) : base("GET", "/inventoryarchive")
  82. {
  83. m_server = server;
  84. }
  85. public override string ContentType
  86. {
  87. get { return "application/x-compressed"; }
  88. }
  89. public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  90. {
  91. byte[] buffer = new byte[] {};
  92. UUID ownerID;
  93. // Split the URL up to get the asset ID out
  94. string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/');
  95. if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out ownerID))
  96. {
  97. Uri owner = Utils.GetOpenSimUri(ownerID);
  98. InventoryCollection inventory;
  99. BackendResponse storageResponse = m_server.InventoryProvider.TryFetchInventory(owner, out inventory);
  100. if (storageResponse == BackendResponse.Success)
  101. {
  102. m_log.DebugFormat("[INVENTORYARCHIVE]: Archiving inventory for user UUID {0}", ownerID);
  103. buffer = ArchiveInventoryCollection(inventory);
  104. httpResponse.StatusCode = (int) HttpStatusCode.OK;
  105. }
  106. else
  107. {
  108. httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError;
  109. }
  110. }
  111. else
  112. {
  113. m_log.Warn("[INVENTORYARCHIVE]: Unrecognized inventory archive request: " + httpRequest.Url.PathAndQuery);
  114. }
  115. return buffer;
  116. }
  117. }
  118. private static byte[] ArchiveInventoryCollection(InventoryCollection inventory)
  119. {
  120. byte[] buffer = new byte[] {};
  121. // Fill in each folder's Children dictionary.
  122. InventoryFolderWithChildren rootFolder = BuildInventoryHierarchy(ref inventory);
  123. // TODO: It's probably a bad idea to tar to memory for large
  124. // inventories.
  125. MemoryStream ms = new MemoryStream();
  126. GZipStream gzs = new GZipStream(ms, CompressionMode.Compress, true);
  127. TarArchiveWriter archive = new TarArchiveWriter(gzs);
  128. WriteInventoryFolderToArchive(archive, rootFolder, ArchiveConstants.INVENTORY_PATH);
  129. archive.Close();
  130. ms.Seek(0, SeekOrigin.Begin);
  131. buffer = ms.GetBuffer();
  132. Array.Resize<byte>(ref buffer, (int) ms.Length);
  133. ms.Close();
  134. return buffer;
  135. }
  136. private static InventoryFolderWithChildren BuildInventoryHierarchy(ref InventoryCollection inventory)
  137. {
  138. m_log.DebugFormat("[INVENTORYARCHIVE]: Building inventory hierarchy");
  139. InventoryFolderWithChildren rootFolder = null;
  140. foreach (InventoryFolderWithChildren parentFolder in inventory.Folders.Values)
  141. {
  142. // Grab the root folder, it has no parents.
  143. if (UUID.Zero == parentFolder.ParentID) rootFolder = parentFolder;
  144. foreach (InventoryFolderWithChildren folder in inventory.Folders.Values)
  145. if (parentFolder.ID == folder.ParentID)
  146. parentFolder.Children.Add(folder.ID, folder);
  147. foreach (InventoryItemBase item in inventory.Items.Values)
  148. if (parentFolder.ID == item.Folder)
  149. parentFolder.Children.Add(item.ID, item);
  150. }
  151. return rootFolder;
  152. }
  153. private static void WriteInventoryFolderToArchive(
  154. TarArchiveWriter archive, InventoryFolderWithChildren folder, string path)
  155. {
  156. path += string.Format("{0}{1}{2}/", folder.Name, ArchiveConstants.INVENTORY_NODE_NAME_COMPONENT_SEPARATOR, folder.ID);
  157. archive.WriteDir(path);
  158. foreach (InventoryNodeBase inventoryNode in folder.Children.Values)
  159. {
  160. if (inventoryNode is InventoryFolderWithChildren)
  161. {
  162. WriteInventoryFolderToArchive(archive, (InventoryFolderWithChildren) inventoryNode, path);
  163. }
  164. else if (inventoryNode is InventoryItemBase)
  165. {
  166. WriteInventoryItemToArchive(archive, (InventoryItemBase) inventoryNode, path);
  167. }
  168. }
  169. }
  170. private static void WriteInventoryItemToArchive(TarArchiveWriter archive, InventoryItemBase item, string path)
  171. {
  172. string filename = string.Format("{0}{1}_{2}.xml", path, item.Name, item.ID);
  173. string serialization = UserInventoryItemSerializer.Serialize(item);
  174. archive.WriteFile(filename, serialization);
  175. //m_assetGatherer.GatherAssetUuids(item.AssetID, (AssetType) item.AssetType, assetUuids);
  176. }
  177. }
  178. }