PageRenderTime 37ms CodeModel.GetById 22ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Aurora/Services/GenericServices/SimulationService/Simulation/ObjectHandlers.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 225 lines | 155 code | 26 blank | 44 comment | 47 complexity | fe9dc2c9ae865a8d6528ed0eca3b1f82 MD5 | raw file
  1/*
  2 * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
 29using Aurora.Framework.ConsoleFramework;
 30using Aurora.Framework.Modules;
 31using Aurora.Framework.SceneInfo.Entities;
 32using Aurora.Framework.Services;
 33using Aurora.Framework.Utilities;
 34using Nini.Config;
 35using OpenMetaverse;
 36using OpenMetaverse.StructuredData;
 37using System;
 38using System.Collections;
 39using System.Net;
 40using GridRegion = Aurora.Framework.Services.GridRegion;
 41
 42namespace Aurora.Services
 43{
 44    public class ObjectHandler
 45    {
 46        private readonly ISimulationService m_SimulationService;
 47        private readonly bool m_allowForeignIncomingObjects;
 48
 49        public ObjectHandler()
 50        {
 51        }
 52
 53        public ObjectHandler(ISimulationService sim, IConfigSource source)
 54        {
 55            IConfig simulationConfig = source.Configs["Handlers"];
 56            if (simulationConfig != null)
 57                m_allowForeignIncomingObjects = simulationConfig.GetBoolean("AllowIncomingForeignObjects",
 58                                                                            m_allowForeignIncomingObjects);
 59            m_SimulationService = sim.InnerService;
 60        }
 61
 62        public Hashtable Handler(Hashtable request)
 63        {
 64            //MainConsole.Instance.Debug("[CONNECTION DEBUGGING]: ObjectHandler Called");
 65
 66            //MainConsole.Instance.Debug("---------------------------");
 67            //MainConsole.Instance.Debug(" >> uri=" + request["uri"]);
 68            //MainConsole.Instance.Debug(" >> content-type=" + request["content-type"]);
 69            //MainConsole.Instance.Debug(" >> http-method=" + request["http-method"]);
 70            //MainConsole.Instance.Debug("---------------------------\n");
 71
 72            Hashtable responsedata = new Hashtable();
 73            responsedata["content_type"] = "text/html";
 74
 75            UUID objectID;
 76            UUID regionID;
 77            string action;
 78            if (!WebUtils.GetParams((string) request["uri"], out objectID, out regionID, out action) ||
 79                m_allowForeignIncomingObjects)
 80            {
 81                //MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: Invalid parameters for object message {0}", request["uri"]);
 82                responsedata["int_response_code"] = 404;
 83                responsedata["str_response_string"] = "false";
 84
 85                return responsedata;
 86            }
 87
 88            // Next, let's parse the verb
 89            string method = (string) request["http-method"];
 90            if (method.Equals("POST"))
 91            {
 92                DoObjectPost(request, responsedata, regionID);
 93                return responsedata;
 94            }
 95            else if (method.Equals("PUT"))
 96            {
 97                DoObjectPut(request, responsedata, regionID);
 98                return responsedata;
 99            }
100                //else if (method.Equals("DELETE"))
101                //{
102                //    DoObjectDelete(request, responsedata, agentID, action, regionHandle);
103                //    return responsedata;
104                //}
105            else
106            {
107                MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: method {0} not supported in object message", method);
108                responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
109                responsedata["str_response_string"] = "Mthod not allowed";
110
111                return responsedata;
112            }
113        }
114
115        protected virtual void DoObjectPost(Hashtable request, Hashtable responsedata, UUID regionID)
116        {
117            OSDMap args = WebUtils.GetOSDMap((string) request["body"]);
118            if (args == null)
119            {
120                responsedata["int_response_code"] = 400;
121                responsedata["str_response_string"] = "false";
122                return;
123            }
124            // retrieve the input arguments
125            int x = 0, y = 0;
126            UUID uuid = UUID.Zero;
127            string regionname = string.Empty;
128            if (args.ContainsKey("destination_x") && args["destination_x"] != null)
129                Int32.TryParse(args["destination_x"].AsString(), out x);
130            if (args.ContainsKey("destination_y") && args["destination_y"] != null)
131                Int32.TryParse(args["destination_y"].AsString(), out y);
132            if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
133                UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
134            if (args.ContainsKey("destination_name") && args["destination_name"] != null)
135                regionname = args["destination_name"].ToString();
136
137            GridRegion destination = new GridRegion
138                                         {RegionID = uuid, RegionLocX = x, RegionLocY = y, RegionName = regionname};
139
140            string sogXmlStr = "";
141            if (args.ContainsKey("sog") && args["sog"] != null)
142                sogXmlStr = args["sog"].AsString();
143
144            ISceneEntity sog = null;
145            try
146            {
147                //MainConsole.Instance.DebugFormat("[OBJECT HANDLER]: received {0}", sogXmlStr);
148                IRegionSerialiserModule mod =
149                    m_SimulationService.Scene.RequestModuleInterface<IRegionSerialiserModule>();
150                if (mod != null)
151                    sog = mod.DeserializeGroupFromXml2(sogXmlStr, m_SimulationService.Scene);
152            }
153            catch (Exception ex)
154            {
155                MainConsole.Instance.InfoFormat("[OBJECT HANDLER]: exception on deserializing scene object {0}", ex);
156                responsedata["int_response_code"] = HttpStatusCode.BadRequest;
157                responsedata["str_response_string"] = "Bad request";
158                return;
159            }
160
161            bool result = false;
162
163            if (sog == null)
164            {
165                MainConsole.Instance.ErrorFormat(
166                    "[OBJECT HANDLER]: error on deserializing scene object as the object was null!");
167
168                responsedata["int_response_code"] = HttpStatusCode.OK;
169                responsedata["str_response_string"] = result.ToString();
170            }
171
172            try
173            {
174                // This is the meaning of POST object
175                result = m_SimulationService.CreateObject(destination, sog);
176            }
177            catch (Exception e)
178            {
179                MainConsole.Instance.DebugFormat("[OBJECT HANDLER]: Exception in CreateObject: {0}", e.StackTrace);
180            }
181
182            responsedata["int_response_code"] = HttpStatusCode.OK;
183            responsedata["str_response_string"] = result.ToString();
184        }
185
186        protected virtual void DoObjectPut(Hashtable request, Hashtable responsedata, UUID regionID)
187        {
188            OSDMap args = WebUtils.GetOSDMap((string) request["body"]);
189            if (args == null)
190            {
191                responsedata["int_response_code"] = 400;
192                responsedata["str_response_string"] = "false";
193                return;
194            }
195
196            // retrieve the input arguments
197            int x = 0, y = 0;
198            UUID uuid = UUID.Zero;
199            string regionname = string.Empty;
200            if (args.ContainsKey("destination_x") && args["destination_x"] != null)
201                Int32.TryParse(args["destination_x"].AsString(), out x);
202            if (args.ContainsKey("destination_y") && args["destination_y"] != null)
203                Int32.TryParse(args["destination_y"].AsString(), out y);
204            if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
205                UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
206            if (args.ContainsKey("destination_name") && args["destination_name"] != null)
207                regionname = args["destination_name"].ToString();
208
209            GridRegion destination = new GridRegion
210                                         {RegionID = uuid, RegionLocX = x, RegionLocY = y, RegionName = regionname};
211
212            UUID userID = UUID.Zero, itemID = UUID.Zero;
213            if (args.ContainsKey("userid") && args["userid"] != null)
214                userID = args["userid"].AsUUID();
215            if (args.ContainsKey("itemid") && args["itemid"] != null)
216                itemID = args["itemid"].AsUUID();
217
218            // This is the meaning of PUT object
219            bool result = m_SimulationService.CreateObject(destination, userID, itemID);
220
221            responsedata["int_response_code"] = 200;
222            responsedata["str_response_string"] = result.ToString();
223        }
224    }
225}