Search in sources :

Example 1 with DefaultMultipartHttpServletRequest

use of org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest in project com.revolsys.open by revolsys.

the class PathAliasController method forward.

public static boolean forward(final HttpServletRequest request, final HttpServletResponse response, final String path) throws IOException, ServletException {
    final RequestDispatcher requestDispatcher = request.getRequestDispatcher(path);
    if (requestDispatcher == null) {
        return false;
    } else {
        final HttpServletRequest wrappedRequest;
        if (request instanceof DefaultMultipartHttpServletRequest) {
            final DefaultMultipartHttpServletRequest multiPartRequest = (DefaultMultipartHttpServletRequest) request;
            wrappedRequest = new DefaultMultipartHttpServletRequest(multiPartRequest, multiPartRequest.getMultiFileMap(), new HashMap<String, String[]>(), new HashMap<String, String>()) {

                @Override
                public String getPathInfo() {
                    return path;
                }
            };
        } else {
            wrappedRequest = new HttpServletRequestWrapper(request) {

                @Override
                public String getPathInfo() {
                    return path;
                }
            };
        }
        final Object forwardPath = request.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
        if (forwardPath == null) {
            final String originalUri = request.getRequestURI();
            wrappedRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, originalUri);
        }
        requestDispatcher.forward(wrappedRequest, response);
        wrappedRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, forwardPath);
    }
    return true;
}
Also used : DefaultMultipartHttpServletRequest(org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) DefaultMultipartHttpServletRequest(org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 2 with DefaultMultipartHttpServletRequest

use of org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest in project Gemma by PavlidisLab.

the class CommonsMultipartMonitoredResolver method resolveMultipart.

@Override
public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException {
    String enc = determineEncoding(request);
    ServletFileUpload fileUpload = this.newFileUpload(request);
    DiskFileItemFactory newFactory = (DiskFileItemFactory) fileUpload.getFileItemFactory();
    fileUpload.setSizeMax(sizeMax);
    newFactory.setRepository(this.uploadTempDir);
    fileUpload.setHeaderEncoding(enc);
    try {
        MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
        Map<String, String[]> multipartParams = new HashMap<>();
        // Extract multipart files and multipart parameters.
        List<?> fileItems = fileUpload.parseRequest(request);
        for (Object fileItem1 : fileItems) {
            FileItem fileItem = (FileItem) fileItem1;
            if (fileItem.isFormField()) {
                String value;
                String fieldName = fileItem.getFieldName();
                try {
                    value = fileItem.getString(enc);
                } catch (UnsupportedEncodingException ex) {
                    logger.warn("Could not decode multipart item '" + fieldName + "' with encoding '" + enc + "': using platform default");
                    value = fileItem.getString();
                }
                String[] curParam = multipartParams.get(fieldName);
                if (curParam == null) {
                    // simple form field
                    multipartParams.put(fieldName, new String[] { value });
                } else {
                    // array of simple form fields
                    String[] newParam = StringUtils.addStringToArray(curParam, value);
                    multipartParams.put(fieldName, newParam);
                }
            } else {
                // multipart file field
                MultipartFile file = new CommonsMultipartFile(fileItem);
                multipartFiles.set(file.getName(), file);
                if (logger.isDebugEnabled()) {
                    logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize() + " bytes with original filename [" + file.getOriginalFilename() + "]");
                }
            }
        }
        return new DefaultMultipartHttpServletRequest(request, multipartFiles, multipartParams, null);
    } catch (FileUploadException ex) {
        return new FailedMultipartHttpServletRequest(request, ex.getMessage());
    }
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultMultipartHttpServletRequest(org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) FileItem(org.apache.commons.fileupload.FileItem) CommonsMultipartFile(org.springframework.web.multipart.commons.CommonsMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Aggregations

HashMap (java.util.HashMap)2 DefaultMultipartHttpServletRequest (org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 CommonsMultipartFile (org.springframework.web.multipart.commons.CommonsMultipartFile)1