1 /*
2 * Copyright 2007 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.server.endpoint;
18
19 import org.springframework.validation.BindException;
20 import org.springframework.validation.Errors;
21 import org.springframework.validation.ValidationUtils;
22 import org.springframework.validation.Validator;
23 import org.springframework.ws.context.MessageContext;
24
25 /**
26 * Extension of the {@link AbstractMarshallingPayloadEndpoint} which validates the request payload with {@link
27 * Validator}(s). The desired validators can be set using properties, and <strong>must</strong> {@link
28 * Validator#supports(Class) support} the request object.
29 *
30 * @author Arjen Poutsma
31 * @since 1.0.2
32 */
33 public abstract class AbstractValidatingMarshallingPayloadEndpoint extends AbstractMarshallingPayloadEndpoint {
34
35 /** Default request object name used for validating request objects. */
36 public static final String DEFAULT_REQUEST_NAME = "request";
37
38 private String requestName = DEFAULT_REQUEST_NAME;
39
40 private Validator[] validators;
41
42 /** Return the name of the request object for validation error codes. */
43 public String getRequestName() {
44 return requestName;
45 }
46
47 /** Set the name of the request object user for validation errors. */
48 public void setRequestName(String requestName) {
49 this.requestName = requestName;
50 }
51
52 /** Return the primary Validator for this controller. */
53 public Validator getValidator() {
54 Validator[] validators = getValidators();
55 return (validators != null && validators.length > 0 ? validators[0] : null);
56 }
57
58 /**
59 * Set the primary {@link Validator} for this endpoint. The {@link Validator} is must support the unmarshalled
60 * class. If there are one or more existing validators set already when this method is called, only the specified
61 * validator will be kept. Use {@link #setValidators(Validator[])} to set multiple validators.
62 */
63 public void setValidator(Validator validator) {
64 this.validators = new Validator[]{validator};
65 }
66
67 /** Return the Validators for this controller. */
68 public Validator[] getValidators() {
69 return validators;
70 }
71
72 /** Set the Validators for this controller. The Validator must support the specified command class. */
73 public void setValidators(Validator[] validators) {
74 this.validators = validators;
75 }
76
77 protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception {
78 Validator[] validators = getValidators();
79 if (validators != null) {
80 Errors errors = new BindException(requestObject, getRequestName());
81 for (int i = 0; i < validators.length; i++) {
82 ValidationUtils.invokeValidator(validators[i], requestObject, errors);
83 }
84 if (errors.hasErrors()) {
85 return onValidationErrors(messageContext, requestObject, errors);
86 }
87 }
88 return true;
89 }
90
91 /**
92 * Callback for post-processing validation errors. Called when validator(s) have been specified, and validation
93 * fails.
94 *
95 * @param messageContext the message context
96 * @param requestObject the object unmarshalled from the {@link MessageContext#getRequest() request}
97 * @param errors validation errors holder
98 * @return <code>true</code> to continue and call {@link #invokeInternal(Object)}; <code>false</code> otherwise
99 */
100 protected abstract boolean onValidationErrors(MessageContext messageContext, Object requestObject, Errors errors);
101 }