Search in sources :

Example 1 with MultipartException

use of cn.taketoday.web.bind.MultipartException in project today-infrastructure by TAKETODAY.

the class RequestPartMethodArgumentResolver method resolveArgument.

@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter parameter = resolvable.getParameter();
    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, context);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            var inputMessage = new RequestPartServletServerHttpRequest(context, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            BindingContext binderFactory = context.getBindingContext();
            if (binderFactory != null) {
                WebDataBinder binder = binderFactory.createBinder(context, arg, name);
                if (arg != null) {
                    validateIfApplicable(binder, parameter);
                    if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                        throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                    }
                }
                binderFactory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
            }
        } catch (MissingRequestPartException | MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!context.isMultipart()) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : BindingContext(cn.taketoday.web.BindingContext) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) RequestPart(cn.taketoday.web.annotation.RequestPart) MultipartException(cn.taketoday.web.bind.MultipartException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 2 with MultipartException

use of cn.taketoday.web.bind.MultipartException in project today-framework by TAKETODAY.

the class LightRequestContext method getRequestParts.

/**
 * @throws FileSizeExceededException
 */
private List<RequestPart> getRequestParts() {
    if (requestParts == null) {
        try {
            final long contentLength = getContentLength();
            final LightHttpConfig config = this.config;
            final MultipartConfiguration multipartConfig = config.getMultipartConfig();
            if (contentLength > multipartConfig.getMaxRequestSize().toBytes()) {
                throw new FileSizeExceededException(multipartConfig.getMaxRequestSize(), null).setActual(DataSize.ofBytes(contentLength));
            }
            final MultipartIterator multipartIterator = new MultipartIterator(request);
            final MultipartInputStream inputStream = multipartIterator.getInputStream();
            final ArrayList<RequestPart> parts = new ArrayList<>();
            while (multipartIterator.hasNext(inputStream)) {
                parts.add(multipartIterator.obtainNext(config, multipartConfig));
            }
            requestParts = parts;
        } catch (IOException e) {
            throw new MultipartException("multipart read failed", e);
        }
    }
    return requestParts;
}
Also used : MultipartConfiguration(cn.taketoday.web.multipart.MultipartConfiguration) MultipartException(cn.taketoday.web.bind.MultipartException) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with MultipartException

use of cn.taketoday.web.bind.MultipartException in project today-framework by TAKETODAY.

the class ServletMultipartRequest method parseRequest.

private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        LinkedMultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            files.add(part.getName(), new ServletPartMultipartFile(part, filename));
        }
        return files;
    } catch (IOException e) {
        throw new MultipartException("MultipartFile parsing failed.", e);
    } catch (ServletException e) {
        throw new NotMultipartRequestException("This is not a multipart request", e);
    } catch (Throwable ex) {
        String msg = ex.getMessage();
        if (msg != null && msg.contains("size") && msg.contains("exceed")) {
            throw new MaxUploadSizeExceededException(-1, ex);
        }
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Example 4 with MultipartException

use of cn.taketoday.web.bind.MultipartException in project today-framework by TAKETODAY.

the class RequestPartMethodArgumentResolver method resolveArgument.

@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    MethodParameter parameter = resolvable.getParameter();
    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, context);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            var inputMessage = new RequestPartServletServerHttpRequest(context, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            validateIfApplicable(context, parameter, arg);
        } catch (MissingRequestPartException | MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!context.isMultipart()) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : RequestPart(cn.taketoday.web.annotation.RequestPart) MultipartException(cn.taketoday.web.bind.MultipartException) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 5 with MultipartException

use of cn.taketoday.web.bind.MultipartException in project today-infrastructure by TAKETODAY.

the class ServletMultipartRequest method parseRequest.

private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        LinkedMultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            if (filename != null) {
                if (filename.startsWith("=?") && filename.endsWith("?=")) {
                    filename = MimeDelegate.decode(filename);
                }
                files.add(part.getName(), new ServletPartMultipartFile(part, filename));
            }
        }
        return files;
    } catch (IOException e) {
        throw new MultipartException("MultipartFile parsing failed.", e);
    } catch (ServletException e) {
        throw new NotMultipartRequestException("This is not a multipart request", e);
    } catch (Throwable ex) {
        String msg = ex.getMessage();
        if (msg != null && msg.contains("size") && msg.contains("exceed")) {
            throw new MaxUploadSizeExceededException(-1, ex);
        }
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Aggregations

MultipartException (cn.taketoday.web.bind.MultipartException)5 IOException (java.io.IOException)3 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MethodParameter (cn.taketoday.core.MethodParameter)2 ContentDisposition (cn.taketoday.http.ContentDisposition)2 Nullable (cn.taketoday.lang.Nullable)2 RequestPart (cn.taketoday.web.annotation.RequestPart)2 NotMultipartRequestException (cn.taketoday.web.bind.NotMultipartRequestException)2 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)2 MaxUploadSizeExceededException (cn.taketoday.web.multipart.MaxUploadSizeExceededException)2 MultipartFile (cn.taketoday.web.multipart.MultipartFile)2 ServletException (jakarta.servlet.ServletException)2 Part (jakarta.servlet.http.Part)2 FileSizeExceededException (cn.taketoday.http.FileSizeExceededException)1 BindingContext (cn.taketoday.web.BindingContext)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1 WebDataBinder (cn.taketoday.web.bind.WebDataBinder)1 MultipartConfiguration (cn.taketoday.web.multipart.MultipartConfiguration)1 ServletPartMultipartFile (cn.taketoday.web.multipart.ServletPartMultipartFile)1 ArrayList (java.util.ArrayList)1