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/vault/renewal/model/dombuilders/MessageDOMBuilder.java $
27   * $Id: MessageDOMBuilder.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28   */
29  package dk.sosi.seal.vault.renewal.model.dombuilders;
30  
31  import dk.sosi.seal.model.constants.NameSpaces;
32  import dk.sosi.seal.model.constants.SOAPTags;
33  import dk.sosi.seal.model.dombuilders.DOMBuilderException;
34  import dk.sosi.seal.vault.renewal.model.Argument;
35  import dk.sosi.seal.xml.XmlUtil;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  import org.w3c.dom.NodeList;
39  
40  /**
41   * Class for building DOM objects from renewal messages.
42   * 
43   * @author thomas@signaturgruppen.dk
44   * @author $LastChangedBy: chg@lakeside.dk $
45   * @version $Revision: 8697 $
46   * @since 1.0
47   */
48  public abstract class MessageDOMBuilder { //NOPMD
49  	protected Document document;
50  	protected Element body;
51  	
52      protected void addNameSpaceAttribute(String name, String value) {
53          Element documentElement = document.getDocumentElement();
54          addNameSpaceAttributeToElement(name, value, documentElement);
55      }
56  
57  	protected void addNameSpaceAttributeToElement(String name, String value, Element documentElement) {
58  		if(documentElement.getAttributeNS(NameSpaces.XMLNS_SCHEMA,name)==null || documentElement.getAttributeNS(NameSpaces.XMLNS_SCHEMA,name).equals("")) {
59          	documentElement.setAttributeNS(NameSpaces.XMLNS_SCHEMA, NameSpaces.NS_XMLNS+":"+name, value);
60          }
61  	}
62  
63      /**
64       * Initializes a SOAP message with elements that are common to all
65       * renewal messages.
66       */
67      protected void initializeSOAP(boolean setupHeader) {
68      	Element root = document.getDocumentElement();
69          if (root == null) {
70              root = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.ENVELOPE_PREFIXED);
71              document.appendChild(root);
72          }
73  
74          addNameSpaceAttribute(NameSpaces.NS_SOAP, NameSpaces.SOAP_SCHEMA);
75          addNameSpaceAttribute(NameSpaces.NS_XSI, NameSpaces.XMLSCHEMAINSTANCE_SCHEMA);
76          addNameSpaceAttribute(NameSpaces.NS_XSD, NameSpaces.XSD_SCHEMA);
77  
78  		//Add empty header element
79  		NodeList nodes;
80          
81  		if (setupHeader) {
82  	        nodes = root.getElementsByTagNameNS(NameSpaces.SOAP_SCHEMA, SOAPTags.HEADER_UNPREFIXED);
83  	        if (nodes.getLength() == 0) {
84  				Element header = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.HEADER_PREFIXED);
85  				root.appendChild(header);
86  	        } else if(nodes.getLength() == 1) { 
87  	        	//OK, one header element is expected
88  	        } else { //NOPMD
89  	        	throw new DOMBuilderException("Too many soap:Header elements in document!", null);
90  	        }
91  
92  		}		
93  
94          nodes = root.getElementsByTagNameNS(NameSpaces.SOAP_SCHEMA, SOAPTags.BODY_UNPREFIXED);
95          if (nodes.getLength() == 0) {
96              body = document.createElementNS(NameSpaces.SOAP_SCHEMA, SOAPTags.BODY_PREFIXED);
97              root.appendChild(body);
98          } else if(nodes.getLength() == 1) {
99          	body = (Element)nodes.item(0);
100         } else {
101         	throw new DOMBuilderException("Too many soap:Body elements in document!", null);
102         }
103     }
104     
105     /**
106      * Adds a method argument to the DOM representation of the message.
107      * @param method
108      * @param argument
109      */
110 	protected void addMethodArgument(Element method, Argument argument) {
111 		Element argumentElement = document.createElement(argument.getName());
112 		
113 		if(argument.getType().equals(String.class)) {
114 			argumentElement.setAttribute(Constants.XSI_TYPE, Constants.XSD_STRING);
115 			argumentElement.appendChild(document.createTextNode((String) argument.getValue()));
116 		} else if(argument.getType().equals(byte[].class)) {
117 			argumentElement.setAttribute(Constants.XSI_TYPE, Constants.XSD_BASE64BINARY);
118 			argumentElement.appendChild(document.createTextNode(XmlUtil.toBase64((byte[]) argument.getValue())));
119 		} else if(argument.getType().equals(Integer.class) || argument.getType().equals(Integer.TYPE)) {
120 			argumentElement.setAttribute(Constants.XSI_TYPE, Constants.XSD_INT);
121 			argumentElement.appendChild(document.createTextNode(argument.getValue().toString()));
122 		} else {
123 			throw new DOMBuilderException("Unsupported argument type: " + argument.getType().getName(), null);
124 		}
125 		
126 		method.appendChild(argumentElement);
127 		
128 	}
129 
130 
131 	
132 }