View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
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   * The Class ITKSOAPException.
36   *
37   * @author Michael Odling-Smee
38   * @author Nicholas Jones
39   * @since 0.1
40   */
41  public class ITKSOAPException extends ITKMessagingException {
42  	
43  	/**
44  	 * Instantiates a new iTKSOAP exception.
45  	 *
46  	 * @param itkMessagingException the itk messaging exception
47  	 */
48  	public ITKSOAPException(ITKMessagingException itkMessagingException) {
49  		//May throw NullPointer if itkMessagingException is null
50  		super(itkMessagingException.getRelatedMessageProperties(), itkMessagingException.getErrorCode(), itkMessagingException.getMessage(), itkMessagingException.getCause());
51  		super.setErrorId(itkMessagingException.getErrorId());
52  	}
53  	
54  	/**
55  	 * Instantiates a new iTKSOAP exception.
56  	 *
57  	 * @param itkMessageProperties the itk message properties
58  	 * @param errorCode the error code
59  	 * @param arg0 the arg0
60  	 * @param arg1 the arg1
61  	 */
62  	public ITKSOAPException(ITKMessageProperties itkMessageProperties, int errorCode, String arg0, Throwable arg1) {
63  		super(itkMessageProperties, errorCode, arg0, arg1);
64  	}
65  
66  	/**
67  	 * Instantiates a new iTKSOAP exception.
68  	 *
69  	 * @param itkMessageProperties the itk message properties
70  	 * @param errorCode the error code
71  	 * @param arg0 the arg0
72  	 */
73  	public ITKSOAPException(ITKMessageProperties itkMessageProperties, int errorCode, String arg0) {
74  		super(itkMessageProperties, errorCode, arg0);
75  	}
76  
77  	/** The Constant serialVersionUID. */
78  	private static final long serialVersionUID = -4655001784827242123L;
79  
80  	/**
81  	 * Instantiates a new iTKSOAP exception.
82  	 *
83  	 * @param arg0 the arg0
84  	 * @param arg1 the arg1
85  	 */
86  	public ITKSOAPException(String arg0, Throwable arg1) {
87  		super(arg0, arg1);
88  	}
89  
90  	/**
91  	 * Instantiates a new iTKSOAP exception.
92  	 *
93  	 * @param arg0 the arg0
94  	 */
95  	public ITKSOAPException(String arg0) {
96  		super(arg0);
97  	}
98  
99  	/**
100 	 * Instantiates a new iTKSOAP exception.
101 	 *
102 	 * @param arg0 the arg0
103 	 */
104 	public ITKSOAPException(Throwable arg0) {
105 		super(arg0);
106 	}
107 	
108 	/**
109 	 * Builds a complete soap wrapped SOAPFault.
110 	 *
111 	 * @return the string
112 	 */
113 	//@Requirement(traceTo={"WS-STD-03","WS-STD-04"})
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 			//Set props
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 		//TODO - need information to determine whether it is a client or server soap fault
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 			// TODO Auto-generated catch block
143 			e.printStackTrace();
144 		}
145 		return serialisedMessage;
146 	}
147 	
148 	
149 	/**
150 	 * Parses the soap fault.
151 	 *
152 	 * @param soapFaultXML the soap fault xml
153 	 * @return the iTKSOAP exception
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 }