Core HTTP protocol support library for Eclipse Jetty providing header handling, parsing, generation, compliance validation, and content processing capabilities
—
HTTP content handling with factory patterns, caching, compression support, virtual file systems, and efficient resource management.
Core content interface with metadata and resource management.
/**
* HTTP content with metadata and resource management
*/
interface HttpContent {
/** Get Content-Type header field */
HttpField getContentType();
/** Get Content-Type header value */
String getContentTypeValue();
/** Get character encoding from Content-Type */
String getCharacterEncoding();
/** Get MIME type information */
MimeTypes.Type getMimeType();
/** Get Content-Encoding header field */
HttpField getContentEncoding();
/** Get Content-Encoding header value */
String getContentEncodingValue();
/** Get Content-Length header field */
HttpField getContentLength();
/** Get Content-Length header value */
long getContentLengthValue();
/** Get last modified time as Instant */
Instant getLastModifiedInstant();
/** Get Last-Modified header field */
HttpField getLastModified();
/** Get Last-Modified header value */
String getLastModifiedValue();
/** Get ETag header field */
HttpField getETag();
/** Get ETag header value */
String getETagValue();
/** Get underlying resource */
Resource getResource();
/** Get content as ByteBuffer */
ByteBuffer getByteBuffer();
/** Get memory footprint in bytes */
long getBytesOccupied();
/** Get available pre-compressed formats */
Set<CompressedContentFormat> getPreCompressedContentFormats();
/** Release content resources */
void release();
/** Retain content reference */
boolean retain();
/**
* Factory interface for creating HTTP content
*/
interface Factory {
/** Get content for path */
HttpContent getContent(String path);
/** Get content with accept-encoding consideration */
HttpContent getContent(String path, int maxBufferSize);
}
/**
* Base wrapper for HTTP content
*/
abstract class Wrapper implements HttpContent {
/** Get wrapped content */
protected HttpContent getWrapped();
/** Unwrap to original content */
HttpContent unwrap();
}
}HTTP content backed by Resource with efficient metadata handling.
/**
* HTTP content backed by Resource
*/
class ResourceHttpContent implements HttpContent {
/** Create content from resource and content type */
ResourceHttpContent(Resource resource, String contentType);
/** Create content from resource with MIME type detection */
ResourceHttpContent(Resource resource, MimeTypes mimeTypes);
/** Create content from resource, MIME types, and buffer pool */
ResourceHttpContent(Resource resource, MimeTypes mimeTypes,
ByteBufferPool bufferPool);
/** Get underlying resource */
Resource getResource();
/** Check if content can be cached */
boolean isCacheable();
/** Get buffer pool used for content */
ByteBufferPool getBufferPool();
}Various factory implementations for different content sources and caching strategies.
/**
* Factory for resource-based HTTP content
*/
class ResourceHttpContentFactory implements HttpContent.Factory {
/** Create factory with resource factory and MIME types */
ResourceHttpContentFactory(ResourceFactory resourceFactory, MimeTypes mimeTypes);
/** Create with resource factory, MIME types, and buffer pool */
ResourceHttpContentFactory(ResourceFactory resourceFactory, MimeTypes mimeTypes,
ByteBufferPool bufferPool);
/** Get content for path */
HttpContent getContent(String path);
/** Get resource factory */
ResourceFactory getResourceFactory();
/** Get MIME types */
MimeTypes getMimeTypes();
/** Get buffer pool */
ByteBufferPool getBufferPool();
}
/**
* Caching HTTP content factory with size and count limits
*/
class CachingHttpContentFactory implements HttpContent.Factory {
/** Create caching factory with authority and buffer pool */
CachingHttpContentFactory(HttpContent.Factory authority, ByteBufferPool bufferPool);
/** Get content with caching */
HttpContent getContent(String path);
/** Get maximum cached file size */
int getMaxCachedFileSize();
/** Set maximum cached file size */
void setMaxCachedFileSize(int maxCachedFileSize);
/** Get maximum total cache size */
long getMaxCacheSize();
/** Set maximum total cache size */
void setMaxCacheSize(long maxCacheSize);
/** Get maximum number of cached files */
int getMaxCachedFiles();
/** Set maximum number of cached files */
void setMaxCachedFiles(int maxCachedFiles);
/** Check if using direct byte buffers */
boolean isUseDirectByteBuffers();
/** Set whether to use direct byte buffers */
void setUseDirectByteBuffers(boolean useDirectByteBuffers);
/** Get current cache size in bytes */
long getCachedSize();
/** Get current number of cached files */
int getCachedFiles();
/** Flush/clear entire cache */
void flushCache();
/** Remove specific content from cache */
boolean flushContent(String path);
}
/**
* Validating cache with periodic sweeping of stale entries
*/
class ValidatingCachingHttpContentFactory extends CachingHttpContentFactory
implements Runnable {
/** Create validating cache */
ValidatingCachingHttpContentFactory(HttpContent.Factory authority,
ByteBufferPool bufferPool);
/** Get sweep period in milliseconds */
long getSweepPeriod();
/** Set sweep period in milliseconds */
void setSweepPeriod(long sweepPeriod);
/** Run cache validation sweep */
void run();
/** Start background sweeping */
void start();
/** Stop background sweeping */
void stop();
}
/**
* Factory with pre-compression support
*/
class PreCompressedHttpContentFactory implements HttpContent.Factory {
/** Create with delegate factory and compression formats */
PreCompressedHttpContentFactory(HttpContent.Factory factory,
CompressedContentFormat... formats);
/** Get content with compression negotiation */
HttpContent getContent(String path);
/** Get supported compression formats */
CompressedContentFormat[] getFormats();
/** Get delegate factory */
HttpContent.Factory getFactory();
}
/**
* Virtual file system content factory
*/
class VirtualHttpContentFactory implements HttpContent.Factory {
/** Create virtual content factory */
VirtualHttpContentFactory();
/** Add virtual path mapping */
void addPath(String path, Resource resource);
/** Remove virtual path mapping */
boolean removePath(String path);
/** Get content for virtual path */
HttpContent getContent(String path);
/** Get all virtual paths */
Set<String> getPaths();
}
/**
* File extension to MIME type mapping factory
*/
class FileMappingHttpContentFactory implements HttpContent.Factory {
/** Create with delegate factory and extension mappings */
FileMappingHttpContentFactory(HttpContent.Factory factory);
/** Add file extension mapping */
void addMapping(String extension, String mimeType);
/** Remove file extension mapping */
boolean removeMapping(String extension);
/** Get content with extension-based MIME type */
HttpContent getContent(String path);
}Specialized content implementations for specific use cases.
/**
* Pre-compressed content wrapper
*/
class PreCompressedHttpContent implements HttpContent {
/** Create pre-compressed content wrapper */
PreCompressedHttpContent(HttpContent content, HttpContent precompressedContent,
CompressedContentFormat format);
/** Get original uncompressed content */
HttpContent getContent();
/** Get pre-compressed content */
HttpContent getPreCompressedContent();
/** Get compression format */
CompressedContentFormat getFormat();
}
/**
* Compressed content format definition
*/
class CompressedContentFormat {
/** GZIP compression format */
static final CompressedContentFormat GZIP;
/** Brotli compression format */
static final CompressedContentFormat BR;
/** Create custom compression format */
CompressedContentFormat(String encoding, String extension);
/** Get encoding name (e.g., "gzip", "br") */
String getEncoding();
/** Get file extension (e.g., ".gz", ".br") */
String getExtension();
/** Check if format matches encoding */
boolean matches(String encoding);
}MIME type detection and management for content.
/**
* MIME type detection and management
*/
class MimeTypes {
/** Create MIME types with default mappings */
MimeTypes();
/** Get MIME type for file extension */
String getMimeByExtension(String filename);
/** Get MIME type information */
Type getType(String mimeType);
/** Add MIME type mapping */
void addMimeMapping(String extension, String mimeType);
/** Get all known extensions */
Set<String> getExtensions();
/**
* MIME type information
*/
static class Type {
/** Get MIME type string */
String asString();
/** Get primary type */
String getPrimaryType();
/** Get sub type */
String getSubType();
/** Get character set */
String getCharset();
/** Check if type matches */
boolean is(String type);
/** Check if text content */
boolean isText();
/** Check if binary content */
boolean isBinary();
}
}Usage Examples:
import org.eclipse.jetty.http.content.*;
import org.eclipse.jetty.util.resource.*;
// Basic resource content factory
ResourceFactory resourceFactory = ResourceFactory.root()
.newResource(Path.of("/var/www/html"));
MimeTypes mimeTypes = new MimeTypes();
ByteBufferPool bufferPool = new ArrayByteBufferPool();
HttpContent.Factory contentFactory = new ResourceHttpContentFactory(
resourceFactory, mimeTypes, bufferPool
);
// Get content for a file
HttpContent content = contentFactory.getContent("/index.html");
if (content != null) {
String contentType = content.getContentTypeValue(); // "text/html"
long contentLength = content.getContentLengthValue(); // file size
String etag = content.getETagValue(); // ETag for caching
ByteBuffer buffer = content.getByteBuffer(); // file content
// Release when done
content.release();
}
// Caching content factory for performance
HttpContent.Factory cachingFactory = new CachingHttpContentFactory(contentFactory, bufferPool);
CachingHttpContentFactory cache = (CachingHttpContentFactory) cachingFactory;
// Configure cache limits
cache.setMaxCachedFileSize(1024 * 1024); // 1MB max file size
cache.setMaxCacheSize(50 * 1024 * 1024); // 50MB total cache size
cache.setMaxCachedFiles(1000); // max 1000 files
cache.setUseDirectByteBuffers(true); // use direct buffers
// Monitor cache usage
long cacheSize = cache.getCachedSize();
int cachedFiles = cache.getCachedFiles();
// Clear cache when needed
cache.flushCache();
// Pre-compression support
CompressedContentFormat[] formats = {
CompressedContentFormat.GZIP,
CompressedContentFormat.BR
};
HttpContent.Factory compressedFactory = new PreCompressedHttpContentFactory(
cachingFactory, formats
);
// Will serve .gz or .br versions if available and client supports
HttpContent compressedContent = compressedFactory.getContent("/app.js");
// Virtual file system
VirtualHttpContentFactory virtualFactory = new VirtualHttpContentFactory();
// Map virtual paths to resources
Resource configResource = resourceFactory.newResource(Path.of("/etc/myapp/config.json"));
virtualFactory.addPath("/api/config", configResource);
HttpContent virtualContent = virtualFactory.getContent("/api/config");
// File extension mapping
FileMappingHttpContentFactory mappingFactory =
new FileMappingHttpContentFactory(contentFactory);
// Add custom MIME type mappings
mappingFactory.addMapping(".myext", "application/x-myformat");
// MIME type detection
MimeTypes.Type htmlType = mimeTypes.getType("text/html");
boolean isText = htmlType.isText(); // true
String charset = htmlType.getCharset(); // "utf-8" if specified
String mimeType = mimeTypes.getMimeByExtension("image.png"); // "image/png"
// Working with content metadata
if (content != null) {
// Check modification time
Instant lastModified = content.getLastModifiedInstant();
// Get headers for HTTP response
HttpField contentTypeField = content.getContentType();
HttpField contentLengthField = content.getContentLength();
HttpField lastModifiedField = content.getLastModified();
HttpField etagField = content.getETag();
// Check available compressions
Set<CompressedContentFormat> compressions =
content.getPreCompressedContentFormats();
boolean hasGzip = compressions.contains(CompressedContentFormat.GZIP);
// Memory usage
long memoryUsage = content.getBytesOccupied();
}
// Validating cache with automatic cleanup
ValidatingCachingHttpContentFactory validatingCache =
new ValidatingCachingHttpContentFactory(contentFactory, bufferPool);
validatingCache.setSweepPeriod(60 * 1000); // sweep every minute
validatingCache.start(); // start background sweeping
// Custom content implementation
class DatabaseHttpContent implements HttpContent {
private final String contentType;
private final byte[] data;
private final Instant lastModified;
// Implement all HttpContent methods...
@Override
public String getContentTypeValue() { return contentType; }
@Override
public long getContentLengthValue() { return data.length; }
@Override
public ByteBuffer getByteBuffer() {
return ByteBuffer.wrap(data).asReadOnlyBuffer();
}
// ... other implementations
}
// Resource management
try {
HttpContent managedContent = contentFactory.getContent("/large-file.dat");
if (managedContent != null) {
// Use content
processContent(managedContent);
}
} finally {
// Always release resources
if (managedContent != null) {
managedContent.release();
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-http