aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs')
-rw-r--r--OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs182
1 files changed, 182 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs
new file mode 100644
index 0000000..2558fa0
--- /dev/null
+++ b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs
@@ -0,0 +1,182 @@
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
28using Nini.Config;
29using log4net;
30using System;
31using System.Reflection;
32using System.IO;
33using System.Net;
34using System.Text;
35using System.Text.RegularExpressions;
36using System.Xml;
37using System.Xml.Serialization;
38using System.Collections.Generic;
39using OpenSim.Server.Base;
40using OpenSim.Services.Interfaces;
41using OpenSim.Framework;
42using OpenSim.Framework.Servers.HttpServer;
43using OpenMetaverse;
44
45namespace OpenSim.Server.Handlers.Presence
46{
47 public class PresenceServerPostHandler : BaseStreamHandler
48 {
49 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50
51 private IPresenceService m_PresenceService;
52
53 public PresenceServerPostHandler(IPresenceService service) :
54 base("POST", "/presence")
55 {
56 m_PresenceService = service;
57 }
58
59 public override byte[] Handle(string path, Stream requestData,
60 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
61 {
62 StreamReader sr = new StreamReader(requestData);
63 string body = sr.ReadToEnd();
64 sr.Close();
65 body = body.Trim();
66
67 //m_log.DebugFormat("[XXX]: query String: {0}", body);
68
69 try
70 {
71 Dictionary<string, string> request =
72 ServerUtils.ParseQueryString(body);
73
74 if (!request.ContainsKey("METHOD"))
75 return FailureResult();
76
77 string method = request["METHOD"];
78
79 switch (method)
80 {
81 case "report":
82 return Report(request);
83 }
84 m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method);
85 }
86 catch (Exception e)
87 {
88 m_log.Debug("[PRESENCE HANDLER]: Exception {0}" + e);
89 }
90
91 return FailureResult();
92
93 }
94
95 byte[] Report(Dictionary<string, string> request)
96 {
97 PresenceInfo info = new PresenceInfo();
98 info.Data = new Dictionary<string, string>();
99
100 if (request["PrincipalID"] == null || request["RegionID"] == null)
101 return FailureResult();
102
103 if (!UUID.TryParse(request["PrincipalID"].ToString(),
104 out info.PrincipalID))
105 return FailureResult();
106
107 if (!UUID.TryParse(request["RegionID"].ToString(),
108 out info.RegionID))
109 return FailureResult();
110
111 foreach (KeyValuePair<string, string> kvp in request)
112 {
113 if (kvp.Key == "METHOD" ||
114 kvp.Key == "PrincipalID" ||
115 kvp.Key == "RegionID")
116 continue;
117
118 info.Data[kvp.Key] = kvp.Value;
119 }
120
121 if (m_PresenceService.Report(info))
122 return SuccessResult();
123
124 return FailureResult();
125 }
126
127 private byte[] SuccessResult()
128 {
129 XmlDocument doc = new XmlDocument();
130
131 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
132 "", "");
133
134 doc.AppendChild(xmlnode);
135
136 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
137 "");
138
139 doc.AppendChild(rootElement);
140
141 XmlElement result = doc.CreateElement("", "Result", "");
142 result.AppendChild(doc.CreateTextNode("Success"));
143
144 rootElement.AppendChild(result);
145
146 return DocToBytes(doc);
147 }
148
149 private byte[] FailureResult()
150 {
151 XmlDocument doc = new XmlDocument();
152
153 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
154 "", "");
155
156 doc.AppendChild(xmlnode);
157
158 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
159 "");
160
161 doc.AppendChild(rootElement);
162
163 XmlElement result = doc.CreateElement("", "Result", "");
164 result.AppendChild(doc.CreateTextNode("Failure"));
165
166 rootElement.AppendChild(result);
167
168 return DocToBytes(doc);
169 }
170
171 private byte[] DocToBytes(XmlDocument doc)
172 {
173 MemoryStream ms = new MemoryStream();
174 XmlTextWriter xw = new XmlTextWriter(ms, null);
175 xw.Formatting = Formatting.Indented;
176 doc.WriteTo(xw);
177 xw.Flush();
178
179 return ms.ToArray();
180 }
181 }
182}