1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.server.endpoint;
18
19 import java.util.Iterator;
20 import java.util.Locale;
21 import javax.xml.namespace.QName;
22
23 import org.springframework.context.MessageSource;
24 import org.springframework.context.MessageSourceAware;
25 import org.springframework.validation.Errors;
26 import org.springframework.validation.ObjectError;
27 import org.springframework.validation.Validator;
28 import org.springframework.ws.context.MessageContext;
29 import org.springframework.ws.server.endpoint.AbstractValidatingMarshallingPayloadEndpoint;
30 import org.springframework.ws.soap.SoapBody;
31 import org.springframework.ws.soap.SoapFault;
32 import org.springframework.ws.soap.SoapFaultDetail;
33 import org.springframework.ws.soap.SoapFaultDetailElement;
34 import org.springframework.ws.soap.SoapMessage;
35 import org.springframework.xml.namespace.QNameUtils;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public abstract class AbstractFaultCreatingValidatingMarshallingPayloadEndpoint
50 extends AbstractValidatingMarshallingPayloadEndpoint implements MessageSourceAware {
51
52
53
54
55
56
57 public static final QName DEFAULT_DETAIL_ELEMENT_NAME =
58 QNameUtils.createQName("http://springframework.org/spring-ws", "ValidationError", "spring-ws");
59
60
61
62
63
64
65 public static final String DEFAULT_FAULTSTRING_OR_REASON = "Validation error";
66
67 private boolean addValidationErrorDetail = true;
68
69 private QName detailElementName = DEFAULT_DETAIL_ELEMENT_NAME;
70
71 private String faultStringOrReason = DEFAULT_FAULTSTRING_OR_REASON;
72
73 private Locale faultStringOrReasonLocale = Locale.ENGLISH;
74
75 private MessageSource messageSource;
76
77
78
79
80
81
82
83
84 public boolean getAddValidationErrorDetail() {
85 return addValidationErrorDetail;
86 }
87
88
89
90
91
92
93
94
95 public void setAddValidationErrorDetail(boolean addValidationErrorDetail) {
96 this.addValidationErrorDetail = addValidationErrorDetail;
97 }
98
99
100 public QName getDetailElementName() {
101 return detailElementName;
102 }
103
104
105
106
107
108
109
110 public void setDetailElementName(QName detailElementName) {
111 this.detailElementName = detailElementName;
112 }
113
114
115 public String getFaultStringOrReason() {
116 return faultStringOrReason;
117 }
118
119
120
121
122
123
124
125
126 public void setFaultStringOrReason(String faultStringOrReason) {
127 this.faultStringOrReason = faultStringOrReason;
128 }
129
130
131 public Locale getFaultLocale() {
132 return faultStringOrReasonLocale;
133 }
134
135
136
137
138
139
140
141 public void setFaultStringOrReasonLocale(Locale faultStringOrReasonLocale) {
142 this.faultStringOrReasonLocale = faultStringOrReasonLocale;
143 }
144
145 public final void setMessageSource(MessageSource messageSource) {
146 this.messageSource = messageSource;
147 }
148
149
150
151
152
153
154
155
156
157
158
159 protected final boolean onValidationErrors(MessageContext messageContext, Object requestObject, Errors errors) {
160 for (Iterator iterator = errors.getAllErrors().iterator(); iterator.hasNext();) {
161 ObjectError objectError = (ObjectError) iterator.next();
162 String msg = messageSource.getMessage(objectError, getFaultLocale());
163 logger.warn("Validation error on request object[" + requestObject + "]: " + msg);
164 }
165 if (messageContext.getResponse() instanceof SoapMessage) {
166 SoapMessage response = (SoapMessage) messageContext.getResponse();
167 SoapBody body = response.getSoapBody();
168 SoapFault fault = body.addClientOrSenderFault(getFaultStringOrReason(), getFaultLocale());
169 if (getAddValidationErrorDetail()) {
170 SoapFaultDetail detail = fault.addFaultDetail();
171 for (Iterator iterator = errors.getAllErrors().iterator(); iterator.hasNext();) {
172 ObjectError objectError = (ObjectError) iterator.next();
173 String msg = messageSource.getMessage(objectError, getFaultLocale());
174 SoapFaultDetailElement detailElement = detail.addFaultDetailElement(getDetailElementName());
175 detailElement.addText(msg);
176 }
177 }
178 }
179 return false;
180 }
181 }