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/modelbuilders/RequestModelBuilder.java $
27   * $Id: RequestModelBuilder.java 8697 2011-09-02 10:33:55Z chg@lakeside.dk $
28   */
29  
30  package dk.sosi.seal.vault.renewal.modelbuilders;
31  
32  import dk.sosi.seal.model.constants.NameSpaces;
33  import dk.sosi.seal.modelbuilders.ModelBuildException;
34  import dk.sosi.seal.vault.renewal.model.Request;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Node;
37  import org.w3c.dom.NodeList;
38  
39  import java.lang.reflect.Constructor;
40  import java.lang.reflect.InvocationTargetException;
41  
42  /**
43   * Class to build renewal request model objects from DOM representations.
44   * 
45   * @author thomas@signaturgruppen.dk
46   * @author $LastChangedBy: chg@lakeside.dk $
47   * @version $Revision: 8697 $
48   * @since 1.0
49   */
50  public class RequestModelBuilder extends MessageModelBuilder { //NOPMD 
51  
52  	/**
53  	 * Build a message model object
54  	 * @param doc
55  	 * 		DOM representation of the renewal message object
56  	 * @return The constructed <code>Request</code> object.
57  	 * @throws ModelBuildException
58  	 */
59  	public Request buildModel(Document doc) throws ModelBuildException {
60  
61  		NodeList methods = doc.getElementsByTagNameNS(NameSpaces.SOAP_SCHEMA, "Body").item(0).getChildNodes();
62  		if(methods.getLength() != 1) {
63  			throw new ModelBuildException("Wrong number of result elements in request.");
64  		}
65  			
66  		Node result = methods.item(0);
67  		String rType = getRequestType(result);
68  		
69  		try {
70  			
71  			Class<?> requestType =  Class.forName(rType);
72  			
73  			NodeList fields = result.getChildNodes();
74  			Object[] constructorArgs = new Object[fields.getLength()];
75  			Class<?>[] constructorArgTypes = new Class[fields.getLength()];
76  			for(int i = 0; i < fields.getLength(); i++) {
77  				constructorArgs[i] = getFieldValue(fields.item(i));
78  				constructorArgTypes[i] = constructorArgs[i].getClass();
79  			}
80  			
81  			Constructor<?> constructor =  requestType.getConstructor(constructorArgTypes);
82  			return (Request) constructor.newInstance(constructorArgs);
83  		} catch (ClassNotFoundException e) {
84  			throw new ModelBuildException("Failed to construct request", e);
85  		} catch (SecurityException e) {
86  			throw new ModelBuildException("Failed to construct request", e);
87  		} catch (NoSuchMethodException e) {
88  			throw new ModelBuildException("Failed to construct request", e);		
89  		} catch (IllegalArgumentException e) {
90  			throw new ModelBuildException("Failed to construct request", e);
91  		} catch (InstantiationException e) {
92  			throw new ModelBuildException("Failed to construct request", e);
93  		} catch (IllegalAccessException e) {
94  			throw new ModelBuildException("Failed to construct request", e);
95  		} catch (InvocationTargetException e) {
96  			throw new ModelBuildException("Failed to construct request", e);
97  		}
98  		
99  	}
100 
101 	/**
102 	 * Compute the name of the model class to build an instance of
103 	 * @param method 
104 	 * 		The method element from XML 
105 	 * @return
106 	 * 		Name of model class
107 	 */
108 	private String getRequestType(Node method) {
109 		String usedPackage = Request.class.getPackage().getName();
110 		String requestType = method.getNodeName().split(":")[1];
111 		requestType = requestType.substring(0, 1).toUpperCase() + requestType.substring(1) + "Request";
112 		return usedPackage + "." + requestType;
113 	}
114 }