001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic.context;
050:
051: import com.caucho.portal.generic.Cache;
052: import com.caucho.portal.generic.CacheKey;
053:
054: import java.io.IOException;
055: import java.io.OutputStream;
056: import java.io.PrintWriter;
057: import java.io.Writer;
058: import java.util.ArrayList;
059: import java.util.LinkedHashMap;
060: import java.util.Map;
061: import java.util.logging.Level;
062:
063: public class CachingResponseHandler extends AbstractResponseHandler {
064: private Cache _cache;
065: private String _namespace;
066: private int _expirationCache;
067: private boolean _isPrivate;
068:
069: private Writer _cacheWriter;
070: private OutputStream _cacheOutputStream;
071:
072: private LinkedHashMap<String, Object> _propertiesMap;
073: private LinkedHashMap<String, Object> _cachePropertiesMap;
074:
075: public CachingResponseHandler(ResponseHandler responseHandler,
076: Cache cache, String namespace, int expirationCache,
077: boolean isPrivate) {
078: super (responseHandler);
079: _cache = cache;
080: _namespace = namespace;
081: _expirationCache = expirationCache;
082: _isPrivate = isPrivate;
083: }
084:
085: private LinkedHashMap<String, Object> getMapForProperty(String name) {
086: if (name.startsWith("Cache-")) {
087: if (_cachePropertiesMap == null)
088: _cachePropertiesMap = new LinkedHashMap<String, Object>();
089:
090: return _cachePropertiesMap;
091: } else {
092: if (_propertiesMap == null)
093: _propertiesMap = new LinkedHashMap<String, Object>();
094:
095: return _propertiesMap;
096: }
097: }
098:
099: public void setProperty(String name, String value) {
100: super .setProperty(name, value);
101:
102: Map<String, Object> map = getMapForProperty(name);
103: ;
104:
105: map.put(name, value);
106: }
107:
108: public void addProperty(String name, String value) {
109: super .addProperty(name, value);
110:
111: Map<String, Object> map = getMapForProperty(name);
112:
113: Object currentValue = map.get(name);
114:
115: if (currentValue == null) {
116: ArrayList<String> values = new ArrayList<String>();
117: values.add(value);
118: map.put(name, values);
119: } else if (currentValue instanceof String) {
120: ArrayList<String> values = new ArrayList<String>();
121: values.add((String) currentValue);
122: values.add(value);
123: map.put(name, values);
124: } else {
125: ArrayList<String> values = (ArrayList<String>) currentValue;
126: values.add(value);
127: }
128: }
129:
130: public void finish(int expirationCache, CacheKey cacheKey,
131: Map<String, String> requestAttributesMap)
132: throws IOException {
133: Cache cache = _cache;
134: OutputStream cacheOutputStream = _cacheOutputStream;
135: Writer cacheWriter = _cacheWriter;
136:
137: _cacheOutputStream = null;
138: _cacheWriter = null;
139: _cache = null;
140: _namespace = null;
141: _isPrivate = false;
142: _expirationCache = 0;
143:
144: String enc = null;
145:
146: boolean fail = true;
147:
148: try {
149: if (cacheWriter != null)
150: enc = getCharacterEncoding();
151:
152: boolean isError = isError();
153:
154: super .finish();
155:
156: if (!isError) {
157:
158: Map<String, Object> cachePropertiesMap = null;
159: Map<String, Object> propertiesMap = null;
160:
161: if (_cachePropertiesMap != null
162: && !_cachePropertiesMap.isEmpty())
163: cachePropertiesMap = _cachePropertiesMap;
164:
165: if (_propertiesMap != null && !_propertiesMap.isEmpty())
166: propertiesMap = _propertiesMap;
167:
168: if (cacheWriter != null) {
169: cache.finishCaching(cacheWriter, expirationCache,
170: cacheKey, enc, cachePropertiesMap,
171: propertiesMap, requestAttributesMap);
172:
173: fail = false;
174: }
175:
176: if (cacheOutputStream != null) {
177: cache.finishCaching(cacheOutputStream,
178: expirationCache, cacheKey,
179: cachePropertiesMap, propertiesMap,
180: requestAttributesMap);
181:
182: fail = false;
183: }
184: }
185: } finally {
186:
187: if (fail) {
188: if (cacheWriter != null) {
189: cache.finishCaching(cacheWriter, 0, null, null,
190: null, null, null);
191: }
192:
193: if (cacheOutputStream != null) {
194: cache.finishCaching(cacheOutputStream, 0, null,
195: null, null, null);
196: }
197: }
198: }
199: }
200:
201: public void finish() throws IOException {
202: throw new UnsupportedOperationException();
203: }
204:
205: public PrintWriter getWriter() throws IOException {
206: PrintWriter writer = super .getWriter();
207:
208: try {
209: _cacheWriter = _cache.getCachingWriter(_namespace,
210: _expirationCache, _isPrivate);
211: } catch (Exception ex) {
212: log.log(Level.WARNING, ex.toString(), ex);
213: }
214:
215: return writer;
216: }
217:
218: public OutputStream getOutputStream() throws IOException {
219: OutputStream outputStream = super .getOutputStream();
220:
221: try {
222: _cacheOutputStream = _cache.getCachingOutputStream(
223: _namespace, _expirationCache, _isPrivate);
224: } catch (Exception ex) {
225: log.log(Level.WARNING, ex.toString(), ex);
226: }
227:
228: return outputStream;
229: }
230:
231: protected void print(char buf[], int off, int len)
232: throws IOException {
233: super .print(buf, off, len);
234:
235: if (_cacheWriter != null)
236: _cacheWriter.write(buf, off, len);
237: }
238:
239: protected void print(String str, int off, int len)
240: throws IOException {
241: super .print(str, off, len);
242:
243: if (_cacheWriter != null)
244: _cacheWriter.write(str, off, len);
245: }
246:
247: protected void print(char c) throws IOException {
248: super .print(c);
249:
250: if (_cacheWriter != null)
251: _cacheWriter.write((int) c);
252: }
253:
254: protected void write(byte[] buf, int off, int len)
255: throws IOException {
256: super .write(buf, off, len);
257:
258: if (_cacheOutputStream != null)
259: _cacheOutputStream.write(buf, off, len);
260: }
261:
262: protected void write(byte b) throws IOException {
263: super .write(b);
264:
265: if (_cacheOutputStream != null)
266: _cacheOutputStream.write((int) b);
267: }
268: }
|