Serving Local Files

By default, Highlighter can fetch a document for from any URL provided ("uri" parameter of the web service). To avoid overhead of reading documents over HTTP, you can setup Highlighter to map received document URLs to local file system path.

The serving of local files is controlled by allowFileReadForMappedUri flag and the uriMappings section that may list one or more transformation rules. For example:

highlighter {
service { 
allowFileReadForMappedUri = true // an important one as file access is disabled by default! 
uriMappings = [
{
searchPattern = "\\.doc$"
replacement = ".pdf"
last = false
},
{
searchPattern = "/foo"
replacement = "/somewhere-else"
},
{
searchPattern = "[^:]+://[^:/]+(:[0-9]+)?/Content/(.*)" // Map URLs with path starting with /Content/
replacement = "C:/foo/bar/Content/$2" // to local Content directory paths
},
{
searchPattern = "/(.*)\\?param=value" // Note backspace escaping
replacement = "D:/local/path/$1" // note forward slashes for Windows path
}
}
}

The following options are supported by the transformation rule:

  • searchPattern - Regex expression for matching input URI. By default, matching is case sensitive. To ignore case, start your pattern with "(?i)". (You can use this online tool to test your expressions.)

  • replacement - Regex replacement.

  • last - By default, transformation completes with the first pattern matching input URI. When set to false, this option tells Highlighter to apply transformation and continues with the next rule.

  • replaceFirst - Set to true to replace only the first pattern match.

  • urlDecode - Set to true to the URL-decode text. If this option is specified in the same rule with search/replace options, the string will be decoded after the replacement is done (if searchPattern matches).

Note that for Windows file paths, we are using forward slashes instead of backslashes. (Single backslash is used as an escape character so we would have to use double backslashes to prevent config file related errors.)

Example

highlighter {
service { 
allowFileReadForMappedUri = true
uriMappings = [
# Extract path part of URL
{
searchPattern = "[^:]+://[^:/]+(:[0-9]+)?/(.*)"
replacement = "/$2"
last = false
}
# Decode path (in case file path contains a white space which would be encoded in URL)
{
urlDecode = true
}
# Map top level directory in extracted path to file system location
{
searchPattern = "^/virtualdir/"
replacement = "M:/virtualdir/path/"
}
{
searchPattern = "^/onshare/"
replacement = "\\\\\\\\smbshare/path/" # Note double escaping for UNC path
}
{
searchPattern = "^/"
replacement = "S:/some/other/path/"
}
}
}


comments powered by Disqus