All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itcoon.common.tracelog.component.RequestWrapper Maven / Gradle / Ivy

package com.itcoon.common.tracelog.component;

import org.springframework.util.StreamUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 

* @since 2019-09-07 */ public class RequestWrapper extends HttpServletRequestWrapper { private String body; private Charset charSet; public RequestWrapper(HttpServletRequest request) { super(request); try { String charSetStr = request.getCharacterEncoding(); if (charSetStr == null) { charSetStr = "UTF-8"; } charSet = Charset.forName(charSetStr); this.body = StreamUtils.copyToString(request.getInputStream(), charSet); } catch (IOException e) { e.printStackTrace(); } } public String getRequestBody(){ return this.body; } @Override public ServletInputStream getInputStream() { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes(charSet)); ServletInputStream servletInputStream = new ServletInputStream() { @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } @Override public int read() { return byteArrayInputStream.read(); } }; return servletInputStream; } @Override public BufferedReader getReader() { return new BufferedReader(new InputStreamReader(this.getInputStream())); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy