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

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

Introduction

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

Prototype

public void setAttribute(String name, Object o) 

Source Link

Document

The default behavior of this method is to return setAttribute(String name, Object o) on the wrapped request object.

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  .  java2s.c o  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);
}