JsonRpcResponse.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 System;
  28. using System.Net;
  29. using OpenMetaverse.StructuredData;
  30. namespace OpenSim.Framework.Servers.HttpServer
  31. {
  32. public sealed class ErrorCode
  33. {
  34. private ErrorCode() {}
  35. public const int ParseError = -32700;
  36. public const int InvalidRequest = -32600;
  37. public const int MethodNotFound = -32601;
  38. public const int InvalidParams = -32602;
  39. public const int InternalError = -32604;
  40. }
  41. public class JsonRpcError
  42. {
  43. internal OSDMap Error = new OSDMap();
  44. public int Code
  45. {
  46. get
  47. {
  48. if (Error.ContainsKey("code"))
  49. return Error["code"].AsInteger();
  50. else
  51. return 0;
  52. }
  53. set
  54. {
  55. Error["code"] = OSD.FromInteger(value);
  56. }
  57. }
  58. public string Message
  59. {
  60. get
  61. {
  62. if (Error.ContainsKey("message"))
  63. return Error["message"].AsString();
  64. else
  65. return null;
  66. }
  67. set
  68. {
  69. Error["message"] = OSD.FromString(value);
  70. }
  71. }
  72. public OSD Data
  73. {
  74. get; set;
  75. }
  76. }
  77. public class JsonRpcResponse
  78. {
  79. public string JsonRpc
  80. {
  81. get
  82. {
  83. return Reply["jsonrpc"].AsString();
  84. }
  85. set
  86. {
  87. Reply["jsonrpc"] = OSD.FromString(value);
  88. }
  89. }
  90. public string Id
  91. {
  92. get
  93. {
  94. return Reply["id"].AsString();
  95. }
  96. set
  97. {
  98. Reply["id"] = OSD.FromString(value);
  99. }
  100. }
  101. public OSD Result
  102. {
  103. get; set;
  104. }
  105. public JsonRpcError Error
  106. {
  107. get; set;
  108. }
  109. public OSDMap Reply = new OSDMap();
  110. public JsonRpcResponse()
  111. {
  112. Error = new JsonRpcError();
  113. }
  114. public string Serialize()
  115. {
  116. if (Result != null)
  117. Reply["result"] = Result;
  118. if (Error.Code != 0)
  119. {
  120. Reply["error"] = (OSD)Error.Error;
  121. }
  122. string result = string.Empty;
  123. try
  124. {
  125. result = OSDParser.SerializeJsonString(Reply);
  126. }
  127. catch
  128. {
  129. }
  130. return result;
  131. }
  132. }
  133. }