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: https://svn.softwareborsen.dk/sosi/trunk/modules/seal/src/main/java/dk/sosi/seal/model/dombuilders/SAMLReplyDOMBuilder.java $
27   * $Id: SAMLReplyDOMBuilder.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.Reply;
32  import dk.sosi.seal.model.constants.MedComTags;
33  import dk.sosi.seal.model.constants.NameSpaces;
34  import dk.sosi.seal.model.constants.SOAPTags;
35  import dk.sosi.seal.vault.CredentialVault;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  
39  /**
40   * DomBuilder for SOSI compliant SAML replies. <p/> <b>This class should only be
41   * accessed through model classes</b>
42   * </p>
43   * 
44   * @author Jan Riis
45   * @author $LastChangedBy: chg@lakeside.dk $
46   * @since 1.0
47   */
48  public class SAMLReplyDOMBuilder extends SOAPMessageDOMBuilder {
49  
50  	protected SAMLUtil samlUtil = new SAMLUtil();
51  
52  	/**
53  	 * Constructs a SOAP builder for SAML replies.
54  	 * 
55  	 * @param document
56  	 *            the enclosing DOM document
57  	 * @param reply
58  	 *            The <code>Reply</code> model element
59  	 * @param vault
60  	 *            The credential valt with system signature
61  	 */
62  	public SAMLReplyDOMBuilder(Document document, Reply reply, CredentialVault vault) {
63  
64  		super(document, reply, vault);
65  	}
66  
67  	/**
68  	 * Builds the document element.
69  	 */
70  	protected void _buildDOMDocument(Document document, Element header, Element body) {
71  
72  		Reply reply = (Reply) getMessage();
73  
74  		// Create wss:security element here
75  		Element wssSecurity = samlUtil.createSecurityHeader(document, header, reply);
76  
77  		if (reply.getIDCard() != null) {
78  			wssSecurity.appendChild(reply.getIDCard().serialize2DOMDocument(reply.getFactory(), document));
79  		}
80  
81  		Element medComHeader = samlUtil.createMedcomHeader(document, header);
82  
83  		// Medcom attributes
84  		if (reply.getIDCard() != null) {
85  			samlUtil.createSecurityLevel(document, medComHeader, reply.getIDCard().getAuthenticationLevel().getLevel());
86  		} else {
87  			samlUtil.createSecurityLevel(document, medComHeader, 1);
88  		}
89  		Element medcomLinking = samlUtil.createMedcomLinking(document, medComHeader, reply);
90  		Element inResponseToMessageID = (Element) medcomLinking.appendChild(document.createElementNS(
91  				NameSpaces.MEDCOM_SCHEMA,
92  					MedComTags.IN_RESPONSE_TO_MESSAGE_ID_PREFIXED));
93  		inResponseToMessageID.appendChild(document.createTextNode(reply.getRequestID()));
94  
95  		// Set FlowStatus
96  		if (reply.isFault()) {
97  			Element soapFault = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.FAULT_PREFIXED);
98  
99  			Element faultcode = document.createElement(SOAPTags.FAULTCODE);
100 			faultcode.appendChild(document.createTextNode("Server"));
101 			soapFault.appendChild(faultcode);
102 
103 			Element detail = document.createElement(SOAPTags.DETAIL);
104 			Element medcomFaultCode = document.createElementNS(NameSpaces.MEDCOM_SCHEMA, MedComTags.FAULT_CODE_PREFIXED);
105 			medcomFaultCode.appendChild(document.createTextNode(reply.getFaultCode()));
106 			detail.appendChild(medcomFaultCode);
107 			soapFault.appendChild(detail);
108 
109 			Element faultstring = document.createElement(SOAPTags.FAULTSTRING);
110 			faultstring.appendChild(document.createTextNode(reply.getFaultString()));
111 			soapFault.appendChild(faultstring);
112 
113 			body.appendChild(soapFault);
114 		} else {
115 			Element flowStatus = document.createElementNS(NameSpaces.MEDCOM_SCHEMA, MedComTags.FLOW_STATUS_PREFIXED);
116 			flowStatus.appendChild(document.createTextNode(reply.getFlowStatus()));
117 			medComHeader.appendChild(flowStatus);
118 		}
119 	}
120 }