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

The following examples show how to use org.springframework.web.context.request.ServletWebRequest#getResponse() . 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 spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Customize the response for AsyncRequestTimeoutException.
 * <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
 */
@Nullable
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
		HttpServletResponse response = servletWebRequest.getResponse();
		if (response != null && response.isCommitted()) {
			if (logger.isWarnEnabled()) {
				logger.warn("Async request timed out");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 2
Source File: ServletInvocableHandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Set the response status according to the {@link ResponseStatus} annotation.
 */
private void setResponseStatus(ServletWebRequest webRequest) throws IOException {
	HttpStatus status = getResponseStatus();
	if (status == null) {
		return;
	}

	HttpServletResponse response = webRequest.getResponse();
	if (response != null) {
		String reason = getResponseStatusReason();
		if (StringUtils.hasText(reason)) {
			response.sendError(status.value(), reason);
		}
		else {
			response.setStatus(status.value());
		}
	}

	// To be picked up by RedirectView
	webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, status);
}
 
Example 3
Source File: ResponseEntityExceptionHandler.java    From java-technology-stack with MIT License 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
 */
@Nullable
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(
		AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {

	if (webRequest instanceof ServletWebRequest) {
		ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;
		HttpServletResponse response = servletWebRequest.getResponse();
		if (response != null && response.isCommitted()) {
			if (logger.isWarnEnabled()) {
				logger.warn("Async request timed out");
			}
			return null;
		}
	}

	return handleExceptionInternal(ex, null, headers, status, webRequest);
}
 
Example 4
Source File: ServletInvocableHandlerMethod.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Set the response status according to the {@link ResponseStatus} annotation.
 */
private void setResponseStatus(ServletWebRequest webRequest) throws IOException {
	HttpStatus status = getResponseStatus();
	if (status == null) {
		return;
	}

	HttpServletResponse response = webRequest.getResponse();
	if (response != null) {
		String reason = getResponseStatusReason();
		if (StringUtils.hasText(reason)) {
			response.sendError(status.value(), reason);
		}
		else {
			response.setStatus(status.value());
		}
	}

	// To be picked up by RedirectView
	webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, status);
}
 
Example 5
Source File: ImageCodeProcessor.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * 发送图形验证码,将其写到响应中
 *
 * @param request   the request
 * @param imageCode the image code
 *
 * @throws Exception the exception
 */
@Override
protected void send(ServletWebRequest request, ImageCode imageCode) throws Exception {
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	ImageIO.write(imageCode.getImage(), "JPEG", bos);

	SecurityResult result = SecurityResult.ok(bos.toByteArray());

	String json = objectMapper.writeValueAsString(result);
	HttpServletResponse response = request.getResponse();
	response.setCharacterEncoding("UTF-8");
	response.getWriter().write(json);
}
 
Example 6
Source File: PropertiesHandlerMethodReturnValueHandler.java    From SpringAll with MIT License 5 votes vote down vote up
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    Properties properties = (Properties) returnValue;

    ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest;

    HttpServletResponse response = servletWebRequest.getResponse();
    ServletServerHttpResponse servletServerHttpResponse = new ServletServerHttpResponse(response);

    // 获取请求头
    HttpHeaders headers = servletServerHttpResponse.getHeaders();

    MediaType contentType = headers.getContentType();
    // 获取编码
    Charset charset = null;
    if (contentType != null) {
        charset = contentType.getCharset();
    }

    charset = charset == null ? Charset.forName("UTF-8") : charset;

    // 获取请求体
    OutputStream body = servletServerHttpResponse.getBody();
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(body, charset);

    properties.store(outputStreamWriter, "Serialized by PropertiesHandlerMethodReturnValueHandler#handleReturnValue");

    // 告诉 Spring MVC 请求已经处理完毕
    mavContainer.setRequestHandled(true);
}
 
Example 7
Source File: AnnotationMethodHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest)
		throws ServletException, IOException {

	HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
	List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
	if (acceptedMediaTypes.isEmpty()) {
		acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
	}
	MediaType.sortByQualityValue(acceptedMediaTypes);
	HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
	Class<?> returnValueType = returnValue.getClass();
	if (this.messageConverters != null) {
		for (MediaType acceptedMediaType : acceptedMediaTypes) {
			for (HttpMessageConverter messageConverter : this.messageConverters) {
				if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
					messageConverter.write(returnValue, acceptedMediaType, outputMessage);
					return new ModelAndView();
				}
			}
		}
	}
	if (logger.isWarnEnabled()) {
		logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " +
				acceptedMediaTypes);
	}
	return null;
}
 
Example 8
Source File: AnnotationMethodHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
private ModelAndView handleResponseBody(Object returnValue, ServletWebRequest webRequest)
		throws ServletException, IOException {

	HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
	List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
	if (acceptedMediaTypes.isEmpty()) {
		acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
	}
	MediaType.sortByQualityValue(acceptedMediaTypes);
	HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
	Class<?> returnValueType = returnValue.getClass();
	if (this.messageConverters != null) {
		for (MediaType acceptedMediaType : acceptedMediaTypes) {
			for (HttpMessageConverter messageConverter : this.messageConverters) {
				if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
					messageConverter.write(returnValue, acceptedMediaType, outputMessage);
					return new ModelAndView();
				}
			}
		}
	}
	if (logger.isWarnEnabled()) {
		logger.warn("Could not find HttpMessageConverter that supports return type [" + returnValueType + "] and " +
				acceptedMediaTypes);
	}
	return null;
}