View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.server.endpoint.interceptor;
18  
19  import java.util.Locale;
20  import javax.xml.namespace.QName;
21  import javax.xml.transform.TransformerException;
22  
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.endpoint.interceptor.AbstractValidatingInterceptor;
25  import org.springframework.ws.soap.SoapBody;
26  import org.springframework.ws.soap.SoapFault;
27  import org.springframework.ws.soap.SoapFaultDetail;
28  import org.springframework.ws.soap.SoapFaultDetailElement;
29  import org.springframework.ws.soap.SoapMessage;
30  import org.springframework.xml.namespace.QNameUtils;
31  
32  import org.xml.sax.SAXParseException;
33  
34  /**
35   * Subclass of <code>AbstractValidatingInterceptor</code> that creates a SOAP Fault whenever the request message cannot
36   * be validated. The contents of the SOAP Fault can be specified by setting the <code>addValidationErrorDetail</code>,
37   * <code>faultStringOrReason</code>, or  <code>detailElementName</code> properties. Further customizing can be
38   * accomplished by overriding <code>handleRequestValidationErrors</code>.
39   *
40   * @author Arjen Poutsma
41   * @see #setAddValidationErrorDetail(boolean)
42   * @see #setFaultStringOrReason(String)
43   * @see #DEFAULT_FAULTSTRING_OR_REASON
44   * @see #setDetailElementName(javax.xml.namespace.QName)
45   * @see #DEFAULT_DETAIL_ELEMENT_NAME
46   * @see #handleResponseValidationErrors(org.springframework.ws.context.MessageContext,org.xml.sax.SAXParseException[])
47   * @since 1.0.0
48   */
49  public abstract class AbstractFaultCreatingValidatingInterceptor extends AbstractValidatingInterceptor {
50  
51      /**
52       * Default SOAP Fault Detail name used when a validation errors occur on the request.
53       *
54       * @see #setDetailElementName(javax.xml.namespace.QName)
55       */
56      public static final QName DEFAULT_DETAIL_ELEMENT_NAME =
57              QNameUtils.createQName("http://springframework.org/spring-ws", "ValidationError", "spring-ws");
58  
59      /**
60       * Default SOAP Fault string used when a validation errors occur on the request.
61       *
62       * @see #setFaultStringOrReason(String)
63       */
64      public static final String DEFAULT_FAULTSTRING_OR_REASON = "Validation error";
65  
66      private boolean addValidationErrorDetail = true;
67  
68      private QName detailElementName = DEFAULT_DETAIL_ELEMENT_NAME;
69  
70      private String faultStringOrReason = DEFAULT_FAULTSTRING_OR_REASON;
71  
72      private Locale faultStringOrReasonLocale = Locale.ENGLISH;
73  
74      /**
75       * Returns whether a SOAP Fault detail element should be created when a validation error occurs. This detail element
76       * will contain the exact validation errors. It is only added when the underlying message is a
77       * <code>SoapMessage</code>. Defaults to <code>true</code>.
78       *
79       * @see org.springframework.ws.soap.SoapFault#addFaultDetail()
80       */
81      public boolean getAddValidationErrorDetail() {
82          return addValidationErrorDetail;
83      }
84  
85      /**
86       * Indicates whether a SOAP Fault detail element should be created when a validation error occurs. This detail
87       * element will contain the exact validation errors. It is only added when the underlying message is a
88       * <code>SoapMessage</code>. Defaults to <code>true</code>.
89       *
90       * @see org.springframework.ws.soap.SoapFault#addFaultDetail()
91       */
92      public void setAddValidationErrorDetail(boolean addValidationErrorDetail) {
93          this.addValidationErrorDetail = addValidationErrorDetail;
94      }
95  
96      /** Returns the fault detail element name when validation errors occur on the request. */
97      public QName getDetailElementName() {
98          return detailElementName;
99      }
100 
101     /**
102      * Sets the fault detail element name when validation errors occur on the request. Defaults to
103      * <code>DEFAULT_DETAIL_ELEMENT_NAME</code>.
104      *
105      * @see #DEFAULT_DETAIL_ELEMENT_NAME
106      */
107     public void setDetailElementName(QName detailElementName) {
108         this.detailElementName = detailElementName;
109     }
110 
111     /** Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request. */
112     public String getFaultStringOrReason() {
113         return faultStringOrReason;
114     }
115 
116     /**
117      * Sets the SOAP <code>faultstring</code> or <code>Reason</code> used when validation errors occur on the request.
118      * It is only added when the underlying message is a <code>SoapMessage</code>. Defaults to
119      * <code>DEFAULT_FAULTSTRING_OR_REASON</code>.
120      *
121      * @see #DEFAULT_FAULTSTRING_OR_REASON
122      */
123     public void setFaultStringOrReason(String faultStringOrReason) {
124         this.faultStringOrReason = faultStringOrReason;
125     }
126 
127     /** Returns the SOAP fault reason locale used when validation errors occur on the request. */
128     public Locale getFaultStringOrReasonLocale() {
129         return faultStringOrReasonLocale;
130     }
131 
132     /**
133      * Sets the SOAP fault reason locale used when validation errors occur on the request.  It is only added when the
134      * underlying message is a <code>SoapMessage</code>. Defaults to English.
135      *
136      * @see java.util.Locale#ENGLISH
137      */
138     public void setFaultStringOrReasonLocale(Locale faultStringOrReasonLocale) {
139         this.faultStringOrReasonLocale = faultStringOrReasonLocale;
140     }
141 
142     /**
143      * Template method that is called when the request message contains validation errors. This implementation logs all
144      * errors, returns <code>false</code>, and creates a [email protected] SoapBody#addClientOrSenderFault(String,Locale) client or
145      * sender} [email protected] SoapFault}, adding a [email protected] SoapFaultDetail} with all errors if the
146      * <code>addValidationErrorDetail</code> property is <code>true</code>.
147      *
148      * @param messageContext the message context
149      * @param errors         the validation errors
150      * @return <code>true</code> to continue processing the request, <code>false</code> (the default) otherwise
151      */
152     @Override
153     protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors)
154             throws TransformerException {
155         for (SAXParseException error : errors) {
156             logger.warn("XML validation error on request: " + error.getMessage());
157         }
158         if (messageContext.getResponse() instanceof SoapMessage) {
159             SoapMessage response = (SoapMessage) messageContext.getResponse();
160             SoapBody body = response.getSoapBody();
161             SoapFault fault = body.addClientOrSenderFault(getFaultStringOrReason(), getFaultStringOrReasonLocale());
162             if (getAddValidationErrorDetail()) {
163                 SoapFaultDetail detail = fault.addFaultDetail();
164                 for (SAXParseException error : errors) {
165                     SoapFaultDetailElement detailElement = detail.addFaultDetailElement(getDetailElementName());
166                     detailElement.addText(error.getMessage());
167                 }
168             }
169         }
170         return false;
171     }
172 }