Java Code Examples for org.springframework.web.context.request.ServletWebRequest#getNativeRequest()

The following examples show how to use org.springframework.web.context.request.ServletWebRequest#getNativeRequest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResponseEntityExceptionHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Customize the response for NoHandlerFoundException.
 * <p>This method delegates to {@link #handleExceptionInternal}.
 * @param ex the exception
 * @param headers the headers to be written to the response
 * @param status the selected response status
 * @param webRequest the current request
 * @return a {@code ResponseEntity} instance
 * @since 4.2.8
 */
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletRequest = (ServletWebRequest) webRequest;
		HttpServletRequest request = servletRequest.getNativeRequest(HttpServletRequest.class);
		HttpServletResponse response = servletRequest.getNativeResponse(HttpServletResponse.class);
		if (response.isCommitted()) {
			if (logger.isErrorEnabled()) {
				logger.error("Async timeout for " + request.getMethod() + " [" + request.getRequestURI() + "]");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 2
Source File: ServletInvocableHandlerMethod.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void disableContentCachingIfNecessary(ServletWebRequest webRequest) {
	if (!isRequestNotModified(webRequest)) {
		HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
		Assert.notNull(response, "Expected HttpServletResponse");
		if (StringUtils.hasText(response.getHeader(HttpHeaders.ETAG))) {
			HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
			Assert.notNull(request, "Expected HttpServletRequest");
			ShallowEtagHeaderFilter.disableContentCaching(request);
		}
	}
}
 
Example 3
Source File: ExceptionHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
 */
@Override
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request,
		HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {

	ServletInvocableHandlerMethod exceptionHandlerMethod = getExceptionHandlerMethod(handlerMethod, exception);
	if (exceptionHandlerMethod == null) {
		return null;
	}

	exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
	exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);

	ServletWebRequest webRequest = new ServletWebRequest(request, response);
	ModelAndViewContainer mavContainer = new ModelAndViewContainer();

	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Invoking @ExceptionHandler method: " + exceptionHandlerMethod);
		}
		Throwable cause = exception.getCause();
		if (cause != null) {
			// Expose cause as provided argument as well
			exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod);
		}
		else {
			// Otherwise, just the given exception as-is
			exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
		}
	}
	catch (Throwable invocationEx) {
		// Any other than the original exception is unintended here,
		// probably an accident (e.g. failed assertion or the like).
		if (invocationEx != exception && logger.isWarnEnabled()) {
			logger.warn("Failed to invoke @ExceptionHandler method: " + exceptionHandlerMethod, invocationEx);
		}
		// Continue with default processing of the original exception...
		return null;
	}

	if (mavContainer.isRequestHandled()) {
		return new ModelAndView();
	}
	else {
		ModelMap model = mavContainer.getModel();
		HttpStatus status = mavContainer.getStatus();
		ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
		mav.setViewName(mavContainer.getViewName());
		if (!mavContainer.isViewReference()) {
			mav.setView((View) mavContainer.getView());
		}
		if (model instanceof RedirectAttributes) {
			Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
			request = webRequest.getNativeRequest(HttpServletRequest.class);
			RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
		}
		return mav;
	}
}