Search in sources :

Example 1 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project FP-PSP-SERVER by FundacionParaguaya.

the class LogRequestFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
    int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
    // pass through filter chain to do the actual request handling
    filterChain.doFilter(wrappedRequest, response);
    status = response.getStatus();
    // only log request if there was an error
    if (status == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
        Map<String, Object> trace = getTrace(wrappedRequest, status);
        // body can only be read after the actual request handling was done!
        getBody(wrappedRequest, trace);
        logTrace(trace);
    }
}
Also used : ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 2 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project loc-framework by lord-of-code.

the class LocAccessLogger method appendRequestDetailMessage.

void appendRequestDetailMessage(boolean includeRequest, HttpServletRequest request) {
    StringBuilder msg = new StringBuilder();
    if (includeRequest && isNormalRequest(request)) {
        msg.append(";headers=").append(new ServletServerHttpRequest(request).getHeaders());
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            byte[] buf = wrapper.getContentAsByteArray();
            if (buf.length > 0) {
                int length = Math.min(buf.length, properties.getRequestBodyLength());
                String payload;
                try {
                    payload = new String(buf, 0, length, wrapper.getCharacterEncoding());
                } catch (UnsupportedEncodingException ex) {
                    payload = "[unknown]";
                }
                msg.append(";payload=").append(payload);
            }
        }
    }
    normalMsg.append(msg.toString());
    normalMsg.append(REQUEST_SUFFIX);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 3 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project tutorials by eugenp.

the class TaxiFareRequestInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    String postData;
    HttpServletRequest requestCacheWrapperObject = null;
    try {
        // Uncomment to produce the stream closed issue
        // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
        // To overcome request stream closed issue
        requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
        requestCacheWrapperObject.getParameterMap();
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
        LOGGER.info("REQUEST DATA: " + postData);
    }
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 4 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project tutorials by eugenp.

the class RequestLoggingUtil method readPayload.

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;
}
Also used : ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper)

Example 5 with ContentCachingRequestWrapper

use of org.springframework.web.util.ContentCachingRequestWrapper in project spring-cloud-function by spring-cloud.

the class FluxHandlerMethodArgumentResolver method resolveArgument.

@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(handler);
    if (type == null) {
        type = Object.class;
    }
    boolean message = inspector.isMessage(handler);
    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.fromJson(new InputStreamReader(nativeRequest.getInputStream()), ResolvableType.forClassWithGenerics(ArrayList.class, type).getType());
        } catch (JsonSyntaxException e) {
            nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
            body = Arrays.asList(mapper.fromJson(new String(nativeRequest.getContentAsByteArray()), type));
        }
    }
    if (message) {
        List<Object> messages = new ArrayList<>();
        MessageHeaders headers = HeaderUtils.fromHttp(new ServletServerHttpRequest(webRequest.getNativeRequest(HttpServletRequest.class)).getHeaders());
        for (Object payload : body) {
            messages.add(MessageUtils.create(handler, payload, headers));
        }
        body = messages;
    }
    return new FluxRequest<Object>(body);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) ContentCachingRequestWrapper(org.springframework.web.util.ContentCachingRequestWrapper) HttpServletRequest(javax.servlet.http.HttpServletRequest) JsonSyntaxException(com.google.gson.JsonSyntaxException) MessageHeaders(org.springframework.messaging.MessageHeaders)

Aggregations

ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ContentCachingResponseWrapper (org.springframework.web.util.ContentCachingResponseWrapper)3 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 FilterChain (jakarta.servlet.FilterChain)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 StringJoiner (java.util.StringJoiner)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1