aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/GridServer/GridRestModule.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Grid/GridServer/GridRestModule.cs269
1 files changed, 269 insertions, 0 deletions
diff --git a/OpenSim/Grid/GridServer/GridRestModule.cs b/OpenSim/Grid/GridServer/GridRestModule.cs
new file mode 100644
index 0000000..1ed0ac1
--- /dev/null
+++ b/OpenSim/Grid/GridServer/GridRestModule.cs
@@ -0,0 +1,269 @@
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 OpenSim 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.Collections;
30using System.Collections.Generic;
31using System.IO;
32using System.Reflection;
33using System.Xml;
34using log4net;
35using OpenMetaverse;
36using OpenSim.Data;
37using OpenSim.Framework;
38using OpenSim.Framework.Communications;
39using OpenSim.Framework.Servers;
40
41namespace OpenSim.Grid.GridServer
42{
43 public class GridRestModule
44 {
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46
47 private GridDBService m_gridDBService;
48 private IGridCore m_gridCore;
49
50 protected GridConfig m_config;
51
52 /// <value>
53 /// Used to notify old regions as to which OpenSim version to upgrade to
54 /// </value>
55 private string m_opensimVersion;
56
57 protected BaseHttpServer m_httpServer;
58
59 /// <summary>
60 /// Constructor
61 /// </summary>
62 /// <param name="opensimVersion">
63 /// Used to notify old regions as to which OpenSim version to upgrade to
64 /// </param>
65 public GridRestModule(string opensimVersion, GridDBService gridDBService, IGridCore gridCore, GridConfig config)
66 {
67 m_opensimVersion = opensimVersion;
68 m_gridDBService = gridDBService;
69 m_gridCore = gridCore;
70 m_config = config;
71 m_httpServer = m_gridCore.GetHttpServer();
72 }
73
74 public void Initialise()
75 {
76 m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/sims/", RestGetSimMethod));
77 m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/sims/", RestSetSimMethod));
78
79 m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/regions/", RestGetRegionMethod));
80 m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", RestSetRegionMethod));
81 }
82
83 /// <summary>
84 /// Performs a REST Get Operation
85 /// </summary>
86 /// <param name="request"></param>
87 /// <param name="path"></param>
88 /// <param name="param"></param>
89 /// <param name="httpRequest">HTTP request header object</param>
90 /// <param name="httpResponse">HTTP response header object</param>
91 /// <returns></returns>
92 public string RestGetRegionMethod(string request, string path, string param,
93 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
94 {
95 return RestGetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse);
96 }
97
98 /// <summary>
99 /// Performs a REST Set Operation
100 /// </summary>
101 /// <param name="request"></param>
102 /// <param name="path"></param>
103 /// <param name="param"></param>
104 /// <param name="httpRequest">HTTP request header object</param>
105 /// <param name="httpResponse">HTTP response header object</param>
106 /// <returns></returns>
107 public string RestSetRegionMethod(string request, string path, string param,
108 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
109 {
110 return RestSetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse);
111 }
112
113 /// <summary>
114 /// Returns information about a sim via a REST Request
115 /// </summary>
116 /// <param name="request"></param>
117 /// <param name="path"></param>
118 /// <param name="param">A string representing the sim's UUID</param>
119 /// <param name="httpRequest">HTTP request header object</param>
120 /// <param name="httpResponse">HTTP response header object</param>
121 /// <returns>Information about the sim in XML</returns>
122 public string RestGetSimMethod(string request, string path, string param,
123 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
124 {
125 string respstring = String.Empty;
126
127 RegionProfileData TheSim;
128
129 UUID UUID;
130 if (UUID.TryParse(param, out UUID))
131 {
132 TheSim = m_gridDBService.GetRegion(UUID);
133
134 if (!(TheSim == null))
135 {
136 respstring = "<Root>";
137 respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
138 respstring += "<sim>";
139 respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
140 respstring += "<regionname>" + TheSim.regionName + "</regionname>";
141 respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>";
142 respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
143 respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
144 respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
145 respstring += "<estate_id>1</estate_id>";
146 respstring += "</sim>";
147 respstring += "</Root>";
148 }
149 }
150 else
151 {
152 respstring = "<Root>";
153 respstring += "<error>Param must be a UUID</error>";
154 respstring += "</Root>";
155 }
156
157 return respstring;
158 }
159
160 /// <summary>
161 /// Creates or updates a sim via a REST Method Request
162 /// BROKEN with SQL Update
163 /// </summary>
164 /// <param name="request"></param>
165 /// <param name="path"></param>
166 /// <param name="param"></param>
167 /// <param name="httpRequest">HTTP request header object</param>
168 /// <param name="httpResponse">HTTP response header object</param>
169 /// <returns>"OK" or an error</returns>
170 public string RestSetSimMethod(string request, string path, string param,
171 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
172 {
173 Console.WriteLine("Processing region update via REST method");
174 RegionProfileData theSim;
175 theSim = m_gridDBService.GetRegion(new UUID(param));
176 if (theSim == null)
177 {
178 theSim = new RegionProfileData();
179 UUID UUID = new UUID(param);
180 theSim.UUID = UUID;
181 theSim.regionRecvKey = m_config.SimRecvKey;
182 }
183
184 XmlDocument doc = new XmlDocument();
185 doc.LoadXml(request);
186 XmlNode rootnode = doc.FirstChild;
187 XmlNode authkeynode = rootnode.ChildNodes[0];
188 if (authkeynode.Name != "authkey")
189 {
190 return "ERROR! bad XML - expected authkey tag";
191 }
192
193 XmlNode simnode = rootnode.ChildNodes[1];
194 if (simnode.Name != "sim")
195 {
196 return "ERROR! bad XML - expected sim tag";
197 }
198
199 //theSim.regionSendKey = Cfg;
200 theSim.regionRecvKey = m_config.SimRecvKey;
201 theSim.regionSendKey = m_config.SimSendKey;
202 theSim.regionSecret = m_config.SimRecvKey;
203 theSim.regionDataURI = String.Empty;
204 theSim.regionAssetURI = m_config.DefaultAssetServer;
205 theSim.regionAssetRecvKey = m_config.AssetRecvKey;
206 theSim.regionAssetSendKey = m_config.AssetSendKey;
207 theSim.regionUserURI = m_config.DefaultUserServer;
208 theSim.regionUserSendKey = m_config.UserSendKey;
209 theSim.regionUserRecvKey = m_config.UserRecvKey;
210
211 for (int i = 0; i < simnode.ChildNodes.Count; i++)
212 {
213 switch (simnode.ChildNodes[i].Name)
214 {
215 case "regionname":
216 theSim.regionName = simnode.ChildNodes[i].InnerText;
217 break;
218
219 case "sim_ip":
220 theSim.serverIP = simnode.ChildNodes[i].InnerText;
221 break;
222
223 case "sim_port":
224 theSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
225 break;
226
227 case "region_locx":
228 theSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
229 theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
230 break;
231
232 case "region_locy":
233 theSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
234 theSim.regionHandle = Utils.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
235 break;
236 }
237 }
238
239 theSim.serverURI = "http://" + theSim.serverIP + ":" + theSim.serverPort + "/";
240 bool requirePublic = false;
241 bool requireValid = true;
242
243 if (requirePublic &&
244 (theSim.serverIP.StartsWith("172.16") || theSim.serverIP.StartsWith("192.168") ||
245 theSim.serverIP.StartsWith("10.") || theSim.serverIP.StartsWith("0.") ||
246 theSim.serverIP.StartsWith("255.")))
247 {
248 return "ERROR! Servers must register with public addresses.";
249 }
250
251 if (requireValid && (theSim.serverIP.StartsWith("0.") || theSim.serverIP.StartsWith("255.")))
252 {
253 return "ERROR! 0.*.*.* / 255.*.*.* Addresses are invalid, please check your server config and try again";
254 }
255
256 try
257 {
258 m_log.Info("[DATA]: " +
259 "Updating / adding via " + m_gridDBService.GetNumberOfPlugins() + " storage provider(s) registered.");
260
261 return m_gridDBService.CheckReservations(theSim, authkeynode);
262 }
263 catch (Exception e)
264 {
265 return "ERROR! Could not save to database! (" + e.ToString() + ")";
266 }
267 }
268 }
269}