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
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
44
45
46
47
48
49
50 public class RequestModelBuilder extends MessageModelBuilder {
51
52
53
54
55
56
57
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
103
104
105
106
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 }