1 //-----------------------------------------------------------------------
2 // <copyright file="WSTrustFeb2005ResponseSerializer.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6 
7 namespace System.IdentityModel.Protocols.WSTrust
8 {
9     using System.Xml;
10 
11     /// <summary>
12     /// Class for serializing a WS-Trust Feb 2005 RequestSecurityTokenResponse to an XmlWriter
13     /// </summary>
14     public class WSTrustFeb2005ResponseSerializer : WSTrustResponseSerializer
15     {
16         /// <summary>
17         /// Deserializes the RSTR from the XmlReader to a RequestSecurityTokenResponse object.
18         /// </summary>
19         /// <param name="reader">XML reader over the RSTR</param>
20         /// <param name="context">Current Serialization context.</param>
21         /// <returns>RequestSecurityTokenResponse object if the deserialization was successful</returns>
22         /// <exception cref="ArgumentNullException">The given reader or context parameter is null</exception>
23         /// <exception cref="WSTrustSerializationException">There was an error parsing the RSTR</exception>
ReadXml(XmlReader reader, WSTrustSerializationContext context)24         public override RequestSecurityTokenResponse ReadXml(XmlReader reader, WSTrustSerializationContext context)
25         {
26             if (reader == null)
27             {
28                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
29             }
30 
31             if (context == null)
32             {
33                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
34             }
35 
36             return WSTrustSerializationHelper.CreateResponse(reader, context, this, WSTrustConstantsAdapter.TrustFeb2005);
37         }
38 
39         /// <summary>
40         /// Override of the base class that Reads a specific child element inside the RSTR.
41         /// </summary>
42         /// <param name="reader">Reader pointing at an element to read inside the RSTR.</param>
43         /// <param name="rstr">The RequestSecurityTokenResponse element that is being populated from the reader.</param>
44         /// <param name="context">Current Serialization context.</param>
45         /// <exception cref="ArgumentNullException">Either reader or rstr or context parameter is null.</exception>
46         /// <exception cref="WSTrustSerializationException">Unable to deserialize the current parameter.</exception>
ReadXmlElement(XmlReader reader, RequestSecurityTokenResponse rstr, WSTrustSerializationContext context)47         public override void ReadXmlElement(XmlReader reader, RequestSecurityTokenResponse rstr, WSTrustSerializationContext context)
48         {
49             if (reader == null)
50             {
51                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
52             }
53 
54             if (rstr == null)
55             {
56                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rstr");
57             }
58 
59             if (context == null)
60             {
61                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
62             }
63 
64             WSTrustSerializationHelper.ReadRSTRXml(reader, rstr, context, WSTrustConstantsAdapter.TrustFeb2005);
65         }
66 
67         /// <summary>
68         /// Writes out the supported elements on the response object.
69         /// </summary>
70         /// <param name="rstr">The response instance</param>
71         /// <param name="writer">The writer to write to</param>
72         /// <param name="context">Current Serialization context.</param>
73         /// <exception cref="ArgumentNullException">Either rstr or writer or context parameter is null.</exception>
WriteKnownResponseElement(RequestSecurityTokenResponse rstr, XmlWriter writer, WSTrustSerializationContext context)74         public override void WriteKnownResponseElement(RequestSecurityTokenResponse rstr, XmlWriter writer, WSTrustSerializationContext context)
75         {
76             if (rstr == null)
77             {
78                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rstr");
79             }
80 
81             if (writer == null)
82             {
83                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
84             }
85 
86             if (context == null)
87             {
88                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
89             }
90 
91             WSTrustSerializationHelper.WriteKnownResponseElement(rstr, writer, context, this, WSTrustConstantsAdapter.TrustFeb2005);
92         }
93 
94         /// <summary>
95         /// Serializes the given RequestSecurityTokenResponse into the XmlWriter
96         /// </summary>
97         /// <param name="response">RequestSecurityTokenRespone object to be serialized</param>
98         /// <param name="writer">XML writer to serialize into</param>
99         /// <param name="context">Current Serialization context.</param>
100         /// <exception cref="ArgumentNullException">The response or writer or context parameter is null.</exception>
WriteXml(RequestSecurityTokenResponse response, XmlWriter writer, WSTrustSerializationContext context)101         public override void WriteXml(RequestSecurityTokenResponse response, XmlWriter writer, WSTrustSerializationContext context)
102         {
103             if (response == null)
104             {
105                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("response");
106             }
107 
108             if (writer == null)
109             {
110                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
111             }
112 
113             if (context == null)
114             {
115                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
116             }
117 
118             WSTrustSerializationHelper.WriteResponse(response, writer, context, this, WSTrustConstantsAdapter.TrustFeb2005);
119         }
120 
121         /// <summary>
122         /// Override of the Base class method that writes a specific RSTR parameter to the outgoing stream.
123         /// </summary>
124         /// <param name="writer">Writer to which the RSTR is serialized</param>
125         /// <param name="elementName">The Local name of the element to be written.</param>
126         /// <param name="elementValue">The value of the element.</param>
127         /// <param name="rstr">The entire RSTR object that is being serialized.</param>
128         /// <param name="context">Current Serialization context.</param>
129         /// <exception cref="ArgumentNullException">Either writer or rstr or context is null.</exception>
130         /// <exception cref="ArgumentException">elementName is null or an empty string.</exception>
WriteXmlElement(XmlWriter writer, string elementName, object elementValue, RequestSecurityTokenResponse rstr, WSTrustSerializationContext context)131         public override void WriteXmlElement(XmlWriter writer, string elementName, object elementValue, RequestSecurityTokenResponse rstr, WSTrustSerializationContext context)
132         {
133             if (writer == null)
134             {
135                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer");
136             }
137 
138             if (string.IsNullOrEmpty(elementName))
139             {
140                 throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("elementName");
141             }
142 
143             if (rstr == null)
144             {
145                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rstr");
146             }
147 
148             if (context == null)
149             {
150                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
151             }
152 
153             WSTrustSerializationHelper.WriteRSTRXml(writer, elementName, elementValue, context, WSTrustConstantsAdapter.TrustFeb2005);
154         }
155 
156         /// <summary>
157         /// Checks if the given reader is positioned at a RequestSecurityTokenResponse element with namespace
158         /// 'http://schemas.xmlsoap.org/ws/2005/02/trust'
159         /// </summary>
160         /// <param name="reader">The reader to read from</param>
161         /// <returns>
162         /// 'True' if the reader is positioned at a RequestSecurityTokenResponse element with namespace
163         /// 'http://schemas.xmlsoap.org/ws/2005/02/trust'.
164         /// </returns>
165         /// <exception cref="ArgumentNullException">The input argument is null.</exception>
CanRead(XmlReader reader)166         public override bool CanRead(XmlReader reader)
167         {
168             if (reader == null)
169             {
170                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
171             }
172 
173             return reader.IsStartElement(WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse, WSTrustFeb2005Constants.NamespaceURI);
174         }
175     }
176 }
177