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