diff options
Diffstat (limited to 'OpenSim/Addons/OfflineIM/Remote')
-rw-r--r-- | OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs | 171 | ||||
-rw-r--r-- | OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRobustConnector.cs | 223 |
2 files changed, 394 insertions, 0 deletions
diff --git a/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs new file mode 100644 index 0000000..047b8be --- /dev/null +++ b/OpenSim/Addons/OfflineIM/Remote/OfflineIMServiceRemoteConnector.cs | |||
@@ -0,0 +1,171 @@ | |||
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.Linq; | ||
31 | using System.Reflection; | ||
32 | using System.Text; | ||
33 | |||
34 | using OpenSim.Framework; | ||
35 | using OpenSim.Framework.ServiceAuth; | ||
36 | using OpenSim.Server.Base; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | |||
39 | using OpenMetaverse; | ||
40 | using log4net; | ||
41 | using Nini.Config; | ||
42 | |||
43 | namespace OpenSim.OfflineIM | ||
44 | { | ||
45 | public class OfflineIMServiceRemoteConnector : IOfflineIMService | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private string m_ServerURI = string.Empty; | ||
50 | private IServiceAuth m_Auth; | ||
51 | private object m_Lock = new object(); | ||
52 | |||
53 | public OfflineIMServiceRemoteConnector(string url) | ||
54 | { | ||
55 | m_ServerURI = url; | ||
56 | m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: Offline IM server at {0}", m_ServerURI); | ||
57 | } | ||
58 | |||
59 | public OfflineIMServiceRemoteConnector(IConfigSource config) | ||
60 | { | ||
61 | IConfig cnf = config.Configs["Messaging"]; | ||
62 | if (cnf == null) | ||
63 | { | ||
64 | m_log.WarnFormat("[OfflineIM.V2.RemoteConnector]: Missing Messaging configuration"); | ||
65 | return; | ||
66 | } | ||
67 | |||
68 | m_ServerURI = cnf.GetString("OfflineMessageURL", string.Empty); | ||
69 | |||
70 | /// This is from BaseServiceConnector | ||
71 | string authType = Util.GetConfigVarFromSections<string>(config, "AuthType", new string[] { "Network", "Messaging" }, "None"); | ||
72 | |||
73 | switch (authType) | ||
74 | { | ||
75 | case "BasicHttpAuthentication": | ||
76 | m_Auth = new BasicHttpAuthentication(config, "Messaging"); | ||
77 | break; | ||
78 | } | ||
79 | /// | ||
80 | m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: Offline IM server at {0} with auth {1}", | ||
81 | m_ServerURI, (m_Auth == null ? "None" : m_Auth.GetType().ToString())); | ||
82 | } | ||
83 | |||
84 | #region IOfflineIMService | ||
85 | public List<GridInstantMessage> GetMessages(UUID principalID) | ||
86 | { | ||
87 | List<GridInstantMessage> ims = new List<GridInstantMessage>(); | ||
88 | |||
89 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
90 | sendData["PrincipalID"] = principalID; | ||
91 | Dictionary<string, object> ret = MakeRequest("GET", sendData); | ||
92 | |||
93 | if (ret == null) | ||
94 | return ims; | ||
95 | |||
96 | if (!ret.ContainsKey("RESULT")) | ||
97 | return ims; | ||
98 | |||
99 | string result = ret["RESULT"].ToString(); | ||
100 | if (result == "NULL" || result.ToLower() == "false") | ||
101 | { | ||
102 | string reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error"; | ||
103 | m_log.DebugFormat("[OfflineIM.V2.RemoteConnector]: GetMessages for {0} failed: {1}", principalID, reason); | ||
104 | return ims; | ||
105 | } | ||
106 | |||
107 | foreach (object v in ((Dictionary<string, object>)ret["RESULT"]).Values) | ||
108 | { | ||
109 | GridInstantMessage m = OfflineIMDataUtils.GridInstantMessage((Dictionary<string, object>)v); | ||
110 | ims.Add(m); | ||
111 | } | ||
112 | |||
113 | return ims; | ||
114 | } | ||
115 | |||
116 | public bool StoreMessage(GridInstantMessage im, out string reason) | ||
117 | { | ||
118 | reason = string.Empty; | ||
119 | Dictionary<string, object> sendData = OfflineIMDataUtils.GridInstantMessage(im); | ||
120 | |||
121 | Dictionary<string, object> ret = MakeRequest("STORE", sendData); | ||
122 | |||
123 | if (ret == null) | ||
124 | { | ||
125 | reason = "Bad response from server"; | ||
126 | return false; | ||
127 | } | ||
128 | |||
129 | string result = ret["RESULT"].ToString(); | ||
130 | if (result == "NULL" || result.ToLower() == "false") | ||
131 | { | ||
132 | reason = ret.ContainsKey("REASON") ? ret["REASON"].ToString() : "Unknown error"; | ||
133 | return false; | ||
134 | } | ||
135 | |||
136 | return true; | ||
137 | } | ||
138 | |||
139 | public void DeleteMessages(UUID userID) | ||
140 | { | ||
141 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
142 | sendData["UserID"] = userID; | ||
143 | |||
144 | MakeRequest("DELETE", sendData); | ||
145 | } | ||
146 | |||
147 | #endregion | ||
148 | |||
149 | |||
150 | #region Make Request | ||
151 | |||
152 | private Dictionary<string, object> MakeRequest(string method, Dictionary<string, object> sendData) | ||
153 | { | ||
154 | sendData["METHOD"] = method; | ||
155 | |||
156 | string reply = string.Empty; | ||
157 | lock (m_Lock) | ||
158 | reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
159 | m_ServerURI + "/offlineim", | ||
160 | ServerUtils.BuildQueryString(sendData), | ||
161 | m_Auth); | ||
162 | |||
163 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse( | ||
164 | reply); | ||
165 | |||
166 | return replyData; | ||
167 | } | ||
168 | #endregion | ||
169 | |||
170 | } | ||
171 | } | ||
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 | } | ||