CapsHandlers.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. using System.Collections;
  28. using System.Collections.Generic;
  29. using OpenSim.Framework.Servers;
  30. namespace OpenSim.Framework.Communications.Capabilities
  31. {
  32. /// <summary>
  33. /// CapsHandlers is a cap handler container but also takes
  34. /// care of adding and removing cap handlers to and from the
  35. /// supplied BaseHttpServer.
  36. /// </summary>
  37. public class CapsHandlers
  38. {
  39. private Dictionary <string, IRequestHandler> m_capsHandlers = new Dictionary<string, IRequestHandler>();
  40. private BaseHttpServer m_httpListener;
  41. private string m_httpListenerHostName;
  42. private uint m_httpListenerPort;
  43. /// <summary></summary>
  44. /// CapsHandlers is a cap handler container but also takes
  45. /// care of adding and removing cap handlers to and from the
  46. /// supplied BaseHttpServer.
  47. /// </summary>
  48. /// <param name="httpListener">base HTTP server</param>
  49. /// <param name="httpListenerHostname">host name of the HTTP
  50. /// server</param>
  51. /// <param name="httpListenerPort">HTTP port</param>
  52. public CapsHandlers(BaseHttpServer httpListener, string httpListenerHostname, uint httpListenerPort)
  53. {
  54. m_httpListener = httpListener;
  55. m_httpListenerHostName = httpListenerHostname;
  56. m_httpListenerPort = httpListenerPort;
  57. }
  58. /// <summary>
  59. /// Remove the cap handler for a capability.
  60. /// </summary>
  61. /// <param name="capsName">name of the capability of the cap
  62. /// handler to be removed</param>
  63. public void Remove(string capsName)
  64. {
  65. // This line must be here, or caps will break!
  66. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[capsName].Path);
  67. m_capsHandlers.Remove(capsName);
  68. }
  69. public bool ContainsCap(string cap)
  70. {
  71. return m_capsHandlers.ContainsKey(cap);
  72. }
  73. /// <summary>
  74. /// The indexer allows us to treat the CapsHandlers object
  75. /// in an intuitive dictionary like way.
  76. /// </summary>
  77. /// <Remarks>
  78. /// The indexer will throw an exception when you try to
  79. /// retrieve a cap handler for a cap that is not contained in
  80. /// CapsHandlers.
  81. /// </Remarks>
  82. public IRequestHandler this[string idx]
  83. {
  84. get
  85. {
  86. return m_capsHandlers[idx];
  87. }
  88. set
  89. {
  90. if (m_capsHandlers.ContainsKey(idx))
  91. {
  92. m_httpListener.RemoveStreamHandler("POST", m_capsHandlers[idx].Path);
  93. m_capsHandlers.Remove(idx);
  94. }
  95. if (null == value) return;
  96. m_capsHandlers[idx] = value;
  97. m_httpListener.AddStreamHandler(value);
  98. }
  99. }
  100. /// <summary>
  101. /// Return the list of cap names for which this CapsHandlers
  102. /// object contains cap handlers.
  103. /// </summary>
  104. public string[] Caps
  105. {
  106. get
  107. {
  108. string[] __keys = new string[m_capsHandlers.Keys.Count];
  109. m_capsHandlers.Keys.CopyTo(__keys, 0);
  110. return __keys;
  111. }
  112. }
  113. /// <summary>
  114. /// Return an LLSD-serializable Hashtable describing the
  115. /// capabilities and their handler details.
  116. /// </summary>
  117. public Hashtable CapsDetails
  118. {
  119. get
  120. {
  121. Hashtable caps = new Hashtable();
  122. string baseUrl = "http://" + m_httpListenerHostName + ":" + m_httpListenerPort.ToString();
  123. foreach (string capsName in m_capsHandlers.Keys)
  124. {
  125. // skip SEED cap
  126. if ("SEED" == capsName) continue;
  127. caps[capsName] = baseUrl + m_capsHandlers[capsName].Path;
  128. }
  129. return caps;
  130. }
  131. }
  132. }
  133. }