JsonRpcRequestManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. OSD osdtmp;
  93. if (!response.TryGetValue("_Result", out osdtmp) || !(osdtmp is OSDMap))
  94. {
  95. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  96. method, uri, OSDParser.SerializeJsonString(response));
  97. return false;
  98. }
  99. response = osdtmp as OSDMap;
  100. if (response.TryGetValue("error", out osdtmp))
  101. {
  102. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  103. method, uri, OSDParser.SerializeJsonString(osdtmp));
  104. return false;
  105. }
  106. if (!response.TryGetValue("result", out osdtmp) || !(osdtmp is OSDMap))
  107. {
  108. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  109. method, uri, OSDParser.SerializeJsonString(response));
  110. return false;
  111. }
  112. OSD.DeserializeMembers(ref parameters, (OSDMap)osdtmp);
  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. OSD osdtmp;
  153. if (!response.TryGetValue("_Result", out osdtmp) || !(osdtmp is OSDMap))
  154. {
  155. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
  156. method, uri, OSDParser.SerializeJsonString(response));
  157. return false;
  158. }
  159. response = osdtmp as OSDMap;
  160. if (response.TryGetValue("error", out osdtmp))
  161. {
  162. data = osdtmp;
  163. m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
  164. method, uri, OSDParser.SerializeJsonString(osdtmp));
  165. return false;
  166. }
  167. data = response;
  168. return true;
  169. }
  170. }
  171. }