PageRenderTime 93ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs

https://github.com/AlericInglewood/opensimulator
C# | 652 lines | 479 code | 122 blank | 51 comment | 86 complexity | 8a265999bd5f41be8bec56a33abc236a MD5 | raw file
Possible License(s): MIT
  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. using Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Grid
  45. {
  46. public class GridServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. #pragma warning disable 414
  50. private static string LogHeader = "[GRID HANDLER]";
  51. #pragma warning restore 414
  52. private IGridService m_GridService;
  53. public GridServerPostHandler(IGridService service) :
  54. base("POST", "/grid")
  55. {
  56. m_GridService = service;
  57. }
  58. protected override byte[] ProcessRequest(string path, Stream requestData,
  59. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  60. {
  61. StreamReader sr = new StreamReader(requestData);
  62. string body = sr.ReadToEnd();
  63. sr.Close();
  64. body = body.Trim();
  65. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  66. try
  67. {
  68. Dictionary<string, object> request =
  69. ServerUtils.ParseQueryString(body);
  70. if (!request.ContainsKey("METHOD"))
  71. return FailureResult();
  72. string method = request["METHOD"].ToString();
  73. switch (method)
  74. {
  75. case "register":
  76. return Register(request);
  77. case "deregister":
  78. return Deregister(request);
  79. case "get_neighbours":
  80. return GetNeighbours(request);
  81. case "get_region_by_uuid":
  82. return GetRegionByUUID(request);
  83. case "get_region_by_position":
  84. return GetRegionByPosition(request);
  85. case "get_region_by_name":
  86. return GetRegionByName(request);
  87. case "get_regions_by_name":
  88. return GetRegionsByName(request);
  89. case "get_region_range":
  90. return GetRegionRange(request);
  91. case "get_default_regions":
  92. return GetDefaultRegions(request);
  93. case "get_default_hypergrid_regions":
  94. return GetDefaultHypergridRegions(request);
  95. case "get_fallback_regions":
  96. return GetFallbackRegions(request);
  97. case "get_hyperlinks":
  98. return GetHyperlinks(request);
  99. case "get_region_flags":
  100. return GetRegionFlags(request);
  101. }
  102. m_log.DebugFormat("[GRID HANDLER]: unknown method request {0}", method);
  103. }
  104. catch (Exception e)
  105. {
  106. m_log.ErrorFormat("[GRID HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
  107. }
  108. return FailureResult();
  109. }
  110. #region Method-specific handlers
  111. byte[] Register(Dictionary<string, object> request)
  112. {
  113. UUID scopeID = UUID.Zero;
  114. if (request.ContainsKey("SCOPEID"))
  115. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  116. else
  117. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region");
  118. int versionNumberMin = 0, versionNumberMax = 0;
  119. if (request.ContainsKey("VERSIONMIN"))
  120. Int32.TryParse(request["VERSIONMIN"].ToString(), out versionNumberMin);
  121. else
  122. m_log.WarnFormat("[GRID HANDLER]: no minimum protocol version in request to register region");
  123. if (request.ContainsKey("VERSIONMAX"))
  124. Int32.TryParse(request["VERSIONMAX"].ToString(), out versionNumberMax);
  125. else
  126. m_log.WarnFormat("[GRID HANDLER]: no maximum protocol version in request to register region");
  127. // Check the protocol version
  128. if ((versionNumberMin > ProtocolVersions.ServerProtocolVersionMax && versionNumberMax < ProtocolVersions.ServerProtocolVersionMax))
  129. {
  130. // Can't do, there is no overlap in the acceptable ranges
  131. return FailureResult();
  132. }
  133. Dictionary<string, object> rinfoData = new Dictionary<string, object>();
  134. GridRegion rinfo = null;
  135. try
  136. {
  137. foreach (KeyValuePair<string, object> kvp in request)
  138. rinfoData[kvp.Key] = kvp.Value.ToString();
  139. rinfo = new GridRegion(rinfoData);
  140. }
  141. catch (Exception e)
  142. {
  143. m_log.DebugFormat("[GRID HANDLER]: exception unpacking region data: {0}", e);
  144. }
  145. string result = "Error communicating with grid service";
  146. if (rinfo != null)
  147. result = m_GridService.RegisterRegion(scopeID, rinfo);
  148. if (result == String.Empty)
  149. return SuccessResult();
  150. else
  151. return FailureResult(result);
  152. }
  153. byte[] Deregister(Dictionary<string, object> request)
  154. {
  155. UUID regionID = UUID.Zero;
  156. if (request.ContainsKey("REGIONID"))
  157. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  158. else
  159. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region");
  160. bool result = m_GridService.DeregisterRegion(regionID);
  161. if (result)
  162. return SuccessResult();
  163. else
  164. return FailureResult();
  165. }
  166. byte[] GetNeighbours(Dictionary<string, object> request)
  167. {
  168. UUID scopeID = UUID.Zero;
  169. if (request.ContainsKey("SCOPEID"))
  170. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  171. else
  172. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  173. UUID regionID = UUID.Zero;
  174. if (request.ContainsKey("REGIONID"))
  175. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  176. else
  177. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  178. List<GridRegion> rinfos = m_GridService.GetNeighbours(scopeID, regionID);
  179. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  180. Dictionary<string, object> result = new Dictionary<string, object>();
  181. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  182. result["result"] = "null";
  183. else
  184. {
  185. int i = 0;
  186. foreach (GridRegion rinfo in rinfos)
  187. {
  188. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  189. result["region" + i] = rinfoDict;
  190. i++;
  191. }
  192. }
  193. string xmlString = ServerUtils.BuildXmlResponse(result);
  194. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  195. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  196. }
  197. byte[] GetRegionByUUID(Dictionary<string, object> request)
  198. {
  199. UUID scopeID = UUID.Zero;
  200. if (request.ContainsKey("SCOPEID"))
  201. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  202. else
  203. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  204. UUID regionID = UUID.Zero;
  205. if (request.ContainsKey("REGIONID"))
  206. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  207. else
  208. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  209. GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  210. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  211. Dictionary<string, object> result = new Dictionary<string, object>();
  212. if (rinfo == null)
  213. result["result"] = "null";
  214. else
  215. result["result"] = rinfo.ToKeyValuePairs();
  216. string xmlString = ServerUtils.BuildXmlResponse(result);
  217. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  218. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  219. }
  220. byte[] GetRegionByPosition(Dictionary<string, object> request)
  221. {
  222. UUID scopeID = UUID.Zero;
  223. if (request.ContainsKey("SCOPEID"))
  224. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  225. else
  226. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position");
  227. int x = 0, y = 0;
  228. if (request.ContainsKey("X"))
  229. Int32.TryParse(request["X"].ToString(), out x);
  230. else
  231. m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position");
  232. if (request.ContainsKey("Y"))
  233. Int32.TryParse(request["Y"].ToString(), out y);
  234. else
  235. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position");
  236. // m_log.DebugFormat("{0} GetRegionByPosition: loc=<{1},{2}>", LogHeader, x, y);
  237. GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  238. Dictionary<string, object> result = new Dictionary<string, object>();
  239. if (rinfo == null)
  240. result["result"] = "null";
  241. else
  242. result["result"] = rinfo.ToKeyValuePairs();
  243. string xmlString = ServerUtils.BuildXmlResponse(result);
  244. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  245. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  246. }
  247. byte[] GetRegionByName(Dictionary<string, object> request)
  248. {
  249. UUID scopeID = UUID.Zero;
  250. if (request.ContainsKey("SCOPEID"))
  251. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  252. else
  253. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name");
  254. string regionName = string.Empty;
  255. if (request.ContainsKey("NAME"))
  256. regionName = request["NAME"].ToString();
  257. else
  258. m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name");
  259. GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  260. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  261. Dictionary<string, object> result = new Dictionary<string, object>();
  262. if (rinfo == null)
  263. result["result"] = "null";
  264. else
  265. result["result"] = rinfo.ToKeyValuePairs();
  266. string xmlString = ServerUtils.BuildXmlResponse(result);
  267. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  268. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  269. }
  270. byte[] GetRegionsByName(Dictionary<string, object> request)
  271. {
  272. UUID scopeID = UUID.Zero;
  273. if (request.ContainsKey("SCOPEID"))
  274. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  275. else
  276. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name");
  277. string regionName = string.Empty;
  278. if (request.ContainsKey("NAME"))
  279. regionName = request["NAME"].ToString();
  280. else
  281. m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name");
  282. int max = 0;
  283. if (request.ContainsKey("MAX"))
  284. Int32.TryParse(request["MAX"].ToString(), out max);
  285. else
  286. m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name");
  287. List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max);
  288. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  289. Dictionary<string, object> result = new Dictionary<string, object>();
  290. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  291. result["result"] = "null";
  292. else
  293. {
  294. int i = 0;
  295. foreach (GridRegion rinfo in rinfos)
  296. {
  297. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  298. result["region" + i] = rinfoDict;
  299. i++;
  300. }
  301. }
  302. string xmlString = ServerUtils.BuildXmlResponse(result);
  303. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  304. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  305. }
  306. byte[] GetRegionRange(Dictionary<string, object> request)
  307. {
  308. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  309. UUID scopeID = UUID.Zero;
  310. if (request.ContainsKey("SCOPEID"))
  311. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  312. else
  313. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  314. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  315. if (request.ContainsKey("XMIN"))
  316. Int32.TryParse(request["XMIN"].ToString(), out xmin);
  317. else
  318. m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range");
  319. if (request.ContainsKey("XMAX"))
  320. Int32.TryParse(request["XMAX"].ToString(), out xmax);
  321. else
  322. m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range");
  323. if (request.ContainsKey("YMIN"))
  324. Int32.TryParse(request["YMIN"].ToString(), out ymin);
  325. else
  326. m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range");
  327. if (request.ContainsKey("YMAX"))
  328. Int32.TryParse(request["YMAX"].ToString(), out ymax);
  329. else
  330. m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range");
  331. List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  332. Dictionary<string, object> result = new Dictionary<string, object>();
  333. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  334. result["result"] = "null";
  335. else
  336. {
  337. int i = 0;
  338. foreach (GridRegion rinfo in rinfos)
  339. {
  340. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  341. result["region" + i] = rinfoDict;
  342. i++;
  343. }
  344. }
  345. string xmlString = ServerUtils.BuildXmlResponse(result);
  346. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  347. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  348. }
  349. byte[] GetDefaultRegions(Dictionary<string, object> request)
  350. {
  351. //m_log.DebugFormat("[GRID HANDLER]: GetDefaultRegions");
  352. UUID scopeID = UUID.Zero;
  353. if (request.ContainsKey("SCOPEID"))
  354. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  355. else
  356. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  357. List<GridRegion> rinfos = m_GridService.GetDefaultRegions(scopeID);
  358. Dictionary<string, object> result = new Dictionary<string, object>();
  359. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  360. result["result"] = "null";
  361. else
  362. {
  363. int i = 0;
  364. foreach (GridRegion rinfo in rinfos)
  365. {
  366. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  367. result["region" + i] = rinfoDict;
  368. i++;
  369. }
  370. }
  371. string xmlString = ServerUtils.BuildXmlResponse(result);
  372. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  373. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  374. }
  375. byte[] GetDefaultHypergridRegions(Dictionary<string, object> request)
  376. {
  377. //m_log.DebugFormat("[GRID HANDLER]: GetDefaultRegions");
  378. UUID scopeID = UUID.Zero;
  379. if (request.ContainsKey("SCOPEID"))
  380. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  381. else
  382. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  383. List<GridRegion> rinfos = m_GridService.GetDefaultHypergridRegions(scopeID);
  384. Dictionary<string, object> result = new Dictionary<string, object>();
  385. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  386. result["result"] = "null";
  387. else
  388. {
  389. int i = 0;
  390. foreach (GridRegion rinfo in rinfos)
  391. {
  392. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  393. result["region" + i] = rinfoDict;
  394. i++;
  395. }
  396. }
  397. string xmlString = ServerUtils.BuildXmlResponse(result);
  398. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  399. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  400. }
  401. byte[] GetFallbackRegions(Dictionary<string, object> request)
  402. {
  403. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  404. UUID scopeID = UUID.Zero;
  405. if (request.ContainsKey("SCOPEID"))
  406. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  407. else
  408. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get fallback regions");
  409. int x = 0, y = 0;
  410. if (request.ContainsKey("X"))
  411. Int32.TryParse(request["X"].ToString(), out x);
  412. else
  413. m_log.WarnFormat("[GRID HANDLER]: no X in request to get fallback regions");
  414. if (request.ContainsKey("Y"))
  415. Int32.TryParse(request["Y"].ToString(), out y);
  416. else
  417. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get fallback regions");
  418. List<GridRegion> rinfos = m_GridService.GetFallbackRegions(scopeID, x, y);
  419. Dictionary<string, object> result = new Dictionary<string, object>();
  420. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  421. result["result"] = "null";
  422. else
  423. {
  424. int i = 0;
  425. foreach (GridRegion rinfo in rinfos)
  426. {
  427. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  428. result["region" + i] = rinfoDict;
  429. i++;
  430. }
  431. }
  432. string xmlString = ServerUtils.BuildXmlResponse(result);
  433. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  434. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  435. }
  436. byte[] GetHyperlinks(Dictionary<string, object> request)
  437. {
  438. //m_log.DebugFormat("[GRID HANDLER]: GetHyperlinks");
  439. UUID scopeID = UUID.Zero;
  440. if (request.ContainsKey("SCOPEID"))
  441. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  442. else
  443. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get linked regions");
  444. List<GridRegion> rinfos = m_GridService.GetHyperlinks(scopeID);
  445. Dictionary<string, object> result = new Dictionary<string, object>();
  446. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  447. result["result"] = "null";
  448. else
  449. {
  450. int i = 0;
  451. foreach (GridRegion rinfo in rinfos)
  452. {
  453. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  454. result["region" + i] = rinfoDict;
  455. i++;
  456. }
  457. }
  458. string xmlString = ServerUtils.BuildXmlResponse(result);
  459. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  460. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  461. }
  462. byte[] GetRegionFlags(Dictionary<string, object> request)
  463. {
  464. UUID scopeID = UUID.Zero;
  465. if (request.ContainsKey("SCOPEID"))
  466. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  467. else
  468. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  469. UUID regionID = UUID.Zero;
  470. if (request.ContainsKey("REGIONID"))
  471. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  472. else
  473. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  474. int flags = m_GridService.GetRegionFlags(scopeID, regionID);
  475. // m_log.DebugFormat("[GRID HANDLER]: flags for region {0}: {1}", regionID, flags);
  476. Dictionary<string, object> result = new Dictionary<string, object>();
  477. result["result"] = flags.ToString();
  478. string xmlString = ServerUtils.BuildXmlResponse(result);
  479. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  480. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  481. }
  482. #endregion
  483. #region Misc
  484. private byte[] SuccessResult()
  485. {
  486. XmlDocument doc = new XmlDocument();
  487. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  488. "", "");
  489. doc.AppendChild(xmlnode);
  490. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  491. "");
  492. doc.AppendChild(rootElement);
  493. XmlElement result = doc.CreateElement("", "Result", "");
  494. result.AppendChild(doc.CreateTextNode("Success"));
  495. rootElement.AppendChild(result);
  496. return DocToBytes(doc);
  497. }
  498. private byte[] FailureResult()
  499. {
  500. return FailureResult(String.Empty);
  501. }
  502. private byte[] FailureResult(string msg)
  503. {
  504. XmlDocument doc = new XmlDocument();
  505. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  506. "", "");
  507. doc.AppendChild(xmlnode);
  508. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  509. "");
  510. doc.AppendChild(rootElement);
  511. XmlElement result = doc.CreateElement("", "Result", "");
  512. result.AppendChild(doc.CreateTextNode("Failure"));
  513. rootElement.AppendChild(result);
  514. XmlElement message = doc.CreateElement("", "Message", "");
  515. message.AppendChild(doc.CreateTextNode(msg));
  516. rootElement.AppendChild(message);
  517. return DocToBytes(doc);
  518. }
  519. private byte[] DocToBytes(XmlDocument doc)
  520. {
  521. MemoryStream ms = new MemoryStream();
  522. XmlTextWriter xw = new XmlTextWriter(ms, null);
  523. xw.Formatting = Formatting.Indented;
  524. doc.WriteTo(xw);
  525. xw.Flush();
  526. return ms.ToArray();
  527. }
  528. #endregion
  529. }
  530. }