/**
* Write to stream the content of the processed resource bundle.
*/
public void serveProcessedBundle()
throws IOException {
final WroConfiguration configuration = context.getConfig();
final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse();
OutputStream os = null;
try {
final CacheKey cacheKey = getSafeCacheKey(request);
initAggregatedFolderPath(request, cacheKey.getType());
final CacheValue cacheValue = cacheStrategy.get(cacheKey);
// TODO move ETag check in wroManagerFactory
final String ifNoneMatch = request.getHeader(HttpHeader.IF_NONE_MATCH.toString());
// enclose etag value in quotes to be compliant with the RFC
final String etagValue = String.format("\"%s\"", cacheValue.getHash());
if (etagValue != null && etagValue.equals(ifNoneMatch)) {
LOG.debug("ETag hash detected: {}. Sending {} status code", etagValue, HttpServletResponse.SC_NOT_MODIFIED);
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
// because we cannot return null, return a stream containing nothing.
// TODO close output stream?
return;
}
/**
* Set contentType before actual content is written, solves <br/>
* <a href="http://code.google.com/p/wro4j/issues/detail?id=341">issue341</a>
*/
response.setContentType(cacheKey.getType().getContentType() + "; charset=" + configuration.getEncoding());
// set ETag header
response.setHeader(HttpHeader.ETAG.toString(), etagValue);
os = response.getOutputStream();
if (cacheValue.getRawContent() != null) {
// use gziped response if supported & Set content length based on gzip flag
if (isGzipAllowed()) {
response.setContentLength(cacheValue.getGzippedContent().length);
// add gzip header and gzip response
response.setHeader(HttpHeader.CONTENT_ENCODING.toString(), "gzip");
response.setHeader("Vary", "Accept-Encoding");
IOUtils.write(cacheValue.getGzippedContent(), os);
} else {
//using getRawContent().length() is not the same and can return 2Bytes smaller size.
response.setContentLength(cacheValue.getRawContent().getBytes(configuration.getEncoding()).length);
IOUtils.write(cacheValue.getRawContent(), os, configuration.getEncoding());
}
}
} finally {
if (os != null) {
IOUtils.closeQuietly(os);