OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CapsHandlers.cs
Go to the documentation of this file.
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 
28 using System.Collections;
29 using System.Collections.Generic;
30 using OpenSim.Framework.Servers;
31 using OpenSim.Framework.Servers.HttpServer;
32 
33 namespace OpenSim.Framework.Capabilities
34 {
40  public class CapsHandlers
41  {
42  private Dictionary<string, IRequestHandler> m_capsHandlers = new Dictionary<string, IRequestHandler>();
43  private IHttpServer m_httpListener;
44  private string m_httpListenerHostName;
45  private uint m_httpListenerPort;
46  private bool m_useSSL = false;
47 
56  public CapsHandlers(BaseHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
57  : this(httpListener,httpListenerHostname,httpListenerPort, false)
58  {
59  }
60 
70  public CapsHandlers(IHttpServer httpListener, string httpListenerHostname, uint httpListenerPort, bool https)
71  {
72  m_httpListener = httpListener;
73  m_httpListenerHostName = httpListenerHostname;
74  m_httpListenerPort = httpListenerPort;
75  m_useSSL = https;
76  if (httpListener != null && m_useSSL)
77  {
78  m_httpListenerHostName = httpListener.SSLCommonName;
79  m_httpListenerPort = httpListener.SSLPort;
80  }
81  }
82 
88  public void Remove(string capsName)
89  {
90  lock (m_capsHandlers)
91  {
92  m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
93  m_httpListener.RemoveStreamHandler("PUT", m_capsHandlers[capsName].Path);
94  m_httpListener.RemoveStreamHandler("GET", m_capsHandlers[capsName].Path);
95  m_capsHandlers.Remove(capsName);
96  }
97  }
98 
99  public bool ContainsCap(string cap)
100  {
101  lock (m_capsHandlers)
102  return m_capsHandlers.ContainsKey(cap);
103  }
104 
114  public IRequestHandler this[string idx]
115  {
116  get
117  {
118  lock (m_capsHandlers)
119  return m_capsHandlers[idx];
120  }
121 
122  set
123  {
124  lock (m_capsHandlers)
125  {
126  if (m_capsHandlers.ContainsKey(idx))
127  {
128  m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
129  m_capsHandlers.Remove(idx);
130  }
131 
132  if (null == value) return;
133 
134  m_capsHandlers[idx] = value;
135  m_httpListener.AddStreamHandler(value);
136  }
137  }
138  }
139 
144  public string[] Caps
145  {
146  get
147  {
148  lock (m_capsHandlers)
149  {
150  string[] __keys = new string[m_capsHandlers.Keys.Count];
151  m_capsHandlers.Keys.CopyTo(__keys, 0);
152  return __keys;
153  }
154  }
155  }
156 
162  public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
163  {
164  Hashtable caps = new Hashtable();
165  string protocol = "http://";
166 
167  if (m_useSSL)
168  protocol = "https://";
169 
170  string baseUrl = protocol + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
171 
172  lock (m_capsHandlers)
173  {
174  foreach (string capsName in m_capsHandlers.Keys)
175  {
176  if (excludeSeed && "SEED" == capsName)
177  continue;
178 
179  if (requestedCaps != null && !requestedCaps.Contains(capsName))
180  continue;
181 
182  caps[capsName] = baseUrl + m_capsHandlers[capsName].Path;
183  }
184  }
185 
186  return caps;
187  }
188 
195  public Dictionary<string, IRequestHandler> GetCapsHandlers()
196  {
197  lock (m_capsHandlers)
198  return new Dictionary<string, IRequestHandler>(m_capsHandlers);
199  }
200  }
201 }
Dictionary< string, IRequestHandler > GetCapsHandlers()
Returns a copy of the dictionary of all the HTTP cap handlers
CapsHandlers(IHttpServer httpListener, string httpListenerHostname, uint httpListenerPort, bool https)
Definition: CapsHandlers.cs:70
void Remove(string capsName)
Remove the cap handler for a capability.
Definition: CapsHandlers.cs:88
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
CapsHandlers is a cap handler container but also takes care of adding and removing cap handlers to an...
Definition: CapsHandlers.cs:40
Hashtable GetCapsDetails(bool excludeSeed, List< string > requestedCaps)
Return an LLSD-serializable Hashtable describing the capabilities and their handler details...
CapsHandlers(BaseHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
Definition: CapsHandlers.cs:56