io.undertow.server.handlers.cache.DirectBufferCache Java Examples
The following examples show how to use
io.undertow.server.handlers.cache.DirectBufferCache.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CachingResourceManager.java From lams with GNU General Public License v2.0 | 6 votes |
public CachingResourceManager(final int metadataCacheSize, final long maxFileSize, final DirectBufferCache dataCache, final ResourceManager underlyingResourceManager, final int maxAge) { this.maxFileSize = maxFileSize; this.underlyingResourceManager = underlyingResourceManager; this.dataCache = dataCache; this.cache = new LRUCache<>(metadataCacheSize, maxAge); this.maxAge = maxAge; if(underlyingResourceManager.isResourceChangeListenerSupported()) { try { underlyingResourceManager.registerResourceChangeListener(new ResourceChangeListener() { @Override public void handleChanges(Collection<ResourceChangeEvent> changes) { for(ResourceChangeEvent change : changes) { invalidate(change.getResource()); } } }); } catch (Exception e) { UndertowLogger.ROOT_LOGGER.couldNotRegisterChangeListener(e); } } }
Example #2
Source File: RangeRequestTestCase.java From quarkus-http with Apache License 2.0 | 6 votes |
@BeforeClass public static void setup() throws URISyntaxException { Path rootPath = Paths.get(RangeRequestTestCase.class.getResource("range.txt").toURI()).getParent(); PathHandler path = Handlers.path(); path.addPrefixPath("/path", new ByteRangeHandler(new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { exchange.setResponseHeader(HttpHeaderNames.LAST_MODIFIED, DateUtils.toDateString(new Date(10000))); exchange.setResponseHeader(HttpHeaderNames.ETAG, "\"someetag\""); exchange.setResponseContentLength("0123456789".length()); exchange.writeAsync(Unpooled.copiedBuffer("0123456789", StandardCharsets.UTF_8), true, IoCallback.END_EXCHANGE, null); } }, true)); path.addPrefixPath("/resource", new ResourceHandler( new PathResourceManager(rootPath, 10485760)) .setDirectoryListingEnabled(true)); path.addPrefixPath("/cachedresource", new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)) .setDirectoryListingEnabled(true)); path.addPrefixPath("/resource-blocking", new BlockingHandler(new ResourceHandler( new PathResourceManager(rootPath, 10485760)) .setDirectoryListingEnabled(true))); path.addPrefixPath("/cachedresource-blocking", new BlockingHandler(new ResourceHandler(new CachingResourceManager(1000, 1000000, new DirectBufferCache(1000, 10, 10000), new PathResourceManager(rootPath, 10485760), -1)) .setDirectoryListingEnabled(true))); DefaultServer.setRootHandler(path); }
Example #3
Source File: Server.java From divolte-collector with Apache License 2.0 | 5 votes |
private static HttpHandler createStaticResourceHandler() { final ResourceManager staticResources = new ClassPathResourceManager(Server.class.getClassLoader(), "static"); // Cache tuning is copied from Undertow unit tests. final ResourceManager cachedResources = new CachingResourceManager(100, 65536, new DirectBufferCache(1024, 10, 10480), staticResources, (int)Duration.ofDays(1).getSeconds()); final ResourceHandler resourceHandler = new ResourceHandler(cachedResources); resourceHandler.setWelcomeFiles("index.html"); return resourceHandler; }
Example #4
Source File: DefaultServletCachedResourceTestCase.java From quarkus-http with Apache License 2.0 | 5 votes |
@BeforeClass public static void setup() throws ServletException { final PathHandler root = new PathHandler(); final ServletContainer container = ServletContainer.Factory.newInstance(); DeploymentInfo builder = new DeploymentInfo() .setClassIntrospecter(TestClassIntrospector.INSTANCE) .setClassLoader(ServletPathMappingTestCase.class.getClassLoader()) .setContextPath("/servletContext") .setDeploymentName("servletContext.war") .setResourceManager(new CachingResourceManager(100, 10000,new DirectBufferCache(1000,1,1000000),new TestResourceLoader(DefaultServletCachedResourceTestCase.class), 100000)); builder.addServlet(new ServletInfo("DefaultTestServlet", PathTestServlet.class) .addMapping("/path/default")); builder.addServlet(new ServletInfo("default", DefaultServlet.class) .addInitParam("directory-listing", "true") .addMapping("/*")); //see UNDERTOW-458 builder.addFilter(new FilterInfo("date-header", GetDateFilter.class)); builder.addFilterUrlMapping("date-header", "/*", DispatcherType.REQUEST); builder.addFilter(new FilterInfo("Filter", HelloFilter.class)); builder.addFilterUrlMapping("Filter", "/filterpath/*", DispatcherType.REQUEST); DeploymentManager manager = container.addDeployment(builder); manager.deploy(); root.addPrefixPath(builder.getContextPath(), manager.start()); DefaultServer.setRootHandler(root); }
Example #5
Source File: CachedResource.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public Long getContentLength() { //we always use the underlying size unless the data is cached in the buffer cache //to prevent a mis-match between size on disk and cached size final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if(dataCache == null) { return underlyingResource.getContentLength(); } final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey); if(existing == null || !existing.enabled()) { return underlyingResource.getContentLength(); } //we only return the return (long)existing.size(); }
Example #6
Source File: CachingResourceManager.java From quarkus-http with Apache License 2.0 | 5 votes |
public CachingResourceManager(final int metadataCacheSize, final long maxFileSize, final DirectBufferCache dataCache, final ResourceManager underlyingResourceManager, final int maxAge) { this.maxFileSize = maxFileSize; this.underlyingResourceManager = underlyingResourceManager; this.dataCache = dataCache; this.cache = new LRUCache<>(metadataCacheSize, maxAge); this.maxAge = maxAge; }
Example #7
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 5 votes |
@Override public Long getContentLength() { //we always use the underlying size unless the data is cached in the buffer cache //to prevent a mis-match between size on disk and cached size final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if (dataCache == null) { return underlyingResource.getContentLength(); } final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey); if (existing == null || !existing.enabled()) { return underlyingResource.getContentLength(); } //we only return the return (long) existing.size(); }
Example #8
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 4 votes |
public CachingWriteFunction(DirectBufferCache.CacheEntry cacheEntry, Long length) { this.cacheEntry = cacheEntry; this.length = length; }
Example #9
Source File: CachingResourceManager.java From quarkus-http with Apache License 2.0 | 4 votes |
DirectBufferCache getDataCache() { return dataCache; }
Example #10
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 4 votes |
DereferenceCallback(DirectBufferCache.CacheEntry entry, final IoCallback<T> callback) { this.entry = entry; this.callback = callback; }
Example #11
Source File: CachedResource.java From lams with GNU General Public License v2.0 | 4 votes |
public void invalidate() { final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if(dataCache != null) { dataCache.remove(cacheKey); } }
Example #12
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 4 votes |
@Override public void serveRangeAsync(OutputChannel sender, HttpServerExchange exchange, long start, long end) { final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if (dataCache == null) { ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); return; } final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey); final Long length = getContentLength(); //if it is not eligible to be served from the cache if (length == null || length > cachingResourceManager.getMaxFileSize()) { ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); return; } //it is not cached yet, just serve it directly if (existing == null || !existing.enabled() || !existing.reference()) { //it is not cached yet, we can't use a range request to establish the cached item //so we just serve it ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end); } else { //serve straight from the cache ByteBuf[] buffers; boolean ok = false; try { LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers(); buffers = new ByteBuf[pooled.length]; for (int i = 0; i < buffers.length; i++) { // Keep position from mutating buffers[i] = pooled[i].getBuffer().duplicate(); } ok = true; } finally { if (!ok) { existing.dereference(); } } if (start > 0) { long startDec = start; long endCount = 0; //handle the start of the range for (ByteBuf b : buffers) { if (endCount == end) { b.clear(); continue; } else if (endCount + b.readableBytes() < end) { endCount += b.readableBytes(); } else { b.writerIndex((int) (b.readerIndex() + (end - endCount))); endCount = end; } if (b.readableBytes() >= startDec) { startDec = 0; b.readerIndex((int) (b.readerIndex() + startDec)); } else { startDec -= b.readableBytes(); b.clear(); } } } sender.writeAsync(Unpooled.wrappedBuffer(buffers), true, new DereferenceCallback(existing, IoCallback.END_EXCHANGE), null); } }
Example #13
Source File: CachedResource.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public void serveRange(Sender sender, HttpServerExchange exchange, long start, long end, IoCallback completionCallback) { final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if(dataCache == null) { ((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback); return; } final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey); final Long length = getContentLength(); //if it is not eligible to be served from the cache if (length == null || length > cachingResourceManager.getMaxFileSize()) { underlyingResource.serve(sender, exchange, completionCallback); return; } //it is not cached yet, just serve it directly if (existing == null || !existing.enabled() || !existing.reference()) { //it is not cached yet, install a wrapper to grab the data ((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback); } else { //serve straight from the cache ByteBuffer[] buffers; boolean ok = false; try { LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers(); buffers = new ByteBuffer[pooled.length]; for (int i = 0; i < buffers.length; i++) { // Keep position from mutating buffers[i] = pooled[i].getBuffer().duplicate(); } ok = true; } finally { if (!ok) { existing.dereference(); } } if(start > 0) { long startDec = start; long endCount = 0; //handle the start of the range for(ByteBuffer b : buffers) { if(endCount == end) { b.limit(b.position()); continue; } else if(endCount + b.remaining() < end) { endCount += b.remaining(); } else { b.limit((int) (b.position() + (end - endCount))); endCount = end; } if(b.remaining() >= startDec) { startDec = 0; b.position((int) (b.position() + startDec)); } else { startDec -= b.remaining(); b.position(b.limit()); } } } sender.send(buffers, new DereferenceCallback(existing, completionCallback)); } }
Example #14
Source File: CachedResource.java From lams with GNU General Public License v2.0 | 4 votes |
DereferenceCallback(DirectBufferCache.CacheEntry entry, final IoCallback callback) { this.entry = entry; this.callback = callback; }
Example #15
Source File: CachingResourceManager.java From lams with GNU General Public License v2.0 | 4 votes |
DirectBufferCache getDataCache() { return dataCache; }
Example #16
Source File: Uml.java From umlbot with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) { // https://devcenter.heroku.com/articles/dynos#local-environment-variables LOG.info(System.getenv()); String url = System.getenv("URL"); if (url == null || url.isEmpty()) { LOG.fatal("URL is not defined."); return; } try { URL u = new URL(url); if (u.getProtocol().startsWith("http") == false) { LOG.fatal("URL protocol must be http"); return; } } catch (IOException e) { LOG.fatal("URL is not valid."); return; } String token = System.getenv("TOKEN"); if (token == null || token.isEmpty()) { LOG.fatal("TOKEN is not defined."); return; } Set<String> tokens = new HashSet<>(Arrays.asList(token.split(","))); String port = System.getenv("PORT"); int p = 8080; if (port != null && Pattern.matches("\\d{1,5}", port)) { int i = Integer.parseInt(port); if (0 < i && i < 65536) { p = i; } } App app = new App() { @Override protected HttpHandler buildHandlers() { DirectBufferCache cache = new DirectBufferCache(1024, 10, 1024 * 1024 * 200); return new CacheHandler(cache, super.buildHandlers()); } }; new Uml(app, url, tokens); app.listen(p).addShutdownHook(); }
Example #17
Source File: CachedResource.java From quarkus-http with Apache License 2.0 | 4 votes |
public void invalidate() { final DirectBufferCache dataCache = cachingResourceManager.getDataCache(); if (dataCache != null) { dataCache.remove(cacheKey); } }