aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework')
-rw-r--r--OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs215
1 files changed, 215 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs
new file mode 100644
index 0000000..1d69dcb
--- /dev/null
+++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs
@@ -0,0 +1,215 @@
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
28using System;
29using System.Net;
30using System.Net.Sockets;
31using System.Reflection;
32using System.Text;
33using System.IO;
34using OpenMetaverse.StructuredData;
35using OpenMetaverse;
36using log4net;
37
38namespace 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 #region Web Util
52 /// <summary>
53 /// Sends json-rpc request with a serializable type.
54 /// </summary>
55 /// <returns>
56 /// OSD Map.
57 /// </returns>
58 /// <param name='parameters'>
59 /// Serializable type .
60 /// </param>
61 /// <param name='method'>
62 /// Json-rpc method to call.
63 /// </param>
64 /// <param name='uri'>
65 /// URI of json-rpc service.
66 /// </param>
67 /// <param name='jsonId'>
68 /// Id for our call.
69 /// </param>
70 public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
71 {
72 if (jsonId == null)
73 throw new ArgumentNullException ("jsonId");
74 if (uri == null)
75 throw new ArgumentNullException ("uri");
76 if (method == null)
77 throw new ArgumentNullException ("method");
78 if (parameters == null)
79 throw new ArgumentNullException ("parameters");
80
81 // Prep our payload
82 OSDMap json = new OSDMap();
83
84 json.Add("jsonrpc", OSD.FromString("2.0"));
85 json.Add("id", OSD.FromString(jsonId));
86 json.Add("method", OSD.FromString(method));
87
88 json.Add("params", OSD.SerializeMembers(parameters));
89
90 string jsonRequestData = OSDParser.SerializeJsonString(json);
91 byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
92
93 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
94
95 webRequest.ContentType = "application/json-rpc";
96 webRequest.Method = "POST";
97
98 Stream dataStream = webRequest.GetRequestStream();
99 dataStream.Write(content, 0, content.Length);
100 dataStream.Close();
101
102 WebResponse webResponse = null;
103 try
104 {
105 webResponse = webRequest.GetResponse();
106 }
107 catch (WebException e)
108 {
109 Console.WriteLine("Web Error" + e.Message);
110 Console.WriteLine ("Please check input");
111 return false;
112 }
113
114 Stream rstream = webResponse.GetResponseStream();
115
116 OSDMap mret = new OSDMap();
117 try
118 {
119 mret = (OSDMap)OSDParser.DeserializeJson(rstream);
120 }
121 catch (Exception e)
122 {
123 m_log.DebugFormat("[JSONRPC]: JsonRpcRequest Error {0}", e.Message);
124 return false;
125 }
126
127
128 if (mret.ContainsKey("error"))
129 return false;
130
131 // get params...
132 OSD.DeserializeMembers(ref parameters, (OSDMap) mret["result"]);
133 return true;
134 }
135
136 /// <summary>
137 /// Sends json-rpc request with OSD parameter.
138 /// </summary>
139 /// <returns>
140 /// The rpc request.
141 /// </returns>
142 /// <param name='data'>
143 /// data - incoming as parameters, outgong as result/error
144 /// </param>
145 /// <param name='method'>
146 /// Json-rpc method to call.
147 /// </param>
148 /// <param name='uri'>
149 /// URI of json-rpc service.
150 /// </param>
151 /// <param name='jsonId'>
152 /// If set to <c>true</c> json identifier.
153 /// </param>
154 public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
155 {
156 OSDMap map = new OSDMap();
157
158 map["jsonrpc"] = "2.0";
159 if(string.IsNullOrEmpty(jsonId))
160 map["id"] = UUID.Random().ToString();
161 else
162 map["id"] = jsonId;
163
164 map["method"] = method;
165 map["params"] = data;
166
167 string jsonRequestData = OSDParser.SerializeJsonString(map);
168 byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
169
170 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
171 webRequest.ContentType = "application/json-rpc";
172 webRequest.Method = "POST";
173
174 Stream dataStream = webRequest.GetRequestStream();
175 dataStream.Write(content, 0, content.Length);
176 dataStream.Close();
177
178 WebResponse webResponse = null;
179 try
180 {
181 webResponse = webRequest.GetResponse();
182 }
183 catch (WebException e)
184 {
185 Console.WriteLine("Web Error" + e.Message);
186 Console.WriteLine ("Please check input");
187 return false;
188 }
189
190 Stream rstream = webResponse.GetResponseStream();
191
192 OSDMap response = new OSDMap();
193 try
194 {
195 response = (OSDMap)OSDParser.DeserializeJson(rstream);
196 }
197 catch (Exception e)
198 {
199 m_log.DebugFormat("[JSONRPC]: JsonRpcRequest Error {0}", e.Message);
200 return false;
201 }
202
203 if(response.ContainsKey("error"))
204 {
205 data = response["error"];
206 return false;
207 }
208
209 data = response;
210
211 return true;
212 }
213 #endregion Web Util
214 }
215}