With Appway 10, the Htdocs feature has been moved from the Appway Platform into a new Extension named Htdocs Extension. Also consider that the configuration property
nm.htdocs.root property described in this article is now a configuration property of the Htdocs Extension. If you upgrade from Appway 9.1, you might need to follow some upgrade steps: see the
System Upgrade: 9.1 to 10.x for more information.
The HtdocsServlet servlet is used to serve static content from arbitrary directories.
The URL paths /htdocs/ and /internal/htdocs/ are mapped to this servlet. By default, the servlet is disabled. Set the configuration property nm.htdocs.root to an existing directory to enable the servlet. Example:
nm.htdocs.root = content/downloads
A relative path is interpreted as relative to the Data Home.
If a user now requests a file /htdocs/foo/bar.html, the servlet checks whether the file foo/bar.html exists inside the root folder.
The behavior of the servlet is controlled using htdocs.properties files. Every directory (including the root directory) can contain such a file. The settings in this file are valid for the current directory and all files and sub-directories inside of it. If a sub-directory again contains a properties file, it overrides the settings from the parent directory.
In order to restrict access to a directory (including all files and sub-directories), add a property access to the properties file:
access = <Expression>
You can enter an arbitrary Appway expression, as long as it returns a Boolean. If the expression evaluates to false, access is denied. Example:
access = USERINROLE('User')
If a URL points to a directory, the servlet generates a directory listing with all (non-hidden) folders and files in this directory. If you don't like the default layout generated by the servlet, you can implement your own listing. Add an index property to the properties file. You can choose between 3 different index generators:
index = file:<FileName>index = screen:<ScreenId>index = function:<FunctionName>
If the mode file is used, the servlet extracts the file name from the index value (everything after the colon) and searches for a file with this name in the current directory. Example:
index = file:index.html
If the mode screen is used, the servlet extracts the screen ID from the index value and searches for a Screen with this ID using a LatestCommittedFilter. If the Screen exists, the servlet renders it to generate a directory listing. Example:
index = screen:PhotoAlbum
Note: The Screen should have a variable $directory of the type String and mode Assign. The servlet assigns the current directory's absolute path to this variable.
If the mode function is used, the servlet extracts the function name from the index value and searches for this value. The function should accept exactly one parameter of type String. The servlet passes the current directory's absolute path to the function. Example:
index = function:CreateDirListing
Inside the function body, you have access to the request and response objects using the functions HTTPREQUEST() and HTTPRESONSE(). Whatever the function returns is converted into a String and returned as directory listing code.
Sometimes you may want to give users access to multiple directories not located under a common parent directory. Example: You would like to make content/downloads and work/efs/uploadedFiles/documentation/uploads/images browsable.
The problem is that the configuration property nm.htdocs.root accepts only one directory. The solution is to add alias directory definitions to the properties file. Set the root to content/downloads and then create a htdocs.properties file in this directory. Add the following line to it:
alias.images = work/efs/uploadedFiles/documentation/uploads/images
Now, if a user accesses the URL /htdocs/images/test/scr001.jpg, the servlet returns the content of the file work/efs/uploadedFiles/documentation/uploads/images/test/scr001.jpg.
Add as many alias definitions as you like. Whenever the servlet finds a non-existing directory in the path, it checks if there is an alias definition in the properties file of the parent directory.
Note: This is the only property that is not inherited by sub-directories.
Use the log property to specify a path pointing to a log file. Every activity (directory listing, file download etc.) is recorded in this log file. The file contains the following information:
- Date and time
- User ID (can be empty for unauthorized users)
- Action ("ListDirectory", "DownloadFile", "AccessDenied", "NotFound")
- Path to directory or file
The individual values are separated by semi-colons ";". The following block shows some example entries:
2009-10-08 14:07:44.622+0200;User001;ListDirectory;/downloads/
2009-10-08 14:07:50.023+0200;User001;DownloadFile;/downloads/backup.zip
2009-10-08 14:09:15.522+0200;User001;NotFound;/downloads/export.zip
The value of the log property can be an absolute path. If it is a relative path, it is interpreted relative to the Data Home, not relative to the directory where the htdocs.properties file is located. Example:
log = logs/htdocs.log
By default, this property is not set and no log file is written.
You can "abuse" the access control expression to execute arbitrary script code whenever a directory or file is accessed. To do so, extend your access control expression with a call to your custom logging function.