opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 switch (method)
100 {
101 case "GET":
102 return HandleGet(request);
103 case "STORE":
104 return HandleStore(request);
105 case "DELETE":
106 return HandleDelete(request);
107 }
108 m_log.DebugFormat("[OFFLINE IM HANDLER]: unknown method request: {0}", method);
109 }
110 catch (Exception e)
111 {
112 m_log.DebugFormat("[OFFLINE IM HANDLER]: Exception {0}", e.StackTrace);
113 }
114  
115 return FailureResult();
116 }
117  
118 byte[] HandleStore(Dictionary<string, object> request)
119 {
120 Dictionary<string, object> result = new Dictionary<string, object>();
121  
122 GridInstantMessage im = OfflineIMDataUtils.GridInstantMessage(request);
123  
124 string reason = string.Empty;
125  
126 bool success = m_OfflineIMService.StoreMessage(im, out reason);
127  
128 result["RESULT"] = success.ToString();
129 if (!success)
130 result["REASON"] = reason;
131  
132 string xmlString = ServerUtils.BuildXmlResponse(result);
133  
134 //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
135 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
136 }
137  
138 byte[] HandleGet(Dictionary<string, object> request)
139 {
140 Dictionary<string, object> result = new Dictionary<string, object>();
141  
142 if (!request.ContainsKey("PrincipalID"))
143 NullResult(result, "Bad network data");
144 else
145 {
146 UUID principalID = new UUID(request["PrincipalID"].ToString());
147 List<GridInstantMessage> ims = m_OfflineIMService.GetMessages(principalID);
148  
149 Dictionary<string, object> dict = new Dictionary<string, object>();
150 int i = 0;
151 foreach (GridInstantMessage m in ims)
152 dict["im-" + i++] = OfflineIMDataUtils.GridInstantMessage(m);
153  
154 result["RESULT"] = dict;
155 }
156  
157 string xmlString = ServerUtils.BuildXmlResponse(result);
158  
159 //m_log.DebugFormat("[XXX]: resp string: {0}", xmlString);
160 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
161 }
162  
163 byte[] HandleDelete(Dictionary<string, object> request)
164 {
165 if (!request.ContainsKey("UserID"))
166 {
167 return FailureResult();
168 }
169 else
170 {
171 UUID userID = new UUID(request["UserID"].ToString());
172 m_OfflineIMService.DeleteMessages(userID);
173  
174 return SuccessResult();
175 }
176 }
177  
178 #region Helpers
179  
180 private void NullResult(Dictionary<string, object> result, string reason)
181 {
182 result["RESULT"] = "NULL";
183 result["REASON"] = reason;
184 }
185  
186 private byte[] FailureResult()
187 {
188 return BoolResult(false);
189 }
190  
191 private byte[] SuccessResult()
192 {
193 return BoolResult(true);
194 }
195  
196 private byte[] BoolResult(bool value)
197 {
198 XmlDocument doc = new XmlDocument();
199  
200 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
201 "", "");
202  
203 doc.AppendChild(xmlnode);
204  
205 XmlElement rootElement = doc.CreateElement("", "ServerResponse",
206 "");
207  
208 doc.AppendChild(rootElement);
209  
210 XmlElement result = doc.CreateElement("", "RESULT", "");
211 result.AppendChild(doc.CreateTextNode(value.ToString()));
212  
213 rootElement.AppendChild(result);
214  
215 return DocToBytes(doc);
216 }
217  
218 private byte[] DocToBytes(XmlDocument doc)
219 {
220 MemoryStream ms = new MemoryStream();
221 XmlTextWriter xw = new XmlTextWriter(ms, null);
222 xw.Formatting = Formatting.Indented;
223 doc.WriteTo(xw);
224 xw.Flush();
225  
226 return ms.ToArray();
227 }
228  
229 #endregion
230 }
231 }