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 * <soap:Header>
66 * <wsa:Action>http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue</wsa:Action>
67 * ...
68 * </soap:Header>
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 * <soap:Body>
83 * <wst:RequestSecurityTokenResponseCollection>
84 * <wst:RequestSecurityTokenResponse Context="urn:uuid:00000">
85 * ...
86 * </wst:RequestSecurityTokenResponse>
87 * </wst:RequestSecurityTokenResponseCollection>
88 * </soap:Body>
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 * <soap:Body>
106 * <wst:RequestSecurityTokenResponseCollection>
107 * <wst:RequestSecurityTokenResponse ...>
108 * ...
109 * <wst:Lifetime>
110 * <wsu:Created>2011-07-23T15:32:12Z</wsu:Created>
111 * ...
112 * </wst:Lifetime>
113 * </wst:RequestSecurityTokenResponse ...>
114 * <wst:RequestSecurityTokenResponseCollection>
115 * <soap:Body>
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 * <soap:Body>
134 * <wst:RequestSecurityTokenResponseCollection>
135 * <wst:RequestSecurityTokenResponse ...>
136 * ...
137 * <wsp:AppliesTo>
138 * <wsa:EndpointReference>
139 * <wsa:Address>http://fmk-online.dk</wsa:Address>
140 * </wsa:EndpointReference>
141 * </wsp:AppliesTo>
142 * ...
143 * </wst:RequestSecurityTokenResponse ...>
144 * <wst:RequestSecurityTokenResponseCollection>
145 * <soap:Body>
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 * <soap:Body>
164 * <wst:RequestSecurityTokenResponseCollection>
165 * <wst:RequestSecurityTokenResponse ...>
166 * ...
167 * <wst:Lifetime>
168 * ...
169 * <wsu:Expires>2011-07-23T15:37:12Z</wsu:Expires>
170 * </wst:Lifetime>
171 * </wst:RequestSecurityTokenResponse ...>
172 * <wst:RequestSecurityTokenResponseCollection>
173 * <soap:Body>
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 * <soap:Body>
192 * <soap:Fault>
193 * <faultcode>wst:FailedAuthentication</faultcode>
194 * ...
195 * </soap:Fault>
196 * </soap:Body>
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 * <soap:Body>
215 * <soap:Fault>
216 * <faultstring>Authentication failed: Token in request signed by untrusted party</faultstring>
217 * ...
218 * </soap:Fault>
219 * </soap:Body>
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 * <soap:Body>
238 * <soap:Fault>
239 * ...
240 * <faultactor>http://sosi.dk/sts</faultactor>
241 * </soap:Fault>
242 * </soap:Body>
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 * <soap:Body>
261 * <wst:RequestSecurityTokenResponseCollection>
262 * <wst:RequestSecurityTokenResponse ...>
263 * ...
264 * <wst:RequestedSecurityToken>
265 * <saml:Assertion IssueInstant="2011-07-23T15:32:12Z" ...>
266 * ...
267 * </saml:Assertion>
268 * </wst:RequestedSecurityToken>
269 * </wst:RequestSecurityTokenResponse ...>
270 * </wst:RequestSecurityTokenResponseCollection>
271 * </soap:Body>
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 * <soap:Header>
289 * ...
290 * <wsa:MessageID>urn:uuid:99999777-0000-0000</wsa:MessageID>
291 * ...
292 * </soap:Header>
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 "Relates to" part of the SOAP header.
304 *
305 * <pre>
306 * <soap:Header>
307 * ...
308 * <wsa:RelatesTo>urn:uuid:99999999-0000-0000</wsa:RelatesTo>
309 * </soap:Header>
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 * <soap:Body>
324 * <wst:RequestSecurityTokenResponseCollection>
325 * <wst:RequestSecurityTokenResponse ...>
326 * <wst:TokenType>http://docs.oasis-open.org/wss/oasis-wss-saml-token- profile-1.1#SAMLV2.0</wst:TokenType>
327 * ...
328 * </wst:RequestSecurityTokenResponse ...>
329 * <wst:RequestSecurityTokenResponseCollection>
330 * <soap:Body>
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 }