Search in sources :

Example 11 with ValidationException

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);
                }
            });
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with ValidationException

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);
    }
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 13 with ValidationException

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();
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) Collection(java.util.Collection)

Example 14 with ValidationException

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");
}
Also used : ValidationException(com.opensymphony.xwork2.validator.ValidationException) Mock(com.mockobjects.dynamic.Mock) StrutsException(org.apache.struts2.StrutsException) ValidationException(com.opensymphony.xwork2.validator.ValidationException)

Aggregations

List (java.util.List)6 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)3 ValueStack (com.opensymphony.xwork2.util.ValueStack)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 Mock (com.mockobjects.dynamic.Mock)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 FileManager (com.opensymphony.xwork2.FileManager)1 FileManagerFactory (com.opensymphony.xwork2.FileManagerFactory)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 SimpleAnnotationAction (com.opensymphony.xwork2.SimpleAnnotationAction)1 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 OgnlValueStack (com.opensymphony.xwork2.ognl.OgnlValueStack)1 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)1 ValidationException (com.opensymphony.xwork2.validator.ValidationException)1 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)1 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)1