1 5 package com.opensymphony.oscache.web.filter; 6 7 import java.io.*; 8 9 import java.util.Locale ; 10 import java.util.zip.GZIPInputStream ; 11 12 import javax.servlet.ServletResponse ; 13 import javax.servlet.http.HttpServletResponse ; 14 15 23 public class ResponseContent implements Serializable { 24 private transient ByteArrayOutputStream bout = new ByteArrayOutputStream(1000); 25 private Locale locale = null; 26 private String contentEncoding = null; 27 private String contentType = null; 28 private byte[] content = null; 29 private long expires = Long.MAX_VALUE; 30 private long lastModified = -1; 31 private long maxAge = -60; 32 33 public String getContentType() { 34 return contentType; 35 } 36 37 41 public void setContentType(String value) { 42 contentType = value; 43 } 44 45 public long getLastModified() { 46 return lastModified; 47 } 48 49 public void setLastModified(long value) { 50 lastModified = value; 51 } 52 53 public String getContentEncoding() { 54 return contentEncoding; 55 } 56 57 public void setContentEncoding(String contentEncoding) { 58 this.contentEncoding = contentEncoding; 59 } 60 61 65 public void setLocale(Locale value) { 66 locale = value; 67 } 68 69 72 public long getExpires() { 73 return expires; 74 } 75 76 80 public void setExpires(long value) { 81 expires = value; 82 } 83 84 89 public long getMaxAge() { 90 return maxAge; 91 } 92 93 98 public void setMaxAge(long value) { 99 maxAge = value; 100 } 101 102 107 public OutputStream getOutputStream() { 108 return bout; 109 } 110 111 117 public int getSize() { 118 return (content != null) ? content.length : (-1); 119 } 120 121 126 public void commit() { 127 if (bout != null) { 128 content = bout.toByteArray(); 129 bout = null; 130 } 131 } 132 133 139 public void writeTo(ServletResponse response) throws IOException { 140 writeTo(response, false, false); 141 } 142 143 151 public void writeTo(ServletResponse response, boolean fragment, boolean acceptsGZip) throws IOException { 152 if (contentType != null) { 154 response.setContentType(contentType); 155 } 156 157 if (fragment) { 158 acceptsGZip = false; 160 } else { 161 if (response instanceof HttpServletResponse ) { 163 HttpServletResponse httpResponse = (HttpServletResponse ) response; 164 165 if (lastModified != -1) { 167 httpResponse.setDateHeader(CacheFilter.HEADER_LAST_MODIFIED, lastModified); 168 } 169 170 if (expires != Long.MAX_VALUE) { 172 httpResponse.setDateHeader(CacheFilter.HEADER_EXPIRES, expires); 173 } 174 175 if (maxAge == CacheFilter.MAX_AGE_NO_INIT || maxAge == CacheFilter.MAX_AGE_TIME) { 177 } else if (maxAge > 0) { long currentMaxAge = maxAge / 1000 - System.currentTimeMillis() / 1000; 180 if (currentMaxAge < 0) { 181 currentMaxAge = 0; 182 } 183 httpResponse.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + currentMaxAge); 184 } else { 185 httpResponse.addHeader(CacheFilter.HEADER_CACHE_CONTROL, "max-age=" + (-maxAge)); 186 } 187 188 } 189 } 190 191 if (locale != null) { 192 response.setLocale(locale); 193 } 194 195 OutputStream out = new BufferedOutputStream(response.getOutputStream()); 196 197 if (isContentGZiped()) { 198 if (acceptsGZip) { 199 ((HttpServletResponse ) response).addHeader(CacheFilter.HEADER_CONTENT_ENCODING, "gzip"); 200 response.setContentLength(content.length); 201 out.write(content); 202 } else { 203 ByteArrayInputStream bais = new ByteArrayInputStream(content); 205 GZIPInputStream zis = new GZIPInputStream (bais); 206 207 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 208 int numBytesRead = 0; 209 byte[] tempBytes = new byte[4196]; 210 211 while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) { 212 baos.write(tempBytes, 0, numBytesRead); 213 } 214 215 byte[] result = baos.toByteArray(); 216 217 response.setContentLength(result.length); 218 out.write(result); 219 } 220 } else { 221 response.setContentLength(content.length); 224 out.write(content); 225 } 226 out.flush(); 227 } 228 229 230 233 public boolean isContentGZiped() { 234 return "gzip".equals(contentEncoding); 235 } 236 237 } 238 | Popular Tags |