|
| 1 | +package com.example.arInfra.endpoint.rest.controller; |
| 2 | + |
| 3 | +import static java.lang.String.format; |
| 4 | +import static org.owasp.encoder.Encode.forJava; |
| 5 | + |
| 6 | +import com.example.arInfra.InfraGenerated; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ProblemDetail; |
| 11 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 12 | +import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 13 | +import org.springframework.web.multipart.MaxUploadSizeExceededException; |
| 14 | +import org.springframework.web.multipart.MultipartException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Global exception handler for multipart file upload errors. |
| 18 | + * |
| 19 | + * <p>Provides secure error handling without exposing sensitive system information. |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +@InfraGenerated |
| 23 | +@RestControllerAdvice |
| 24 | +public class MultipartExceptionHandler { |
| 25 | + |
| 26 | + @Value("${spring.servlet.multipart.max-file-size:10MB}") |
| 27 | + private String maxFileSize; |
| 28 | + |
| 29 | + @Value("${spring.servlet.multipart.max-request-size:10MB}") |
| 30 | + private String maxRequestSize; |
| 31 | + |
| 32 | + /** |
| 33 | + * Handles file size exceeded exceptions with user-friendly messages. |
| 34 | + * |
| 35 | + * @param ex the exception |
| 36 | + * @return problem detail response |
| 37 | + */ |
| 38 | + @ExceptionHandler(MaxUploadSizeExceededException.class) |
| 39 | + public ProblemDetail handleMaxUploadSizeExceeded(MaxUploadSizeExceededException ex) { |
| 40 | + log.warn("File upload size limit exceeded", ex); |
| 41 | + |
| 42 | + ProblemDetail problemDetail = |
| 43 | + ProblemDetail.forStatusAndDetail( |
| 44 | + HttpStatus.PAYLOAD_TOO_LARGE, |
| 45 | + format( |
| 46 | + "Upload size exceeds the maximum allowed. Max file size: %s, Max request size: %s", |
| 47 | + forJava(maxFileSize), forJava(maxRequestSize))); |
| 48 | + |
| 49 | + problemDetail.setTitle("File Size Limit Exceeded"); |
| 50 | + problemDetail.setProperty("maxFileSize", maxFileSize); |
| 51 | + problemDetail.setProperty("maxRequestSize", maxRequestSize); |
| 52 | + |
| 53 | + return problemDetail; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Handles general multipart exceptions. |
| 58 | + * |
| 59 | + * @param ex the exception |
| 60 | + * @return problem detail response |
| 61 | + */ |
| 62 | + @ExceptionHandler(MultipartException.class) |
| 63 | + public ProblemDetail handleMultipartException(MultipartException ex) { |
| 64 | + log.error("Multipart request processing failed", ex); |
| 65 | + |
| 66 | + ProblemDetail problemDetail = |
| 67 | + ProblemDetail.forStatusAndDetail( |
| 68 | + HttpStatus.BAD_REQUEST, |
| 69 | + "Failed to process multipart request. Please ensure your file meets the requirements."); |
| 70 | + |
| 71 | + problemDetail.setTitle("Multipart Request Error"); |
| 72 | + |
| 73 | + return problemDetail; |
| 74 | + } |
| 75 | +} |
0 commit comments