View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 java.io.ByteArrayInputStream;
13  import java.io.IOException;
14  
15  import javax.servlet.ServletInputStream;
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletRequestWrapper;
18  
19  import org.apache.log4j.lf5.util.StreamUtils;
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          }
36      }
37  
38      @Override
39      public ServletInputStream getInputStream() throws IOException
40      {
41          if (this.cachedServletInputStream != null)
42          {
43              return this.cachedServletInputStream;
44          }
45          else
46          {
47              return super.getInputStream();
48          }
49      }
50  
51      private static class CachedServletInputStream extends ServletInputStream
52      {
53  
54          private ByteArrayInputStream cachedStream;
55  
56          public CachedServletInputStream(ServletInputStream servletInputStream)
57          {
58              try
59              {
60                  byte[] bytes = StreamUtils.getBytes(servletInputStream);
61                  this.cachedStream = new ByteArrayInputStream(bytes);
62              }
63              catch (IOException e)
64              {
65              }
66          }
67  
68          @Override
69          public int available() throws IOException
70          {
71              return this.cachedStream.available();
72          }
73  
74          @Override
75          public void close() throws IOException
76          {
77              this.cachedStream.close();
78          }
79  
80          @Override
81          protected void finalize() throws Throwable
82          {
83          }
84  
85          @Override
86          public synchronized void mark(int readlimit)
87          {
88              this.cachedStream.mark(readlimit);
89          }
90  
91          @Override
92          public boolean markSupported()
93          {
94              return true;
95          }
96  
97          @Override
98          public int read() throws IOException
99          {
100             return this.cachedStream.read();
101         }
102 
103         @Override
104         public int read(byte[] b, int off, int len) throws IOException
105         {
106             return this.cachedStream.read(b, off, len);
107         }
108 
109         @Override
110         public int read(byte[] b) throws IOException
111         {
112             return this.cachedStream.read(b);
113         }
114 
115         @Override
116         public synchronized void reset() throws IOException
117         {
118             this.cachedStream.reset();
119         }
120 
121         @Override
122         public long skip(long n) throws IOException
123         {
124             return this.cachedStream.skip(n);
125         }
126     }
127 }