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);
}
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;
}
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);
}
}
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);
}
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);
}
}
Aggregations