2 // System.Web.Services.Configuration.WebServicesConfigurationSectionHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Collections
;
33 using System
.Configuration
;
36 namespace System
.Web
.Services
.Configuration
44 Documentation
= 1 << 3,
46 HttpPostLocalhost
= 1 << 5,
47 AnyHttpSoap
= HttpSoap
| HttpSoap12
,
53 volatile static WSConfig instance
;
57 ArrayList extensionTypes
= new ArrayList();
58 ArrayList extensionImporterTypes
= new ArrayList();
59 ArrayList extensionReflectorTypes
= new ArrayList();
60 ArrayList formatExtensionTypes
= new ArrayList();
61 static readonly object lockobj
= new object ();
63 public WSConfig (WSConfig parent
, object context
)
68 protocols
= parent
.protocols
;
69 wsdlHelpPage
= parent
.wsdlHelpPage
;
70 if (wsdlHelpPage
!= null)
71 filePath
= parent
.filePath
;
73 filePath
= context
as string;
76 static WSProtocol
ParseProtocol (string protoName
, out string error
)
82 proto
= (WSProtocol
) Enum
.Parse (typeof (WSProtocol
), protoName
);
84 error
= "Invalid protocol name";
91 // Methods to modify configuration values
92 public bool AddProtocol (string protoName
, out string error
)
94 if (protoName
== "All") {
95 error
= "Invalid protocol name";
99 WSProtocol proto
= ParseProtocol (protoName
, out error
);
107 public bool RemoveProtocol (string protoName
, out string error
)
109 if (protoName
== "All") {
110 error
= "Invalid protocol name";
114 WSProtocol proto
= ParseProtocol (protoName
, out error
);
122 public void ClearProtocol ()
127 // Methods to query/get configuration
128 public static bool IsSupported (WSProtocol proto
)
130 return ((Instance
.protocols
& proto
) == proto
&& (proto
!= 0) && (proto
!= WSProtocol
.All
));
134 public string WsdlHelpPage
{
135 get { return wsdlHelpPage; }
136 set { wsdlHelpPage = value; }
139 public string ConfigFilePath
{
140 get { return filePath; }
141 set { filePath = value; }
144 static public WSConfig Instance
{
146 //TODO: use HttpContext to get the configuration
147 if (instance
!= null)
151 if (instance
!= null)
154 instance
= (WSConfig
) ConfigurationSettings
.GetConfig ("system.web/webServices");
161 public ArrayList ExtensionTypes
{
162 get { return extensionTypes; }
165 public ArrayList ExtensionImporterTypes
{
166 get { return extensionImporterTypes; }
169 public ArrayList ExtensionReflectorTypes
{
170 get { return extensionReflectorTypes; }
173 public ArrayList FormatExtensionTypes
{
174 get { return formatExtensionTypes; }
179 enum WSExtensionGroup
185 class WSExtensionConfig
189 WSExtensionGroup
group;
191 public Exception
SetType (string typeName
)
193 Exception exc
= null;
196 type
= Type
.GetType (typeName
, true);
197 } catch (Exception e
) {
204 public Exception
SetPriority (string prio
)
206 if (prio
== null || prio
== "")
209 Exception exc
= null;
211 priority
= Int32
.Parse (prio
);
212 } catch (Exception e
) {
219 public Exception
SetGroup (string grp
)
221 if (grp
== null || grp
== "")
224 Exception exc
= null;
226 group = (WSExtensionGroup
) Int32
.Parse (grp
);
227 if (group < WSExtensionGroup
.High
|| group > WSExtensionGroup
.Low
)
228 throw new ArgumentOutOfRangeException ("group", "Must be 0 or 1");
229 } catch (Exception e
) {
241 public int Priority
{
242 get { return priority; }
245 public WSExtensionGroup Group
{
246 get { return group; }
250 class WebServicesConfigurationSectionHandler
: IConfigurationSectionHandler
252 public object Create (object parent
, object context
, XmlNode section
)
254 WSConfig config
= new WSConfig (parent
as WSConfig
, context
);
256 if (section
.Attributes
!= null && section
.Attributes
.Count
!= 0)
257 ThrowException ("Unrecognized attribute", section
);
259 XmlNodeList nodes
= section
.ChildNodes
;
260 foreach (XmlNode child
in nodes
) {
261 XmlNodeType ntype
= child
.NodeType
;
262 if (ntype
== XmlNodeType
.Whitespace
|| ntype
== XmlNodeType
.Comment
)
265 if (ntype
!= XmlNodeType
.Element
)
266 ThrowException ("Only elements allowed", child
);
268 string name
= child
.Name
;
269 if (name
== "protocols") {
270 ConfigProtocols (child
, config
);
274 if (name
== "soapExtensionTypes") {
275 ConfigSoapExtensionTypes (child
, config
.ExtensionTypes
);
279 if (name
== "soapExtensionReflectorTypes") {
280 ConfigSoapExtensionTypes (child
, config
.ExtensionReflectorTypes
);
284 if (name
== "soapExtensionImporterTypes") {
285 ConfigSoapExtensionTypes (child
, config
.ExtensionImporterTypes
);
289 if (name
== "serviceDescriptionFormatExtensionTypes") {
290 ConfigFormatExtensionTypes (child
, config
);
294 if (name
== "wsdlHelpGenerator") {
295 string href
= AttValue ("href", child
, false);
296 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
297 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
299 config
.ConfigFilePath
= context
as string;
300 config
.WsdlHelpPage
= href
;
304 ThrowException ("Unexpected element", child
);
310 static void ConfigProtocols (XmlNode section
, WSConfig config
)
312 if (section
.Attributes
!= null && section
.Attributes
.Count
!= 0)
313 ThrowException ("Unrecognized attribute", section
);
315 XmlNodeList nodes
= section
.ChildNodes
;
316 foreach (XmlNode child
in nodes
) {
317 XmlNodeType ntype
= child
.NodeType
;
318 if (ntype
== XmlNodeType
.Whitespace
|| ntype
== XmlNodeType
.Comment
)
321 if (ntype
!= XmlNodeType
.Element
)
322 ThrowException ("Only elements allowed", child
);
324 string name
= child
.Name
;
327 string protoName
= AttValue ("name", child
, false);
328 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
329 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
331 if (!config
.AddProtocol (protoName
, out error
))
332 ThrowException (error
, child
);
337 if (name
== "remove") {
338 string protoName
= AttValue ("name", child
, false);
339 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
340 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
342 if (!config
.RemoveProtocol (protoName
, out error
))
343 ThrowException (error
, child
);
348 if (name
== "clear") {
349 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
350 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
352 config
.ClearProtocol ();
356 ThrowException ("Unexpected element", child
);
360 static void ConfigSoapExtensionTypes (XmlNode section
, ArrayList extensions
)
362 if (section
.Attributes
!= null && section
.Attributes
.Count
!= 0)
363 ThrowException ("Unrecognized attribute", section
);
365 XmlNodeList nodes
= section
.ChildNodes
;
366 foreach (XmlNode child
in nodes
) {
367 XmlNodeType ntype
= child
.NodeType
;
368 if (ntype
== XmlNodeType
.Whitespace
|| ntype
== XmlNodeType
.Comment
)
371 if (ntype
!= XmlNodeType
.Element
)
372 ThrowException ("Only elements allowed", child
);
374 string name
= child
.Name
;
376 string seType
= AttValue ("type", child
, false);
377 string priority
= AttValue ("priority", child
);
378 string group = AttValue ("group", child
);
379 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
380 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
382 WSExtensionConfig wse
= new WSExtensionConfig ();
383 Exception e
= wse
.SetType (seType
);
385 ThrowException (e
.Message
, child
);
387 e
= wse
.SetPriority (priority
);
389 ThrowException (e
.Message
, child
);
391 e
= wse
.SetGroup (group);
393 ThrowException (e
.Message
, child
);
395 extensions
.Add (wse
);
399 ThrowException ("Unexpected element", child
);
403 static void ConfigFormatExtensionTypes (XmlNode section
, WSConfig config
)
405 if (section
.Attributes
!= null && section
.Attributes
.Count
!= 0)
406 ThrowException ("Unrecognized attribute", section
);
408 XmlNodeList nodes
= section
.ChildNodes
;
409 foreach (XmlNode child
in nodes
) {
410 XmlNodeType ntype
= child
.NodeType
;
411 if (ntype
== XmlNodeType
.Whitespace
|| ntype
== XmlNodeType
.Comment
)
414 if (ntype
!= XmlNodeType
.Element
)
415 ThrowException ("Only elements allowed", child
);
417 string name
= child
.Name
;
419 string typeName
= AttValue ("name", child
, false);
420 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
421 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
424 config
.FormatExtensionTypes
.Add (Type
.GetType (typeName
, true));
425 } catch (Exception e
) {
426 ThrowException (e
.Message
, child
);
431 ThrowException ("Unexpected element", child
);
435 // To save some typing...
436 static string AttValue (string name
, XmlNode node
, bool optional
)
438 return HandlersUtil
.ExtractAttributeValue (name
, node
, optional
);
441 static string AttValue (string name
, XmlNode node
)
443 return HandlersUtil
.ExtractAttributeValue (name
, node
, true);
446 static void ThrowException (string message
, XmlNode node
)
448 HandlersUtil
.ThrowException (message
, node
);
455 private HandlersUtil ()
459 static internal string ExtractAttributeValue (string attKey
, XmlNode node
)
461 return ExtractAttributeValue (attKey
, node
, false);
464 static internal string ExtractAttributeValue (string attKey
, XmlNode node
, bool optional
)
466 if (node
.Attributes
== null) {
470 ThrowException ("Required attribute not found: " + attKey
, node
);
473 XmlNode att
= node
.Attributes
.RemoveNamedItem (attKey
);
477 ThrowException ("Required attribute not found: " + attKey
, node
);
480 string value = att
.Value
;
481 if (value == String
.Empty
) {
482 string opt
= optional
? "Optional" : "Required";
483 ThrowException (opt
+ " attribute is empty: " + attKey
, node
);
489 static internal void ThrowException (string msg
, XmlNode node
)
491 if (node
!= null && node
.Name
!= String
.Empty
)
492 msg
= msg
+ " (node name: " + node
.Name
+ ") ";
493 throw new ConfigurationException (msg
, node
);