GetDisplayNamesHandler.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Reflection;
  32. using System.IO;
  33. using System.Web;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Services.Interfaces;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  42. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  43. namespace OpenSim.Capabilities.Handlers
  44. {
  45. public class GetDisplayNamesHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. protected IUserManagement m_UserManagement;
  50. public GetDisplayNamesHandler(string path, IUserManagement umService, string name, string description)
  51. : base("GET", path, name, description)
  52. {
  53. m_UserManagement = umService;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  56. {
  57. // m_log.DebugFormat("[GET_DISPLAY_NAMES]: called {0}", httpRequest.Url.Query);
  58. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  59. string[] ids = query.GetValues("ids");
  60. if (m_UserManagement == null)
  61. {
  62. m_log.Error("[GET_DISPLAY_NAMES]: Cannot fetch display names without a user management component");
  63. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
  64. return new byte[0];
  65. }
  66. Dictionary<UUID,string> names = m_UserManagement.GetUsersNames(ids, UUID.Zero);
  67. OSDMap osdReply = new OSDMap();
  68. OSDArray agents = new OSDArray();
  69. osdReply["agents"] = agents;
  70. foreach (KeyValuePair<UUID,string> kvp in names)
  71. {
  72. if (string.IsNullOrEmpty(kvp.Value))
  73. continue;
  74. if(kvp.Key == UUID.Zero)
  75. continue;
  76. string[] parts = kvp.Value.Split(new char[] {' '});
  77. OSDMap osdname = new OSDMap();
  78. if(parts[0] == "Unknown")
  79. {
  80. osdname["display_name_next_update"] = OSD.FromDate(DateTime.UtcNow.AddHours(1));
  81. osdname["display_name_expires"] = OSD.FromDate(DateTime.UtcNow.AddHours(2));
  82. }
  83. else
  84. {
  85. osdname["display_name_next_update"] = OSD.FromDate(DateTime.UtcNow.AddDays(8));
  86. osdname["display_name_expires"] = OSD.FromDate(DateTime.UtcNow.AddMonths(1));
  87. }
  88. osdname["display_name"] = OSD.FromString(kvp.Value);
  89. osdname["legacy_first_name"] = parts[0];
  90. osdname["legacy_last_name"] = parts[1];
  91. osdname["username"] = OSD.FromString(kvp.Value);
  92. osdname["id"] = OSD.FromUUID(kvp.Key);
  93. osdname["is_display_name_default"] = OSD.FromBoolean(true);
  94. agents.Add(osdname);
  95. }
  96. // Full content request
  97. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.OK;
  98. //httpResponse.ContentLength = ??;
  99. httpResponse.ContentType = "application/llsd+xml";
  100. string reply = OSDParser.SerializeLLSDXmlString(osdReply);
  101. return System.Text.Encoding.UTF8.GetBytes(reply);
  102. }
  103. }
  104. }