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