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

2014-11-24 08:12:19 · 作者: · 浏览: 11
ontentType);
}
}

/**
* Set the serializableCookies
*
* @param pageInfo
* @param response
*/
protected void setCookies(final PageInfo pageInfo,
final HttpServletResponse response) {

final Collection cookies = pageInfo.getSerializableCookies();
for (Iterator iterator = cookies.iterator(); iterator.hasNext();) {
final Cookie cookie = ((SerializableCookie) iterator.next())
.toCookie();
response.addCookie(cookie);
}
}

/**
* Status code
*
* @param response
* @param pageInfo
*/
protected void setStatus(final HttpServletResponse response,
final PageInfo pageInfo) {
response.setStatus(pageInfo.getStatusCode());
}

protected void writeContent(final HttpServletRequest request,
final HttpServletResponse response, final PageInfo pageInfo)
throws IOException, ResponseHeadersNotModifiableException {
byte[] body;

boolean shouldBodyBeZero = ResponseUtil.shouldBodyBeZero(request,
pageInfo.getStatusCode());
if (shouldBodyBeZero) {
body = new byte[0];
} else if (acceptsGzipEncoding(request)) {
body = pageInfo.getGzippedBody();
if (ResponseUtil.shouldGzippedBodyBeZero(body, request)) {
body = new byte[0];
} else {
ResponseUtil.addGzipHeader(response);
}

} else {
body = pageInfo.getUngzippedBody();
}

response.setContentLength(body.length);
OutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(body);
out.flush();
}
}

对于checkNoReentry的实现,很简单
[java]
* Check that this caching filter is not being reentered by the same
* recursively. Recursive calls will block indefinitely because the first
* request has not yet unblocked the cache.
*


* This condition usually indicates an error in filter chaining or
* RequestDispatcher dispatching.
*
* @param httpRequest
* @throws FilterNonReentrantException
* if reentry is detected
*/
protected void checkNoReentry(final HttpServletRequest httpRequest)
throws FilterNonReentrantException {
String filterName = getClass().getName();
if (visitLog.hasVisited()) {
throw new FilterNonReentrantException(
"The request thread is attempting to reenter" + " filter "
+ filterName + ". URL: "
+ httpRequest.getRequestURL());
} else {
// mark this thread as already visited
visitLog.markAsVisited();
if (LOG.isDebugEnabled()) {
LOG.debug("Thread {} has been marked as visited.", Thread
.currentThread().getName());
}
}
}

/**
* threadlocal class to check for reentry
*
* @author hhuynh
*
*/
private static class VisitLog extends ThreadLocal {
@Override
protected Boolean initialValue() {
return false;
}

public boolean hasVisited() {
return get();
}

public void markAsVisited() {
set(true);
}

public void clear() {
super.remove();
}
}

当然还有两个抽象方法
[jav