Search in sources :

Example 11 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testSimpleField.

public void testSimpleField() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleField", null, null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 12 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testMemberObject.

public void testMemberObject() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "memberObject", null, null);
    MemberObject action = (MemberObject) baseActionProxy.getAction();
    action.getPerson().setName(null);
    action.getPerson().setEmail(null);
    action.getPerson().getAddress().setStreet("short");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(3, fieldErrors.size());
    assertValue(fieldErrors, "person.name", Collections.singletonList("person.name cannot be null"));
    assertValue(fieldErrors, "person.email", Collections.singletonList("person.email cannot be null"));
    assertValue(fieldErrors, "person.address.street", Collections.singletonList("person.address.street cannot be shorter than 7 characters"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 13 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testProgrammaticValidationDontInvokeValidate.

public void testProgrammaticValidationDontInvokeValidate() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldNoValidate", null, null);
    SimpleField action = (SimpleField) baseActionProxy.getAction();
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
    assertFalse(action.isValidateCalled());
    assertTrue(action.isValidateExecuteCalled());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 14 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testValidationInMethods.

public void testValidationInMethods() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "validationInMethods", null, null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(4, fieldErrors.size());
    assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
    assertValue(fieldErrors, "SisyphusHasTheAnswer", Collections.singletonList("SisyphusHasTheAnswer cannot be null"));
    assertValue(fieldErrors, "thereAnyMeaningInLife", Collections.singletonList("thereAnyMeaningInLife cannot be null"));
    assertValue(fieldErrors, "theMeaningOfLife", Collections.singletonList("theMeaningOfLife cannot be null"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 15 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class FileUploadInterceptor method intercept.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
     */
public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext ac = invocation.getInvocationContext();
    HttpServletRequest request = ac.getServletRequest();
    if (!(request instanceof MultiPartRequestWrapper)) {
        if (LOG.isDebugEnabled()) {
            ActionProxy proxy = invocation.getProxy();
            LOG.debug(getTextMessage("struts.messages.bypass.request", new String[] { proxy.getNamespace(), proxy.getActionName() }));
        }
        return invocation.invoke();
    }
    ValidationAware validation = null;
    Object action = invocation.getAction();
    if (action instanceof ValidationAware) {
        validation = (ValidationAware) action;
    }
    MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
    if (multiWrapper.hasErrors() && validation != null) {
        TextProvider textProvider = getTextProvider(action);
        for (LocalizedMessage error : multiWrapper.getErrors()) {
            String errorMessage;
            if (textProvider.hasKey(error.getTextKey())) {
                errorMessage = textProvider.getText(error.getTextKey(), Arrays.asList(error.getArgs()));
            } else {
                errorMessage = textProvider.getText("struts.messages.error.uploading", error.getDefaultMessage());
            }
            validation.addActionError(errorMessage);
        }
    }
    // bind allowed Files
    Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
    while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
        // get the value of this input tag
        String inputName = (String) fileParameterNames.nextElement();
        // get the content type
        String[] contentType = multiWrapper.getContentTypes(inputName);
        if (isNonEmpty(contentType)) {
            // get the name of the file from the input tag
            String[] fileName = multiWrapper.getFileNames(inputName);
            if (isNonEmpty(fileName)) {
                // get a File object for the uploaded File
                UploadedFile[] files = multiWrapper.getFiles(inputName);
                if (files != null && files.length > 0) {
                    List<UploadedFile> acceptedFiles = new ArrayList<>(files.length);
                    List<String> acceptedContentTypes = new ArrayList<>(files.length);
                    List<String> acceptedFileNames = new ArrayList<>(files.length);
                    String contentTypeName = inputName + "ContentType";
                    String fileNameName = inputName + "FileName";
                    for (int index = 0; index < files.length; index++) {
                        if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation)) {
                            acceptedFiles.add(files[index]);
                            acceptedContentTypes.add(contentType[index]);
                            acceptedFileNames.add(fileName[index]);
                        }
                    }
                    if (!acceptedFiles.isEmpty()) {
                        Map<String, Parameter> newParams = new HashMap<>();
                        newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new UploadedFile[acceptedFiles.size()])));
                        newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])));
                        newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])));
                        ac.getParameters().appendAll(newParams);
                    }
                }
            } else {
                if (LOG.isWarnEnabled()) {
                    LOG.warn(getTextMessage(action, "struts.messages.invalid.file", new String[] { inputName }));
                }
            }
        } else {
            if (LOG.isWarnEnabled()) {
                LOG.warn(getTextMessage(action, "struts.messages.invalid.content.type", new String[] { inputName }));
            }
        }
    }
    // invoke action
    return invocation.invoke();
}
Also used : MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) UploadedFile(org.apache.struts2.dispatcher.multipart.UploadedFile) Parameter(org.apache.struts2.dispatcher.Parameter) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) LocalizedMessage(org.apache.struts2.dispatcher.LocalizedMessage)

Aggregations

ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)42 ActionProxy (com.opensymphony.xwork2.ActionProxy)38 List (java.util.List)28 HashMap (java.util.HashMap)23 Map (java.util.Map)8 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 Collection (java.util.Collection)5 ModelDrivenAction (org.apache.struts.beanvalidation.actions.ModelDrivenAction)5 ActionContext (com.opensymphony.xwork2.ActionContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ValidateGroupAction (org.apache.struts.beanvalidation.actions.ValidateGroupAction)2 HttpParameters (org.apache.struts2.dispatcher.HttpParameters)2 Parameter (org.apache.struts2.dispatcher.Parameter)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1