JsonRpcRequestManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 System.Net.Sockets;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.IO;
  33. using OpenMetaverse.StructuredData;
  34. using OpenMetaverse;
  35. using log4net;
  36. namespace OpenSim.Framework.Servers.HttpServer
  37. {
  38. /// <summary>
  39. /// Json rpc request manager.
  40. /// </summary>
  41. public class JsonRpcRequestManager
  42. {
  43. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. public JsonRpcRequestManager()
  45. {
  46. }
  47. /// <summary>
  48. /// Sends json-rpc request with a serializable type.
  49. /// </summary>
  50. /// <returns>
  51. /// OSD Map.
  52. /// </returns>
  53. /// <param name='parameters'>
  54. /// Serializable type .
  55. /// </param>
  56. /// <param name='method'>
  57. /// Json-rpc method to call.
  58. /// </param>
  59. /// <param name='uri'>
  60. /// URI of json-rpc service.
  61. /// </param>
  62. /// <param name='jsonId'>
  63. /// Id for our call.
  64. /// </param>
  65. public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
  66. {
  67. if (jsonId == null)
  68. throw new ArgumentNullException("jsonId");
  69. if (uri == null)
  70. throw new ArgumentNullException("uri");
  71. if (method == null)
  72. throw new ArgumentNullException("method");
  73. if (parameters == null)
  74. throw new ArgumentNullException("parameters");
  75. OSDMap request = new OSDMap();
  76. request.Add("jsonrpc", OSD.FromString("2.0"));
  77. request.Add("id", OSD.FromString(jsonId));
  78. request.Add("method", OSD.FromString(method));
  79. request.Add("params", OSD.SerializeMembers(parameters));
  80. OSDMap response;
  81. try
  82. {
  83. response = WebUtil.PostToService(uri, request, 10000, true);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  88. return false;
  89. }
  90. if (!response.ContainsKey("_Result"))
  91. {
  92. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  93. method, uri, OSDParser.SerializeJsonString(response));
  94. return false;
  95. }
  96. response = (OSDMap)response["_Result"];
  97. OSD data;
  98. if (response.ContainsKey("error"))
  99. {
  100. data = response["error"];
  101. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  102. method, uri, OSDParser.SerializeJsonString(data));
  103. return false;
  104. }
  105. if (!response.ContainsKey("result"))
  106. {
  107. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  108. method, uri, OSDParser.SerializeJsonString(response));
  109. return false;
  110. }
  111. data = response["result"];
  112. OSD.DeserializeMembers(ref parameters, (OSDMap)data);
  113. return true;
  114. }
  115. /// <summary>
  116. /// Sends json-rpc request with OSD parameter.
  117. /// </summary>
  118. /// <returns>
  119. /// The rpc request.
  120. /// </returns>
  121. /// <param name='data'>
  122. /// data - incoming as parameters, outgoing as result/error
  123. /// </param>
  124. /// <param name='method'>
  125. /// Json-rpc method to call.
  126. /// </param>
  127. /// <param name='uri'>
  128. /// URI of json-rpc service.
  129. /// </param>
  130. /// <param name='jsonId'>
  131. /// If set to <c>true</c> json identifier.
  132. /// </param>
  133. public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  134. {
  135. if (string.IsNullOrEmpty(jsonId))
  136. jsonId = UUID.Random().ToString();
  137. OSDMap request = new OSDMap();
  138. request.Add("jsonrpc", OSD.FromString("2.0"));
  139. request.Add("id", OSD.FromString(jsonId));
  140. request.Add("method", OSD.FromString(method));
  141. request.Add("params", data);
  142. OSDMap response;
  143. try
  144. {
  145. response = WebUtil.PostToService(uri, request, 10000, true);
  146. }
  147. catch (Exception e)
  148. {
  149. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  150. return false;
  151. }
  152. if (!response.ContainsKey("_Result"))
  153. {
  154. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  155. method, uri, OSDParser.SerializeJsonString(response));
  156. return false;
  157. }
  158. response = (OSDMap)response["_Result"];
  159. if (response.ContainsKey("error"))
  160. {
  161. data = response["error"];
  162. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  163. method, uri, OSDParser.SerializeJsonString(data));
  164. return false;
  165. }
  166. data = response;
  167. return true;
  168. }
  169. }
  170. }