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

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

Introduction

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

Prototype

public ContentCachingRequestWrapper(HttpServletRequest request) 

Source Link

Document

Create a new ContentCachingRequestWrapper for the given servlet request.

Usage

From source file:it.reply.orchestrator.config.filters.CustomRequestLoggingFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    long startTime = System.nanoTime();
    boolean isFirstRequest = !isAsyncDispatch(request);
    HttpServletRequest requestToUse = request;

    String requestId = "";// safeTrimmedString(request.getHeader(X_REQUEST_ID));

    if (requestId.isEmpty()) {
        requestId = "req-" + UUID.randomUUID().toString();
    }/*from   w ww  .j a v  a  2s .  c o  m*/
    try {
        MDC.put(REQUEST_ID_MDC_KEY, requestId);
        response.addHeader(X_REQUEST_ID, requestId);

        if (isIncludePayload() && isFirstRequest && !(request instanceof ContentCachingRequestWrapper)) {
            requestToUse = new ContentCachingRequestWrapper(request);
        }

        boolean shouldLog = shouldLog(requestToUse);
        if (shouldLog && isFirstRequest) {
            beforeRequest(requestToUse);
        }
        try {
            filterChain.doFilter(requestToUse, response);
        } finally {
            if (shouldLog && !isAsyncStarted(requestToUse)) {
                afterRequest(requestToUse, response, getElapsedMillisec(startTime));
            }
        }
    } finally {
        MDC.remove(REQUEST_ID_MDC_KEY);
    }
}

From source file:org.dspace.app.rest.utils.MultipartFileSenderTest.java

/**
 * This method will be run before every test as per @Before. It will
 * initialize resources required for the tests.
 * <p>/*from  ww  w .j  a  v  a  2s .  c  om*/
 * Other methods can be annotated with @Before here or in subclasses
 * but no execution order is guaranteed
 */
@Before
public void init() throws AuthorizeException {
    try {
        String content = "0123456789";

        this.is = IOUtils.toInputStream(content, CharEncoding.UTF_8);
        this.fileName = "Test-Item.txt";
        this.mimeType = "text/plain";
        this.lastModified = new Date().getTime();
        this.length = content.getBytes().length;
        this.checksum = "testsum";

        this.request = mock(HttpServletRequest.class);
        this.response = new MockHttpServletResponse();

        //Using wrappers so we can save the content of the bodies and use them for tests
        this.requestWrapper = new ContentCachingRequestWrapper(request);
        this.responseWrapper = new ContentCachingResponseWrapper(response);
    } catch (IOException ex) {
        log.error("IO Error in init", ex);
        fail("SQL Error in init: " + ex.getMessage());
    }
}

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  ww  w  .  j a  v a2  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);
}