Example usage for org.springframework.web.util ContentCachingRequestWrapper getInputStream

List of usage examples for org.springframework.web.util ContentCachingRequestWrapper getInputStream

Introduction

In this page you can find the example usage for org.springframework.web.util ContentCachingRequestWrapper getInputStream.

Prototype

@Override
    public ServletInputStream getInputStream() throws IOException 

Source Link

Usage

From source file:org.springframework.cloud.function.web.flux.request.FluxHandlerMethodArgumentResolver.java

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
    Class<?> type = inspector.getInputType(inspector.getName(handler));
    if (type == null) {
        type = Object.class;
    }/*from   w w  w .j av a 2  s . co m*/
    List<Object> body;
    ContentCachingRequestWrapper nativeRequest = new ContentCachingRequestWrapper(
            webRequest.getNativeRequest(HttpServletRequest.class));
    if (logger.isDebugEnabled()) {
        logger.debug("Resolving request body into type: " + type);
    }
    if (isPlainText(webRequest) && CharSequence.class.isAssignableFrom(type)) {
        body = Arrays
                .asList(StreamUtils.copyToString(nativeRequest.getInputStream(), Charset.forName("UTF-8")));
    } else {
        try {
            body = mapper.readValue(nativeRequest.getInputStream(),
                    mapper.getTypeFactory().constructCollectionLikeType(ArrayList.class, type));
        } catch (JsonMappingException e) {
            nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
            body = Arrays.asList(mapper.readValue(nativeRequest.getContentAsByteArray(), type));
        }
    }
    return new FluxRequest<Object>(body);
}