/** * Creates a model and view to transport the error message * * @param request the current HTTP request * @param response the current HTTP response * @param view the name of the view to use, can be null if the request should be handled as Ajax * upload or there is no mapping for the request and the default non-ajax upload view should * be used * @return the created model and view or null if the response was written as JSON */ private ModelAndView prepareAjaxModelAndView( HttpServletRequest request, HttpServletResponse response, String errorMessage) { ObjectNode jsonResponse = JsonRequestHelper.createJsonErrorResponse(errorMessage); try { // Note: the JSON cannot be written directly to the response since this is not supported // by exception resolvers. Main problem is that some headers like encoding won't be set // correctly and response is written twice return ControllerHelper.prepareModelAndViewForJsonResponse( request, response, jsonResponse, false); } catch (IOException e) { // should not occur because response is not written directly LOGGER.error("Unexpected exception handling MaxUploadSizeExceededException", e); return null; } }
/** * Implementation of the exception resolving method which will only handle the {@link * MaxUploadSizeExceededException}. * * @param request the current HTTP request * @param response the response * @param handler the executed handler, which is null in case of an MaxUploadSizeExceededException * @param ex the exception that got thrown * @return the model and view or null if the exception is not a MaxUploadSizeExceededException or * no view is defined */ @Override public ModelAndView resolveException( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { if (ex instanceof MaxUploadSizeExceededException) { String errorMessage = MessageHelper.getText( request, "error.blogpost.upload.filesize.limit", new Object[] {getHumanReadableUploadSizeLimit()}); String targetPath = ClientUrlHelper.removeIds(request.getServletPath() + request.getPathInfo()); String view = null; if (requestMappings != null) { view = requestMappings.get(targetPath); } if (view != null) { MessageHelper.saveErrorMessage(request, errorMessage); MessageHelper.setMessageTarget(request, targetPath); return new ModelAndView(ControllerHelper.replaceModuleInViewName(view)); } return prepareAjaxModelAndView(request, response, errorMessage); } return null; }