diff options
Diffstat (limited to 'OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs')
-rw-r--r-- | OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs | 446 |
1 files changed, 446 insertions, 0 deletions
diff --git a/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs b/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs new file mode 100644 index 0000000..b222d4b --- /dev/null +++ b/OpenSim/Capabilities/Handlers/FetchInventoryDescendents/FetchInvDescHandler.cs | |||
@@ -0,0 +1,446 @@ | |||
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.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Nini.Config; | ||
34 | using OpenMetaverse; | ||
35 | using OpenMetaverse.StructuredData; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Framework.Capabilities; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Framework.Servers.HttpServer; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using Caps = OpenSim.Framework.Capabilities.Caps; | ||
42 | |||
43 | namespace OpenSim.Capabilities.Handlers | ||
44 | { | ||
45 | public class WebFetchInvDescHandler | ||
46 | { | ||
47 | private static readonly ILog m_log = | ||
48 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | private IInventoryService m_InventoryService; | ||
51 | private ILibraryService m_LibraryService; | ||
52 | // private object m_fetchLock = new Object(); | ||
53 | |||
54 | public WebFetchInvDescHandler(IInventoryService invService, ILibraryService libService) | ||
55 | { | ||
56 | m_InventoryService = invService; | ||
57 | m_LibraryService = libService; | ||
58 | } | ||
59 | |||
60 | public string FetchInventoryDescendentsRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
61 | { | ||
62 | // lock (m_fetchLock) | ||
63 | // { | ||
64 | // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request {0}", request); | ||
65 | |||
66 | // nasty temporary hack here, the linden client falsely | ||
67 | // identifies the uuid 00000000-0000-0000-0000-000000000000 | ||
68 | // as a string which breaks us | ||
69 | // | ||
70 | // correctly mark it as a uuid | ||
71 | // | ||
72 | request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>"); | ||
73 | |||
74 | // another hack <integer>1</integer> results in a | ||
75 | // System.ArgumentException: Object type System.Int32 cannot | ||
76 | // be converted to target type: System.Boolean | ||
77 | // | ||
78 | request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>"); | ||
79 | request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>"); | ||
80 | |||
81 | Hashtable hash = new Hashtable(); | ||
82 | try | ||
83 | { | ||
84 | hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request)); | ||
85 | } | ||
86 | catch (LLSD.LLSDParseException e) | ||
87 | { | ||
88 | m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace); | ||
89 | m_log.Error("Request: " + request); | ||
90 | } | ||
91 | |||
92 | ArrayList foldersrequested = (ArrayList)hash["folders"]; | ||
93 | |||
94 | string response = ""; | ||
95 | |||
96 | for (int i = 0; i < foldersrequested.Count; i++) | ||
97 | { | ||
98 | string inventoryitemstr = ""; | ||
99 | Hashtable inventoryhash = (Hashtable)foldersrequested[i]; | ||
100 | |||
101 | LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents(); | ||
102 | |||
103 | try | ||
104 | { | ||
105 | LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest); | ||
106 | } | ||
107 | catch (Exception e) | ||
108 | { | ||
109 | m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e); | ||
110 | } | ||
111 | LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest); | ||
112 | |||
113 | inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply); | ||
114 | inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", ""); | ||
115 | inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", ""); | ||
116 | |||
117 | response += inventoryitemstr; | ||
118 | } | ||
119 | |||
120 | if (response.Length == 0) | ||
121 | { | ||
122 | // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants. | ||
123 | // Therefore, I'm concluding that the client only has so many threads available to do requests | ||
124 | // and when a thread stalls.. is stays stalled. | ||
125 | // Therefore we need to return something valid | ||
126 | response = "<llsd><map><key>folders</key><array /></map></llsd>"; | ||
127 | } | ||
128 | else | ||
129 | { | ||
130 | response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>"; | ||
131 | } | ||
132 | |||
133 | // m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Replying to CAPS fetch inventory request"); | ||
134 | //m_log.Debug("[WEB FETCH INV DESC HANDLER] "+response); | ||
135 | |||
136 | return response; | ||
137 | |||
138 | // } | ||
139 | } | ||
140 | |||
141 | /// <summary> | ||
142 | /// Construct an LLSD reply packet to a CAPS inventory request | ||
143 | /// </summary> | ||
144 | /// <param name="invFetch"></param> | ||
145 | /// <returns></returns> | ||
146 | private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch) | ||
147 | { | ||
148 | LLSDInventoryDescendents reply = new LLSDInventoryDescendents(); | ||
149 | LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents(); | ||
150 | contents.agent_id = invFetch.owner_id; | ||
151 | contents.owner_id = invFetch.owner_id; | ||
152 | contents.folder_id = invFetch.folder_id; | ||
153 | |||
154 | reply.folders.Array.Add(contents); | ||
155 | InventoryCollection inv = new InventoryCollection(); | ||
156 | inv.Folders = new List<InventoryFolderBase>(); | ||
157 | inv.Items = new List<InventoryItemBase>(); | ||
158 | int version = 0; | ||
159 | int descendents = 0; | ||
160 | |||
161 | inv | ||
162 | = Fetch( | ||
163 | invFetch.owner_id, invFetch.folder_id, invFetch.owner_id, | ||
164 | invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version, out descendents); | ||
165 | |||
166 | if (inv != null && inv.Folders != null) | ||
167 | { | ||
168 | foreach (InventoryFolderBase invFolder in inv.Folders) | ||
169 | { | ||
170 | contents.categories.Array.Add(ConvertInventoryFolder(invFolder)); | ||
171 | } | ||
172 | |||
173 | descendents += inv.Folders.Count; | ||
174 | } | ||
175 | |||
176 | if (inv != null && inv.Items != null) | ||
177 | { | ||
178 | foreach (InventoryItemBase invItem in inv.Items) | ||
179 | { | ||
180 | contents.items.Array.Add(ConvertInventoryItem(invItem)); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | contents.descendents = descendents; | ||
185 | contents.version = version; | ||
186 | |||
187 | // m_log.DebugFormat( | ||
188 | // "[WEB FETCH INV DESC HANDLER]: Replying to request for folder {0} (fetch items {1}, fetch folders {2}) with {3} items and {4} folders for agent {5}", | ||
189 | // invFetch.folder_id, | ||
190 | // invFetch.fetch_items, | ||
191 | // invFetch.fetch_folders, | ||
192 | // contents.items.Array.Count, | ||
193 | // contents.categories.Array.Count, | ||
194 | // invFetch.owner_id); | ||
195 | |||
196 | return reply; | ||
197 | } | ||
198 | |||
199 | /// <summary> | ||
200 | /// Handle the caps inventory descendents fetch. | ||
201 | /// </summary> | ||
202 | /// <param name="agentID"></param> | ||
203 | /// <param name="folderID"></param> | ||
204 | /// <param name="ownerID"></param> | ||
205 | /// <param name="fetchFolders"></param> | ||
206 | /// <param name="fetchItems"></param> | ||
207 | /// <param name="sortOrder"></param> | ||
208 | /// <param name="version"></param> | ||
209 | /// <returns>An empty InventoryCollection if the inventory look up failed</returns> | ||
210 | private InventoryCollection Fetch( | ||
211 | UUID agentID, UUID folderID, UUID ownerID, | ||
212 | bool fetchFolders, bool fetchItems, int sortOrder, out int version, out int descendents) | ||
213 | { | ||
214 | // m_log.DebugFormat( | ||
215 | // "[WEB FETCH INV DESC HANDLER]: Fetching folders ({0}), items ({1}) from {2} for agent {3}", | ||
216 | // fetchFolders, fetchItems, folderID, agentID); | ||
217 | |||
218 | // FIXME MAYBE: We're not handling sortOrder! | ||
219 | |||
220 | version = 0; | ||
221 | descendents = 0; | ||
222 | |||
223 | InventoryFolderImpl fold; | ||
224 | if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null && agentID == m_LibraryService.LibraryRootFolder.Owner) | ||
225 | { | ||
226 | if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(folderID)) != null) | ||
227 | { | ||
228 | InventoryCollection ret = new InventoryCollection(); | ||
229 | ret.Folders = new List<InventoryFolderBase>(); | ||
230 | ret.Items = fold.RequestListOfItems(); | ||
231 | descendents = ret.Folders.Count + ret.Items.Count; | ||
232 | |||
233 | return ret; | ||
234 | } | ||
235 | } | ||
236 | |||
237 | InventoryCollection contents = new InventoryCollection(); | ||
238 | |||
239 | if (folderID != UUID.Zero) | ||
240 | { | ||
241 | InventoryCollection fetchedContents = m_InventoryService.GetFolderContent(agentID, folderID); | ||
242 | |||
243 | if (fetchedContents == null) | ||
244 | { | ||
245 | m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of folder {0} for user {1}", folderID, agentID); | ||
246 | return contents; | ||
247 | } | ||
248 | |||
249 | contents = fetchedContents; | ||
250 | InventoryFolderBase containingFolder = new InventoryFolderBase(); | ||
251 | containingFolder.ID = folderID; | ||
252 | containingFolder.Owner = agentID; | ||
253 | containingFolder = m_InventoryService.GetFolder(containingFolder); | ||
254 | |||
255 | if (containingFolder != null) | ||
256 | { | ||
257 | // m_log.DebugFormat( | ||
258 | // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}", | ||
259 | // containingFolder.Name, containingFolder.ID, agentID); | ||
260 | |||
261 | version = containingFolder.Version; | ||
262 | |||
263 | if (fetchItems) | ||
264 | { | ||
265 | List<InventoryItemBase> itemsToReturn = contents.Items; | ||
266 | List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn); | ||
267 | |||
268 | // descendents must only include the links, not the linked items we add | ||
269 | descendents = originalItems.Count; | ||
270 | |||
271 | // Add target items for links in this folder before the links themselves. | ||
272 | foreach (InventoryItemBase item in originalItems) | ||
273 | { | ||
274 | if (item.AssetType == (int)AssetType.Link) | ||
275 | { | ||
276 | InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID)); | ||
277 | |||
278 | // Take care of genuinely broken links where the target doesn't exist | ||
279 | // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate, | ||
280 | // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles | ||
281 | // rather than having to keep track of every folder requested in the recursion. | ||
282 | if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link) | ||
283 | itemsToReturn.Insert(0, linkedItem); | ||
284 | } | ||
285 | } | ||
286 | |||
287 | // Now scan for folder links and insert the items they target and those links at the head of the return data | ||
288 | foreach (InventoryItemBase item in originalItems) | ||
289 | { | ||
290 | if (item.AssetType == (int)AssetType.LinkFolder) | ||
291 | { | ||
292 | InventoryCollection linkedFolderContents = m_InventoryService.GetFolderContent(ownerID, item.AssetID); | ||
293 | List<InventoryItemBase> links = linkedFolderContents.Items; | ||
294 | |||
295 | itemsToReturn.InsertRange(0, links); | ||
296 | |||
297 | foreach (InventoryItemBase link in linkedFolderContents.Items) | ||
298 | { | ||
299 | // Take care of genuinely broken links where the target doesn't exist | ||
300 | // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate, | ||
301 | // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles | ||
302 | // rather than having to keep track of every folder requested in the recursion. | ||
303 | if (link != null) | ||
304 | { | ||
305 | // m_log.DebugFormat( | ||
306 | // "[WEB FETCH INV DESC HANDLER]: Adding item {0} {1} from folder {2} linked from {3}", | ||
307 | // link.Name, (AssetType)link.AssetType, item.AssetID, containingFolder.Name); | ||
308 | |||
309 | InventoryItemBase linkedItem | ||
310 | = m_InventoryService.GetItem(new InventoryItemBase(link.AssetID)); | ||
311 | |||
312 | if (linkedItem != null) | ||
313 | itemsToReturn.Insert(0, linkedItem); | ||
314 | } | ||
315 | } | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | |||
320 | // foreach (InventoryItemBase item in contents.Items) | ||
321 | // { | ||
322 | // m_log.DebugFormat( | ||
323 | // "[WEB FETCH INV DESC HANDLER]: Returning item {0}, type {1}, parent {2} in {3} {4}", | ||
324 | // item.Name, (AssetType)item.AssetType, item.Folder, containingFolder.Name, containingFolder.ID); | ||
325 | // } | ||
326 | |||
327 | // ===== | ||
328 | |||
329 | // | ||
330 | // foreach (InventoryItemBase linkedItem in linkedItemsToAdd) | ||
331 | // { | ||
332 | // m_log.DebugFormat( | ||
333 | // "[WEB FETCH INV DESC HANDLER]: Inserted linked item {0} for link in folder {1} for agent {2}", | ||
334 | // linkedItem.Name, folderID, agentID); | ||
335 | // | ||
336 | // contents.Items.Add(linkedItem); | ||
337 | // } | ||
338 | // | ||
339 | // // If the folder requested contains links, then we need to send those folders first, otherwise the links | ||
340 | // // will be broken in the viewer. | ||
341 | // HashSet<UUID> linkedItemFolderIdsToSend = new HashSet<UUID>(); | ||
342 | // foreach (InventoryItemBase item in contents.Items) | ||
343 | // { | ||
344 | // if (item.AssetType == (int)AssetType.Link) | ||
345 | // { | ||
346 | // InventoryItemBase linkedItem = m_InventoryService.GetItem(new InventoryItemBase(item.AssetID)); | ||
347 | // | ||
348 | // // Take care of genuinely broken links where the target doesn't exist | ||
349 | // // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate, | ||
350 | // // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles | ||
351 | // // rather than having to keep track of every folder requested in the recursion. | ||
352 | // if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link) | ||
353 | // { | ||
354 | // // We don't need to send the folder if source and destination of the link are in the same | ||
355 | // // folder. | ||
356 | // if (linkedItem.Folder != containingFolder.ID) | ||
357 | // linkedItemFolderIdsToSend.Add(linkedItem.Folder); | ||
358 | // } | ||
359 | // } | ||
360 | // } | ||
361 | // | ||
362 | // foreach (UUID linkedItemFolderId in linkedItemFolderIdsToSend) | ||
363 | // { | ||
364 | // m_log.DebugFormat( | ||
365 | // "[WEB FETCH INV DESC HANDLER]: Recursively fetching folder {0} linked by item in folder {1} for agent {2}", | ||
366 | // linkedItemFolderId, folderID, agentID); | ||
367 | // | ||
368 | // int dummyVersion; | ||
369 | // InventoryCollection linkedCollection | ||
370 | // = Fetch( | ||
371 | // agentID, linkedItemFolderId, ownerID, fetchFolders, fetchItems, sortOrder, out dummyVersion); | ||
372 | // | ||
373 | // InventoryFolderBase linkedFolder = new InventoryFolderBase(linkedItemFolderId); | ||
374 | // linkedFolder.Owner = agentID; | ||
375 | // linkedFolder = m_InventoryService.GetFolder(linkedFolder); | ||
376 | // | ||
377 | //// contents.Folders.AddRange(linkedCollection.Folders); | ||
378 | // | ||
379 | // contents.Folders.Add(linkedFolder); | ||
380 | // contents.Items.AddRange(linkedCollection.Items); | ||
381 | // } | ||
382 | // } | ||
383 | } | ||
384 | } | ||
385 | else | ||
386 | { | ||
387 | // Lost items don't really need a version | ||
388 | version = 1; | ||
389 | } | ||
390 | |||
391 | return contents; | ||
392 | |||
393 | } | ||
394 | /// <summary> | ||
395 | /// Convert an internal inventory folder object into an LLSD object. | ||
396 | /// </summary> | ||
397 | /// <param name="invFolder"></param> | ||
398 | /// <returns></returns> | ||
399 | private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder) | ||
400 | { | ||
401 | LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder(); | ||
402 | llsdFolder.folder_id = invFolder.ID; | ||
403 | llsdFolder.parent_id = invFolder.ParentID; | ||
404 | llsdFolder.name = invFolder.Name; | ||
405 | llsdFolder.type = invFolder.Type; | ||
406 | llsdFolder.preferred_type = -1; | ||
407 | |||
408 | return llsdFolder; | ||
409 | } | ||
410 | |||
411 | /// <summary> | ||
412 | /// Convert an internal inventory item object into an LLSD object. | ||
413 | /// </summary> | ||
414 | /// <param name="invItem"></param> | ||
415 | /// <returns></returns> | ||
416 | private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem) | ||
417 | { | ||
418 | LLSDInventoryItem llsdItem = new LLSDInventoryItem(); | ||
419 | llsdItem.asset_id = invItem.AssetID; | ||
420 | llsdItem.created_at = invItem.CreationDate; | ||
421 | llsdItem.desc = invItem.Description; | ||
422 | llsdItem.flags = (int)invItem.Flags; | ||
423 | llsdItem.item_id = invItem.ID; | ||
424 | llsdItem.name = invItem.Name; | ||
425 | llsdItem.parent_id = invItem.Folder; | ||
426 | llsdItem.type = invItem.AssetType; | ||
427 | llsdItem.inv_type = invItem.InvType; | ||
428 | |||
429 | llsdItem.permissions = new LLSDPermissions(); | ||
430 | llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid; | ||
431 | llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions; | ||
432 | llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions; | ||
433 | llsdItem.permissions.group_id = invItem.GroupID; | ||
434 | llsdItem.permissions.group_mask = (int)invItem.GroupPermissions; | ||
435 | llsdItem.permissions.is_owner_group = invItem.GroupOwned; | ||
436 | llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions; | ||
437 | llsdItem.permissions.owner_id = invItem.Owner; | ||
438 | llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions; | ||
439 | llsdItem.sale_info = new LLSDSaleInfo(); | ||
440 | llsdItem.sale_info.sale_price = invItem.SalePrice; | ||
441 | llsdItem.sale_info.sale_type = invItem.SaleType; | ||
442 | |||
443 | return llsdItem; | ||
444 | } | ||
445 | } | ||
446 | } \ No newline at end of file | ||