0
# Content Management
1
2
HTTP content handling with factory patterns, caching, compression support, virtual file systems, and efficient resource management.
3
4
## Capabilities
5
6
### HttpContent Interface
7
8
Core content interface with metadata and resource management.
9
10
```java { .api }
11
/**
12
* HTTP content with metadata and resource management
13
*/
14
interface HttpContent {
15
/** Get Content-Type header field */
16
HttpField getContentType();
17
18
/** Get Content-Type header value */
19
String getContentTypeValue();
20
21
/** Get character encoding from Content-Type */
22
String getCharacterEncoding();
23
24
/** Get MIME type information */
25
MimeTypes.Type getMimeType();
26
27
/** Get Content-Encoding header field */
28
HttpField getContentEncoding();
29
30
/** Get Content-Encoding header value */
31
String getContentEncodingValue();
32
33
/** Get Content-Length header field */
34
HttpField getContentLength();
35
36
/** Get Content-Length header value */
37
long getContentLengthValue();
38
39
/** Get last modified time as Instant */
40
Instant getLastModifiedInstant();
41
42
/** Get Last-Modified header field */
43
HttpField getLastModified();
44
45
/** Get Last-Modified header value */
46
String getLastModifiedValue();
47
48
/** Get ETag header field */
49
HttpField getETag();
50
51
/** Get ETag header value */
52
String getETagValue();
53
54
/** Get underlying resource */
55
Resource getResource();
56
57
/** Get content as ByteBuffer */
58
ByteBuffer getByteBuffer();
59
60
/** Get memory footprint in bytes */
61
long getBytesOccupied();
62
63
/** Get available pre-compressed formats */
64
Set<CompressedContentFormat> getPreCompressedContentFormats();
65
66
/** Release content resources */
67
void release();
68
69
/** Retain content reference */
70
boolean retain();
71
72
/**
73
* Factory interface for creating HTTP content
74
*/
75
interface Factory {
76
/** Get content for path */
77
HttpContent getContent(String path);
78
79
/** Get content with accept-encoding consideration */
80
HttpContent getContent(String path, int maxBufferSize);
81
}
82
83
/**
84
* Base wrapper for HTTP content
85
*/
86
abstract class Wrapper implements HttpContent {
87
/** Get wrapped content */
88
protected HttpContent getWrapped();
89
90
/** Unwrap to original content */
91
HttpContent unwrap();
92
}
93
}
94
```
95
96
### ResourceHttpContent Class
97
98
HTTP content backed by Resource with efficient metadata handling.
99
100
```java { .api }
101
/**
102
* HTTP content backed by Resource
103
*/
104
class ResourceHttpContent implements HttpContent {
105
/** Create content from resource and content type */
106
ResourceHttpContent(Resource resource, String contentType);
107
108
/** Create content from resource with MIME type detection */
109
ResourceHttpContent(Resource resource, MimeTypes mimeTypes);
110
111
/** Create content from resource, MIME types, and buffer pool */
112
ResourceHttpContent(Resource resource, MimeTypes mimeTypes,
113
ByteBufferPool bufferPool);
114
115
/** Get underlying resource */
116
Resource getResource();
117
118
/** Check if content can be cached */
119
boolean isCacheable();
120
121
/** Get buffer pool used for content */
122
ByteBufferPool getBufferPool();
123
}
124
```
125
126
### Content Factory Implementations
127
128
Various factory implementations for different content sources and caching strategies.
129
130
```java { .api }
131
/**
132
* Factory for resource-based HTTP content
133
*/
134
class ResourceHttpContentFactory implements HttpContent.Factory {
135
/** Create factory with resource factory and MIME types */
136
ResourceHttpContentFactory(ResourceFactory resourceFactory, MimeTypes mimeTypes);
137
138
/** Create with resource factory, MIME types, and buffer pool */
139
ResourceHttpContentFactory(ResourceFactory resourceFactory, MimeTypes mimeTypes,
140
ByteBufferPool bufferPool);
141
142
/** Get content for path */
143
HttpContent getContent(String path);
144
145
/** Get resource factory */
146
ResourceFactory getResourceFactory();
147
148
/** Get MIME types */
149
MimeTypes getMimeTypes();
150
151
/** Get buffer pool */
152
ByteBufferPool getBufferPool();
153
}
154
155
/**
156
* Caching HTTP content factory with size and count limits
157
*/
158
class CachingHttpContentFactory implements HttpContent.Factory {
159
/** Create caching factory with authority and buffer pool */
160
CachingHttpContentFactory(HttpContent.Factory authority, ByteBufferPool bufferPool);
161
162
/** Get content with caching */
163
HttpContent getContent(String path);
164
165
/** Get maximum cached file size */
166
int getMaxCachedFileSize();
167
168
/** Set maximum cached file size */
169
void setMaxCachedFileSize(int maxCachedFileSize);
170
171
/** Get maximum total cache size */
172
long getMaxCacheSize();
173
174
/** Set maximum total cache size */
175
void setMaxCacheSize(long maxCacheSize);
176
177
/** Get maximum number of cached files */
178
int getMaxCachedFiles();
179
180
/** Set maximum number of cached files */
181
void setMaxCachedFiles(int maxCachedFiles);
182
183
/** Check if using direct byte buffers */
184
boolean isUseDirectByteBuffers();
185
186
/** Set whether to use direct byte buffers */
187
void setUseDirectByteBuffers(boolean useDirectByteBuffers);
188
189
/** Get current cache size in bytes */
190
long getCachedSize();
191
192
/** Get current number of cached files */
193
int getCachedFiles();
194
195
/** Flush/clear entire cache */
196
void flushCache();
197
198
/** Remove specific content from cache */
199
boolean flushContent(String path);
200
}
201
202
/**
203
* Validating cache with periodic sweeping of stale entries
204
*/
205
class ValidatingCachingHttpContentFactory extends CachingHttpContentFactory
206
implements Runnable {
207
208
/** Create validating cache */
209
ValidatingCachingHttpContentFactory(HttpContent.Factory authority,
210
ByteBufferPool bufferPool);
211
212
/** Get sweep period in milliseconds */
213
long getSweepPeriod();
214
215
/** Set sweep period in milliseconds */
216
void setSweepPeriod(long sweepPeriod);
217
218
/** Run cache validation sweep */
219
void run();
220
221
/** Start background sweeping */
222
void start();
223
224
/** Stop background sweeping */
225
void stop();
226
}
227
228
/**
229
* Factory with pre-compression support
230
*/
231
class PreCompressedHttpContentFactory implements HttpContent.Factory {
232
/** Create with delegate factory and compression formats */
233
PreCompressedHttpContentFactory(HttpContent.Factory factory,
234
CompressedContentFormat... formats);
235
236
/** Get content with compression negotiation */
237
HttpContent getContent(String path);
238
239
/** Get supported compression formats */
240
CompressedContentFormat[] getFormats();
241
242
/** Get delegate factory */
243
HttpContent.Factory getFactory();
244
}
245
246
/**
247
* Virtual file system content factory
248
*/
249
class VirtualHttpContentFactory implements HttpContent.Factory {
250
/** Create virtual content factory */
251
VirtualHttpContentFactory();
252
253
/** Add virtual path mapping */
254
void addPath(String path, Resource resource);
255
256
/** Remove virtual path mapping */
257
boolean removePath(String path);
258
259
/** Get content for virtual path */
260
HttpContent getContent(String path);
261
262
/** Get all virtual paths */
263
Set<String> getPaths();
264
}
265
266
/**
267
* File extension to MIME type mapping factory
268
*/
269
class FileMappingHttpContentFactory implements HttpContent.Factory {
270
/** Create with delegate factory and extension mappings */
271
FileMappingHttpContentFactory(HttpContent.Factory factory);
272
273
/** Add file extension mapping */
274
void addMapping(String extension, String mimeType);
275
276
/** Remove file extension mapping */
277
boolean removeMapping(String extension);
278
279
/** Get content with extension-based MIME type */
280
HttpContent getContent(String path);
281
}
282
```
283
284
### Specialized Content Classes
285
286
Specialized content implementations for specific use cases.
287
288
```java { .api }
289
/**
290
* Pre-compressed content wrapper
291
*/
292
class PreCompressedHttpContent implements HttpContent {
293
/** Create pre-compressed content wrapper */
294
PreCompressedHttpContent(HttpContent content, HttpContent precompressedContent,
295
CompressedContentFormat format);
296
297
/** Get original uncompressed content */
298
HttpContent getContent();
299
300
/** Get pre-compressed content */
301
HttpContent getPreCompressedContent();
302
303
/** Get compression format */
304
CompressedContentFormat getFormat();
305
}
306
307
/**
308
* Compressed content format definition
309
*/
310
class CompressedContentFormat {
311
/** GZIP compression format */
312
static final CompressedContentFormat GZIP;
313
314
/** Brotli compression format */
315
static final CompressedContentFormat BR;
316
317
/** Create custom compression format */
318
CompressedContentFormat(String encoding, String extension);
319
320
/** Get encoding name (e.g., "gzip", "br") */
321
String getEncoding();
322
323
/** Get file extension (e.g., ".gz", ".br") */
324
String getExtension();
325
326
/** Check if format matches encoding */
327
boolean matches(String encoding);
328
}
329
```
330
331
### MIME Types Support
332
333
MIME type detection and management for content.
334
335
```java { .api }
336
/**
337
* MIME type detection and management
338
*/
339
class MimeTypes {
340
/** Create MIME types with default mappings */
341
MimeTypes();
342
343
/** Get MIME type for file extension */
344
String getMimeByExtension(String filename);
345
346
/** Get MIME type information */
347
Type getType(String mimeType);
348
349
/** Add MIME type mapping */
350
void addMimeMapping(String extension, String mimeType);
351
352
/** Get all known extensions */
353
Set<String> getExtensions();
354
355
/**
356
* MIME type information
357
*/
358
static class Type {
359
/** Get MIME type string */
360
String asString();
361
362
/** Get primary type */
363
String getPrimaryType();
364
365
/** Get sub type */
366
String getSubType();
367
368
/** Get character set */
369
String getCharset();
370
371
/** Check if type matches */
372
boolean is(String type);
373
374
/** Check if text content */
375
boolean isText();
376
377
/** Check if binary content */
378
boolean isBinary();
379
}
380
}
381
```
382
383
**Usage Examples:**
384
385
```java
386
import org.eclipse.jetty.http.content.*;
387
import org.eclipse.jetty.util.resource.*;
388
389
// Basic resource content factory
390
ResourceFactory resourceFactory = ResourceFactory.root()
391
.newResource(Path.of("/var/www/html"));
392
MimeTypes mimeTypes = new MimeTypes();
393
ByteBufferPool bufferPool = new ArrayByteBufferPool();
394
395
HttpContent.Factory contentFactory = new ResourceHttpContentFactory(
396
resourceFactory, mimeTypes, bufferPool
397
);
398
399
// Get content for a file
400
HttpContent content = contentFactory.getContent("/index.html");
401
if (content != null) {
402
String contentType = content.getContentTypeValue(); // "text/html"
403
long contentLength = content.getContentLengthValue(); // file size
404
String etag = content.getETagValue(); // ETag for caching
405
ByteBuffer buffer = content.getByteBuffer(); // file content
406
407
// Release when done
408
content.release();
409
}
410
411
// Caching content factory for performance
412
HttpContent.Factory cachingFactory = new CachingHttpContentFactory(contentFactory, bufferPool);
413
CachingHttpContentFactory cache = (CachingHttpContentFactory) cachingFactory;
414
415
// Configure cache limits
416
cache.setMaxCachedFileSize(1024 * 1024); // 1MB max file size
417
cache.setMaxCacheSize(50 * 1024 * 1024); // 50MB total cache size
418
cache.setMaxCachedFiles(1000); // max 1000 files
419
cache.setUseDirectByteBuffers(true); // use direct buffers
420
421
// Monitor cache usage
422
long cacheSize = cache.getCachedSize();
423
int cachedFiles = cache.getCachedFiles();
424
425
// Clear cache when needed
426
cache.flushCache();
427
428
// Pre-compression support
429
CompressedContentFormat[] formats = {
430
CompressedContentFormat.GZIP,
431
CompressedContentFormat.BR
432
};
433
434
HttpContent.Factory compressedFactory = new PreCompressedHttpContentFactory(
435
cachingFactory, formats
436
);
437
438
// Will serve .gz or .br versions if available and client supports
439
HttpContent compressedContent = compressedFactory.getContent("/app.js");
440
441
// Virtual file system
442
VirtualHttpContentFactory virtualFactory = new VirtualHttpContentFactory();
443
444
// Map virtual paths to resources
445
Resource configResource = resourceFactory.newResource(Path.of("/etc/myapp/config.json"));
446
virtualFactory.addPath("/api/config", configResource);
447
448
HttpContent virtualContent = virtualFactory.getContent("/api/config");
449
450
// File extension mapping
451
FileMappingHttpContentFactory mappingFactory =
452
new FileMappingHttpContentFactory(contentFactory);
453
454
// Add custom MIME type mappings
455
mappingFactory.addMapping(".myext", "application/x-myformat");
456
457
// MIME type detection
458
MimeTypes.Type htmlType = mimeTypes.getType("text/html");
459
boolean isText = htmlType.isText(); // true
460
String charset = htmlType.getCharset(); // "utf-8" if specified
461
462
String mimeType = mimeTypes.getMimeByExtension("image.png"); // "image/png"
463
464
// Working with content metadata
465
if (content != null) {
466
// Check modification time
467
Instant lastModified = content.getLastModifiedInstant();
468
469
// Get headers for HTTP response
470
HttpField contentTypeField = content.getContentType();
471
HttpField contentLengthField = content.getContentLength();
472
HttpField lastModifiedField = content.getLastModified();
473
HttpField etagField = content.getETag();
474
475
// Check available compressions
476
Set<CompressedContentFormat> compressions =
477
content.getPreCompressedContentFormats();
478
479
boolean hasGzip = compressions.contains(CompressedContentFormat.GZIP);
480
481
// Memory usage
482
long memoryUsage = content.getBytesOccupied();
483
}
484
485
// Validating cache with automatic cleanup
486
ValidatingCachingHttpContentFactory validatingCache =
487
new ValidatingCachingHttpContentFactory(contentFactory, bufferPool);
488
489
validatingCache.setSweepPeriod(60 * 1000); // sweep every minute
490
validatingCache.start(); // start background sweeping
491
492
// Custom content implementation
493
class DatabaseHttpContent implements HttpContent {
494
private final String contentType;
495
private final byte[] data;
496
private final Instant lastModified;
497
498
// Implement all HttpContent methods...
499
500
@Override
501
public String getContentTypeValue() { return contentType; }
502
503
@Override
504
public long getContentLengthValue() { return data.length; }
505
506
@Override
507
public ByteBuffer getByteBuffer() {
508
return ByteBuffer.wrap(data).asReadOnlyBuffer();
509
}
510
511
// ... other implementations
512
}
513
514
// Resource management
515
try {
516
HttpContent managedContent = contentFactory.getContent("/large-file.dat");
517
if (managedContent != null) {
518
// Use content
519
processContent(managedContent);
520
}
521
} finally {
522
// Always release resources
523
if (managedContent != null) {
524
managedContent.release();
525
}
526
}
527
```