cleanups of HttpContentWrapper and ResourceService

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2022-10-13 17:25:47 +11:00
parent 6771db40bf
commit 2d161bfecd
4 changed files with 23 additions and 19 deletions

View File

@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.util.NanoTime; import org.eclipse.jetty.util.NanoTime;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.resource.Resource;
/** /**
* HttpContent.ContentFactory implementation that wraps any other HttpContent.ContentFactory instance * HttpContent.ContentFactory implementation that wraps any other HttpContent.ContentFactory instance
@ -223,23 +224,24 @@ public class CachingContentFactory implements HttpContent.ContentFactory
{ {
super(httpContent); super(httpContent);
if (_delegate.getResource() == null) Resource resource = getResource();
if (resource == null)
throw new IllegalArgumentException("Null Resource"); throw new IllegalArgumentException("Null Resource");
if (!_delegate.getResource().exists()) if (!resource.exists())
throw new IllegalArgumentException("Resource doesn't exist: " + _delegate.getResource()); throw new IllegalArgumentException("Resource doesn't exist: " + resource);
if (_delegate.getResource().isDirectory()) if (resource.isDirectory())
throw new IllegalArgumentException("Directory Resources not supported: " + _delegate.getResource()); throw new IllegalArgumentException("Directory Resources not supported: " + resource);
if (_delegate.getResource().getPath() == null) // only required because we need the Path to access the mapped ByteBuffer or SeekableByteChannel. if (resource.getPath() == null) // only required because we need the Path to access the mapped ByteBuffer or SeekableByteChannel.
throw new IllegalArgumentException("Resource not backed by Path not supported: " + _delegate.getResource()); throw new IllegalArgumentException("Resource not backed by Path not supported: " + resource);
// Resources with negative length cannot be cached. // Resources with negative length cannot be cached.
// But allow resources with zero length. // But allow resources with zero length.
long resourceSize = _delegate.getResource().length(); long resourceSize = resource.length();
if (resourceSize < 0) if (resourceSize < 0)
throw new IllegalArgumentException("Resource with negative size: " + _delegate.getResource()); throw new IllegalArgumentException("Resource with negative size: " + resource);
// TODO: do all the following lazily and asynchronously. // TODO: do all the following lazily and asynchronously.
HttpField etagField = _delegate.getETag(); HttpField etagField = httpContent.getETag();
if (StringUtil.isNotBlank(etagValue)) if (StringUtil.isNotBlank(etagValue))
{ {
etagField = new PreEncodedHttpField(HttpHeader.ETAG, etagValue); etagField = new PreEncodedHttpField(HttpHeader.ETAG, etagValue);
@ -250,7 +252,7 @@ public class CachingContentFactory implements HttpContent.ContentFactory
// Map the content into memory if possible. // Map the content into memory if possible.
_buffer = httpContent.getBuffer(); _buffer = httpContent.getBuffer();
_cacheKey = key; _cacheKey = key;
_lastModifiedValue = _delegate.getResource().lastModified(); _lastModifiedValue = resource.lastModified();
_lastAccessed.set(NanoTime.now()); _lastAccessed.set(NanoTime.now());
} }
@ -279,11 +281,7 @@ public class CachingContentFactory implements HttpContent.ContentFactory
(NanoTime.since(lastChecked) > TimeUnit.SECONDS.toNanos(1)) ? now : lastChecked) != now) (NanoTime.since(lastChecked) > TimeUnit.SECONDS.toNanos(1)) ? now : lastChecked) != now)
return true; return true;
Instant lastModifiedTime = _delegate.getResource().lastModified(); return getLastModifiedInstant().equals(_lastModifiedValue);
if (lastModifiedTime.equals(_lastModifiedValue))
return true;
release();
return false;
} }
@Override @Override

View File

@ -24,13 +24,18 @@ import org.eclipse.jetty.util.resource.Resource;
*/ */
public class HttpContentWrapper implements HttpContent public class HttpContentWrapper implements HttpContent
{ {
protected final HttpContent _delegate; private final HttpContent _delegate;
public HttpContentWrapper(HttpContent content) public HttpContentWrapper(HttpContent content)
{ {
_delegate = content; _delegate = content;
} }
public HttpContent getWrapped()
{
return _delegate;
}
@Override @Override
public HttpField getContentType() public HttpField getContentType()
{ {

View File

@ -209,7 +209,7 @@ public class ResourceService
HttpField contentEncoding = content.getContentEncoding(); HttpField contentEncoding = content.getContentEncoding();
if (contentEncoding != null) if (contentEncoding != null)
response.getHeaders().put(contentEncoding); response.getHeaders().put(contentEncoding);
else if (isGzippedContent(pathInContext)) else if (isImplicitlyGzippedContent(pathInContext))
response.getHeaders().put(HttpHeader.CONTENT_ENCODING, "gzip"); response.getHeaders().put(HttpHeader.CONTENT_ENCODING, "gzip");
// Send the data // Send the data
@ -285,7 +285,7 @@ public class ResourceService
return values; return values;
} }
private boolean isGzippedContent(String path) private boolean isImplicitlyGzippedContent(String path)
{ {
if (path == null || _gzipEquivalentFileExtensions == null) if (path == null || _gzipEquivalentFileExtensions == null)
return false; return false;

View File

@ -95,6 +95,7 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory,
if (_mimeTypes == null) if (_mimeTypes == null)
_mimeTypes = _context == null ? new MimeTypes() : _context.getMimeTypes(); _mimeTypes = _context == null ? new MimeTypes() : _context.getMimeTypes();
// TODO: SHOULD BE CACHING AND EXTENSIBLE.
_resourceService.setContentFactory(new ResourceContentFactory(this, _mimeTypes)); _resourceService.setContentFactory(new ResourceContentFactory(this, _mimeTypes));
_resourceService.setWelcomeFactory(this); _resourceService.setWelcomeFactory(this);