java.lang.Object
dev.katsute.simplehttpserver.handler.file.FileHandler
- All Implemented Interfaces:
HttpHandler,SimpleHttpHandler
A file handler can be used to serve single or multiple files on a server with optional pre/post processing using
A
File options can be added to influence the behavior of added files.
Example:
FileAdapters.
FileAdapter
A FileAdapter determines where a file can be accessed and what content it will return. By default files would be accessible at the file name (including extension) with the file content.
Adding Files
The name parameters in the add methods supersedes theFileAdapter and makes a file accessible at whatever name you set.
FileOptions
File options can be added to influence the behavior of added files.
Context
TheFileOptions.context property determines at where the file will be located with respect to the file handler. By default this is "" and any added files will be accessible directly after the file handler's context.
Example:
/fileHandlerContext/file.txt by default and /fileHandlerContext/optionsContext/file.txt if a context was set.
Loading Options
TheFileOptions.loading option determines how a file should be loaded when added.
FileOptions.FileLoadingOption.PRELOAD- files are read when addedFileOptions.FileLoadingOption.MODIFY- files are read when added and when modifiedFileOptions.FileLoadingOption.CACHE- files are read when requested and cached for a set timeFileOptions.FileLoadingOption.LIVE- files are read when requested
Cache
If the loading optionFileOptions.FileLoadingOption.CACHE is used, the FileOptions.cache determines how long to cache files for in milliseconds.
Walk
When directories are added, if true, will also include subdirectories; if false, will only include files in the immediate directory.- Since:
- 5.0.0
- Version:
- 5.0.0
- Author:
- Katsute
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a file handler.FileHandler(FileAdapter adapter) Creates a file handler, adapting the added files with anFileAdapter. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidaddDirectory(File directory) Adds a directory to the handler using the directory name.final voidaddDirectory(File directory, FileOptions options) Adds a directory to the handler using the directory name from a set of file options.final voidaddDirectory(File directory, String directoryName) Adds a directory to the handler with a set name.final voidaddDirectory(File directory, String directoryName, FileOptions options) Adds a directory to the handler using the directory name from a set of file options.final voidAdds a file to the handler.final voidaddFile(File file, FileOptions options) Adds a file to the handler from a set ofFileOptionss.final voidAdds a file to the handler with a set name.final voidaddFile(File file, String fileName, FileOptions options) Adds a file to the handler with a set name from a set ofFileOptions.final voidAdds multiple files to the handler.final voidaddFiles(File[] files, FileOptions options) Adds multiple files to the handler from a set ofFileOptions.final voidhandle(HttpExchange exchange) Do not override this method, it will causeSimpleHttpHandler.handle(SimpleHttpExchange)to not work.final voidhandle(SimpleHttpExchange exchange) Handles an exchange.voidhandle(SimpleHttpExchange exchange, File source, byte[] bytes) Handles a file exchange.final voidremoveDirectory(File directory) Removes a directory from the handler.final voidremoveDirectory(File directory, FileOptions options) Removes a directory from the handler with file options.final voidremoveDirectory(String context) Removes a directory from the handler at a specified context.final voidremoveFile(File file) Removes a file from the handler.final voidremoveFile(File file, FileOptions options) Removes a file from the handler with file options.final voidremoveFile(String context) Removes a file from the handler at the specified context.toString()
-
Constructor Details
-
FileHandler
public FileHandler()Creates a file handler.- Since:
- 5.0.0
-
FileHandler
Creates a file handler, adapting the added files with anFileAdapter.- Parameters:
adapter- file adapter- Since:
- 5.0.0
- See Also:
-
-
Method Details
-
addFile
Adds a file to the handler.- Parameters:
file- file to add- Since:
- 5.0.0
- See Also:
-
addFile
Adds a file to the handler from a set ofFileOptionss.- Parameters:
file- file to addoptions- file options- Since:
- 5.0.0
- See Also:
-
addFile
Adds a file to the handler with a set name. Ignores theFileAdapterif set.- Parameters:
file- file to addfileName- file name to use- Since:
- 5.0.0
- See Also:
-
addFile
Adds a file to the handler with a set name from a set ofFileOptions. Ignores theFileAdapterif set.- Parameters:
file- file to addfileName- file name to useoptions- file options- Since:
- 5.0.0
- See Also:
-
addFiles
Adds multiple files to the handler.- Parameters:
files- files to add- Since:
- 5.0.0
- See Also:
-
addFiles
Adds multiple files to the handler from a set ofFileOptions.- Parameters:
files- files to addoptions- file options- Since:
- 5.0.0
- See Also:
-
addDirectory
Adds a directory to the handler using the directory name.- Parameters:
directory- directory to add- Since:
- 5.0.0
- See Also:
-
addDirectory
Adds a directory to the handler using the directory name from a set of file options.- Parameters:
directory- directory to addoptions- file options- Since:
- 5.0.0
- See Also:
-
addDirectory
Adds a directory to the handler with a set name.- Parameters:
directory- directory to adddirectoryName- directory name to use- Since:
- 5.0.0
- See Also:
-
addDirectory
Adds a directory to the handler using the directory name from a set of file options.- Parameters:
directory- directory to adddirectoryName- directory name to useoptions- file options- Since:
- 5.0.0
- See Also:
-
removeFile
Removes a file from the handler at the specified context.- Parameters:
context- context- Since:
- 5.0.0
- See Also:
-
removeFile
Removes a file from the handler.- Parameters:
file- file to remove- Since:
- 5.0.0
- See Also:
-
removeFile
Removes a file from the handler with file options. Only required ifFileOptions.contextwas used.- Parameters:
file- file to removeoptions- file options- Since:
- 5.0.0
- See Also:
-
removeDirectory
Removes a directory from the handler at a specified context.- Parameters:
context- context- Since:
- 5.0.0
- See Also:
-
removeDirectory
Removes a directory from the handler.- Parameters:
directory- directory to remove- Since:
- 5.0.0
- See Also:
-
removeDirectory
Removes a directory from the handler with file options. Only required ifFileOptions.contextwas used.- Parameters:
directory- directory to removeoptions- file options- Since:
- 5.0.0
- See Also:
-
handle
Description copied from interface:SimpleHttpHandlerHandles an exchange.
Http handlers will not throw an exception in the main thread, you must use a try-catch to expose them. All requests must be closed withHttpExchange.close(), otherwise the handler will rerun the request multiple times.- Specified by:
handlein interfaceSimpleHttpHandler- Parameters:
exchange- http exchange- Throws:
IOException- IO exception- See Also:
-
handle
Description copied from interface:SimpleHttpHandlerDo not override this method, it will causeSimpleHttpHandler.handle(SimpleHttpExchange)to not work. UseSimpleHttpHandler.handle(SimpleHttpExchange)instead.- Specified by:
handlein interfaceHttpHandler- Specified by:
handlein interfaceSimpleHttpHandler- Parameters:
exchange- the exchange containing the request from the client and used to send the response- Throws:
IOException- IO exception- See Also:
-
handle
Handles a file exchange. The file bytes are the bytes after post processing ifFileAdapteris used.- Parameters:
exchange- http exchangesource- file sourcebytes- file bytes- Throws:
IOException- IO exception- Since:
- 5.0.0
-
toString
-