Java Code Examples for org.springframework.web.util.WebUtils#getNativeRequest()
The following examples show how to use
org.springframework.web.util.WebUtils#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: DispatcherServlet.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " + "this typically results from an additional MultipartFilter in web.xml"); } else if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) instanceof MultipartException) { logger.debug("Multipart resolution failed for current request before - " + "skipping re-resolution for undisturbed error rendering"); } else { return this.multipartResolver.resolveMultipart(request); } } // If not returned before: return original request. return request; }
Example 2
Source File: RequestLoggingUtil.java From tutorials with MIT License | 6 votes |
public static String readPayload(final HttpServletRequest request) throws IOException { String payloadData = null; ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (null != contentCachingRequestWrapper) { byte[] buf = contentCachingRequestWrapper.getContentAsByteArray(); if (buf.length > 0) { payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding()); } } return payloadData; }
Example 3
Source File: RequestParamMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 6 votes |
@Override protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest); if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) { return mpArg; } Object arg = null; if (multipartRequest != null) { List<MultipartFile> files = multipartRequest.getFiles(name); if (!files.isEmpty()) { arg = (files.size() == 1 ? files.get(0) : files); } } if (arg == null) { String[] paramValues = request.getParameterValues(name); if (paramValues != null) { arg = (paramValues.length == 1 ? paramValues[0] : paramValues); } } return arg; }
Example 4
Source File: RequestLoggingFilterTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void payloadMaxLength() throws Exception { filter.setIncludePayload(true); filter.setMaxPayloadLength(3); final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); final byte[] requestBody = "Hello World".getBytes(StandardCharsets.UTF_8); request.setContent(requestBody); FilterChain filterChain = (filterRequest, filterResponse) -> { ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream()); assertArrayEquals(requestBody, buf); ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class); assertArrayEquals("Hel".getBytes(StandardCharsets.UTF_8), wrapper.getContentAsByteArray()); }; filter.doFilter(request, response, filterChain); assertNotNull(filter.afterRequestMessage); assertTrue(filter.afterRequestMessage.contains("Hel")); assertFalse(filter.afterRequestMessage.contains("Hello World")); }
Example 5
Source File: MockAsyncContext.java From java-technology-stack with MIT License | 5 votes |
@Override public void complete() { MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(this.request, MockHttpServletRequest.class); if (mockRequest != null) { mockRequest.setAsyncStarted(false); } for (AsyncListener listener : this.listeners) { try { listener.onComplete(new AsyncEvent(this, this.request, this.response)); } catch (IOException ex) { throw new IllegalStateException("AsyncListener failure", ex); } } }
Example 6
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 5 votes |
static MultipartHttpServletRequest asMultipartHttpServletRequest(HttpServletRequest request) { MultipartHttpServletRequest unwrapped = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); if (unwrapped != null) { return unwrapped; } return adaptToMultipartHttpServletRequest(request); }
Example 7
Source File: DispatcherServlet.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Clean up any resources used by the given multipart request (if any). * @param request current HTTP request * @see MultipartResolver#cleanupMultipart */ protected void cleanupMultipart(HttpServletRequest request) { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); if (multipartRequest != null) { this.multipartResolver.cleanupMultipart(multipartRequest); } }
Example 8
Source File: DispatcherServlet.java From java-technology-stack with MIT License | 5 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { if (request.getDispatcherType().equals(DispatcherType.REQUEST)) { logger.trace("Request already resolved to MultipartHttpServletRequest, e.g. by MultipartFilter"); } } else if (hasMultipartException(request) ) { logger.debug("Multipart resolution previously failed for current request - " + "skipping re-resolution for undisturbed error rendering"); } else { try { return this.multipartResolver.resolveMultipart(request); } catch (MultipartException ex) { if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) { logger.debug("Multipart resolution failed for error dispatch", ex); // Keep processing error dispatch with regular request handle below } else { throw ex; } } } } // If not returned before: return original request. return request; }
Example 9
Source File: MockAsyncContext.java From spring-analysis-note with MIT License | 5 votes |
@Override public void complete() { MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(this.request, MockHttpServletRequest.class); if (mockRequest != null) { mockRequest.setAsyncStarted(false); } for (AsyncListener listener : this.listeners) { try { listener.onComplete(new AsyncEvent(this, this.request, this.response)); } catch (IOException ex) { throw new IllegalStateException("AsyncListener failure", ex); } } }
Example 10
Source File: DispatcherServlet.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " + "this typically results from an additional MultipartFilter in web.xml"); } else if (hasMultipartException(request) ) { logger.debug("Multipart resolution failed for current request before - " + "skipping re-resolution for undisturbed error rendering"); } else { try { return this.multipartResolver.resolveMultipart(request); } catch (MultipartException ex) { if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) { logger.debug("Multipart resolution failed for error dispatch", ex); // Keep processing error dispatch with regular request handle below } else { throw ex; } } } } // If not returned before: return original request. return request; }
Example 11
Source File: MockAsyncContext.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public void complete() { MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class); if (mockRequest != null) { mockRequest.setAsyncStarted(false); } for (AsyncListener listener : this.listeners) { try { listener.onComplete(new AsyncEvent(this, this.request, this.response)); } catch (IOException e) { throw new IllegalStateException("AsyncListener failure", e); } } }
Example 12
Source File: DispatcherServlet.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Clean up any resources used by the given multipart request (if any). * @param request current HTTP request * @see MultipartResolver#cleanupMultipart */ protected void cleanupMultipart(HttpServletRequest request) { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); if (multipartRequest != null) { this.multipartResolver.cleanupMultipart(multipartRequest); } }
Example 13
Source File: ComplexWebApplicationContext.java From spring-analysis-note with MIT License | 5 votes |
@Override public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException { WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request); if (!(wac instanceof ComplexWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext"); } if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) { throw new ServletException("Not in a MultipartHttpServletRequest"); } if (request.getParameter("fail") != null) { throw new ModelAndViewDefiningException(new ModelAndView("failed1")); } if (request.getParameter("access") != null) { throw new IllegalAccessException("illegal access"); } if (request.getParameter("servlet") != null) { throw new ServletRequestBindingException("servlet"); } if (request.getParameter("exception") != null) { throw new RuntimeException("servlet"); } if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) { throw new ServletException("Incorrect LocaleResolver"); } if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) { throw new ServletException("Incorrect Locale"); } if (RequestContextUtils.getTimeZone(request) != null) { throw new ServletException("Incorrect TimeZone"); } if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone"); } if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) { throw new ServletException("Incorrect ThemeResolver"); } if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name"); } RequestContext rc = new RequestContext(request); rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1")); rc.changeTheme("theme2"); if (!Locale.US.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } if (!Locale.US.equals(LocaleContextHolder.getLocale())) { throw new ServletException("Incorrect Locale"); } if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) { throw new ServletException("Incorrect TimeZone"); } if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone"); } if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name"); } }
Example 14
Source File: ServletWebRequest.java From spring-analysis-note with MIT License | 4 votes |
@Override public <T> T getNativeRequest(@Nullable Class<T> requiredType) { return WebUtils.getNativeRequest(getRequest(), requiredType); }
Example 15
Source File: MultipartResolutionDelegate.java From spring-analysis-note with MIT License | 4 votes |
@Nullable public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); boolean isMultipart = (multipartRequest != null || isMultipartContent(request)); if (MultipartFile.class == parameter.getNestedParameterType()) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFile(name) : null); } else if (isMultipartFileCollection(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } return (multipartRequest != null ? multipartRequest.getFiles(name) : null); } else if (isMultipartFileArray(parameter)) { if (multipartRequest == null && isMultipart) { multipartRequest = new StandardMultipartHttpServletRequest(request); } if (multipartRequest != null) { List<MultipartFile> multipartFiles = multipartRequest.getFiles(name); return multipartFiles.toArray(new MultipartFile[0]); } else { return null; } } else if (Part.class == parameter.getNestedParameterType()) { return (isMultipart ? request.getPart(name): null); } else if (isPartCollection(parameter)) { return (isMultipart ? resolvePartList(request, name) : null); } else if (isPartArray(parameter)) { return (isMultipart ? resolvePartList(request, name).toArray(new Part[0]) : null); } else { return UNRESOLVABLE; } }
Example 16
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 4 votes |
public static boolean isMultipartRequest(HttpServletRequest request) { return (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null || isMultipartContent(request)); }
Example 17
Source File: ComplexWebApplicationContext.java From java-technology-stack with MIT License | 4 votes |
@Override public void doSomething(HttpServletRequest request) throws ServletException, IllegalAccessException { WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request); if (!(wac instanceof ComplexWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext"); } if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) == null) { throw new ServletException("Not in a MultipartHttpServletRequest"); } if (request.getParameter("fail") != null) { throw new ModelAndViewDefiningException(new ModelAndView("failed1")); } if (request.getParameter("access") != null) { throw new IllegalAccessException("illegal access"); } if (request.getParameter("servlet") != null) { throw new ServletRequestBindingException("servlet"); } if (request.getParameter("exception") != null) { throw new RuntimeException("servlet"); } if (!(RequestContextUtils.getLocaleResolver(request) instanceof SessionLocaleResolver)) { throw new ServletException("Incorrect LocaleResolver"); } if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } if (!Locale.CANADA.equals(LocaleContextHolder.getLocale())) { throw new ServletException("Incorrect Locale"); } if (RequestContextUtils.getTimeZone(request) != null) { throw new ServletException("Incorrect TimeZone"); } if (!TimeZone.getDefault().equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone"); } if (!(RequestContextUtils.getThemeResolver(request) instanceof SessionThemeResolver)) { throw new ServletException("Incorrect ThemeResolver"); } if (!"theme".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name"); } RequestContext rc = new RequestContext(request); rc.changeLocale(Locale.US, TimeZone.getTimeZone("GMT+1")); rc.changeTheme("theme2"); if (!Locale.US.equals(RequestContextUtils.getLocale(request))) { throw new ServletException("Incorrect Locale"); } if (!Locale.US.equals(LocaleContextHolder.getLocale())) { throw new ServletException("Incorrect Locale"); } if (!TimeZone.getTimeZone("GMT+1").equals(RequestContextUtils.getTimeZone(request))) { throw new ServletException("Incorrect TimeZone"); } if (!TimeZone.getTimeZone("GMT+1").equals(LocaleContextHolder.getTimeZone())) { throw new ServletException("Incorrect TimeZone"); } if (!"theme2".equals(RequestContextUtils.getThemeResolver(request).resolveThemeName(request))) { throw new ServletException("Incorrect theme name"); } }
Example 18
Source File: MultipartResolutionDelegate.java From java-technology-stack with MIT License | 4 votes |
public static boolean isMultipartRequest(HttpServletRequest request) { return (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null || isMultipartContent(request)); }
Example 19
Source File: DispatcherServlet.java From spring-analysis-note with MIT License | 4 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { if (request.getDispatcherType().equals(DispatcherType.REQUEST)) { logger.trace("Request already resolved to MultipartHttpServletRequest, e.g. by MultipartFilter"); } } else if (hasMultipartException(request)) { logger.debug("Multipart resolution previously failed for current request - " + "skipping re-resolution for undisturbed error rendering"); } else { try { return this.multipartResolver.resolveMultipart(request); } catch (MultipartException ex) { if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) { logger.debug("Multipart resolution failed for error dispatch", ex); // Keep processing error dispatch with regular request handle below } else { throw ex; } } } } // If not returned before: return original request. return request; }
Example 20
Source File: ServletRequestDataBinder.java From java-technology-stack with MIT License | 3 votes |
/** * Bind the parameters of the given request to this binder's target, * also binding multipart files in case of a multipart request. * <p>This call can create field errors, representing basic binding * errors like a required field (code "required"), or type mismatch * between value and bean property (code "typeMismatch"). * <p>Multipart files are bound via their parameter name, just like normal * HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property, * invoking a "setUploadedFile" setter method. * <p>The type of the target property for a multipart file can be MultipartFile, * byte[], or String. The latter two receive the contents of the uploaded file; * all metadata like original file name, content type, etc are lost in those cases. * @param request request with parameters to bind (can be multipart) * @see org.springframework.web.multipart.MultipartHttpServletRequest * @see org.springframework.web.multipart.MultipartFile * @see #bind(org.springframework.beans.PropertyValues) */ public void bind(ServletRequest request) { MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request); MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class); if (multipartRequest != null) { bindMultipart(multipartRequest.getMultiFileMap(), mpvs); } addBindValues(mpvs, request); doBind(mpvs); }