}
}
}
}
protected void doFilter(final HttpServletRequest request,
final HttpServletResponse response, final FilterChain chain)
throws AlreadyGzippedException, AlreadyCommittedException,
FilterNonReentrantException, LockTimeoutException, Exception {
if (response.isCommitted()) {
throw new AlreadyCommittedException(
"Response already committed before doing buildPage.");
}
logRequestHeaders(request);//记录request的日志
PageInfo pageInfo = buildPageInfo(request, response, chain);//从缓存里面取出缓存页面信息,如果缓存中不存在,则通过 chain.doFilter(request, wrapper);处理后,再把页面存入缓存,
if (pageInfo.isOk()) {
if (response.isCommitted()) {
throw new AlreadyCommittedException(
"Response already committed after doing buildPage"
+ " but before writing response from PageInfo.");
}
writeResponse(request, response, pageInfo);//更新request的header,status,cookie等相关信息,因为如果从缓存获取的话,需要把所有信息都写到response
}
}
//再来看看buildPageInfo的处理
/**
* Build page info either using the cache or building the page directly.
*
* Some requests are for page fragments which should never be gzipped, or
* for other pages which are not gzipped.
protected PageInfo buildPageInfo(final HttpServletRequest request,
final HttpServletResponse response, final FilterChain chain)
throws Exception {
// Look up the cached page
final String key = calculateKey(request);//构造key,抽象方法,由子类覆盖
PageInfo pageInfo = null;
try {
checkNoReentry(request); //检查是否是同一个请求再次今夕,通过localthread来判断,如果已经进入过,则抛出FilterNonReentrantException异常,否则记录已经该请求进入处理
Element element = blockingCache.get(key); //从缓存取出对象
if (element == null || element.getObjectValue() == null) {
try {
// Page is not cached - build the response, cache it, and
// send to client
pageInfo = buildPage(request, response, chain);//如果缓存不存在,则正常处理,
if (pageInfo.isOk()) {//如果处理ok,
if (LOG.isDebugEnabled()) {
LOG.debug("PageInfo ok. Adding to cache "
+ blockingCache.getName() + " with key "
+ key);
}
blockingCache.put(new Element(key, pageInfo));//则把页面内容重新存入缓存
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("PageInfo was not ok(200). Putting null into cache "
+ blockingCache.getName()
+ " with key "
+ key);
}
blockingCache.put(new Element(key, null));//如果处理出错,则缓存设置为null
}
} catch (final Throwable throwable) {
// Must unlock the cache if the above fails. Will be log