AvatarPickerSearchHandler.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Generic;
  29. using System.Collections.Specialized;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Web;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Capabilities;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. //using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Capabilities.Handlers
  44. {
  45. public class AvatarPickerSearchHandler : BaseStreamHandler
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IPeople m_PeopleService;
  50. public AvatarPickerSearchHandler(string path, IPeople peopleService, string name, string description)
  51. : base("GET", path, name, description)
  52. {
  53. m_PeopleService = peopleService;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  56. {
  57. // Try to parse the texture ID from the request URL
  58. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  59. string names = query.GetOne("names");
  60. string psize = query.GetOne("page_size");
  61. string pnumber = query.GetOne("page");
  62. if (m_PeopleService == null)
  63. return FailureResponse(names, (int)System.Net.HttpStatusCode.InternalServerError, httpResponse);
  64. if (string.IsNullOrEmpty(names) || names.Length < 3)
  65. return FailureResponse(names, (int)System.Net.HttpStatusCode.BadRequest, httpResponse);
  66. m_log.DebugFormat("[AVATAR PICKER SEARCH]: search for {0}", names);
  67. int page_size = (string.IsNullOrEmpty(psize) ? 500 : Int32.Parse(psize));
  68. int page_number = (string.IsNullOrEmpty(pnumber) ? 1 : Int32.Parse(pnumber));
  69. // Full content request
  70. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.OK;
  71. //httpResponse.ContentLength = ??;
  72. httpResponse.ContentType = "application/llsd+xml";
  73. List<UserData> users = m_PeopleService.GetUserData(names, page_size, page_number);
  74. LLSDAvatarPicker osdReply = new LLSDAvatarPicker();
  75. osdReply.next_page_url = httpRequest.RawUrl;
  76. foreach (UserData u in users)
  77. osdReply.agents.Array.Add(ConvertUserData(u));
  78. string reply = LLSDHelpers.SerialiseLLSDReply(osdReply);
  79. return System.Text.Encoding.UTF8.GetBytes(reply);
  80. }
  81. private LLSDPerson ConvertUserData(UserData user)
  82. {
  83. LLSDPerson p = new LLSDPerson();
  84. p.legacy_first_name = user.FirstName;
  85. p.legacy_last_name = user.LastName;
  86. p.display_name = user.FirstName + " " + user.LastName;
  87. if (user.LastName.StartsWith("@"))
  88. p.username = user.FirstName.ToLower() + user.LastName.ToLower();
  89. else
  90. p.username = user.FirstName.ToLower() + "." + user.LastName.ToLower();
  91. p.id = user.Id;
  92. p.is_display_name_default = false;
  93. return p;
  94. }
  95. private byte[] FailureResponse(string names, int statuscode, IOSHttpResponse httpResponse)
  96. {
  97. m_log.Error("[AVATAR PICKER SEARCH]: Error searching for " + names);
  98. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  99. return System.Text.Encoding.UTF8.GetBytes(string.Empty);
  100. }
  101. }
  102. }