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/modelbuilders/RequestModelBuilder.java $
27 * $Id: RequestModelBuilder.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28 */
29 package dk.sosi.seal.modelbuilders;
30
31 import dk.sosi.seal.SOSIFactory;
32 import dk.sosi.seal.model.AuthenticationLevel;
33 import dk.sosi.seal.model.Request;
34 import dk.sosi.seal.model.SignatureUtil;
35 import dk.sosi.seal.model.constants.DSTags;
36 import dk.sosi.seal.model.constants.NameSpaces;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Node;
39 import org.w3c.dom.NodeList;
40
41 /**
42 * Builds <code>Request</code> model objects from a DOM document.
43 *
44 * @author Jan
45 * @author $LastChangedBy: chg@lakeside.dk $
46 * @since 1.0
47 */
48
49 public class RequestModelBuilder extends MessageModelBuilder {
50
51 public RequestModelBuilder(SOSIFactory fac) {
52
53 super(fac);
54 }
55
56 /**
57 * Builds a Request objects from a DOM document.
58 *
59 * @param doc
60 * The DOM document used for the Request.
61 */
62 public Request buildModel(Document doc) throws ModelBuildException {
63
64 // Extract parameters
65
66 boolean noRep = false;
67 Node node = doc.getElementsByTagNameNS(NameSpaces.MEDCOM_SCHEMA, "RequireNonRepudiationReceipt").item(0);
68 if (node != null) {
69 String noRepString = node.getChildNodes().item(0).getNodeValue();
70 noRep = !"no".equals(noRepString);
71 }
72
73 Request request = factory.createNewRequest(noRep, null);
74
75 // Message parameters
76 super.buildModel(request, doc);
77
78 if (AuthenticationLevel.MOCES_TRUSTED_USER.equals(request.getIDCard().getAuthenticationLevel()) ||
79 AuthenticationLevel.VOCES_TRUSTED_SYSTEM.equals(request.getIDCard().getAuthenticationLevel())) {
80 // Validate Signatures
81 NodeList signatures = doc.getElementsByTagNameNS(NameSpaces.DSIG_SCHEMA, DSTags.SIGNATURE);
82 if (signatures.getLength() == 0)
83 // In SOSI authlvl 3-4, signatures are mandatory on requests
84 throw new SignatureInvalidModelBuildException("ID Card has no signature", request.getMessageID(), request.getFlowID(), request.getDGWSVersion());
85 SignatureUtil.validateAllSignatures(request, signatures, factory.getFederation(), factory.getCredentialVault(),true);
86 }
87 return request;
88 }
89 }