Class ServeStatic

java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.service.Service
services.moleculer.web.middleware.HttpMiddleware
services.moleculer.web.middleware.ServeStatic
All Implemented Interfaces:
services.moleculer.service.MoleculerLifecycle, HttpConstants

public class ServeStatic extends HttpMiddleware implements HttpConstants
Service to serve files from within a given root directory. When a file is not found, instead of sending a 404 response. Supports content compression, automatic "Content-Type" detection, and ETAGs. Sample:
ServeStatic staticHandler = new ServeStatic("/", "/www");
staticHandler.setEnableReloading(true) // During the development
route.use(staticHandler);
  • Field Details

    • defaultContentTypes

      protected static final HashMap<String,String> defaultContentTypes
    • path

      protected String path
      URL prefix (eg. "/files" or "/static")
    • localDirectory

      protected String localDirectory
      Local directory prefix (eg. "C:\content" or "www" or "WEB-INF\static")
    • formattedLocalDirectory

      protected String formattedLocalDirectory
      Formatted local directory prefix (eg. "C:/content" or "/www" or "/WEB-INF/static")
    • enableReloading

      protected boolean enableReloading
      Enables content reloading (in production mode set it to "false" for the better performance)
    • numberOfCachedFiles

      protected int numberOfCachedFiles
      Maximum number of cached files
    • cacheDelay

      protected long cacheDelay
      Do not reload cache until... (MILLISECONDS)
    • maxCachedFileSize

      protected int maxCachedFileSize
      Enable caching for smaller files only (BYTES)
    • useETags

      protected boolean useETags
      Use ETag headers
    • compressAbove

      protected int compressAbove
      Compress key and/or value above this size (BYTES), 0 = disable compression
    • compressionLevel

      protected int compressionLevel
      Compression level (best speed = 1, best compression = 9).
    • packetSize

      protected int packetSize
      Size of packets.
    • packetDelay

      protected long packetDelay
      Time between each packet sent. This may be necessary because the other Threads will get some CPU-time.
    • contentTypes

      protected final HashMap<String,String> contentTypes
    • fileCache

      protected io.datatree.dom.Cache<String, ServeStatic.CachedFile> fileCache
  • Constructor Details

    • ServeStatic

      public ServeStatic()
    • ServeStatic

      public ServeStatic(String path)
    • ServeStatic

      public ServeStatic(String path, String rootDirectory)
  • Method Details

    • started

      public void started(services.moleculer.ServiceBroker broker) throws Exception
      Specified by:
      started in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      started in class services.moleculer.service.MoleculerComponent
      Throws:
      Exception
    • install

      public RequestProcessor install(RequestProcessor next, io.datatree.Tree config)
      Specified by:
      install in class HttpMiddleware
    • stopped

      public void stopped()
      Specified by:
      stopped in interface services.moleculer.service.MoleculerLifecycle
      Overrides:
      stopped in class services.moleculer.service.MoleculerComponent
    • getPacketSize

      public int getPacketSize()
    • setPacketSize

      public void setPacketSize(int packetSize)
    • getPacketDelay

      public long getPacketDelay()
    • setPacketDelay

      public void setPacketDelay(long packetDelay)
    • getLocalDirectory

      public String getLocalDirectory()
    • setLocalDirectory

      public void setLocalDirectory(String localDirectory)
    • setContentType

      public void setContentType(String extension, String contentType)
    • getContentType

      public String getContentType(String extension)
    • getNumberOfCachedFiles

      public int getNumberOfCachedFiles()
    • setNumberOfCachedFiles

      public void setNumberOfCachedFiles(int numberOfCachedFiles)
    • isEnableReloading

      public boolean isEnableReloading()
    • setEnableReloading

      public void setEnableReloading(boolean enableReloading)
    • getCacheDelay

      public long getCacheDelay()
    • setCacheDelay

      public void setCacheDelay(long cacheDelay)
    • getMaxCachedFileSize

      public int getMaxCachedFileSize()
    • setMaxCachedFileSize

      public void setMaxCachedFileSize(int maxCachedFileSize)
    • getCompressAbove

      public int getCompressAbove()
    • setCompressAbove

      public void setCompressAbove(int compressAbove)
    • isUseETags

      public boolean isUseETags()
    • setUseETags

      public void setUseETags(boolean useETags)
    • getCompressionLevel

      public int getCompressionLevel()
    • setCompressionLevel

      public void setCompressionLevel(int compressionLevel)