1public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
2
3private static final Logger logger = Logger.getLogger(CustomHttpServletRequestWrapper.class);
4private final String body;
5
6public CustomHttpServletRequestWrapper(HttpServletRequest request) {
7 super(request);
8
9 StringBuilder stringBuilder = new StringBuilder();
10 BufferedReader bufferedReader = null;
11
12 try {
13 InputStream inputStream = request.getInputStream();
14
15 if (inputStream != null) {
16 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
17
18 char[] charBuffer = new char[128];
19 int bytesRead = -1;
20
21 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
22 stringBuilder.append(charBuffer, 0, bytesRead);
23 }
24 } else {
25 stringBuilder.append("");
26 }
27 } catch (IOException ex) {
28 logger.error("Error reading the request body...");
29 } finally {
30 if (bufferedReader != null) {
31 try {
32 bufferedReader.close();
33 } catch (IOException ex) {
34 logger.error("Error closing bufferedReader...");
35 }
36 }
37 }
38
39 body = stringBuilder.toString();
40}
41
42@Override
43public ServletInputStream getInputStream () throws IOException {
44 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
45
46 ServletInputStream inputStream = new ServletInputStream() {
47 public int read () throws IOException {
48 return byteArrayInputStream.read();
49 }
50 };
51
52 return inputStream;
53}
54}