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