use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class RepopulateConversionErrorFieldValidatorSupport method repopulateField.
public void repopulateField(Object object) throws ValidationException {
ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
String fieldName = getFieldName();
String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
if (conversionErrors.containsKey(fullFieldName)) {
Object value = conversionErrors.get(fullFieldName).getValue();
final Map<Object, Object> fakeParams = new LinkedHashMap<Object, Object>();
boolean doExprOverride = false;
if (value instanceof String[]) {
// take the first element, if possible
String[] tmpValue = (String[]) value;
if ((tmpValue.length > 0)) {
doExprOverride = true;
fakeParams.put(fullFieldName, escape(tmpValue[0]));
} else {
LOG.warn("value is an empty array of String or with first element in it as null [{}], will not repopulate conversion error", value);
}
} else if (value instanceof String) {
String tmpValue = (String) value;
doExprOverride = true;
fakeParams.put(fullFieldName, escape(tmpValue));
} else {
// opps... it should be
LOG.warn("conversion error value is not a String or array of String but instead is [{}], will not repopulate conversion error", value);
}
if (doExprOverride) {
invocation.addPreResultListener(new PreResultListener() {
public void beforeResult(ActionInvocation invocation, String resultCode) {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.setExprOverrides(fakeParams);
}
});
}
}
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class ConversionErrorFieldValidator method doValidate.
/**
* The validation implementation must guarantee that setValidatorContext will
* be called with a non-null ValidatorContext before validate is called.
*
* @param object the object to be validated
* @throws ValidationException in case of validation problems
*/
@Override
public void doValidate(Object object) throws ValidationException {
String fieldName = getFieldName();
String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
ActionContext context = ActionContext.getContext();
Map<String, ConversionData> conversionErrors = context.getConversionErrors();
if (conversionErrors.containsKey(fullFieldName)) {
if (StringUtils.isBlank(defaultMessage)) {
defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, conversionErrors.get(fullFieldName).getToClass(), context.getValueStack());
}
addFieldError(fieldName, object);
}
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class VisitorFieldValidator method validate.
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
Object value = this.getFieldValue(fieldName, object);
if (value == null) {
LOG.warn("The visited object is null, VisitorValidator will not be able to handle validation properly. Please make sure the visited object is not null for VisitorValidator to function properly");
return;
}
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(object);
String visitorContext = (context == null) ? ActionContext.getContext().getName() : context;
if (value instanceof Collection) {
Collection coll = (Collection) value;
Object[] array = coll.toArray();
validateArrayElements(array, fieldName, visitorContext);
} else if (value instanceof Object[]) {
Object[] array = (Object[]) value;
validateArrayElements(array, fieldName, visitorContext);
} else {
validateObject(fieldName, value, visitorContext);
}
stack.pop();
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class ExceptionMappingInterceptorTest method testThrownExceptionMatching2.
public void testThrownExceptionMatching2() throws Exception {
this.setUpWithExceptionMappings();
Mock action = new Mock(Action.class);
Exception exception = new ValidationException("test");
mockInvocation.expectAndThrow("invoke", exception);
mockInvocation.matchAndReturn("getAction", action.proxy());
String result = interceptor.intercept(invocation);
assertNotNull(stack.findValue("exception"));
assertEquals(stack.findValue("exception"), exception);
assertEquals(result, "throwable");
}
Aggregations