Search in sources :

Example 1 with MultipartException

use of org.springframework.web.multipart.MultipartException in project spring-framework by spring-projects.

the class CommonsMultipartResolver method parseRequest.

/**
	 * Parse the given servlet request, resolving its multipart elements.
	 * @param request the request to parse
	 * @return the parsing result
	 * @throws MultipartException if multipart resolution failed.
	 */
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
    String encoding = determineEncoding(request);
    FileUpload fileUpload = prepareFileUpload(encoding);
    try {
        List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
        return parseFileItems(fileItems, encoding);
    } catch (FileUploadBase.SizeLimitExceededException ex) {
        throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
    } catch (FileUploadBase.FileSizeLimitExceededException ex) {
        throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
    } catch (FileUploadException ex) {
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) MaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUploadBase(org.apache.commons.fileupload.FileUploadBase) MultipartException(org.springframework.web.multipart.MultipartException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileUpload(org.apache.commons.fileupload.FileUpload) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 2 with MultipartException

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

the class RsExceptionResolver method resolveException.

@Override
public ModelAndView resolveException(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception exception) {
    try {
        if (exception instanceof PageNotFoundException) {
            final PageNotFoundException pageNotFound = (PageNotFoundException) exception;
            response.sendError(HttpServletResponse.SC_NOT_FOUND, pageNotFound.getMessage());
            return new ModelAndView();
        } else if (exception instanceof IllegalArgumentException) {
            final IllegalArgumentException illegalArgument = (IllegalArgumentException) exception;
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, illegalArgument.getMessage());
            return new ModelAndView();
        } else if (exception instanceof RedirectException) {
            final RedirectException redirect = (RedirectException) exception;
            response.sendRedirect(redirect.getUrl());
            return new ModelAndView();
        } else if (exception instanceof MultipartException) {
            final MultipartException multiPartException = (MultipartException) exception;
            Throwable cause = multiPartException.getCause();
            if (cause == null) {
                cause = exception;
            }
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid HTTP multi-part request: " + cause.getMessage());
            return new ModelAndView();
        }
    } catch (final IOException e) {
    }
    return null;
}
Also used : MultipartException(org.springframework.web.multipart.MultipartException) ModelAndView(org.springframework.web.servlet.ModelAndView) IOException(java.io.IOException)

Example 3 with MultipartException

use of org.springframework.web.multipart.MultipartException in project mecaworks by KadarH.

the class ErrorHandler method handle.

@ExceptionHandler(value = { ApplicationException.class, MultipartException.class })
public ModelAndView handle(HttpServletRequest request, Exception e) {
    /*if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
			throw e;*/
    ModelAndView mav = new ModelAndView();
    if (e instanceof MultipartException) {
        mav.addObject("exception", new Exception("Image upload échouée, vérifier la taille du fichier (max = 5MB)", e));
    } else {
        mav.addObject("exception", e);
    }
    mav.setViewName("error/exception");
    log.warn("Application exception thrown - url : [" + request.getMethod() + "]" + request.getRequestURI(), e);
    return mav;
}
Also used : MultipartException(org.springframework.web.multipart.MultipartException) ModelAndView(org.springframework.web.servlet.ModelAndView) MultipartException(org.springframework.web.multipart.MultipartException) ApplicationException(me.kadarh.mecaworks.service.exceptions.ApplicationException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 4 with MultipartException

use of org.springframework.web.multipart.MultipartException in project ngtesting-platform by aaronchen2k.

the class PJCommonsMultipartResolver method parseRequest.

@Override
public MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
    String encoding = "utf-8";
    FileUpload fileUpload = prepareFileUpload(encoding);
    PJProgressListener uploadProgressListener = new PJProgressListener(request);
    fileUpload.setProgressListener(uploadProgressListener);
    try {
        List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
        return parseFileItems(fileItems, encoding);
    } catch (FileUploadBase.SizeLimitExceededException ex) {
        throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
    } catch (FileUploadException ex) {
        throw new MultipartException("Could not parse multipart servlet request", ex);
    }
}
Also used : MaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) MultipartException(org.springframework.web.multipart.MultipartException) PJProgressListener(com.ngtesting.platform.servlet.PJProgressListener) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload)

Example 5 with MultipartException

use of org.springframework.web.multipart.MultipartException in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolver method resolveArgument.

@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    Assert.state(servletRequest != null, "No HttpServletRequest");
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
    String name = getPartName(parameter, requestPart);
    parameter = parameter.nestedIfOptional();
    Object arg = null;
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            if (binderFactory != null) {
                WebDataBinder binder = binderFactory.createBinder(request, arg, name);
                if (arg != null) {
                    validateIfApplicable(binder, parameter);
                    if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                        throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                    }
                }
                if (mavContainer != null) {
                    mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
                }
            }
        } catch (MissingServletRequestPartException | MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingServletRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpInputMessage(org.springframework.http.HttpInputMessage) WebDataBinder(org.springframework.web.bind.WebDataBinder) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) RequestPart(org.springframework.web.bind.annotation.RequestPart) MultipartException(org.springframework.web.multipart.MultipartException) RequestPartServletServerHttpRequest(org.springframework.web.multipart.support.RequestPartServletServerHttpRequest) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Nullable(org.springframework.lang.Nullable)

Aggregations

MultipartException (org.springframework.web.multipart.MultipartException)5 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 MaxUploadSizeExceededException (org.springframework.web.multipart.MaxUploadSizeExceededException)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 PJProgressListener (com.ngtesting.platform.servlet.PJProgressListener)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 IOException (java.io.IOException)1 ApplicationException (me.kadarh.mecaworks.service.exceptions.ApplicationException)1 FileItem (org.apache.commons.fileupload.FileItem)1 FileUpload (org.apache.commons.fileupload.FileUpload)1 FileUploadBase (org.apache.commons.fileupload.FileUploadBase)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 Nullable (org.springframework.lang.Nullable)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1 WebDataBinder (org.springframework.web.bind.WebDataBinder)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 RequestPart (org.springframework.web.bind.annotation.RequestPart)1 MissingServletRequestPartException (org.springframework.web.multipart.support.MissingServletRequestPartException)1 RequestPartServletServerHttpRequest (org.springframework.web.multipart.support.RequestPartServletServerHttpRequest)1