* System.Web.Services.dll.sources: Added
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / WebServiceHelper.cs
1 //
2 // System.Web.Services.Protocols.WebServiceHelper.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) Ximian, Inc. 2003
8 //
9
10 using System;
11 using System.Text;
12 using System.Xml;
13 using System.Xml.Schema;
14 using System.Xml.Serialization;
15
16 namespace System.Web.Services.Protocols
17 {
18         internal class WebServiceHelper
19         {
20                 public const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
21
22                 public static string GetServiceNamespace (Type t)
23                 {
24                         object[] ats = t.GetCustomAttributes (typeof(WebServiceAttribute), true);
25                         if (ats.Length == 0) return WebServiceAttribute.DefaultNamespace;
26                         else return ((WebServiceAttribute)ats[0]).Namespace;
27                 }
28
29
30                 public static Encoding GetContentEncoding (string cts)
31                 {
32                         string encoding;
33                         string content_type;
34
35                         encoding = "utf-8";
36                         int start = 0;
37                         int idx = cts.IndexOf (';');
38                         if (idx == -1)
39                                 encoding = cts;
40                         content_type = cts.Substring (0, idx);
41                         for (start = idx + 1; idx != -1;)
42                         {
43                                 idx = cts.IndexOf (";", start);
44                                 string body;
45                                 if (idx == -1)
46                                         body = cts.Substring (start);
47                                 else 
48                                 {
49                                         body = cts.Substring (start, idx);
50                                         start = idx + 1;
51                                 }
52                                 if (body.StartsWith ("charset="))
53                                 {
54                                         encoding = body.Substring (8);
55                                 }
56                         }
57
58                         if (content_type != "text/xml")
59                                 throw new Exception ("Content is not XML: " + content_type);
60
61                         return Encoding.GetEncoding (encoding);
62                 }
63
64                 public static void WriteSoapMessage (XmlTextWriter xtw, SoapTypeStubInfo info, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
65                 {
66                         xtw.WriteStartDocument ();
67                         xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
68                         xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
69                         xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
70
71                         // Serialize headers
72                         if (headers != null)
73                         {
74                                 foreach (SoapHeader header in headers) 
75                                 {
76                                         XmlSerializer ser = info.GetHeaderSerializer (header.GetType());
77                                         xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
78                                         ser.Serialize (xtw, header);
79                                         xtw.WriteEndElement ();
80                                 }
81                         }
82
83                         // Serialize body
84                         xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
85                         bodySerializer.Serialize (xtw, bodyContent);
86
87                         xtw.WriteEndElement ();
88                         xtw.WriteEndElement ();
89                         xtw.Flush ();
90                 }
91
92                 public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
93                 {
94                         xmlReader.MoveToContent ();\r
95                         xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);\r
96 \r
97                         headers = ReadHeaders (typeStubInfo, xmlReader);\r
98 \r
99                         xmlReader.MoveToContent ();\r
100                         xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);\r
101                         xmlReader.MoveToContent ();\r
102 \r
103                         body = bodySerializer.Deserialize (xmlReader);\r
104                 }
105
106                 static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, XmlTextReader xmlReader)
107                 {
108                         SoapHeaderCollection headers = new SoapHeaderCollection ();
109                         while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
110                         {
111                                 if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace)
112                                 {
113                                         xmlReader.ReadStartElement ();
114                                         xmlReader.MoveToContent ();
115                                         XmlQualifiedName qname = new XmlQualifiedName (xmlReader.LocalName, xmlReader.NamespaceURI);
116                                         XmlSerializer headerSerializer = typeStubInfo.GetHeaderSerializer (qname);
117                                         if (headerSerializer != null)
118                                         {
119                                                 SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
120                                                 headers.Add (header);
121                                         }
122                                         else
123                                         {
124                                                 while (xmlReader.NodeType == XmlNodeType.EndElement)
125                                                         xmlReader.Skip ();      // TODO: Check if the header has mustUnderstand=true
126                                                 xmlReader.Skip ();
127                                         }
128                                 }
129                                 else
130                                         xmlReader.Skip ();
131                         }
132                         return headers;
133                 }
134         }
135 }