aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs')
-rw-r--r--OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs211
1 files changed, 211 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..a44f471
--- /dev/null
+++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs
@@ -0,0 +1,211 @@
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 using (Stream dataStream = webRequest.GetRequestStream())
103 dataStream.Write(content, 0, content.Length);
104
105 WebResponse webResponse = null;
106 try
107 {
108 webResponse = webRequest.GetResponse();
109 }
110 catch (WebException e)
111 {
112 Console.WriteLine("Web Error" + e.Message);
113 Console.WriteLine ("Please check input");
114 return false;
115 }
116
117 using (webResponse)
118 using (Stream rstream = webResponse.GetResponseStream())
119 {
120 OSDMap mret = (OSDMap)OSDParser.DeserializeJson(rstream);
121
122 if (mret.ContainsKey("error"))
123 return false;
124
125 // get params...
126 OSD.DeserializeMembers(ref parameters, (OSDMap)mret["result"]);
127 return true;
128 }
129 }
130
131 /// <summary>
132 /// Sends json-rpc request with OSD parameter.
133 /// </summary>
134 /// <returns>
135 /// The rpc request.
136 /// </returns>
137 /// <param name='data'>
138 /// data - incoming as parameters, outgong as result/error
139 /// </param>
140 /// <param name='method'>
141 /// Json-rpc method to call.
142 /// </param>
143 /// <param name='uri'>
144 /// URI of json-rpc service.
145 /// </param>
146 /// <param name='jsonId'>
147 /// If set to <c>true</c> json identifier.
148 /// </param>
149 public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
150 {
151 OSDMap map = new OSDMap();
152
153 map["jsonrpc"] = "2.0";
154 if(string.IsNullOrEmpty(jsonId))
155 map["id"] = UUID.Random().ToString();
156 else
157 map["id"] = jsonId;
158
159 map["method"] = method;
160 map["params"] = data;
161
162 string jsonRequestData = OSDParser.SerializeJsonString(map);
163 byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
164
165 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
166 webRequest.ContentType = "application/json-rpc";
167 webRequest.Method = "POST";
168
169 using (Stream dataStream = webRequest.GetRequestStream())
170 dataStream.Write(content, 0, content.Length);
171
172 WebResponse webResponse = null;
173 try
174 {
175 webResponse = webRequest.GetResponse();
176 }
177 catch (WebException e)
178 {
179 Console.WriteLine("Web Error" + e.Message);
180 Console.WriteLine ("Please check input");
181 return false;
182 }
183
184 using (webResponse)
185 using (Stream rstream = webResponse.GetResponseStream())
186 {
187 OSDMap response = new OSDMap();
188 try
189 {
190 response = (OSDMap)OSDParser.DeserializeJson(rstream);
191 }
192 catch (Exception e)
193 {
194 m_log.DebugFormat("[JSONRPC]: JsonRpcRequest Error {0}", e.Message);
195 return false;
196 }
197
198 if (response.ContainsKey("error"))
199 {
200 data = response["error"];
201 return false;
202 }
203
204 data = response;
205
206 return true;
207 }
208 }
209 #endregion Web Util
210 }
211}