001: package clime.messadmin.filter;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStream;
006:
007: import javax.servlet.ServletInputStream;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletRequestWrapper;
010:
011: /**
012: * Used for counting resquest size
013: * @author Cédrik LIME
014: */
015: public class MessAdminRequestWrapper extends HttpServletRequestWrapper {
016: protected long requestLength = -1;
017: private BufferedReader decryptedDataReader;
018: private ServletInputStream decryptedDataInputStream;
019:
020: class DelegatingServletInputStream extends ServletInputStream {
021: private final InputStream sourceStream;
022:
023: /**
024: * Create a new DelegatingServletInputStream.
025: * @param sourceStream the sourceStream InputStream
026: */
027: public DelegatingServletInputStream(InputStream sourceStream) {
028: this .sourceStream = sourceStream;
029: }
030:
031: /** {@inheritDoc} */
032: public int read() throws IOException {
033: int value = sourceStream.read();
034: ++requestLength;
035: return value;
036: }
037:
038: /** {@inheritDoc} */
039: public int available() throws IOException {
040: return sourceStream.available();
041: }
042:
043: /** {@inheritDoc} */
044: public void close() throws IOException {
045: super .close();
046: sourceStream.close();
047: }
048:
049: /** {@inheritDoc} */
050: public void mark(int readlimit) {
051: sourceStream.mark(readlimit);
052: }
053:
054: /** {@inheritDoc} */
055: public void reset() throws IOException {
056: sourceStream.reset();
057: }
058:
059: /** {@inheritDoc} */
060: public boolean markSupported() {
061: return sourceStream.markSupported();
062: }
063: }
064:
065: class DelegatingBufferedReader extends BufferedReader {
066: DelegatingBufferedReader(BufferedReader sourceReader) {
067: super (sourceReader, 1);// source is already buffered
068: }
069:
070: /** {@inheritDoc} */
071: public int read(char[] cbuf, int off, int len)
072: throws IOException {
073: int returnValue = super .read(cbuf, off, len);
074: if (returnValue != -1) {
075: requestLength += returnValue;
076: }
077: return returnValue;
078: }
079: }
080:
081: public MessAdminRequestWrapper(HttpServletRequest request) {
082: super (request);
083: }
084:
085: /** {@inheritDoc} */
086: public synchronized BufferedReader getReader() throws IOException {
087: if (null == decryptedDataReader) {
088: decryptedDataReader = new DelegatingBufferedReader(super
089: .getReader());
090: }
091: return decryptedDataReader;
092: }
093:
094: /** {@inheritDoc} */
095: public synchronized ServletInputStream getInputStream()
096: throws IOException {
097: if (null == decryptedDataInputStream) {
098: decryptedDataInputStream = new DelegatingServletInputStream(
099: super .getInputStream());
100: }
101: return decryptedDataInputStream;
102: }
103:
104: public long getRequestLength() {
105: return (requestLength == -1) ? requestLength
106: : requestLength + 1;
107: }
108: }
|