clockwerk-opensim-stable – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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.Reflection; |
||
30 | using System.Text; |
||
31 | using System.Xml; |
||
32 | using System.Collections.Generic; |
||
33 | using System.IO; |
||
34 | using Nini.Config; |
||
35 | using OpenSim.Framework; |
||
36 | using OpenSim.Server.Base; |
||
37 | using OpenSim.Services.Interfaces; |
||
38 | using OpenSim.Framework.Servers.HttpServer; |
||
39 | using OpenSim.Server.Handlers.Base; |
||
40 | using log4net; |
||
41 | using OpenMetaverse; |
||
42 | |||
43 | namespace OpenSim.OfflineIM |
||
44 | { |
||
45 | public class OfflineIMServiceRobustConnector : ServiceConnector |
||
46 | { |
||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
48 | |||
49 | private IOfflineIMService m_OfflineIMService; |
||
50 | private string m_ConfigName = "Messaging"; |
||
51 | |||
52 | public OfflineIMServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) : |
||
53 | base(config, server, configName) |
||
54 | { |
||
55 | if (configName != String.Empty) |
||
56 | m_ConfigName = configName; |
||
57 | |||
58 | m_log.DebugFormat("[OfflineIM.V2.RobustConnector]: Starting with config name {0}", m_ConfigName); |
||
59 | |||
60 | m_OfflineIMService = new OfflineIMService(config); |
||
61 | |||
62 | server.AddStreamHandler(new OfflineIMServicePostHandler(m_OfflineIMService)); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | public class OfflineIMServicePostHandler : BaseStreamHandler |
||
67 | { |
||
68 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
69 | |||
70 | private IOfflineIMService m_OfflineIMService; |
||
71 | |||
72 | public OfflineIMServicePostHandler(IOfflineIMService service) : |
||
73 | base("POST", "/offlineim") |
||
74 | { |
||
75 | m_OfflineIMService = service; |
||
76 | } |
||
77 | |||
78 | protected override byte[] ProcessRequest(string path, Stream requestData, |
||
79 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) |
||
80 | { |
||
81 | StreamReader sr = new StreamReader(requestData); |
||
82 | string body = sr.ReadToEnd(); |
||
83 | sr.Close(); |
||
84 | body = body.Trim(); |
||
85 | |||
86 | //m_log.DebugFormat("[XXX]: query String: {0}", body); |
||
87 | |||
88 | try |
||
89 | { |
||
90 | Dictionary<string, object> request = |
||
91 | ServerUtils.ParseQueryString(body); |
||
92 | |||
93 | if (!request.ContainsKey("METHOD")) |
||
94 | return FailureResult(); |
||
95 | |||
96 | string method = request["METHOD"].ToString(); |
||
97 | request.Remove("METHOD"); |
||
98 | |||
99 | m_log.DebugFormat("[OfflineIM.V2.Handler]: {0}", method); |
||
100 | switch (method) |
||
101 | { |
||
102 | case "GET": |
||
103 | return HandleGet(request); |
||
104 | case "STORE": |
||
105 | return HandleStore(request); |
||
106 | } |
||
107 | m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method); |
||
108 | } |
||
109 | catch (Exception e) |
||
110 | { |
||
111 | m_log.DebugFormat("[OFFLINE IM HANDLER]: Exception {0}", e.StackTrace); |
||
112 | } |
||
113 | |||
114 | return FailureResult(); |
||
115 | } |
||
116 | |||
117 | byte[] HandleStore(Dictionary<string, object> request) |
||
118 | { |
||
119 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
120 | |||
121 | GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request); |
||
122 | |||
123 | string reason = string.Empty; |
||
124 | |||
125 | bool success = m_OfflineIMService.StoreMessage(im, out reason); |
||
126 | |||
127 | result["RESULT"] = success.ToString(); |
||
128 | if (!success) |
||
129 | result["REASON"] = reason; |
||
130 | |||
131 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
132 | |||
133 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); |
||
134 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
135 | } |
||
136 | |||
137 | byte[] HandleGet(Dictionary<string, object> request) |
||
138 | { |
||
139 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
140 | |||
141 | if (!request.ContainsKey("PrincipalID")) |
||
142 | NullResult(result, "Bad network data"); |
||
143 | else |
||
144 | { |
||
145 | UUID principalID = new UUID(request["PrincipalID"].ToString()); |
||
146 | List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID); |
||
147 | |||
148 | Dictionary<string, object> dict = new Dictionary<string, object>(); |
||
149 | int i = 0; |
||
150 | foreach (GridInstantMessage m in ims) |
||
151 | dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m); |
||
152 | |||
153 | result["RESULT"] = dict; |
||
154 | } |
||
155 | |||
156 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
157 | |||
158 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); |
||
159 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
160 | } |
||
161 | |||
162 | #region Helpers |
||
163 | |||
164 | private void NullResult(Dictionary<string, object> result, string reason) |
||
165 | { |
||
166 | result["RESULT"] = "NULL"; |
||
167 | result["REASON"] = reason; |
||
168 | } |
||
169 | |||
170 | private byte[] FailureResult() |
||
171 | { |
||
172 | return BoolResult(false); |
||
173 | } |
||
174 | |||
175 | private byte[] SuccessResult() |
||
176 | { |
||
177 | return BoolResult(true); |
||
178 | } |
||
179 | |||
180 | private byte[] BoolResult(bool value) |
||
181 | { |
||
182 | XmlDocument doc = new XmlDocument(); |
||
183 | |||
184 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, |
||
185 | "", ""); |
||
186 | |||
187 | doc.AppendChild(xmlnode); |
||
188 | |||
189 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", |
||
190 | ""); |
||
191 | |||
192 | doc.AppendChild(rootElement); |
||
193 | |||
194 | XmlElement result = doc.CreateElement("", "RESULT", ""); |
||
195 | result.AppendChild(doc.CreateTextNode(value.ToString())); |
||
196 | |||
197 | rootElement.AppendChild(result); |
||
198 | |||
199 | return DocToBytes(doc); |
||
200 | } |
||
201 | |||
202 | private byte[] DocToBytes(XmlDocument doc) |
||
203 | { |
||
204 | MemoryStream ms = new MemoryStream(); |
||
205 | XmlTextWriter xw = new XmlTextWriter(ms, null); |
||
206 | xw.Formatting = Formatting.Indented; |
||
207 | doc.WriteTo(xw); |
||
208 | xw.Flush(); |
||
209 | |||
210 | return ms.ToArray(); |
||
211 | } |
||
212 | |||
213 | #endregion |
||
214 | } |
||
215 | } |