diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/PrecompressedHttpContent.java b/jetty-http/src/main/java/org/eclipse/jetty/http/PrecompressedHttpContent.java index 0a2df81bfb4..a5256f91561 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/PrecompressedHttpContent.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/PrecompressedHttpContent.java @@ -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"); } } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/ResourceHttpContent.java b/jetty-http/src/main/java/org/eclipse/jetty/http/ResourceHttpContent.java index 92a5a912d6d..591fc80653c 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/ResourceHttpContent.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/ResourceHttpContent.java @@ -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 entry : precompressedContents.entrySet()) { - _precompressedContents.put(entry.getKey(), new PrecompressedHttpContent(this, entry.getValue(), entry.getKey())); + for (Map.Entry entry : precompressedContents.entrySet()) + { + _precompressedContents.put(entry.getKey(),new PrecompressedHttpContent(this,entry.getValue(),entry.getKey())); } } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java index d4e19e1c5e8..004f17d526a 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceCache.java @@ -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 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 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.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 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.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 entry : precompressedResources.entrySet()) { - _precompressed.put(entry.getKey(), new CachedPrecompressedHttpContent(this, entry.getValue(), entry.getKey())); + for (Map.Entry 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 ret=_precompressed; for (Map.Entry entry:_precompressed.entrySet()) { - if (!entry.getValue().isValid()) { + if (!entry.getValue().isValid()) + { if (ret == _precompressed) ret = new HashMap<>(_precompressed); ret.remove(entry.getKey()); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceContentFactory.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceContentFactory.java index bf418daf76c..d17877d116d 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceContentFactory.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceContentFactory.java @@ -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 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() 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); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java index 082be47b07a..7aef29bab8b 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ResourceService.java @@ -285,7 +285,8 @@ public abstract class ResourceService } } - private CompressedContentFormat getBestPrecompressedContent(List preferredEncodings, Collection availableFormats) { + private CompressedContentFormat getBestPrecompressedContent(List preferredEncodings, Collection availableFormats) + { if (availableFormats.isEmpty()) return null; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java index 6a0bb0bb197..4a8b9ec155a 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java @@ -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; } diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index 0867f258246..1d8ca51f84e 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -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 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); }