// Invoke the next entity in the chain
final ByteArrayOutputStream outstr = new ByteArrayOutputStream();
final GenericResponseWrapper wrapper = new GenericResponseWrapper(response, outstr);
chain.doFilter(request, wrapper);
wrapper.flush();
long timeToLiveSeconds = blockingCache.getCacheConfiguration().getTimeToLiveSeconds();
// Return the page info
return new PageInfo(wrapper.getStatus(), wrapper.getContentType(),
wrapper.getCookies(),
outstr.toByteArray(), false, timeToLiveSeconds, wrapper.getAllHeaders()); //其中第五个参数false表示不存储gzip过的信息
}
/**
* Assembles a response from a cached page include.
* These responses are never gzipped
* The content length should not be set in the response, because it is a fragment of a page.
* Don't write any headers at all.
*/
//也不处理gzip过的信息
protected void writeResponse(final HttpServletResponse response, final PageInfo pageInfo) throws IOException {
// Write the page
final byte[] cachedPage = pageInfo.getUngzippedBody();
//needed to support multilingual
final String page = new String(cachedPage, response.getCharacterEncoding());
String implementationVendor = response.getClass().getPackage().getImplementationVendor();
if (implementationVendor != null && implementationVendor.equals("\"Evermind\"")) {
response.getOutputStream().print(page);
} else {
response.getWriter().write(page);
}
5.PageInfo缓存对象的几个要点
[java]
public class PageInfo implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(PageInfo.class);
private static final int FOUR_KB = 4196;
private static final int GZIP_MAGIC_NUMBER_BYTE_1 = 31;
private static final int GZIP_MAGIC_NUMBER_BYTE_2 = -117;
private static final long ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
//存储的信息包括状态码,contentType,cookie,body,是否gzip过,过期时间,headers等信息
public PageInfo(final int statusCode, final String contentType,
final Collection cookies,
final byte[] body, boolean storeGzipped, long timeToLiveSeconds,
final Collection
//Note that the ordering is switched with headers at the end to deal with the erasure issues with Java generics causing
//a conflict with the deprecated PageInfo header
this.init(statusCode, contentType, headers, cookies, body, storeGzipped, timeToLiveSeconds);
}
/**
* @param ungzipped the bytes to be gzipped
* @return gzipped bytes
*/
//提供的gzip处理方法
private byte[] gzip(byte[] ungzipped) throws IOException, AlreadyGzippedException {
if (isGzipped(ungzipped)) {
throw new AlreadyGzippedException("The byte[] is already gzipped. It should not be gzipped again.");
}
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bytes);
gzipOutputStream.write(ungzipped);
gzipOutputStream