diff options
Diffstat (limited to 'OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs')
-rw-r--r-- | OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs new file mode 100644 index 0000000..b3673da --- /dev/null +++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs | |||
@@ -0,0 +1,223 @@ | |||
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.Framework.ServiceAuth; | ||
40 | using OpenSim.Server.Handlers.Base; | ||
41 | using log4net; | ||
42 | using OpenMetaverse; | ||
43 | |||
44 | namespace OpenSim.OfflineIM | ||
45 | { | ||
46 | public class OfflineIMServiceRobustConnector : ServiceConnector | ||
47 | { | ||
48 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | private IOfflineIMService m_OfflineIMService; | ||
51 | private string m_ConfigName = "Messaging"; | ||
52 | |||
53 | public OfflineIMServiceRobustConnector(IConfigSource config, IHttpServer server, string configName) : | ||
54 | base(config, server, configName) | ||
55 | { | ||
56 | if (configName != String.Empty) | ||
57 | m_ConfigName = configName; | ||
58 | |||
59 | m_log.DebugFormat("[OfflineIM.V2.RobustConnector]: Starting with config name {0}", m_ConfigName); | ||
60 | |||
61 | m_OfflineIMService = new OfflineIMService(config); | ||
62 | |||
63 | IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName); | ||
64 | |||
65 | server.AddStreamHandler(new OfflineIMServicePostHandler(m_OfflineIMService, auth)); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | public class OfflineIMServicePostHandler : BaseStreamHandler | ||
70 | { | ||
71 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
72 | |||
73 | private IOfflineIMService m_OfflineIMService; | ||
74 | |||
75 | public OfflineIMServicePostHandler(IOfflineIMService service, IServiceAuth auth) : | ||
76 | base("POST", "/offlineim", auth) | ||
77 | { | ||
78 | m_OfflineIMService = service; | ||
79 | } | ||
80 | |||
81 | protected override byte[] ProcessRequest(string path, Stream requestData, | ||
82 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
83 | { | ||
84 | StreamReader sr = new StreamReader(requestData); | ||
85 | string body = sr.ReadToEnd(); | ||
86 | sr.Close(); | ||
87 | body = body.Trim(); | ||
88 | |||
89 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
90 | |||
91 | try | ||
92 | { | ||
93 | Dictionary<string, object> request = | ||
94 | ServerUtils.ParseQueryString(body); | ||
95 | |||
96 | if (!request.ContainsKey("METHOD")) | ||
97 | return FailureResult(); | ||
98 | |||
99 | string method = request["METHOD"].ToString(); | ||
100 | request.Remove("METHOD"); | ||
101 | |||
102 | switch (method) | ||
103 | { | ||
104 | case "GET": | ||
105 | return HandleGet(request); | ||
106 | case "STORE": | ||
107 | return HandleStore(request); | ||
108 | case "DELETE": | ||
109 | return HandleDelete(request); | ||
110 | } | ||
111 | m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method); | ||
112 | } | ||
113 | catch (Exception e) | ||
114 | { | ||
115 | m_log.Error(string.Format("[OFFLINE IM HANDLER]: Exception {0} ", e.Message), e); | ||
116 | } | ||
117 | |||
118 | return FailureResult(); | ||
119 | } | ||
120 | |||
121 | byte[] HandleStore(Dictionary<string, object> request) | ||
122 | { | ||
123 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
124 | |||
125 | GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request); | ||
126 | |||
127 | string reason = string.Empty; | ||
128 | |||
129 | bool success = m_OfflineIMService.StoreMessage(im, out reason); | ||
130 | |||
131 | result["RESULT"] = success.ToString(); | ||
132 | if (!success) | ||
133 | result["REASON"] = reason; | ||
134 | |||
135 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
136 | |||
137 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
138 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
139 | } | ||
140 | |||
141 | byte[] HandleGet(Dictionary<string, object> request) | ||
142 | { | ||
143 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
144 | |||
145 | if (!request.ContainsKey("PrincipalID")) | ||
146 | NullResult(result, "Bad network data"); | ||
147 | else | ||
148 | { | ||
149 | UUID principalID = new UUID(request["PrincipalID"].ToString()); | ||
150 | List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID); | ||
151 | |||
152 | Dictionary<string, object> dict = new Dictionary<string, object>(); | ||
153 | int i = 0; | ||
154 | foreach (GridInstantMessage m in ims) | ||
155 | dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m); | ||
156 | |||
157 | result["RESULT"] = dict; | ||
158 | } | ||
159 | |||
160 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
161 | |||
162 | //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString); | ||
163 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); | ||
164 | } | ||
165 | |||
166 | byte[] HandleDelete(Dictionary<string, object> request) | ||
167 | { | ||
168 | if (!request.ContainsKey("UserID")) | ||
169 | { | ||
170 | return FailureResult(); | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | UUID userID = new UUID(request["UserID"].ToString()); | ||
175 | m_OfflineIMService.DeleteMessages(userID); | ||
176 | |||
177 | return SuccessResult(); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | #region Helpers | ||
182 | |||
183 | private void NullResult(Dictionary<string, object> result, string reason) | ||
184 | { | ||
185 | result["RESULT"] = "NULL"; | ||
186 | result["REASON"] = reason; | ||
187 | } | ||
188 | |||
189 | private byte[] FailureResult() | ||
190 | { | ||
191 | return BoolResult(false); | ||
192 | } | ||
193 | |||
194 | private byte[] SuccessResult() | ||
195 | { | ||
196 | return BoolResult(true); | ||
197 | } | ||
198 | |||
199 | private byte[] BoolResult(bool value) | ||
200 | { | ||
201 | XmlDocument doc = new XmlDocument(); | ||
202 | |||
203 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
204 | "", ""); | ||
205 | |||
206 | doc.AppendChild(xmlnode); | ||
207 | |||
208 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
209 | ""); | ||
210 | |||
211 | doc.AppendChild(rootElement); | ||
212 | |||
213 | XmlElement result = doc.CreateElement("", "RESULT", ""); | ||
214 | result.AppendChild(doc.CreateTextNode(value.ToString())); | ||
215 | |||
216 | rootElement.AppendChild(result); | ||
217 | |||
218 | return Util.DocToBytes(doc); | ||
219 | } | ||
220 | |||
221 | #endregion | ||
222 | } | ||
223 | } | ||