View Javadoc

1   /*
2    * The MIT License
3    *
4    * Original work sponsored and donated by National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
5    *
6    * Copyright (C) 2011 National Board of e-Health (NSI), Denmark (http://www.nsi.dk)
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   *
26   * $HeadURL$
27   * $Id$
28   */
29  package dk.sosi.seal.model;
30  
31  import dk.sosi.seal.model.constants.*;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Element;
34  
35  import java.util.Date;
36  
37  /**
38   * Model object representing a response on an <code>IdentityToken</code> request.<br />
39   * The <code>IdentityTokenResponse</code> read values directly from the underlying DOM.<br />
40   * <br />
41   * All operations related to constructing, wrappring, etc. of the <code>IdentityToken</code> should be done through the <code>IDWSHFactory</code>.
42   * 
43   * @author ads
44   * @version $Revision:$
45   * @since 2.1
46   */
47  public class IdentityTokenResponse extends AbstractDOMInfoExtractor {
48  
49      private transient Boolean fault = null;
50  
51      /**
52       * Constructor for the <code>IdentityTokenResponse</code> object.
53       * 
54       * @param dom
55       *            The DOM representation of the <code>IdentityTokenResponse</code>.
56       */
57      public IdentityTokenResponse(Document dom) {
58          this.dom = dom.getDocumentElement();
59      }
60  
61      /**
62       * Retrieve the action part of the SOAP header.
63       * 
64       * <pre>
65       *   &lt;soap:Header&gt;
66       *     &lt;wsa:Action&gt;http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue&lt;/wsa:Action&gt;
67       *     ...
68       *   &lt;/soap:Header&gt;
69       * </pre>
70       * 
71       * @return The action value
72       */
73      public String getAction() {
74          Element ac = getTag(SOAPTags.envelope, SOAPTags.header, WSATags.action);
75          return ac.getTextContent();
76      }
77  
78      /**
79       * Retrieve the context attribute of the <i>wst:RequestSecurityTokenResponse</i> tag.
80       * 
81       * <pre>
82       *  &lt;soap:Body&gt;
83       *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
84       *          &lt;wst:RequestSecurityTokenResponse Context=&quot;urn:uuid:00000&quot;&gt;
85       *              ...
86       *          &lt;/wst:RequestSecurityTokenResponse&gt;
87       *      &lt;/wst:RequestSecurityTokenResponseCollection&gt;
88       *  &lt;/soap:Body&gt;
89       * </pre>
90       * 
91       * @return The context value.
92       */
93      public String getContext() {
94          if(isFault()) {
95              return null;
96          }
97          Element ac = getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse);
98          return ac.getAttribute("Context");
99      }
100 
101     /**
102      * Retrieve when the contained <code>IdentityToken</code> was created.
103      * 
104      * <pre>
105      *  &lt;soap:Body&gt;
106      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
107      *          &lt;wst:RequestSecurityTokenResponse ...&gt;
108      *              ...
109      *              &lt;wst:Lifetime&gt;
110      *                  &lt;wsu:Created&gt;2011-07-23T15:32:12Z&lt;/wsu:Created&gt;
111      *                  ...
112      *              &lt;/wst:Lifetime&gt;
113      *          &lt;/wst:RequestSecurityTokenResponse ...&gt;
114      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
115      *  &lt;soap:Body&gt;
116      * </pre>
117      * 
118      * @return When the token was created.
119      */
120     public Date getCreated() {
121         if(isFault()) {
122             return null;
123         }
124 
125         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse, WSTTags.lifetime, WSUTags.created);
126         return convertToDate(ac, null);
127     }
128 
129     /**
130      * Retrieve the enpoint address.
131      * 
132      * <pre>
133      *  &lt;soap:Body&gt;
134      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
135      *          &lt;wst:RequestSecurityTokenResponse ...&gt;
136      *              ...
137      *              &lt;wsp:AppliesTo&gt;
138      *                  &lt;wsa:EndpointReference&gt;
139      *                      &lt;wsa:Address&gt;http://fmk-online.dk&lt;/wsa:Address&gt;
140      *                  &lt;/wsa:EndpointReference&gt;
141      *              &lt;/wsp:AppliesTo&gt;
142      *              ...
143      *          &lt;/wst:RequestSecurityTokenResponse ...&gt;
144      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
145      *  &lt;soap:Body&gt;
146      * </pre>
147      * 
148      * @return The endpoint address value.
149      */
150     public String getEndPointAddress() {
151         if(isFault()) {
152             return null;
153         }
154 
155         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse, WSPTags.appliesTo, WSATags.endpointReference, WSATags.address);
156         return ac.getTextContent();
157     }
158 
159     /**
160      * Retrieve when the contained <code>IdentityToken</code> expires.
161      * 
162      * <pre>
163      *  &lt;soap:Body&gt;
164      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
165      *          &lt;wst:RequestSecurityTokenResponse ...&gt;
166      *              ...
167      *              &lt;wst:Lifetime&gt;
168      *                  ...
169      *                  &lt;wsu:Expires&gt;2011-07-23T15:37:12Z&lt;/wsu:Expires&gt;
170      *              &lt;/wst:Lifetime&gt;
171      *          &lt;/wst:RequestSecurityTokenResponse ...&gt;
172      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
173      *  &lt;soap:Body&gt;
174      * </pre>
175      * 
176      * @return When the token expires.
177      */
178     public Date getExpires() {
179         if(isFault()) {
180             return null;
181         }
182 
183         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse, WSTTags.lifetime, WSUTags.expires);
184         return convertToDate(ac, null);
185     }
186 
187     /**
188      * Retrieve the code uniquely identifying the fault type.<br />
189      * 
190      * <pre>
191      *   &lt;soap:Body&gt;
192      *     &lt;soap:Fault&gt;
193      *       &lt;faultcode&gt;wst:FailedAuthentication&lt;/faultcode&gt;
194      *       ...
195      *     &lt;/soap:Fault&gt;
196      *   &lt;/soap:Body&gt;
197      * </pre>
198      * 
199      * @return The fault code - or <code>null</code> if the message is not fault message.
200      */
201     public String getFaultCode() {
202         if(!isFault()) {
203             return null;
204         }
205 
206         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, SOAPTags.fault, CommonTags.faultcode);
207         return ac.getTextContent();
208     }
209 
210     /**
211      * Retrieve the fault string - the detailed error message for the fault.<br />
212      * 
213      * <pre>
214      *   &lt;soap:Body&gt;
215      *     &lt;soap:Fault&gt;
216      *       &lt;faultstring&gt;Authentication failed: Token in request signed by untrusted party&lt;/faultstring&gt;
217      *       ...
218      *     &lt;/soap:Fault&gt;
219      *   &lt;/soap:Body&gt;
220      * </pre>
221      * 
222      * @return The fault string - or <code>null</code> if the message is not fault message.
223      */
224     public String getFaultString() {
225         if(!isFault()) {
226             return null;
227         }
228 
229         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, SOAPTags.fault, CommonTags.faultstring);
230         return ac.getTextContent();
231     }
232 
233     /**
234      * Get the identity of the system reporting the fault.<br />
235      * 
236      * <pre>
237      *   &lt;soap:Body&gt;
238      *     &lt;soap:Fault&gt;
239      *       ...
240      *       &lt;faultactor&gt;http://sosi.dk/sts&lt;/faultactor&gt;
241      *     &lt;/soap:Fault&gt;
242      *   &lt;/soap:Body&gt;
243      * </pre>
244      * 
245      * @return The fault factor - or <code>null</code> if the message is not fault message.
246      */
247     public String getFaultActor() {
248         if(!isFault()) {
249             return null;
250         }
251 
252         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, SOAPTags.fault, CommonTags.faultactor);
253         return ac.getTextContent();
254     }
255 
256     /**
257      * Retrieve the contained <code>IdentityToken</code>.
258      * 
259      * <pre>
260      *  &lt;soap:Body&gt;
261      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
262      *          &lt;wst:RequestSecurityTokenResponse ...&gt;
263      *              ...
264      *              &lt;wst:RequestedSecurityToken&gt;
265      *                  &lt;saml:Assertion IssueInstant=&quot;2011-07-23T15:32:12Z&quot; ...&gt;
266      *                      ...
267      *                  &lt;/saml:Assertion&gt;
268      *              &lt;/wst:RequestedSecurityToken&gt;
269      *          &lt;/wst:RequestSecurityTokenResponse ...&gt;
270      *      &lt;/wst:RequestSecurityTokenResponseCollection&gt;
271      *  &lt;/soap:Body&gt;
272      * </pre>
273      * 
274      * @return The contained <code>IdentityToken</code> instance.
275      */
276     public IdentityToken getIdentityToken() {
277         if(isFault()) {
278             return null;
279         }
280 
281         return new IdentityToken(getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse, WSTTags.requestedSecurityToken, SAMLTags.assertion));
282     }
283 
284     /**
285      * Retrieve the MessageID part of the SOAP header.
286      * 
287      * <pre>
288      *   &lt;soap:Header&gt;
289      *     ...
290      *     &lt;wsa:MessageID&gt;urn:uuid:99999777-0000-0000&lt;/wsa:MessageID&gt;
291      *     ...
292      *   &lt;/soap:Header&gt;
293      * </pre>
294      * 
295      * @return The message id.
296      */
297     public String getMessageID() {
298         Element ac = getTag(SOAPTags.envelope, SOAPTags.header, WSATags.messageID);
299         return ac.getTextContent();
300     }
301 
302     /**
303      * Retrieve the &quot;Relates to&quot; part of the SOAP header.
304      * 
305      * <pre>
306      *   &lt;soap:Header&gt;
307      *     ...
308      *     &lt;wsa:RelatesTo&gt;urn:uuid:99999999-0000-0000&lt;/wsa:RelatesTo&gt;
309      *   &lt;/soap:Header&gt;
310      * </pre>
311      * 
312      * @return The <code>RelatesTo</code> value.
313      */
314     public String getRelatesTo() {
315         Element ac = getTag(SOAPTags.envelope, SOAPTags.header, WSATags.relatesTo);
316         return ac.getTextContent();
317     }
318 
319     /**
320      * Retrieve the type of token retrieved.
321      * 
322      * <pre>
323      *  &lt;soap:Body&gt;
324      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
325      *          &lt;wst:RequestSecurityTokenResponse ...&gt;
326      *              &lt;wst:TokenType&gt;http://docs.oasis-open.org/wss/oasis-wss-saml-token- profile-1.1#SAMLV2.0&lt;/wst:TokenType&gt;
327      *              ...
328      *          &lt;/wst:RequestSecurityTokenResponse ...&gt;
329      *      &lt;wst:RequestSecurityTokenResponseCollection&gt;
330      *  &lt;soap:Body&gt;
331      * </pre>
332      * 
333      * @return The token type.
334      */
335     public String getTokenType() {
336         if(isFault()) {
337             return null;
338         }
339 
340         Element ac = getTag(SOAPTags.envelope, SOAPTags.body, WSTTags.requestSecurityTokenResponseCollection, WSTTags.requestSecurityTokenResponse, WSTTags.tokenType);
341         return ac.getTextContent();
342     }
343 
344     /**
345      * Retrieve whether the response from the server is a fault message.
346      * 
347      * @return <code>true</code> if the message is an error message - otherwise <code>false</code>.
348      */
349     public boolean isFault() {
350         if(fault == null) {
351             fault = (getTag(SOAPTags.envelope, SOAPTags.body, SOAPTags.fault) != null);
352         }
353         return fault;
354     }
355 }