Search in sources :

Example 1 with MultiPartRequestWrapper

use of org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper 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)

Example 2 with MultiPartRequestWrapper

use of org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper in project struts by apache.

the class Dispatcher method cleanUpRequest.

/**
 * Removes all the files created by MultiPartRequestWrapper.
 *
 * @param request the HttpServletRequest object.
 * @see org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper
 */
public void cleanUpRequest(HttpServletRequest request) {
    ContainerHolder.clear();
    if (!(request instanceof MultiPartRequestWrapper)) {
        return;
    }
    MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;
    multiWrapper.cleanUp();
}
Also used : MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper)

Example 3 with MultiPartRequestWrapper

use of org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper in project struts by apache.

the class FileUploadInterceptorTest method createMultipartRequest.

private MultiPartRequestWrapper createMultipartRequest(HttpServletRequest req, int maxsize) throws IOException {
    JakartaMultiPartRequest jak = new JakartaMultiPartRequest();
    jak.setMaxSize(String.valueOf(maxsize));
    return new MultiPartRequestWrapper(jak, req, tempDir.getAbsolutePath(), new DefaultLocaleProvider());
}
Also used : DefaultLocaleProvider(com.opensymphony.xwork2.DefaultLocaleProvider) JakartaMultiPartRequest(org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest) MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper)

Example 4 with MultiPartRequestWrapper

use of org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper in project struts by apache.

the class DispatcherTest method testPrepareMultipartRequest.

public void testPrepareMultipartRequest() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    req.setMethod("post");
    req.setContentType("multipart/form-data; boundary=asdcvb345asd");
    Dispatcher du = initDispatcher(Collections.<String, String>emptyMap());
    du.prepare(req, res);
    HttpServletRequest wrapped = du.wrapRequest(req);
    assertTrue(wrapped instanceof MultiPartRequestWrapper);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 5 with MultiPartRequestWrapper

use of org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper in project struts by apache.

the class DispatcherTest method testPrepareMultipartRequestAllAllowedCharacters.

public void testPrepareMultipartRequestAllAllowedCharacters() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    req.setMethod("post");
    req.setContentType("multipart/form-data; boundary=01=23a.bC:D((e)d'z?p+o_r,e-");
    Dispatcher du = initDispatcher(Collections.<String, String>emptyMap());
    du.prepare(req, res);
    HttpServletRequest wrapped = du.wrapRequest(req);
    assertTrue(wrapped instanceof MultiPartRequestWrapper);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MultiPartRequestWrapper(org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MultiPartRequestWrapper (org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ActionProxy (com.opensymphony.xwork2.ActionProxy)1 DefaultLocaleProvider (com.opensymphony.xwork2.DefaultLocaleProvider)1 LocaleProviderFactory (com.opensymphony.xwork2.LocaleProviderFactory)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)1 IOException (java.io.IOException)1 PortletException (javax.portlet.PortletException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 StrutsException (org.apache.struts2.StrutsException)1 LocalizedMessage (org.apache.struts2.dispatcher.LocalizedMessage)1 Parameter (org.apache.struts2.dispatcher.Parameter)1 ActionMapping (org.apache.struts2.dispatcher.mapper.ActionMapping)1 JakartaMultiPartRequest (org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest)1 MultiPartRequest (org.apache.struts2.dispatcher.multipart.MultiPartRequest)1 UploadedFile (org.apache.struts2.dispatcher.multipart.UploadedFile)1 PortletServletRequest (org.apache.struts2.portlet.servlet.PortletServletRequest)1