View Javadoc

1   /*
2    * $Id: CachedHttpServletRequest.java 20875 2011-01-04 16:09:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.servlet;
11  
12  import org.mule.util.IOUtils;
13  
14  import java.io.ByteArrayInputStream;
15  import java.io.IOException;
16  
17  import javax.servlet.ServletInputStream;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletRequestWrapper;
20  
21  public class CachedHttpServletRequest extends HttpServletRequestWrapper
22  {
23  
24      private CachedServletInputStream cachedServletInputStream;
25  
26      public CachedHttpServletRequest(HttpServletRequest request)
27      {
28          super(request);
29          try
30          {
31              this.cachedServletInputStream = new CachedServletInputStream(request.getInputStream());
32          }
33          catch (IOException e)
34          {
35              throw new RuntimeException(e);
36          }
37      }
38  
39      @Override
40      public ServletInputStream getInputStream() throws IOException
41      {
42          if (this.cachedServletInputStream != null)
43          {
44              return this.cachedServletInputStream;
45          }
46          else
47          {
48              return super.getInputStream();
49          }
50      }
51  
52      private static class CachedServletInputStream extends ServletInputStream
53      {
54          private ByteArrayInputStream cachedStream;
55  
56          public CachedServletInputStream(ServletInputStream servletInputStream)
57          {
58              byte[] bytes = IOUtils.toByteArray(servletInputStream);
59              this.cachedStream = new ByteArrayInputStream(bytes);
60          }
61  
62          @Override
63          public int available() throws IOException
64          {
65              return this.cachedStream.available();
66          }
67  
68          @Override
69          public void close() throws IOException
70          {
71              this.cachedStream.close();
72          }
73  
74          @Override
75          public synchronized void mark(int readlimit)
76          {
77              this.cachedStream.mark(readlimit);
78          }
79  
80          @Override
81          public boolean markSupported()
82          {
83              return true;
84          }
85  
86          @Override
87          public int read() throws IOException
88          {
89              return this.cachedStream.read();
90          }
91  
92          @Override
93          public int read(byte[] b, int off, int len) throws IOException
94          {
95              return this.cachedStream.read(b, off, len);
96          }
97  
98          @Override
99          public int read(byte[] b) throws IOException
100         {
101             return this.cachedStream.read(b);
102         }
103 
104         @Override
105         public synchronized void reset() throws IOException
106         {
107             this.cachedStream.reset();
108         }
109 
110         @Override
111         public long skip(long n) throws IOException
112         {
113             return this.cachedStream.skip(n);
114         }
115     }
116 }