KOS has a web server to serve content and interact with the frontend and other applications. Instead of serving files directly from disk directories, KOS uses a Virtual File System (VFS) to manage and serve content. A key benefit of this approach is that KAB contents can be mounted and served without extracting files to disk first, saving disk space.
VFS Sources
The core building block of VFS is the VFSSource which is an interface that returns an input stream given a specific path. KOS has a couple VFS sources built in, the most notable being KabVFSSource and FileSystemVFSSource. The KabVFSSource is used to mount (make available) the contents of a KAB into VFS. The FileSystemVFSSource makes the contents of a directory on the native filesystem available to VFS. Additional VFS sources and the full javadocs for VFS can be found here. Custom implementations of VFS sources can be created to add additional functionality like url redirection, or shortening URLs.
Using VFS
Mounting content to the VFS requires a VFS instance, which you can inject via @Autowired. Once obtained, use its mount() method to add VFSSources. The Application class simplifies this with its getVfs() helper method for the root VFS.
Mount paths
When a VFSSource is mounted, it is not directly mounted to /<your-path> since this could lead to conflicts where some applications might overwrite each other. To solve this class of problem KOS uses appIds to divide resources per app and in this case the url path is prefixed with the appId. When mounting files in a system app they are mounted at /system/<your-path>, while other apps use /app/<appId>/<your-path>. This nearly guarantees that there will not be overlap in paths since appId’s should be chosen to be globally unique.
Example
The Application.load() method is an ideal place for mounting VFSSources as it runs before the app fully initializes and is made available to the web server. This ensures that the VFS contents are available immediately upon startup and prevents access failures for frontend requests or other components.
With this setup, the contents of the assets KAB become accessible via the web server—for example, a request to localhost:8081/app/<appId>/assets/data.json would serve the corresponding file from the mounted source.
