Issue #485 Multiple compressed formats from static content

Fixed formatting from the PR
This commit is contained in:
Greg Wilkins 2016-04-07 16:47:28 +10:00
parent 3261e03edb
commit a72838162d
7 changed files with 97 additions and 66 deletions

View File

@ -36,10 +36,11 @@ public class PrecompressedHttpContent implements HttpContent
public PrecompressedHttpContent(HttpContent content, HttpContent precompressedContent, CompressedContentFormat format)
{
_content=content;
_precompressedContent=precompressedContent;
_format=format;
if (_precompressedContent == null || _format == null) {
_content = content;
_precompressedContent = precompressedContent;
_format = format;
if (_precompressedContent == null || _format == null)
{
throw new NullPointerException("Missing compressed content and/or format");
}
}

View File

@ -61,13 +61,17 @@ public class ResourceHttpContent implements HttpContent
{
_resource=resource;
_contentType=contentType;
_maxBuffer=maxBuffer;
if (precompressedContents == null) {
_maxBuffer = maxBuffer;
if (precompressedContents == null)
{
_precompressedContents = null;
} else {
}
else
{
_precompressedContents = new HashMap<>(precompressedContents.size());
for (Map.Entry<CompressedContentFormat, HttpContent> entry : precompressedContents.entrySet()) {
_precompressedContents.put(entry.getKey(), new PrecompressedHttpContent(this, entry.getValue(), entry.getKey()));
for (Map.Entry<CompressedContentFormat, HttpContent> entry : precompressedContents.entrySet())
{
_precompressedContents.put(entry.getKey(),new PrecompressedHttpContent(this,entry.getValue(),entry.getKey()));
}
}
}

View File

@ -237,73 +237,82 @@ public class ResourceCache implements HttpContent.Factory
private HttpContent load(String pathInContext, Resource resource, int maxBufferSize)
throws IOException
{
if (resource==null || !resource.exists())
if (resource == null || !resource.exists())
return null;
if (resource.isDirectory())
return new ResourceHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),getMaxCachedFileSize());
// Will it fit in the cache?
if (isCacheable(resource))
{
CachedHttpContent content=null;
{
CachedHttpContent content = null;
// Look for precompressed resources
if (_precompressedFormats.length > 0)
{
Map<CompressedContentFormat, CachedHttpContent> precompresssedContents=new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format : _precompressedFormats) {
String compressedPathInContext=pathInContext+format._extension;
CachedHttpContent compressedContent=_cache.get(compressedPathInContext);
if (compressedContent==null || compressedContent.isValid()) {
compressedContent=null;
Map<CompressedContentFormat, CachedHttpContent> precompresssedContents = new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format : _precompressedFormats)
{
String compressedPathInContext = pathInContext + format._extension;
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent == null || compressedContent.isValid())
{
compressedContent = null;
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified()>=resource.lastModified() && compressedResource.length()<resource.length()) {
compressedContent=new CachedHttpContent(compressedPathInContext,compressedResource,null);
CachedHttpContent added=_cache.putIfAbsent(compressedPathInContext,compressedContent);
if (added!=null) {
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified()
&& compressedResource.length() < resource.length())
{
compressedContent = new CachedHttpContent(compressedPathInContext,compressedResource,null);
CachedHttpContent added = _cache.putIfAbsent(compressedPathInContext,compressedContent);
if (added != null)
{
compressedContent.invalidate();
compressedContent=added;
compressedContent = added;
}
}
}
if (compressedContent!=null)
if (compressedContent != null)
precompresssedContents.put(format,compressedContent);
}
content = new CachedHttpContent(pathInContext,resource,precompresssedContents);
}
else
else
content = new CachedHttpContent(pathInContext,resource,null);
// Add it to the cache.
CachedHttpContent added = _cache.putIfAbsent(pathInContext,content);
if (added!=null)
if (added != null)
{
content.invalidate();
content=added;
content = added;
}
return content;
}
// Look for non Cacheable precompressed resource or content
String mt = _mimeTypes.getMimeByExtension(pathInContext);
if (_precompressedFormats.length>0) {
if (_precompressedFormats.length > 0)
{
// Is the precompressed content cached?
Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>();
for (CompressedContentFormat format : _precompressedFormats) {
String compressedPathInContext=pathInContext+format._extension;
CachedHttpContent compressedContent=_cache.get(compressedPathInContext);
if (compressedContent!=null && compressedContent.isValid() && compressedContent.getResource().lastModified()>=resource.lastModified())
for (CompressedContentFormat format : _precompressedFormats)
{
String compressedPathInContext = pathInContext + format._extension;
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent != null && compressedContent.isValid() && compressedContent.getResource().lastModified() >= resource.lastModified())
compressedContents.put(format,compressedContent);
// Is there a precompressed resource?
Resource compressedResource=_factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified()>=resource.lastModified() && compressedResource.length()<resource.length())
compressedContents.put(format,new ResourceHttpContent(compressedResource,_mimeTypes.getMimeByExtension(compressedPathInContext),maxBufferSize));
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified()
&& compressedResource.length() < resource.length())
compressedContents.put(format,
new ResourceHttpContent(compressedResource,_mimeTypes.getMimeByExtension(compressedPathInContext),maxBufferSize));
}
if (!compressedContents.isEmpty())
return new ResourceHttpContent(resource, mt, maxBufferSize, compressedContents);
return new ResourceHttpContent(resource,mt,maxBufferSize,compressedContents);
}
return new ResourceHttpContent(resource,mt,maxBufferSize);
@ -435,12 +444,16 @@ public class ResourceCache implements HttpContent.Factory
_etag=ResourceCache.this._etags?new PreEncodedHttpField(HttpHeader.ETAG,resource.getWeakETag()):null;
if (precompressedResources != null) {
if (precompressedResources != null)
{
_precompressed = new HashMap<>(precompressedResources.size());
for (Map.Entry<CompressedContentFormat, CachedHttpContent> entry : precompressedResources.entrySet()) {
_precompressed.put(entry.getKey(), new CachedPrecompressedHttpContent(this, entry.getValue(), entry.getKey()));
for (Map.Entry<CompressedContentFormat, CachedHttpContent> entry : precompressedResources.entrySet())
{
_precompressed.put(entry.getKey(),new CachedPrecompressedHttpContent(this,entry.getValue(),entry.getKey()));
}
} else {
}
else
{
_precompressed = null;
}
}
@ -570,7 +583,6 @@ public class ResourceCache implements HttpContent.Factory
return _mimeType;
}
/* ------------------------------------------------------------ */
@Override
public void release()
@ -676,7 +688,8 @@ public class ResourceCache implements HttpContent.Factory
Map<CompressedContentFormat, CachedPrecompressedHttpContent> ret=_precompressed;
for (Map.Entry<CompressedContentFormat, CachedPrecompressedHttpContent> entry:_precompressed.entrySet())
{
if (!entry.getValue().isValid()) {
if (!entry.getValue().isValid())
{
if (ret == _precompressed)
ret = new HashMap<>(_precompressed);
ret.remove(entry.getKey());

View File

@ -71,17 +71,21 @@ public class ResourceContentFactory implements Factory
if (resource.isDirectory())
return new ResourceHttpContent(resource,_mimeTypes.getMimeByExtension(resource.toString()),maxBufferSize);
// Look for a precompressed resource or content
String mt = _mimeTypes.getMimeByExtension(pathInContext);
if (_precompressedFormats.length>0) {
if (_precompressedFormats.length > 0)
{
// Is there a compressed resource?
Map<CompressedContentFormat,HttpContent> compressedContents = new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format:_precompressedFormats) {
String compressedPathInContext=pathInContext+format._extension;
Resource compressedResource=_factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified()>=resource.lastModified() && compressedResource.length()<resource.length())
compressedContents.put(format, new ResourceHttpContent(compressedResource,_mimeTypes.getMimeByExtension(compressedPathInContext),maxBufferSize));
Map<CompressedContentFormat, HttpContent> compressedContents = new HashMap<>(_precompressedFormats.length);
for (CompressedContentFormat format : _precompressedFormats)
{
String compressedPathInContext = pathInContext + format._extension;
Resource compressedResource = _factory.getResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified() >= resource.lastModified()
&& compressedResource.length() < resource.length())
compressedContents.put(format,
new ResourceHttpContent(compressedResource,_mimeTypes.getMimeByExtension(compressedPathInContext),maxBufferSize));
}
if (!compressedContents.isEmpty())
return new ResourceHttpContent(resource,mt,maxBufferSize,compressedContents);

View File

@ -285,7 +285,8 @@ public abstract class ResourceService
}
}
private CompressedContentFormat getBestPrecompressedContent(List<String> preferredEncodings, Collection<CompressedContentFormat> availableFormats) {
private CompressedContentFormat getBestPrecompressedContent(List<String> preferredEncodings, Collection<CompressedContentFormat> availableFormats)
{
if (availableFormats.isEmpty())
return null;

View File

@ -322,10 +322,10 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory
@Deprecated
public boolean isGzip()
{
for (CompressedContentFormat formats : _resourceService.getPrecompressedFormats()) {
if (CompressedContentFormat.GZIP._encoding.equals(formats._encoding)) {
for (CompressedContentFormat formats : _resourceService.getPrecompressedFormats())
{
if (CompressedContentFormat.GZIP._encoding.equals(formats._encoding))
return true;
}
}
return false;
}

View File

@ -325,21 +325,29 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
LOG.debug("resource base = "+_resourceBase);
}
private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, boolean gzip) {
private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, boolean gzip)
{
List<CompressedContentFormat> ret = new ArrayList<>();
if (precompressed != null && precompressed.indexOf('=') > 0) {
for (String pair : precompressed.split(",")) {
if (precompressed != null && precompressed.indexOf('=') > 0)
{
for (String pair : precompressed.split(","))
{
String[] setting = pair.split("=");
String encoding = setting[0];
String extension = setting[1];
ret.add(new CompressedContentFormat(encoding, extension));
ret.add(new CompressedContentFormat(encoding,extension));
}
} else if (precompressed != null) {
if (Boolean.parseBoolean(precompressed)) {
}
else if (precompressed != null)
{
if (Boolean.parseBoolean(precompressed))
{
ret.add(CompressedContentFormat.BR);
ret.add(CompressedContentFormat.GZIP);
}
} else if (gzip) {
}
else if (gzip)
{
// gzip handling is for backwards compatibility with older Jetty
ret.add(CompressedContentFormat.GZIP);
}