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:
FileAdapter
s.
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
ConstructorDescriptionCreates a file handler.FileHandler
(FileAdapter adapter) Creates a file handler, adapting the added files with anFileAdapter
. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addDirectory
(File directory) Adds a directory to the handler using the directory name.final void
addDirectory
(File directory, FileOptions options) Adds a directory to the handler using the directory name from a set of file options.final void
addDirectory
(File directory, String directoryName) Adds a directory to the handler with a set name.final void
addDirectory
(File directory, String directoryName, FileOptions options) Adds a directory to the handler using the directory name from a set of file options.final void
Adds a file to the handler.final void
addFile
(File file, FileOptions options) Adds a file to the handler from a set ofFileOptions
s.final void
Adds a file to the handler with a set name.final void
addFile
(File file, String fileName, FileOptions options) Adds a file to the handler with a set name from a set ofFileOptions
.final void
Adds multiple files to the handler.final void
addFiles
(File[] files, FileOptions options) Adds multiple files to the handler from a set ofFileOptions
.final void
handle
(HttpExchange exchange) Do not override this method, it will causeSimpleHttpHandler.handle(SimpleHttpExchange)
to not work.final void
handle
(SimpleHttpExchange exchange) Handles an exchange.void
handle
(SimpleHttpExchange exchange, File source, byte[] bytes) Handles a file exchange.final void
removeDirectory
(File directory) Removes a directory from the handler.final void
removeDirectory
(File directory, FileOptions options) Removes a directory from the handler with file options.final void
removeDirectory
(String context) Removes a directory from the handler at a specified context.final void
removeFile
(File file) Removes a file from the handler.final void
removeFile
(File file, FileOptions options) Removes a file from the handler with file options.final void
removeFile
(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 ofFileOptions
s.- 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 theFileAdapter
if 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 theFileAdapter
if 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.context
was 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.context
was used.- Parameters:
directory
- directory to removeoptions
- file options- Since:
- 5.0.0
- See Also:
-
handle
Description copied from interface:SimpleHttpHandler
Handles 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:
handle
in interfaceSimpleHttpHandler
- Parameters:
exchange
- http exchange- Throws:
IOException
- IO exception- See Also:
-
handle
Description copied from interface:SimpleHttpHandler
Do not override this method, it will causeSimpleHttpHandler.handle(SimpleHttpExchange)
to not work. UseSimpleHttpHandler.handle(SimpleHttpExchange)
instead.- Specified by:
handle
in interfaceHttpHandler
- Specified by:
handle
in 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 ifFileAdapter
is used.- Parameters:
exchange
- http exchangesource
- file sourcebytes
- file bytes- Throws:
IOException
- IO exception- Since:
- 5.0.0
-
toString
-