JsonRpcRequestManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if(string.IsNullOrWhiteSpace(uri))
  76. return false;
  77. OSDMap request = new OSDMap();
  78. request.Add("jsonrpc", OSD.FromString("2.0"));
  79. request.Add("id", OSD.FromString(jsonId));
  80. request.Add("method", OSD.FromString(method));
  81. request.Add("params", OSD.SerializeMembers(parameters));
  82. OSDMap response;
  83. try
  84. {
  85. response = WebUtil.PostToService(uri, request, 10000, true);
  86. }
  87. catch (Exception e)
  88. {
  89. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  90. return false;
  91. }
  92. if (!response.ContainsKey("_Result"))
  93. {
  94. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  95. method, uri, OSDParser.SerializeJsonString(response));
  96. return false;
  97. }
  98. response = (OSDMap)response["_Result"];
  99. OSD data;
  100. if (response.ContainsKey("error"))
  101. {
  102. data = response["error"];
  103. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  104. method, uri, OSDParser.SerializeJsonString(data));
  105. return false;
  106. }
  107. if (!response.ContainsKey("result"))
  108. {
  109. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  110. method, uri, OSDParser.SerializeJsonString(response));
  111. return false;
  112. }
  113. data = response["result"];
  114. OSD.DeserializeMembers(ref parameters, (OSDMap)data);
  115. return true;
  116. }
  117. /// <summary>
  118. /// Sends json-rpc request with OSD parameter.
  119. /// </summary>
  120. /// <returns>
  121. /// The rpc request.
  122. /// </returns>
  123. /// <param name='data'>
  124. /// data - incoming as parameters, outgoing as result/error
  125. /// </param>
  126. /// <param name='method'>
  127. /// Json-rpc method to call.
  128. /// </param>
  129. /// <param name='uri'>
  130. /// URI of json-rpc service.
  131. /// </param>
  132. /// <param name='jsonId'>
  133. /// If set to <c>true</c> json identifier.
  134. /// </param>
  135. public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  136. {
  137. if (string.IsNullOrEmpty(jsonId))
  138. jsonId = UUID.Random().ToString();
  139. OSDMap request = new OSDMap();
  140. request.Add("jsonrpc", OSD.FromString("2.0"));
  141. request.Add("id", OSD.FromString(jsonId));
  142. request.Add("method", OSD.FromString(method));
  143. request.Add("params", data);
  144. OSDMap response;
  145. try
  146. {
  147. response = WebUtil.PostToService(uri, request, 10000, true);
  148. }
  149. catch (Exception e)
  150. {
  151. m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
  152. return false;
  153. }
  154. if (!response.ContainsKey("_Result"))
  155. {
  156. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  157. method, uri, OSDParser.SerializeJsonString(response));
  158. return false;
  159. }
  160. response = (OSDMap)response["_Result"];
  161. if (response.ContainsKey("error"))
  162. {
  163. data = response["error"];
  164. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  165. method, uri, OSDParser.SerializeJsonString(data));
  166. return false;
  167. }
  168. data = response;
  169. return true;
  170. }
  171. }
  172. }