1 48 49 50 package com.caucho.portal.generic.context; 51 52 import com.caucho.portal.generic.Cache; 53 import com.caucho.portal.generic.CacheKey; 54 55 import java.io.IOException ; 56 import java.io.OutputStream ; 57 import java.io.PrintWriter ; 58 import java.io.Writer ; 59 import java.util.ArrayList ; 60 import java.util.LinkedHashMap ; 61 import java.util.Map ; 62 import java.util.logging.Level ; 63 64 public class CachingResponseHandler extends AbstractResponseHandler 65 { 66 private Cache _cache; 67 private String _namespace; 68 private int _expirationCache ; 69 private boolean _isPrivate; 70 71 private Writer _cacheWriter; 72 private OutputStream _cacheOutputStream; 73 74 private LinkedHashMap <String , Object > _propertiesMap; 75 private LinkedHashMap <String , Object > _cachePropertiesMap; 76 77 public CachingResponseHandler( ResponseHandler responseHandler, 78 Cache cache, String namespace, 79 int expirationCache, boolean isPrivate ) 80 { 81 super(responseHandler); 82 _cache = cache; 83 _namespace = namespace; 84 _expirationCache = expirationCache; 85 _isPrivate = isPrivate; 86 } 87 88 private LinkedHashMap <String , Object > getMapForProperty(String name) 89 { 90 if (name.startsWith("Cache-")) { 91 if (_cachePropertiesMap == null) 92 _cachePropertiesMap = new LinkedHashMap <String , Object >(); 93 94 return _cachePropertiesMap; 95 } 96 else { 97 if (_propertiesMap == null) 98 _propertiesMap = new LinkedHashMap <String , Object >(); 99 100 return _propertiesMap; 101 } 102 } 103 104 public void setProperty(String name, String value) 105 { 106 super.setProperty(name, value); 107 108 Map <String , Object > map = getMapForProperty(name);; 109 110 map.put(name, value); 111 } 112 113 public void addProperty(String name, String value) 114 { 115 super.addProperty(name, value); 116 117 Map <String , Object > map = getMapForProperty(name); 118 119 Object currentValue = map.get(name); 120 121 if (currentValue == null) { 122 ArrayList <String > values = new ArrayList <String >(); 123 values.add(value); 124 map.put(name, values); 125 } 126 else if (currentValue instanceof String ) { 127 ArrayList <String > values = new ArrayList <String >(); 128 values.add( (String ) currentValue); 129 values.add(value); 130 map.put(name, values); 131 } 132 else { 133 ArrayList <String > values = (ArrayList <String >) currentValue; 134 values.add(value); 135 } 136 } 137 138 public void finish( int expirationCache, 139 CacheKey cacheKey, 140 Map <String , String > requestAttributesMap ) 141 throws IOException 142 { 143 Cache cache = _cache; 144 OutputStream cacheOutputStream = _cacheOutputStream; 145 Writer cacheWriter = _cacheWriter; 146 147 _cacheOutputStream = null; 148 _cacheWriter = null; 149 _cache = null; 150 _namespace = null; 151 _isPrivate = false; 152 _expirationCache = 0; 153 154 String enc = null; 155 156 boolean fail = true; 157 158 try { 159 if (cacheWriter != null) 160 enc = getCharacterEncoding(); 161 162 boolean isError = isError(); 163 164 super.finish(); 165 166 if (!isError) { 167 168 Map <String , Object > cachePropertiesMap = null; 169 Map <String , Object > propertiesMap = null; 170 171 if ( _cachePropertiesMap != null && !_cachePropertiesMap.isEmpty()) 172 cachePropertiesMap = _cachePropertiesMap; 173 174 if ( _propertiesMap != null && !_propertiesMap.isEmpty()) 175 propertiesMap = _propertiesMap; 176 177 if (cacheWriter != null) { 178 cache.finishCaching( 179 cacheWriter, 180 expirationCache, 181 cacheKey, 182 enc, 183 cachePropertiesMap, 184 propertiesMap, 185 requestAttributesMap ); 186 187 fail = false; 188 } 189 190 if (cacheOutputStream != null) { 191 cache.finishCaching( 192 cacheOutputStream, 193 expirationCache, 194 cacheKey, 195 cachePropertiesMap, 196 propertiesMap, 197 requestAttributesMap ); 198 199 fail = false; 200 } 201 } 202 } 203 finally { 204 205 if (fail) { 206 if (cacheWriter != null) { 207 cache.finishCaching(cacheWriter, 0, null, null, null, null, null); 208 } 209 210 if (cacheOutputStream != null) { 211 cache.finishCaching(cacheOutputStream, 0, null, null, null, null); 212 } 213 } 214 } 215 } 216 217 public void finish() 218 throws IOException 219 { 220 throw new UnsupportedOperationException (); 221 } 222 223 public PrintWriter getWriter() 224 throws IOException 225 { 226 PrintWriter writer = super.getWriter(); 227 228 try { 229 _cacheWriter = _cache.getCachingWriter(_namespace, _expirationCache, _isPrivate); 230 } 231 catch (Exception ex) { 232 log.log(Level.WARNING, ex.toString(), ex); 233 } 234 235 return writer; 236 } 237 238 public OutputStream getOutputStream() 239 throws IOException 240 { 241 OutputStream outputStream = super.getOutputStream(); 242 243 try { 244 _cacheOutputStream 245 = _cache.getCachingOutputStream(_namespace, _expirationCache, _isPrivate); 246 } 247 catch (Exception ex) { 248 log.log(Level.WARNING, ex.toString(), ex); 249 } 250 251 return outputStream; 252 } 253 254 protected void print(char buf[], int off, int len) 255 throws IOException 256 { 257 super.print(buf, off, len); 258 259 if (_cacheWriter != null) 260 _cacheWriter.write(buf, off, len); 261 } 262 263 protected void print(String str, int off, int len) 264 throws IOException 265 { 266 super.print(str, off, len); 267 268 if (_cacheWriter != null) 269 _cacheWriter.write(str, off, len); 270 } 271 272 protected void print(char c) 273 throws IOException 274 { 275 super.print(c); 276 277 if (_cacheWriter != null) 278 _cacheWriter.write((int)c); 279 } 280 281 protected void write(byte[] buf, int off, int len) 282 throws IOException 283 { 284 super.write(buf, off, len); 285 286 if (_cacheOutputStream != null) 287 _cacheOutputStream.write(buf, off, len); 288 } 289 290 protected void write(byte b) 291 throws IOException 292 { 293 super.write(b); 294 295 if (_cacheOutputStream != null) 296 _cacheOutputStream.write((int)b); 297 } 298 } 299 | Popular Tags |