j2ee页面静态化方案encache web cache框架源码分析2(四)

2014-11-24 08:12:19 · 作者: · 浏览: 12
ged
// at Filter
blockingCache.put(new Element(key, null));
throw new Exception(throwable);
}
} else {
pageInfo = (PageInfo) element.getObjectValue();//如果缓存存在,则取出内容
}
} catch (LockTimeoutException e) {
// do not release the lock, because you never acquired it
throw e;
} finally {
// all done building page, reset the re-entrant flag
visitLog.clear();
}
return pageInfo;
}

/**
* Builds the PageInfo object by passing the request along the filter chain
*
* @param request
* @param response
* @param chain
* @return a Serializable value object for the page or page fragment
* @throws AlreadyGzippedException
* if an attempt is made to double gzip the body
* @throws Exception
*/
//缓存不存在时的处理
protected PageInfo buildPage(final HttpServletRequest request,
final HttpServletResponse response, final FilterChain chain)
throws AlreadyGzippedException, Exception {

// Invoke the next entity in the chain
final ByteArrayOutputStream outstr = new ByteArrayOutputStream();
final GenericResponseWrapper wrapper = new GenericResponseWrapper(
response, outstr);//这个responsewrapper,用于保存正常处理后的状态,cookie,header,结果信息,用于缓存
chain.doFilter(request, wrapper);//缓存不存在,则跳过由后续filter处理
wrapper.flush();

long timeToLiveSeconds = blockingCache.getCacheConfiguration()
.getTimeToLiveSeconds();

// Return the page info
return new PageInfo(wrapper.getStatus(), wrapper.getContentType(),
wrapper.getCookies(), outstr.toByteArray(), true,
timeToLiveSeconds, wrapper.getAllHeaders());//构造信息的该请求的缓存对象信息
}

/**
* Writes the response from a PageInfo object.
*


* Headers are set last so that there is an opportunity to override
*
* @param request
* @param response
* @param pageInfo
* @throws IOException
* @throws DataFormatException
* @throws ResponseHeadersNotModifiableException
*
*/
//从pageinfo里面取出response的相关信息写入到当前的response中
protected void writeResponse(final HttpServletRequest request,
final HttpServletResponse response, final PageInfo pageInfo)
throws IOException, DataFormatException,
ResponseHeadersNotModifiableException {
boolean requestAcceptsGzipEncoding = acceptsGzipEncoding(request);

setStatus(response, pageInfo);//设置response的状态
setContentType(response, pageInfo);//设置response的contentType
setCookies(pageInfo, response);//设置response的cookie
// do headers last so that users can override with their own header sets
setHeaders(pageInfo, requestAcceptsGzipEncoding, response);//设置response的header
writeContent(request, response, pageInfo);//设置response的内容
}

//比如:
protected void setContentType(final HttpServletResponse response,
final PageInfo pageInfo) {
String contentType = pageInfo.getContentType();
if (contentType != null && contentType.length() > 0) {
response.setContentType(c