1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package dk.sosi.seal.model.dombuilders;
30
31 import dk.sosi.seal.SOSIFactory;
32 import dk.sosi.seal.model.AuthenticationLevel;
33 import dk.sosi.seal.model.IDCard;
34 import dk.sosi.seal.model.Message;
35 import dk.sosi.seal.model.constants.NameSpaces;
36 import dk.sosi.seal.model.constants.SOAPTags;
37 import dk.sosi.seal.vault.CredentialVault;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42
43 import java.util.Iterator;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.Map;
47
48
49
50
51
52
53
54
55 abstract class SOAPMessageDOMBuilder {
56
57 private Document document;
58 private final Message message;
59
60 private Element header;
61 private Element body;
62 protected CredentialVault vault;
63
64
65
66
67
68
69
70
71
72
73
74 protected SOAPMessageDOMBuilder(Document document, Message message, CredentialVault vault) {
75
76 super();
77 this.document = document;
78 this.message = message;
79 this.vault = vault;
80 initializeSOAP();
81 }
82
83
84
85
86
87
88
89
90
91 protected void initializeSOAP() {
92
93 Element root = document.getDocumentElement();
94 if (root == null) {
95 root = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.ENVELOPE_PREFIXED);
96 document.appendChild(root);
97 }
98
99 Map<String, String> nameSpaces;
100 if(message.isFault()) nameSpaces = NameSpaces.SOSI_FAULT_NAMESPACES;
101 else nameSpaces = NameSpaces.SOSI_NAMESPACES;
102 for (Iterator<String> iter = nameSpaces.keySet().iterator(); iter.hasNext();) {
103 String key = iter.next();
104 addNameSpaceAttribute(key, nameSpaces.get(key));
105 }
106
107 String sealMsgVersion = SOSIFactory.PROPERTYVALUE_SOSI_SEAL_MESSAGE_VERSION;
108 if(message.getFactory() != null)
109 sealMsgVersion = message.getFactory().getProperties().getProperty(SOSIFactory.PROPERTYNAME_SOSI_SEAL_MESSAGE_VERSION, SOSIFactory.PROPERTYVALUE_SOSI_SEAL_MESSAGE_VERSION);
110
111 if("1.0_0".equals(sealMsgVersion)) {
112 root.setAttribute("id", "Envelope");
113 } else if("1.0_1".equals(sealMsgVersion)) {
114 root.setAttributeNS(NameSpaces.WSU_SCHEMA, NameSpaces.NS_WSU+":id", SOAPTags.ENVELOPE_UNPREFIXED);
115 } else if("1.0_2".equals(sealMsgVersion)) {
116 root.setAttributeNS(NameSpaces.WSU_SCHEMA, NameSpaces.NS_WSU+":id", SOAPTags.ENVELOPE_UNPREFIXED);
117 }
118
119 NodeList nodes = root.getElementsByTagNameNS(NameSpaces.SOAP_SCHEMA, SOAPTags.HEADER_UNPREFIXED);
120 if (nodes.getLength() == 0) {
121 header = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.HEADER_PREFIXED);
122 root.appendChild(header);
123 } else if (nodes.getLength() == 1) {
124 header = (Element) nodes.item(0);
125 NodeList children = header.getChildNodes();
126 List<Node> list = new LinkedList<Node>();
127 for (int i = 0; i < children.getLength(); i++) {
128 list.add(children.item(0));
129 }
130 for (Iterator<Node> iter = list.iterator(); iter.hasNext();) {
131 header.removeChild(iter.next());
132 }
133 } else {
134 throw new DOMBuilderException("Too many soap:Header elements in document!", null);
135 }
136
137 nodes = root.getElementsByTagNameNS(NameSpaces.SOAP_SCHEMA, SOAPTags.BODY_UNPREFIXED);
138 if (nodes.getLength() == 0) {
139 body = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.BODY_PREFIXED);
140 root.appendChild(body);
141 } else if (nodes.getLength() == 1) {
142 body = (Element) nodes.item(0);
143 } else {
144 throw new DOMBuilderException("Too many soap:Body elements in document!", null);
145 }
146 }
147
148
149
150
151 protected Message getMessage() {
152
153 return message;
154 }
155
156
157
158
159 public final Document buildDOMDocument() {
160 _buildDOMDocument(document, header, body);
161
162
163
164 IDCard idCard = message.getIDCard();
165 if (idCard != null && idCard.getAuthenticationLevel().getLevel() == AuthenticationLevel.VOCES_TRUSTED_SYSTEM.getLevel()) {
166 idCard.sign(document, vault);
167 }
168
169
170 if (message.getNonSOSIHeaders() != null) {
171 for (Iterator<Element> it = message.getNonSOSIHeaders().iterator(); it.hasNext();) {
172 Element e = it.next();
173 header.appendChild(document.importNode(e, true));
174 }
175 }
176
177
178 if (message.getBody() != null && body.getChildNodes().getLength() == 0) {
179
180 body.appendChild(document.importNode(message.getBody(), true));
181 }
182
183
184
185 return document;
186
187 }
188
189
190
191
192
193
194
195
196
197
198 protected void addNameSpaceAttribute(String name, String value) {
199
200 Element documentElement = document.getDocumentElement();
201 if (documentElement.getAttributeNS(NameSpaces.XMLNS_SCHEMA, name) == null
202 || documentElement.getAttributeNS(NameSpaces.XMLNS_SCHEMA, name).equals("")) {
203 documentElement.setAttributeNS(NameSpaces.XMLNS_SCHEMA, NameSpaces.NS_XMLNS + ":" + name, value);
204 }
205 }
206
207
208
209
210 protected abstract void _buildDOMDocument(Document doc, Element headerElement, Element bodyElement);
211 }