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