WebServicesConfigurationSectionHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // System.Web.Services.Configuration.WebServicesConfigurationSectionHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Configuration;
  32. using System.Xml;
  33. namespace System.Web.Services.Configuration
  34. {
  35. [Flags]
  36. enum WSProtocol
  37. {
  38. HttpSoap = 1,
  39. HttpPost = 1 << 1,
  40. HttpGet = 1 << 2,
  41. Documentation = 1 << 3,
  42. All = 0x0F
  43. }
  44. class WSConfig
  45. {
  46. static WSConfig instance;
  47. WSProtocol protocols;
  48. string wsdlHelpPage;
  49. string filePath;
  50. ArrayList extensionTypes = new ArrayList();
  51. ArrayList extensionImporterTypes = new ArrayList();
  52. ArrayList extensionReflectorTypes = new ArrayList();
  53. ArrayList formatExtensionTypes = new ArrayList();
  54. public WSConfig (WSConfig parent, object context)
  55. {
  56. if (parent == null)
  57. return;
  58. protocols = parent.protocols;
  59. wsdlHelpPage = parent.wsdlHelpPage;
  60. if (wsdlHelpPage != null)
  61. filePath = parent.filePath;
  62. else
  63. filePath = context as string;
  64. }
  65. static WSProtocol ParseProtocol (string protoName, out string error)
  66. {
  67. WSProtocol proto;
  68. error = null;
  69. try {
  70. proto = (WSProtocol) Enum.Parse (typeof (WSProtocol), protoName);
  71. } catch {
  72. error = "Invalid protocol name";
  73. return 0;
  74. }
  75. return proto;
  76. }
  77. // Methods to modify configuration values
  78. public bool AddProtocol (string protoName, out string error)
  79. {
  80. if (protoName == "All") {
  81. error = "Invalid protocol name";
  82. return false;
  83. }
  84. WSProtocol proto = ParseProtocol (protoName, out error);
  85. if (error != null)
  86. return false;
  87. protocols |= proto;
  88. return true;
  89. }
  90. public bool RemoveProtocol (string protoName, out string error)
  91. {
  92. if (protoName == "All") {
  93. error = "Invalid protocol name";
  94. return false;
  95. }
  96. WSProtocol proto = ParseProtocol (protoName, out error);
  97. if (error != null)
  98. return false;
  99. protocols &= ~proto;
  100. return true;
  101. }
  102. public void ClearProtocol ()
  103. {
  104. protocols = 0;
  105. }
  106. // Methods to query/get configuration
  107. public static bool IsSupported (WSProtocol proto)
  108. {
  109. return ((Instance.protocols & proto) == proto && (proto != 0) && (proto != WSProtocol.All));
  110. }
  111. // Properties
  112. public string WsdlHelpPage {
  113. get { return wsdlHelpPage; }
  114. set { wsdlHelpPage = value; }
  115. }
  116. public string ConfigFilePath {
  117. get { return filePath; }
  118. set { filePath = value; }
  119. }
  120. static public WSConfig Instance {
  121. get {
  122. //TODO: use HttpContext to get the configuration
  123. if (instance != null)
  124. return instance;
  125. lock (typeof (WSConfig)) {
  126. if (instance != null)
  127. return instance;
  128. instance = (WSConfig) ConfigurationSettings.GetConfig ("system.web/webServices");
  129. }
  130. return instance;
  131. }
  132. }
  133. public ArrayList ExtensionTypes {
  134. get { return extensionTypes; }
  135. }
  136. public ArrayList ExtensionImporterTypes {
  137. get { return extensionImporterTypes; }
  138. }
  139. public ArrayList ExtensionReflectorTypes {
  140. get { return extensionReflectorTypes; }
  141. }
  142. public ArrayList FormatExtensionTypes {
  143. get { return formatExtensionTypes; }
  144. }
  145. }
  146. enum WSExtensionGroup
  147. {
  148. High,
  149. Low
  150. }
  151. class WSExtensionConfig
  152. {
  153. Type type;
  154. int priority;
  155. WSExtensionGroup group;
  156. public Exception SetType (string typeName)
  157. {
  158. Exception exc = null;
  159. try {
  160. type = Type.GetType (typeName, true);
  161. } catch (Exception e) {
  162. exc = e;
  163. }
  164. return exc;
  165. }
  166. public Exception SetPriority (string prio)
  167. {
  168. if (prio == null || prio == "")
  169. return null;
  170. Exception exc = null;
  171. try {
  172. priority = Int32.Parse (prio);
  173. } catch (Exception e) {
  174. exc = e;
  175. }
  176. return exc;
  177. }
  178. public Exception SetGroup (string grp)
  179. {
  180. if (grp == null || grp == "")
  181. return null;
  182. Exception exc = null;
  183. try {
  184. group = (WSExtensionGroup) Int32.Parse (grp);
  185. if (group < WSExtensionGroup.High || group > WSExtensionGroup.Low)
  186. throw new ArgumentOutOfRangeException ("group", "Must be 0 or 1");
  187. } catch (Exception e) {
  188. exc = e;
  189. }
  190. return exc;
  191. }
  192. // Getters
  193. public Type Type {
  194. get { return type; }
  195. }
  196. public int Priority {
  197. get { return priority; }
  198. }
  199. public WSExtensionGroup Group {
  200. get { return group; }
  201. }
  202. }
  203. class WebServicesConfigurationSectionHandler : IConfigurationSectionHandler
  204. {
  205. public object Create (object parent, object context, XmlNode section)
  206. {
  207. WSConfig config = new WSConfig (parent as WSConfig, context);
  208. if (section.Attributes != null && section.Attributes.Count != 0)
  209. ThrowException ("Unrecognized attribute", section);
  210. XmlNodeList nodes = section.ChildNodes;
  211. foreach (XmlNode child in nodes) {
  212. XmlNodeType ntype = child.NodeType;
  213. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  214. continue;
  215. if (ntype != XmlNodeType.Element)
  216. ThrowException ("Only elements allowed", child);
  217. string name = child.Name;
  218. if (name == "protocols") {
  219. ConfigProtocols (child, config);
  220. continue;
  221. }
  222. if (name == "soapExtensionTypes") {
  223. ConfigSoapExtensionTypes (child, config.ExtensionTypes);
  224. continue;
  225. }
  226. if (name == "soapExtensionReflectorTypes") {
  227. ConfigSoapExtensionTypes (child, config.ExtensionReflectorTypes);
  228. continue;
  229. }
  230. if (name == "soapExtensionImporterTypes") {
  231. ConfigSoapExtensionTypes (child, config.ExtensionImporterTypes);
  232. continue;
  233. }
  234. if (name == "serviceDescriptionFormatExtensionTypes") {
  235. ConfigFormatExtensionTypes (child, config);
  236. continue;
  237. }
  238. if (name == "wsdlHelpGenerator") {
  239. string href = AttValue ("href", child, false);
  240. if (child.Attributes != null && child.Attributes.Count != 0)
  241. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  242. config.ConfigFilePath = context as string;
  243. config.WsdlHelpPage = href;
  244. continue;
  245. }
  246. ThrowException ("Unexpected element", child);
  247. }
  248. return config;
  249. }
  250. static void ConfigProtocols (XmlNode section, WSConfig config)
  251. {
  252. if (section.Attributes != null && section.Attributes.Count != 0)
  253. ThrowException ("Unrecognized attribute", section);
  254. XmlNodeList nodes = section.ChildNodes;
  255. foreach (XmlNode child in nodes) {
  256. XmlNodeType ntype = child.NodeType;
  257. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  258. continue;
  259. if (ntype != XmlNodeType.Element)
  260. ThrowException ("Only elements allowed", child);
  261. string name = child.Name;
  262. string error;
  263. if (name == "add") {
  264. string protoName = AttValue ("name", child, false);
  265. if (child.Attributes != null && child.Attributes.Count != 0)
  266. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  267. if (!config.AddProtocol (protoName, out error))
  268. ThrowException (error, child);
  269. continue;
  270. }
  271. if (name == "remove") {
  272. string protoName = AttValue ("name", child, false);
  273. if (child.Attributes != null && child.Attributes.Count != 0)
  274. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  275. if (!config.RemoveProtocol (protoName, out error))
  276. ThrowException (error, child);
  277. continue;
  278. }
  279. if (name == "clear") {
  280. if (child.Attributes != null && child.Attributes.Count != 0)
  281. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  282. config.ClearProtocol ();
  283. continue;
  284. }
  285. ThrowException ("Unexpected element", child);
  286. }
  287. }
  288. static void ConfigSoapExtensionTypes (XmlNode section, ArrayList extensions)
  289. {
  290. if (section.Attributes != null && section.Attributes.Count != 0)
  291. ThrowException ("Unrecognized attribute", section);
  292. XmlNodeList nodes = section.ChildNodes;
  293. foreach (XmlNode child in nodes) {
  294. XmlNodeType ntype = child.NodeType;
  295. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  296. continue;
  297. if (ntype != XmlNodeType.Element)
  298. ThrowException ("Only elements allowed", child);
  299. string name = child.Name;
  300. if (name == "add") {
  301. string seType = AttValue ("type", child, false);
  302. string priority = AttValue ("priority", child);
  303. string group = AttValue ("group", child);
  304. if (child.Attributes != null && child.Attributes.Count != 0)
  305. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  306. WSExtensionConfig wse = new WSExtensionConfig ();
  307. Exception e = wse.SetType (seType);
  308. if (e != null)
  309. ThrowException (e.Message, child);
  310. e = wse.SetPriority (priority);
  311. if (e != null)
  312. ThrowException (e.Message, child);
  313. e = wse.SetGroup (group);
  314. if (e != null)
  315. ThrowException (e.Message, child);
  316. extensions.Add (wse);
  317. continue;
  318. }
  319. ThrowException ("Unexpected element", child);
  320. }
  321. }
  322. static void ConfigFormatExtensionTypes (XmlNode section, WSConfig config)
  323. {
  324. if (section.Attributes != null && section.Attributes.Count != 0)
  325. ThrowException ("Unrecognized attribute", section);
  326. XmlNodeList nodes = section.ChildNodes;
  327. foreach (XmlNode child in nodes) {
  328. XmlNodeType ntype = child.NodeType;
  329. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  330. continue;
  331. if (ntype != XmlNodeType.Element)
  332. ThrowException ("Only elements allowed", child);
  333. string name = child.Name;
  334. if (name == "add") {
  335. string typeName = AttValue ("name", child, false);
  336. if (child.Attributes != null && child.Attributes.Count != 0)
  337. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  338. try {
  339. config.FormatExtensionTypes.Add (Type.GetType (typeName, true));
  340. } catch (Exception e) {
  341. ThrowException (e.Message, child);
  342. }
  343. continue;
  344. }
  345. ThrowException ("Unexpected element", child);
  346. }
  347. }
  348. // To save some typing...
  349. static string AttValue (string name, XmlNode node, bool optional)
  350. {
  351. return HandlersUtil.ExtractAttributeValue (name, node, optional);
  352. }
  353. static string AttValue (string name, XmlNode node)
  354. {
  355. return HandlersUtil.ExtractAttributeValue (name, node, true);
  356. }
  357. static void ThrowException (string message, XmlNode node)
  358. {
  359. HandlersUtil.ThrowException (message, node);
  360. }
  361. //
  362. }
  363. class HandlersUtil
  364. {
  365. private HandlersUtil ()
  366. {
  367. }
  368. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  369. {
  370. return ExtractAttributeValue (attKey, node, false);
  371. }
  372. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  373. {
  374. if (node.Attributes == null) {
  375. if (optional)
  376. return null;
  377. ThrowException ("Required attribute not found: " + attKey, node);
  378. }
  379. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  380. if (att == null) {
  381. if (optional)
  382. return null;
  383. ThrowException ("Required attribute not found: " + attKey, node);
  384. }
  385. string value = att.Value;
  386. if (value == String.Empty) {
  387. string opt = optional ? "Optional" : "Required";
  388. ThrowException (opt + " attribute is empty: " + attKey, node);
  389. }
  390. return value;
  391. }
  392. static internal void ThrowException (string msg, XmlNode node)
  393. {
  394. if (node != null && node.Name != String.Empty)
  395. msg = msg + " (node name: " + node.Name + ") ";
  396. throw new ConfigurationException (msg, node);
  397. }
  398. }
  399. }