diff options
author | Sean Dague | 2008-05-15 11:32:28 +0000 |
---|---|---|
committer | Sean Dague | 2008-05-15 11:32:28 +0000 |
commit | 0307ad11531efb2fe95dab5dc489e37cb5bbb542 (patch) | |
tree | 1ab2444d259661cb27f5922fd476c245d450214b /OpenSim/ApplicationPlugins/Rest/RestPlugin.cs | |
parent | * Added about half of the planned ODE physics options to OpenSim.ini.example. (diff) | |
download | opensim-SC-0307ad11531efb2fe95dab5dc489e37cb5bbb542.zip opensim-SC-0307ad11531efb2fe95dab5dc489e37cb5bbb542.tar.gz opensim-SC-0307ad11531efb2fe95dab5dc489e37cb5bbb542.tar.bz2 opensim-SC-0307ad11531efb2fe95dab5dc489e37cb5bbb542.tar.xz |
Damn, forgot to manually add these as I keep forgetting that
svn patches don't do adds. :(
Diffstat (limited to 'OpenSim/ApplicationPlugins/Rest/RestPlugin.cs')
-rw-r--r-- | OpenSim/ApplicationPlugins/Rest/RestPlugin.cs | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs new file mode 100644 index 0000000..0e54f4d --- /dev/null +++ b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs | |||
@@ -0,0 +1,246 @@ | |||
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 | */ | ||
28 | |||
29 | using System; | ||
30 | using System.Threading; | ||
31 | using System.Collections; | ||
32 | using System.Collections.Generic; | ||
33 | using System.Net; | ||
34 | using System.Reflection; | ||
35 | using System.Timers; | ||
36 | using libsecondlife; | ||
37 | using Mono.Addins; | ||
38 | using Nwc.XmlRpc; | ||
39 | using Nini.Config; | ||
40 | using OpenSim.Framework; | ||
41 | using OpenSim.Framework.Console; | ||
42 | using OpenSim.Framework.Servers; | ||
43 | using OpenSim.Framework.Communications; | ||
44 | using OpenSim.Region.Environment.Scenes; | ||
45 | |||
46 | // [assembly : Addin] | ||
47 | // [assembly : AddinDependency("OpenSim", "0.5")] | ||
48 | |||
49 | namespace OpenSim.ApplicationPlugins.Rest | ||
50 | { | ||
51 | |||
52 | // [Extension("/OpenSim/Startup")] | ||
53 | public abstract class RestPlugin : IApplicationPlugin | ||
54 | { | ||
55 | #region properties | ||
56 | |||
57 | protected static readonly log4net.ILog m_log = | ||
58 | log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
59 | |||
60 | private IConfig _config; // Configuration source: Rest Plugins | ||
61 | private IConfig _pluginConfig; // Configuration source: Plugin specific | ||
62 | private OpenSimMain _app; // The 'server' | ||
63 | private BaseHttpServer _httpd; // The server's RPC interface | ||
64 | private string _prefix; // URL prefix below which all REST URLs are living | ||
65 | |||
66 | private string _godkey; | ||
67 | private int _reqk; | ||
68 | |||
69 | [ThreadStaticAttribute] | ||
70 | private static string _threadRequestID = String.Empty; | ||
71 | |||
72 | /// <summary> | ||
73 | /// Return an ever increasing request ID for logging | ||
74 | /// </summary> | ||
75 | protected string RequestID | ||
76 | { | ||
77 | get { return _reqk++.ToString(); } | ||
78 | set { _reqk = Convert.ToInt32(value); } | ||
79 | } | ||
80 | |||
81 | /// <summary> | ||
82 | /// Thread-constant message IDs for logging. | ||
83 | /// </summary> | ||
84 | protected string MsgID | ||
85 | { | ||
86 | get { return String.Format("[REST-{0}] #{1}", Name, _threadRequestID); } | ||
87 | set { _threadRequestID = value; } | ||
88 | } | ||
89 | |||
90 | /// <summary> | ||
91 | /// Returns true if Rest Plugins are enabled. | ||
92 | /// </summary> | ||
93 | public bool PluginsAreEnabled | ||
94 | { | ||
95 | get { return null != _config; } | ||
96 | } | ||
97 | |||
98 | /// <summary> | ||
99 | /// Returns true if specific Rest Plugin is enabled. | ||
100 | /// </summary> | ||
101 | public bool IsEnabled | ||
102 | { | ||
103 | get | ||
104 | { | ||
105 | return (null != _pluginConfig) && _pluginConfig.GetBoolean("enabled", false); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | /// <summary> | ||
110 | /// OpenSimMain application | ||
111 | /// </summary> | ||
112 | public OpenSimMain App | ||
113 | { | ||
114 | get { return _app; } | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// RPC server | ||
119 | /// </summary> | ||
120 | public BaseHttpServer HttpServer | ||
121 | { | ||
122 | get { return _httpd; } | ||
123 | } | ||
124 | |||
125 | /// <summary> | ||
126 | /// URL prefix to use for all REST handlers | ||
127 | /// </summary> | ||
128 | public string Prefix | ||
129 | { | ||
130 | get { return _prefix; } | ||
131 | } | ||
132 | |||
133 | /// <summary> | ||
134 | /// Configuration of the plugin | ||
135 | /// </summary> | ||
136 | public IConfig Config | ||
137 | { | ||
138 | get { return _pluginConfig; } | ||
139 | } | ||
140 | |||
141 | /// <summary> | ||
142 | /// Name of the plugin | ||
143 | /// </summary> | ||
144 | public abstract string Name { get; } | ||
145 | |||
146 | /// <summary> | ||
147 | /// Return the config section name | ||
148 | /// </summary> | ||
149 | public abstract string ConfigName { get; } | ||
150 | #endregion properties | ||
151 | |||
152 | |||
153 | #region methods | ||
154 | /// <summary> | ||
155 | /// This method is called by OpenSimMain immediately after loading the | ||
156 | /// plugin and after basic server setup, but before running any server commands. | ||
157 | /// </summary> | ||
158 | /// <remarks> | ||
159 | /// Note that entries MUST be added to the active configuration files before | ||
160 | /// the plugin can be enabled. | ||
161 | /// </remarks> | ||
162 | public virtual void Initialise(OpenSimMain openSim) | ||
163 | { | ||
164 | RequestID = "0"; | ||
165 | MsgID = RequestID; | ||
166 | |||
167 | try | ||
168 | { | ||
169 | if ((_config = openSim.ConfigSource.Configs["RestPlugins"]) == null) { | ||
170 | m_log.WarnFormat("{0} Rest Plugins not configured", MsgID); | ||
171 | return; | ||
172 | } | ||
173 | |||
174 | if (!_config.GetBoolean("enabled", false)) | ||
175 | { | ||
176 | m_log.WarnFormat("{0} Rest Plugins are disabled", MsgID); | ||
177 | return; | ||
178 | } | ||
179 | |||
180 | _app = openSim; | ||
181 | _httpd = openSim.HttpServer; | ||
182 | |||
183 | // Retrieve GOD key value, if any. | ||
184 | _godkey = _config.GetString("god_key", String.Empty); | ||
185 | // Retrive prefix if any. | ||
186 | _prefix = _config.GetString("prefix", "/admin"); | ||
187 | |||
188 | // Get plugin specific config | ||
189 | _pluginConfig = openSim.ConfigSource.Configs[ConfigName]; | ||
190 | |||
191 | m_log.InfoFormat("{0} Rest Plugins Enabled", MsgID); | ||
192 | } | ||
193 | catch (Exception e) | ||
194 | { | ||
195 | // we can safely ignore this, as it just means that | ||
196 | // the key lookup in Configs failed, which signals to | ||
197 | // us that noone is interested in our services...they | ||
198 | // don't know what they are missing out on... | ||
199 | // NOTE: Under the present OpenSim implementation it is | ||
200 | // not possible for the openSim pointer to be null. However | ||
201 | // were the implementation to be changed, this could | ||
202 | // result in a silent initialization failure. Harmless | ||
203 | // except for lack of function and lack of any | ||
204 | // diagnostic indication as to why. The same is true if | ||
205 | // the HTTP server reference is bad. | ||
206 | // We should at least issue a message... | ||
207 | m_log.WarnFormat("{0} Initialization failed: {1}", MsgID, e.Message); | ||
208 | m_log.DebugFormat("{0} Initialization failed: {1}", MsgID, e.ToString()); | ||
209 | } | ||
210 | } | ||
211 | |||
212 | |||
213 | private List<RestStreamHandler> _handlers = new List<RestStreamHandler>(); | ||
214 | |||
215 | public void AddRestStreamHandler(string httpMethod, string path, RestMethod method) | ||
216 | { | ||
217 | if (!path.StartsWith(_prefix)) | ||
218 | { | ||
219 | path = String.Format("{0}{1}", _prefix, path); | ||
220 | } | ||
221 | |||
222 | RestStreamHandler h = new RestStreamHandler(httpMethod, path, method); | ||
223 | _httpd.AddStreamHandler(h); | ||
224 | _handlers.Add(h); | ||
225 | |||
226 | m_log.DebugFormat("{0} Added REST handler {1} {2}", MsgID, httpMethod, path); | ||
227 | } | ||
228 | |||
229 | |||
230 | public bool VerifyGod(string key) | ||
231 | { | ||
232 | if (String.IsNullOrEmpty(key)) return false; | ||
233 | return key == _godkey; | ||
234 | } | ||
235 | |||
236 | public virtual void Close() | ||
237 | { | ||
238 | foreach (RestStreamHandler h in _handlers) | ||
239 | { | ||
240 | _httpd.RemoveStreamHandler(h.HttpMethod, h.Path); | ||
241 | } | ||
242 | _handlers = null; | ||
243 | } | ||
244 | #endregion methods | ||
245 | } | ||
246 | } | ||