Java Code Examples for org.springframework.web.context.request.NativeWebRequest#getNativeResponse()
The following examples show how to use
org.springframework.web.context.request.NativeWebRequest#getNativeResponse() .
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: JsonHandlerMethodReturnValueHandler.java From sca-best-practice with Apache License 2.0 | 6 votes |
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { mavContainer.setRequestHandled(true); HttpServletResponse httpServletResponse = webRequest.getNativeResponse(HttpServletResponse.class); httpServletResponse.setContentType(CONTENT_TYPE); ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(httpServletResponse); Locale locale = (Locale)webRequest.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, RequestAttributes.SCOPE_SESSION); String message; try { message = messageSource.getMessage(DEFAULT_SUCCESS_CODE, new Object[0], locale); } catch (NoSuchMessageException e) { message = DefaultMessagesProperties.getMessage(DEFAULT_SUCCESS_CODE); } JsonResponse jsonResponse = new JsonResponse(DEFAULT_SUCCESS_CODE, message, returnValue); outputMessage.getBody().write(StringUtils.toBytes(JsonUtils.toJson(jsonResponse))); outputMessage.getBody().flush(); }
Example 2
Source File: ApiUtil.java From openapi-generator with Apache License 2.0 | 5 votes |
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) { try { HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); res.setCharacterEncoding("UTF-8"); res.addHeader("Content-Type", contentType); res.getWriter().print(example); } catch (IOException e) { throw new RuntimeException(e); } }
Example 3
Source File: ApiUtil.java From openapi-generator with Apache License 2.0 | 5 votes |
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) { try { HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); res.setCharacterEncoding("UTF-8"); res.addHeader("Content-Type", contentType); res.getWriter().print(example); } catch (IOException e) { throw new RuntimeException(e); } }
Example 4
Source File: ApiUtil.java From openapi-generator with Apache License 2.0 | 5 votes |
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) { try { HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); res.setCharacterEncoding("UTF-8"); res.addHeader("Content-Type", contentType); res.getWriter().print(example); } catch (IOException e) { throw new RuntimeException(e); } }
Example 5
Source File: ApiUtil.java From openapi-generator with Apache License 2.0 | 5 votes |
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) { try { HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); res.setCharacterEncoding("UTF-8"); res.addHeader("Content-Type", contentType); res.getWriter().print(example); } catch (IOException e) { throw new RuntimeException(e); } }
Example 6
Source File: FrameworkServlet.java From java-technology-stack with MIT License | 5 votes |
@Override public <T> void preProcess(NativeWebRequest webRequest, Callable<T> task) { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (request != null) { HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); initContextHolders(request, buildLocaleContext(request), buildRequestAttributes(request, response, null)); } }
Example 7
Source File: JsonBodyReturnValueProcessor.java From rocket-console with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException { mavContainer.setRequestHandled(true); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); JsonResult jsonResult = new JsonResult(returnValue); response.setContentType("application/json; charset=UTF-8"); JsonUtil.writeValue(response.getWriter(), jsonResult); }
Example 8
Source File: WxMessageReturnValueHandler.java From FastBootWeixin with Apache License 2.0 | 5 votes |
@Override public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnType.hasMethodAnnotation(WxAsyncMessage.class)) { wxAsyncMessageTemplate.send(WxWebUtils.getWxMessageParameter(), returnValue); } else { wxMessageTemplate.sendMessage(WxWebUtils.getWxMessageParameter(), returnValue); } HttpServletResponse servletResponse = webRequest.getNativeResponse(HttpServletResponse.class); ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(servletResponse); mavContainer.setRequestHandled(true); outputMessage.getBody(); }
Example 9
Source File: StreamingResponseBodyReturnValueHandler.java From java-technology-stack with MIT License | 5 votes |
@Override @SuppressWarnings("resource") public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue == null) { mavContainer.setRequestHandled(true); return; } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); Assert.state(response != null, "No HttpServletResponse"); ServerHttpResponse outputMessage = new ServletServerHttpResponse(response); if (returnValue instanceof ResponseEntity) { ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue; response.setStatus(responseEntity.getStatusCodeValue()); outputMessage.getHeaders().putAll(responseEntity.getHeaders()); returnValue = responseEntity.getBody(); if (returnValue == null) { mavContainer.setRequestHandled(true); outputMessage.flush(); return; } } ServletRequest request = webRequest.getNativeRequest(ServletRequest.class); Assert.state(request != null, "No ServletRequest"); ShallowEtagHeaderFilter.disableContentCaching(request); Assert.isInstanceOf(StreamingResponseBody.class, returnValue, "StreamingResponseBody expected"); StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue; Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody); WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer); }
Example 10
Source File: ServletResponseMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Set {@link ModelAndViewContainer#setRequestHandled(boolean)} to * {@code false} to indicate that the method signature provides access * to the response. If subsequently the underlying method returns * {@code null}, the request is considered directly handled. */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (mavContainer != null) { mavContainer.setRequestHandled(true); } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); Class<?> paramType = parameter.getParameterType(); if (ServletResponse.class.isAssignableFrom(paramType)) { Object nativeResponse = webRequest.getNativeResponse(paramType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + paramType.getName() + "]: " + response); } return nativeResponse; } else if (OutputStream.class.isAssignableFrom(paramType)) { return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(paramType)) { return response.getWriter(); } else { // should not happen Method method = parameter.getMethod(); throw new UnsupportedOperationException("Unknown parameter type: " + paramType + " in method: " + method); } }
Example 11
Source File: SimpleSignInAdapter.java From lolibox with Apache License 2.0 | 5 votes |
private String extractOriginalUrl(NativeWebRequest request) { HttpServletRequest nativeReq = request.getNativeRequest(HttpServletRequest.class); HttpServletResponse nativeRes = request.getNativeResponse(HttpServletResponse.class); SavedRequest saved = requestCache.getRequest(nativeReq, nativeRes); if (saved == null) { return null; } requestCache.removeRequest(nativeReq, nativeRes); removeAutheticationAttributes(nativeReq.getSession(false)); return saved.getRedirectUrl(); }
Example 12
Source File: ServletResponseMethodArgumentResolver.java From spring-analysis-note with MIT License | 5 votes |
private <T> T resolveNativeResponse(NativeWebRequest webRequest, Class<T> requiredType) { T nativeResponse = webRequest.getNativeResponse(requiredType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + requiredType.getName() + "]: " + webRequest); } return nativeResponse; }
Example 13
Source File: ServletResponseMethodArgumentResolver.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Set {@link ModelAndViewContainer#setRequestHandled(boolean)} to * {@code false} to indicate that the method signature provides access * to the response. If subsequently the underlying method returns * {@code null}, the request is considered directly handled. */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (mavContainer != null) { mavContainer.setRequestHandled(true); } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); Class<?> paramType = parameter.getParameterType(); if (ServletResponse.class.isAssignableFrom(paramType)) { Object nativeResponse = webRequest.getNativeResponse(paramType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + paramType.getName() + "]: " + response); } return nativeResponse; } else if (OutputStream.class.isAssignableFrom(paramType)) { return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(paramType)) { return response.getWriter(); } else { // should not happen Method method = parameter.getMethod(); throw new UnsupportedOperationException("Unknown parameter type: " + paramType + " in method: " + method); } }
Example 14
Source File: TimeoutCallableProcessingInterceptor.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception { HttpServletResponse servletResponse = request.getNativeResponse(HttpServletResponse.class); if (!servletResponse.isCommitted()) { servletResponse.sendError(HttpStatus.SERVICE_UNAVAILABLE.value()); } return CallableProcessingInterceptor.RESPONSE_HANDLED; }
Example 15
Source File: ApiUtil.java From openapi-generator with Apache License 2.0 | 5 votes |
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) { try { HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class); res.setCharacterEncoding("UTF-8"); res.addHeader("Content-Type", contentType); res.getWriter().print(example); } catch (IOException e) { throw new RuntimeException(e); } }
Example 16
Source File: AnnotationMethodHandlerAdapter.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { PortletRequest request = webRequest.getNativeRequest(PortletRequest.class); PortletResponse response = webRequest.getNativeResponse(PortletResponse.class); if (PortletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (PortletResponse.class.isAssignableFrom(parameterType)) { Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (PortletSession.class.isAssignableFrom(parameterType)) { return request.getPortletSession(); } else if (PortletPreferences.class.isAssignableFrom(parameterType)) { return request.getPreferences(); } else if (PortletMode.class.isAssignableFrom(parameterType)) { return request.getPortletMode(); } else if (WindowState.class.isAssignableFrom(parameterType)) { return request.getWindowState(); } else if (PortalContext.class.isAssignableFrom(parameterType)) { return request.getPortalContext(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return request.getLocale(); } else if (InputStream.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("InputStream can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getPortletInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { if (!(request instanceof ClientDataRequest)) { throw new IllegalStateException("Reader can only get obtained for Action/ResourceRequest"); } return ((ClientDataRequest) request).getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("OutputStream can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getPortletOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { if (!(response instanceof MimeResponse)) { throw new IllegalStateException("Writer can only get obtained for Render/ResourceResponse"); } return ((MimeResponse) response).getWriter(); } else if (Event.class == parameterType) { if (!(request instanceof EventRequest)) { throw new IllegalStateException("Event can only get obtained from EventRequest"); } return ((EventRequest) request).getEvent(); } return super.resolveStandardArgument(parameterType, webRequest); }
Example 17
Source File: AnnotationMethodHandlerExceptionResolver.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Resolves standard method arguments. The default implementation handles {@link NativeWebRequest}, * {@link ServletRequest}, {@link ServletResponse}, {@link HttpSession}, {@link Principal}, * {@link Locale}, request {@link InputStream}, request {@link Reader}, response {@link OutputStream}, * response {@link Writer}, and the given {@code thrownException}. * @param parameterType the method parameter type * @param webRequest the request * @param thrownException the exception thrown * @return the argument value, or {@link WebArgumentResolver#UNRESOLVED} */ protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest, Exception thrownException) throws Exception { if (parameterType.isInstance(thrownException)) { return thrownException; } else if (WebRequest.class.isAssignableFrom(parameterType)) { return webRequest; } HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType)) { return request; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { return response; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { return response.getWriter(); } else { return WebArgumentResolver.UNRESOLVED; } }
Example 18
Source File: ResponseBodyEmitterReturnValueHandler.java From spring-analysis-note with MIT License | 4 votes |
@Override @SuppressWarnings("resource") public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { if (returnValue == null) { mavContainer.setRequestHandled(true); return; } HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); Assert.state(response != null, "No HttpServletResponse"); ServerHttpResponse outputMessage = new ServletServerHttpResponse(response); if (returnValue instanceof ResponseEntity) { ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue; response.setStatus(responseEntity.getStatusCodeValue()); outputMessage.getHeaders().putAll(responseEntity.getHeaders()); returnValue = responseEntity.getBody(); returnType = returnType.nested(); if (returnValue == null) { mavContainer.setRequestHandled(true); outputMessage.flush(); return; } } ServletRequest request = webRequest.getNativeRequest(ServletRequest.class); Assert.state(request != null, "No ServletRequest"); ResponseBodyEmitter emitter; if (returnValue instanceof ResponseBodyEmitter) { emitter = (ResponseBodyEmitter) returnValue; } else { emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest); if (emitter == null) { // Not streaming: write headers without committing response.. outputMessage.getHeaders().forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { response.addHeader(headerName, headerValue); } }); return; } } emitter.extendResponse(outputMessage); // At this point we know we're streaming.. ShallowEtagHeaderFilter.disableContentCaching(request); // Wrap the response to ignore further header changes // Headers will be flushed at the first write outputMessage = new StreamingServletServerHttpResponse(outputMessage); DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout()); WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer); HttpMessageConvertingHandler handler = new HttpMessageConvertingHandler(outputMessage, deferredResult); emitter.initialize(handler); }
Example 19
Source File: AnnotationMethodHandlerAdapter.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Override protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType) || MultipartRequest.class.isAssignableFrom(parameterType)) { Object nativeRequest = webRequest.getNativeRequest(parameterType); if (nativeRequest == null) { throw new IllegalStateException( "Current request is not of type [" + parameterType.getName() + "]: " + request); } return nativeRequest; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; Object nativeResponse = webRequest.getNativeResponse(parameterType); if (nativeResponse == null) { throw new IllegalStateException( "Current response is not of type [" + parameterType.getName() + "]: " + response); } return nativeResponse; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { this.responseArgumentUsed = true; return response.getWriter(); } return super.resolveStandardArgument(parameterType, webRequest); }
Example 20
Source File: AbstractMessageConverterMethodProcessor.java From lams with GNU General Public License v2.0 | 2 votes |
/** * Creates a new {@link HttpOutputMessage} from the given {@link NativeWebRequest}. * @param webRequest the web request to create an output message from * @return the output message */ protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest) { HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); return new ServletServerHttpResponse(response); }