aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs')
-rw-r--r--OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs215
1 files changed, 215 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..2b3a01d
--- /dev/null
+++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs
@@ -0,0 +1,215 @@
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 System;
29using System.Reflection;
30using System.Text;
31using System.Xml;
32using System.Collections.Generic;
33using System.IO;
34using Nini.Config;
35using OpenSim.Framework;
36using OpenSim.Server.Base;
37using OpenSim.Services.Interfaces;
38using OpenSim.Framework.Servers.HttpServer;
39using OpenSim.Server.Handlers.Base;
40using log4net;
41using OpenMetaverse;
42
43namespace 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 public override byte[] Handle(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}