123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Xml;
- using log4net;
- using Nini.Config;
- using OpenMetaverse;
- using OpenSim.Framework;
- using OpenSim.Framework.Servers;
- using OpenSim.Framework.Servers.HttpServer;
- namespace OpenSim.ApplicationPlugins.Rest
- {
- public abstract class RestPlugin : IApplicationPlugin
- {
- #region properties
- protected static readonly ILog m_log =
- LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private IConfig _config;
- private IConfig _pluginConfig;
- private OpenSimBase _app;
- private BaseHttpServer _httpd;
- private string _prefix;
-
-
-
-
- private string _godkey;
- private int _reqk;
- [ThreadStatic]
- private static string _threadRequestID = String.Empty;
-
-
-
- protected string RequestID
- {
- get { return _reqk++.ToString(); }
- set { _reqk = Convert.ToInt32(value); }
- }
-
-
-
- protected string MsgID
- {
- get { return String.Format("[REST-{0}] #{1}", Name, _threadRequestID); }
- set { _threadRequestID = value; }
- }
-
-
-
- public bool PluginsAreEnabled
- {
- get { return null != _config; }
- }
-
-
-
- public bool IsEnabled
- {
- get
- {
- return (null != _pluginConfig) && _pluginConfig.GetBoolean("enabled", false);
- }
- }
-
-
-
- public OpenSimBase App
- {
- get { return _app; }
- }
-
-
-
- public BaseHttpServer HttpServer
- {
- get { return _httpd; }
- }
-
-
-
- public string Prefix
- {
- get { return _prefix; }
- }
-
-
-
- protected string GodKey
- {
- get { return _godkey; }
- }
-
-
-
- public IConfig Config
- {
- get { return _pluginConfig; }
- }
-
-
-
- public abstract string Name { get; }
-
-
-
- public abstract string ConfigName { get; }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion properties
- #region methods
-
- private string m_version = "0.0";
- public string Version
- {
- get { return m_version; }
- }
- public void Initialise()
- {
- m_log.Info("[RESTPLUGIN]: " + Name + " cannot be default-initialized!");
- throw new PluginNotInitialisedException(Name);
- }
-
-
-
-
-
-
-
-
- public virtual void Initialise(OpenSimBase openSim)
- {
- RequestID = "0";
- MsgID = RequestID;
- try
- {
- if ((_config = openSim.ConfigSource.Source.Configs["RestPlugins"]) == null)
- {
- m_log.WarnFormat("{0} Rest Plugins not configured", MsgID);
- return;
- }
- if (!_config.GetBoolean("enabled", false))
- {
-
- return;
- }
- _app = openSim;
- _httpd = openSim.HttpServer;
-
- _godkey = _config.GetString("god_key", String.Empty);
-
- _prefix = _config.GetString("prefix", "/admin");
-
- _pluginConfig = openSim.ConfigSource.Source.Configs[ConfigName];
- m_log.InfoFormat("{0} Rest Plugins Enabled", MsgID);
- }
- catch (Exception e)
- {
-
-
-
-
-
-
-
-
-
-
-
-
- m_log.WarnFormat("{0} Initialization failed: {1}", MsgID, e.Message);
- m_log.DebugFormat("{0} Initialization failed: {1}", MsgID, e.ToString());
- }
- }
- public virtual void PostInitialise()
- {
- }
- private List<RestStreamHandler> _handlers = new List<RestStreamHandler>();
- private Dictionary<string, IHttpAgentHandler> _agents = new Dictionary<string, IHttpAgentHandler>();
-
-
-
-
-
-
-
- public virtual void AddRestStreamHandler(string httpMethod, string path, RestMethod method)
- {
- if (!IsEnabled) return;
- if (!path.StartsWith(_prefix))
- {
- path = String.Format("{0}{1}", _prefix, path);
- }
- RestStreamHandler h = new RestStreamHandler(httpMethod, path, method);
- _httpd.AddStreamHandler(h);
- _handlers.Add(h);
- m_log.DebugFormat("{0} Added REST handler {1} {2}", MsgID, httpMethod, path);
- }
-
-
-
-
-
-
-
-
-
-
- public bool AddAgentHandler(string agentName, IHttpAgentHandler handler)
- {
- if (!IsEnabled) return false;
- _agents.Add(agentName, handler);
- return _httpd.AddAgentHandler(agentName, handler);
- }
-
-
-
-
-
-
-
-
-
-
- public bool RemoveAgentHandler(string agentName, IHttpAgentHandler handler)
- {
- if (!IsEnabled) return false;
- if (_agents[agentName] == handler)
- {
- _agents.Remove(agentName);
- return _httpd.RemoveAgentHandler(agentName, handler);
- }
- return false;
- }
-
-
-
-
-
-
-
- protected bool IsGod(OSHttpRequest request)
- {
- string[] keys = request.Headers.GetValues("X-OpenSim-Godkey");
- if (null == keys) return false;
-
- return keys[keys.Length - 1] == _godkey;
- }
-
-
-
-
-
- protected bool IsVerifiedUser(OSHttpRequest request, UUID uuid)
- {
-
- return false;
- }
-
-
-
- public virtual void Close()
- {
- foreach (RestStreamHandler h in _handlers)
- {
- _httpd.RemoveStreamHandler(h.HttpMethod, h.Path);
- }
- _handlers = null;
- foreach (KeyValuePair<string, IHttpAgentHandler> h in _agents)
- {
- _httpd.RemoveAgentHandler(h.Key, h.Value);
- }
- _agents = null;
- }
- public virtual void Dispose()
- {
- Close();
- }
-
-
-
-
-
-
-
- protected string Failure(OSHttpResponse response, OSHttpStatusCode status,
- string method, string format, params string[] msg)
- {
- string m = String.Format(format, msg);
- response.StatusCode = (int) status;
- response.StatusDescription = m;
- m_log.ErrorFormat("{0} {1} failed: {2}", MsgID, method, m);
- return String.Format("<error>{0}</error>", m);
- }
-
-
-
-
-
-
-
- public string Failure(OSHttpResponse response, OSHttpStatusCode status,
- string method, Exception e)
- {
- string m = String.Format("exception occurred: {0}", e.Message);
- response.StatusCode = (int) status;
- response.StatusDescription = m;
- m_log.DebugFormat("{0} {1} failed: {2}", MsgID, method, e.ToString());
- m_log.ErrorFormat("{0} {1} failed: {2}", MsgID, method, e.Message);
- return String.Format("<error>{0}</error>", e.Message);
- }
- #endregion methods
- }
- }
|