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: https://svn.softwareborsen.dk/sosi/trunk/modules/seal/src/main/java/dk/sosi/seal/model/dombuilders/SAMLRequestDOMBuilder.java $
27 * $Id: SAMLRequestDOMBuilder.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28 */
29 package dk.sosi.seal.model.dombuilders;
30
31 import dk.sosi.seal.model.IDCard;
32 import dk.sosi.seal.model.ModelException;
33 import dk.sosi.seal.model.Request;
34 import dk.sosi.seal.model.constants.MedComTags;
35 import dk.sosi.seal.model.constants.NameSpaces;
36 import dk.sosi.seal.vault.CredentialVault;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39
40 /**
41 * Builds a DOM tree representing a SAML request, given a <code>Request</code>
42 * object.
43 * <p>
44 * The DOM builder primarilly builds the SOSI envelope including embedded
45 * <code>IDCard</code> etc. The body element is not built in this builder but
46 * is merely passed (in the constructor) and embedded in the SOAP envelope.
47 *
48 * @author Jan Riis
49 * @author $LastChangedBy: chg@lakeside.dk $
50 * @since 1.0
51 */
52
53 public class SAMLRequestDOMBuilder extends SOAPMessageDOMBuilder {
54
55 /**
56 * Constructs the DOM builder for SAML requests.
57 *
58 * @param document
59 * the enclosing DOM document
60 * @param request
61 * the <code>Request</code> model element
62 * @param vault
63 * The credential valt with system signature
64 */
65 public SAMLRequestDOMBuilder(Document document, Request request, CredentialVault vault) {
66
67 super(document, request, vault);
68 }
69
70 /**
71 * Builds and returns the DOM element for this SAML request.
72 */
73 protected void _buildDOMDocument(Document document, Element header, Element body) {
74
75 Request request = (Request) getMessage();
76
77 SAMLUtil samlUtil = new SAMLUtil();
78 // Create wss:security element here
79 Element wssSecurity = samlUtil.createSecurityHeader(document, header, request);
80
81 // SOSI ID-card as saml:Assertion
82 IDCard idCard = request.getIDCard();
83
84 if(idCard == null) throw new ModelException("No Idcard present in request");
85 wssSecurity.appendChild(idCard.serialize2DOMDocument(request.getFactory(), document));
86
87 Element medComHeader = samlUtil.createMedcomHeader(document, header);
88
89 // medcom:SecurityLevel
90 int authLevel = idCard.getAuthenticationLevel().getLevel();
91 samlUtil.createSecurityLevel(document, medComHeader, authLevel);
92
93 // medcom:Linking
94 samlUtil.createMedcomLinking(document, medComHeader, request);
95
96 // medcom:RequireNonRepudiationReceipt
97 Element nrr = (Element) medComHeader.appendChild(document.createElementNS(
98 NameSpaces.MEDCOM_SCHEMA,
99 MedComTags.REQUIRE_NON_REPUDIATION_RECEIPT_PREFIXED));
100 nrr.appendChild(document.createTextNode((request.isDemandNonRepudiationReceipt()) ? "yes" : "no"));
101 }
102 }