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);
}
}
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);
}
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;
}
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;
}
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);
}
Aggregations