001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.filters;
030:
031: import java.security.*;
032: import java.io.*;
033:
034: import javax.servlet.*;
035: import javax.servlet.http.*;
036:
037: import com.caucho.util.*;
038:
039: /**
040: * Calculates a HTTP Content-MD5 footer following RFC 1864
041: *
042: * @since Resin 3.1.5
043: */
044: public class MD5Filter implements Filter {
045: public void init(FilterConfig config) throws ServletException {
046: }
047:
048: /**
049: * Creates a wrapper to compress the output.
050: */
051: public void doFilter(ServletRequest request,
052: ServletResponse response, FilterChain nextFilter)
053: throws ServletException, IOException {
054: DigestResponse digestResponse = new DigestResponse(response);
055:
056: nextFilter.doFilter(request, digestResponse);
057:
058: digestResponse.finish();
059: }
060:
061: /**
062: * Any cleanup for the filter.
063: */
064: public void destroy() {
065: }
066:
067: class DigestResponse extends CauchoResponseWrapper {
068: private DigestStream _digestStream;
069:
070: DigestResponse(ServletResponse response) {
071: super ((HttpServletResponse) response);
072: }
073:
074: /**
075: * This needs to be bypassed because the file's content
076: * length has nothing to do with the returned length.
077: */
078: public void setContentLength(int length) {
079: }
080:
081: /**
082: * Calculates and returns the proper stream.
083: */
084: protected OutputStream getStream() throws IOException {
085: if (_digestStream == null)
086: _digestStream = new DigestStream(_response
087: .getOutputStream());
088:
089: return _digestStream;
090: }
091:
092: /**
093: * Complets the request.
094: */
095: public void finish() throws IOException, ServletException {
096: if (_digestStream != null)
097: _digestStream.flush();
098:
099: close();
100:
101: if (_digestStream != null) {
102: _digestStream.flush();
103: super .setFooter("Content-MD5", _digestStream
104: .getDigest());
105: }
106: }
107: }
108:
109: static class DigestStream extends OutputStream {
110: private OutputStream _os;
111: private MessageDigest _digest;
112:
113: DigestStream(OutputStream os) {
114: _os = os;
115:
116: try {
117: _digest = MessageDigest.getInstance("MD5");
118: } catch (RuntimeException e) {
119: throw e;
120: } catch (Exception e) {
121: throw new RuntimeException(e);
122: }
123: }
124:
125: /**
126: * Writes to the underlying stream.
127: *
128: * @param ch the byte to write
129: */
130: public void write(int ch) throws IOException {
131: _os.write(ch);
132: _digest.update((byte) ch);
133: }
134:
135: /**
136: * Writes a buffer to the underlying stream.
137: *
138: * @param buffer the byte array to write.
139: * @param offset the offset into the byte array.
140: * @param length the number of bytes to write.
141: */
142: public void write(byte[] buffer, int offset, int length)
143: throws IOException {
144: _os.write(buffer, offset, length);
145: _digest.update(buffer, offset, length);
146: }
147:
148: public void flush() throws IOException {
149: _os.flush();
150: }
151:
152: public void close() throws IOException {
153: _os.close();
154: }
155:
156: public String getDigest() {
157: byte[] bytes = _digest.digest();
158:
159: CharBuffer cb = new CharBuffer();
160: Base64.encode(cb, bytes, 0, bytes.length);
161:
162: return cb.toString();
163: }
164: }
165: }
|