1
2
3
4
5
6
7
8
9
10
11
12
13
14 package uk.nhs.interoperability.transport.WS;
15
16 import java.io.IOException;
17 import java.util.UUID;
18
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.xpath.XPathConstants;
21 import javax.xml.xpath.XPathExpressionException;
22
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Node;
25 import org.xml.sax.SAXException;
26
27 import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
28 import uk.nhs.interoperability.infrastructure.ITKMessagingException;
29 import uk.nhs.interoperability.transform.TransformManager;
30 import uk.nhs.interoperability.util.Logger;
31 import uk.nhs.interoperability.util.xml.DomUtils;
32 import uk.nhs.interoperability.util.xml.XPaths;
33
34
35
36
37
38
39
40
41 public class ITKSOAPException extends ITKMessagingException {
42
43
44
45
46
47
48 public ITKSOAPException(ITKMessagingException itkMessagingException) {
49
50 super(itkMessagingException.getRelatedMessageProperties(), itkMessagingException.getErrorCode(), itkMessagingException.getMessage(), itkMessagingException.getCause());
51 super.setErrorId(itkMessagingException.getErrorId());
52 }
53
54
55
56
57
58
59
60
61
62 public ITKSOAPException(ITKMessageProperties itkMessageProperties, int errorCode, String arg0, Throwable arg1) {
63 super(itkMessageProperties, errorCode, arg0, arg1);
64 }
65
66
67
68
69
70
71
72
73 public ITKSOAPException(ITKMessageProperties itkMessageProperties, int errorCode, String arg0) {
74 super(itkMessageProperties, errorCode, arg0);
75 }
76
77
78 private static final long serialVersionUID = -4655001784827242123L;
79
80
81
82
83
84
85
86 public ITKSOAPException(String arg0, Throwable arg1) {
87 super(arg0, arg1);
88 }
89
90
91
92
93
94
95 public ITKSOAPException(String arg0) {
96 super(arg0);
97 }
98
99
100
101
102
103
104 public ITKSOAPException(Throwable arg0) {
105 super(arg0);
106 }
107
108
109
110
111
112
113
114 public String serialiseXML() {
115 String XML = "<SOAPMessage>";
116 XML += "<MessageId>" + UUID.randomUUID().toString().toUpperCase() + "</MessageId>";
117 String faultActor = null;
118 if (this.getRelatedItkTransportProperties() != null) {
119
120 XML += "<To>" + SOAPUtils.resolveFaultToAddress(this.getRelatedItkTransportProperties()) + "</To>";
121 XML += "<From>" + this.getRelatedItkTransportProperties().getTransportTo() + "</From>";
122 XML += "<RelatesTo>"+ this.getRelatedItkTransportProperties().getTransportMessageId() + "</RelatesTo>";
123 faultActor = this.getRelatedItkTransportProperties().getInvokedUrl();
124 };
125 XML += "<FaultDetail>";
126
127 XML += " <FaultLocation>Server</FaultLocation>";
128 if (faultActor != null) {
129 XML += " <FaultActor>" + faultActor + "</FaultActor>";
130 }
131 XML += " <ErrorID>" + this.getErrorId() + "</ErrorID>";
132 XML += " <ErrorCode codeSystem=\"" + this.getErrorCodeSystem() + "\">" + this.getErrorCode() + "</ErrorCode>";
133 XML += " <ErrorText>" + this.decodeErrorCode() + "</ErrorText>";
134 XML += " <ErrorDiagnosticText>" + this.getLocalizedMessage() + "</ErrorDiagnosticText>";
135 XML += "</FaultDetail>";
136 XML += "</SOAPMessage>";
137 String serialisedMessage ="";
138 try {
139 Logger.trace(XML);
140 serialisedMessage = TransformManager.doTransform("ToSOAPFault.xslt", XML);
141 } catch (Exception e) {
142
143 e.printStackTrace();
144 }
145 return serialisedMessage;
146 }
147
148
149
150
151
152
153
154
155 public static final ITKSOAPException parseSOAPFault(String soapFaultXML) {
156 if (soapFaultXML != null) {
157 try {
158 Document doc = DomUtils.parse(soapFaultXML);
159 Document faultDetail = DomUtils.createDocumentFromNode((Node)XPaths.WSA_SOAP_ERROR_DETAIL_XPATH.evaluate(doc, XPathConstants.NODE));
160 Integer errorCode = Integer.parseInt(XPaths.WSA_SOAP_ERROR_DETAIL_CODE_XPATH.evaluate(faultDetail));
161 String errorText = XPaths.WSA_SOAP_ERROR_DETAIL_DIAGNOSTIC_XPATH.evaluate(faultDetail);
162 String errorId = XPaths.WSA_SOAP_ERROR_DETAIL_ID_XPATH.evaluate(faultDetail);
163 ITKSOAPException soapFault = new ITKSOAPException(errorText);
164 soapFault.setErrorId(errorId);
165 soapFault.setErrorCode(errorCode);
166 return soapFault;
167 } catch (SAXException e) {
168 Logger.error("Could not parse SOAP fault", e);
169 } catch (IOException e) {
170 Logger.error("Could not parse SOAP fault", e);
171 } catch (ParserConfigurationException e) {
172 Logger.error("Could not parse SOAP fault", e);
173 } catch (XPathExpressionException e) {
174 Logger.error("Could not parse SOAP fault", e);
175 }
176 }
177 return new ITKSOAPException("SOAP fault (could not parse soap fault");
178 }
179
180
181
182
183 }