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.modelbuilders.ModelBuildException;
33 import dk.sosi.seal.vault.renewal.model.Response;
34 import dk.sosi.seal.vault.renewal.model.dombuilders.Constants;
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
51 public class ResponseModelBuilder extends MessageModelBuilder {
52
53
54
55
56
57
58
59
60 public Response buildModel(Document doc) throws ModelBuildException {
61
62 NodeList results = doc.getElementsByTagName("result");
63 if(results.getLength() != 1) {
64 throw new ModelBuildException("Wrong number of result elements in response.");
65 }
66
67 Node result = results.item(0);
68 String rType = getResponseType(result);
69
70 try {
71
72 Class<?> responseType = Class.forName(rType);
73 Constructor<?> constructor = responseType.getConstructor(new Class[]{});
74 Response response = (Response) constructor.newInstance(new Object[]{});
75
76 NodeList fields = result.getChildNodes();
77 for(int i = 0; i < fields.getLength(); i++) {
78 if(fields.item(i).getAttributes() != null) {
79 setField(response, fields.item(i));
80 }
81 }
82
83
84 return response;
85
86 } catch (ClassNotFoundException e) {
87 throw new ModelBuildException("Failed to construct response", e);
88 } catch (SecurityException e) {
89 throw new ModelBuildException("Failed to construct response", e);
90 } catch (NoSuchMethodException e) {
91 throw new ModelBuildException("Failed to construct response", e);
92 } catch (IllegalArgumentException e) {
93 throw new ModelBuildException("Failed to construct response", e);
94 } catch (InstantiationException e) {
95 throw new ModelBuildException("Failed to construct response", e);
96 } catch (IllegalAccessException e) {
97 throw new ModelBuildException("Failed to construct response", e);
98 } catch (InvocationTargetException e) {
99 throw new ModelBuildException("Failed to construct response", e);
100 }
101
102
103
104 }
105
106
107
108
109
110
111
112
113
114
115 private String getResponseType(Node result) {
116 String usedPackage = Response.class.getPackage().getName();
117 String responseType = result.getAttributes().getNamedItem(Constants.XSI_TYPE).getNodeValue();
118
119 responseType = responseType.split(":")[1];
120 return usedPackage + "." + responseType;
121 }
122
123
124 }