1717package org .springframework .ws .server .endpoint .interceptor ;
1818
1919import java .io .IOException ;
20- import java .util .Locale ;
21- import javax .xml .namespace .QName ;
2220import javax .xml .transform .Source ;
2321import javax .xml .transform .TransformerException ;
2422
25- import org .apache .commons .logging .Log ;
26- import org .apache .commons .logging .LogFactory ;
2723import org .springframework .beans .factory .InitializingBean ;
2824import org .springframework .core .io .Resource ;
2925import org .springframework .util .Assert ;
3228import org .springframework .ws .WebServiceMessage ;
3329import org .springframework .ws .context .MessageContext ;
3430import org .springframework .ws .server .EndpointInterceptor ;
35- import org .springframework .ws .soap .SoapBody ;
36- import org .springframework .ws .soap .SoapFault ;
37- import org .springframework .ws .soap .SoapFaultDetail ;
38- import org .springframework .ws .soap .SoapFaultDetailElement ;
39- import org .springframework .ws .soap .SoapMessage ;
40- import org .springframework .xml .namespace .QNameUtils ;
4131import org .springframework .xml .transform .TransformerObjectSupport ;
4232import org .springframework .xml .validation .XmlValidator ;
4333import org .springframework .xml .validation .XmlValidatorFactory ;
5040 * <code>getValidationResponseSource</code> template methods.
5141 * <p/>
5242 * By default, only the request message is validated, but this behaviour can be changed using the
53- * <code>validateRequest</code> and <code>validateResponse</code> properties. Responses that contains SOAP faults are
54- * not validated.
43+ * <code>validateRequest</code> and <code>validateResponse</code> properties.
5544 *
5645 * @author Arjen Poutsma
5746 * @see #getValidationRequestSource(org.springframework.ws.WebServiceMessage)
6049public abstract class AbstractValidatingInterceptor extends TransformerObjectSupport
6150 implements EndpointInterceptor , InitializingBean {
6251
63- /**
64- * Default SOAP Fault Detail name used when a validation errors occur on the request.
65- *
66- * @see #setDetailElementName(javax.xml.namespace.QName)
67- */
68- public static final QName DEFAULT_DETAIL_ELEMENT_NAME =
69- QNameUtils .createQName ("http://springframework.org/spring-ws" , "ValidationError" , "spring-ws" );
70-
71- /**
72- * Default SOAP Fault string used when a validation errors occur on the request.
73- *
74- * @see #setFaultStringOrReason(String)
75- */
76- public static final String DEFAULT_FAULTSTRING_OR_REASON = "Validation error" ;
77-
78- protected final Log logger = LogFactory .getLog (getClass ());
79-
80- private boolean addValidationErrorDetail = true ;
81-
82- private QName detailElementName = DEFAULT_DETAIL_ELEMENT_NAME ;
83-
84- private String faultStringOrReason = DEFAULT_FAULTSTRING_OR_REASON ;
85-
86- private Locale faultStringOrReasonLocale = Locale .ENGLISH ;
87-
8852 private String schemaLanguage = XmlValidatorFactory .SCHEMA_W3C_XML ;
8953
9054 private Resource [] schemas ;
@@ -95,73 +59,6 @@ public abstract class AbstractValidatingInterceptor extends TransformerObjectSup
9559
9660 private XmlValidator validator ;
9761
98- public boolean getAddValidationErrorDetail () {
99- return addValidationErrorDetail ;
100- }
101-
102- /**
103- * Indicates whether a SOAP Fault detail element should be created when a validation error occurs. This detail
104- * element will contain the exact validation errors. It is only added when the underlying message is a
105- * <code>SoapMessage</code>. Defaults to <code>true</code>.
106- *
107- * @see org.springframework.ws.soap.SoapFault#addFaultDetail()
108- */
109- public void setAddValidationErrorDetail (boolean addValidationErrorDetail ) {
110- this .addValidationErrorDetail = addValidationErrorDetail ;
111- }
112-
113- /**
114- * Returns the fault detail element name when validation errors occur on the request.
115- */
116- public QName getDetailElementName () {
117- return detailElementName ;
118- }
119-
120- /**
121- * Sets the fault detail element name when validation errors occur on the request. Defaults to
122- * <code>DEFAULT_DETAIL_ELEMENT_NAME</code>.
123- *
124- * @see #DEFAULT_DETAIL_ELEMENT_NAME
125- */
126- public void setDetailElementName (QName detailElementName ) {
127- this .detailElementName = detailElementName ;
128- }
129-
130- /**
131- * Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
132- */
133- public String getFaultStringOrReason () {
134- return faultStringOrReason ;
135- }
136-
137- /**
138- * Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
139- * It is only added when the underlying message is a <code>SoapMessage</code>. Defaults to
140- * <code>DEFAULT_FAULTSTRING_OR_REASON</code>.
141- *
142- * @see #DEFAULT_FAULTSTRING_OR_REASON
143- */
144- public void setFaultStringOrReason (String faultStringOrReason ) {
145- this .faultStringOrReason = faultStringOrReason ;
146- }
147-
148- /**
149- * Returns the SOAP fault reason locale used when validation errors occur on the request.
150- */
151- public Locale getFaultStringOrReasonLocale () {
152- return faultStringOrReasonLocale ;
153- }
154-
155- /**
156- * Sets the SOAP fault reason locale used when validation errors occur on the request. It is only added when the
157- * underlying message is a <code>SoapMessage</code>. Defaults to English.
158- *
159- * @see java.util.Locale#ENGLISH
160- */
161- public void setFaultStringOrReasonLocale (Locale faultStringOrReasonLocale ) {
162- this .faultStringOrReasonLocale = faultStringOrReasonLocale ;
163- }
164-
16562 public String getSchemaLanguage () {
16663 return schemaLanguage ;
16764 }
@@ -236,13 +133,7 @@ public boolean handleRequest(MessageContext messageContext, Object endpoint)
236133 if (requestSource != null ) {
237134 SAXParseException [] errors = validator .validate (requestSource );
238135 if (!ObjectUtils .isEmpty (errors )) {
239- for (int i = 0 ; i < errors .length ; i ++) {
240- logger .warn ("XML validation error on request: " + errors [i ].getMessage ());
241- }
242- if (messageContext .getResponse () instanceof SoapMessage ) {
243- createRequestValidationFault ((SoapMessage ) messageContext .getResponse (), errors );
244- }
245- return false ;
136+ return handleRequestValidationErrors (messageContext , errors );
246137 }
247138 else if (logger .isDebugEnabled ()) {
248139 logger .debug ("Request message validated" );
@@ -252,6 +143,22 @@ else if (logger.isDebugEnabled()) {
252143 return true ;
253144 }
254145
146+ /**
147+ * Template method that is called when the request message contains validation errors. Default implementation logs
148+ * all errors, and returns <code>false</code>, i.e. do not process the request.
149+ *
150+ * @param messageContext the message context
151+ * @param errors the validation errors
152+ * @return <code>true</code> to continue processing the request, <code>false</code> (the default) otherwise
153+ */
154+ protected boolean handleRequestValidationErrors (MessageContext messageContext , SAXParseException [] errors )
155+ throws TransformerException {
156+ for (int i = 0 ; i < errors .length ; i ++) {
157+ logger .warn ("XML validation error on request: " + errors [i ].getMessage ());
158+ }
159+ return false ;
160+ }
161+
255162 /**
256163 * Validates the response message in the given message context. Validation only occurs if
257164 * <code>validateResponse</code> is set to <code>true</code>, which is <strong>not</strong> the default.
@@ -268,10 +175,7 @@ public boolean handleResponse(MessageContext messageContext, Object endpoint) th
268175 if (responseSource != null ) {
269176 SAXParseException [] errors = validator .validate (responseSource );
270177 if (!ObjectUtils .isEmpty (errors )) {
271- for (int i = 0 ; i < errors .length ; i ++) {
272- logger .error ("XML validation error on response: " + errors [i ].getMessage ());
273- }
274- return false ;
178+ return handleResponseValidationErrors (messageContext , errors );
275179 }
276180 else if (logger .isDebugEnabled ()) {
277181 logger .debug ("Response message validated" );
@@ -281,6 +185,21 @@ else if (logger.isDebugEnabled()) {
281185 return true ;
282186 }
283187
188+ /**
189+ * Template method that is called when the response message contains validation errors. Default implementation logs
190+ * all errors, and returns <code>false</code>, i.e. do not send the response.
191+ *
192+ * @param messageContext the message context
193+ * @param errors the validation errors @return <code>true</code> to continue sending the response,
194+ * <code>false</code> (the default) otherwise
195+ */
196+ protected boolean handleResponseValidationErrors (MessageContext messageContext , SAXParseException [] errors ) {
197+ for (int i = 0 ; i < errors .length ; i ++) {
198+ logger .error ("XML validation error on response: " + errors [i ].getMessage ());
199+ }
200+ return false ;
201+ }
202+
284203 public void afterPropertiesSet () throws Exception {
285204 Assert .notEmpty (schemas , "setting either the schema or schemas property is required" );
286205 Assert .hasLength (schemaLanguage , "schemaLanguage is required" );
@@ -293,22 +212,6 @@ public void afterPropertiesSet() throws Exception {
293212 validator = XmlValidatorFactory .createValidator (schemas , schemaLanguage );
294213 }
295214
296- /**
297- * Creates a response soap message containing a <code>SoapFault</code> that descibes the validation errors.
298- */
299- protected void createRequestValidationFault (SoapMessage response , SAXParseException [] errors )
300- throws TransformerException {
301- SoapBody body = response .getSoapBody ();
302- SoapFault fault = body .addClientOrSenderFault (getFaultStringOrReason (), getFaultStringOrReasonLocale ());
303- if (getAddValidationErrorDetail ()) {
304- SoapFaultDetail detail = fault .addFaultDetail ();
305- for (int i = 0 ; i < errors .length ; i ++) {
306- SoapFaultDetailElement detailElement = detail .addFaultDetailElement (getDetailElementName ());
307- detailElement .addText (errors [i ].getMessage ());
308- }
309- }
310- }
311-
312215 /**
313216 * Abstract template method that returns the part of the request message that is to be validated.
314217 *
0 commit comments