Package org.codelibs.fess.helper
Class RelatedContentHelper
java.lang.Object
org.codelibs.fess.helper.AbstractConfigHelper
org.codelibs.fess.helper.RelatedContentHelper
Helper class for managing related content configurations.
This class provides functionality to load, cache, and retrieve related content
based on search queries and virtual host configurations. It supports both exact
term matching and regex pattern matching for flexible content association.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringPlaceholder string used in regex-based related content templates for JavaScript contexts.protected StringPlaceholder string used in regex-based related content templates.protected StringPrefix used to identify regex patterns in related content terms.protected Map<String, org.codelibs.core.misc.Pair<Map<String, String>, List<org.codelibs.core.misc.Pair<Pattern, String>>>> Cache map storing related content configurations organized by virtual host key.protected StringPlaceholder string used in regex-based related content templates for URL contexts.Fields inherited from class org.codelibs.fess.helper.AbstractConfigHelper
reloadInterval -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringapplyQueryPlaceHolders(String template, String query) Substitutes the query placeholders in the given template with the search query, encoding the query for each placeholder's target context (HTML / URL / JavaScript).List<org.codelibs.fess.opensearch.config.exentity.RelatedContent> Retrieves all available related content configurations from the data store.protected StringgetHostKey(org.codelibs.fess.opensearch.config.exentity.RelatedContent entity) Extracts the virtual host key from a RelatedContent entity.String[]getRelatedContents(String query) Retrieves related content for a given search query.voidinit()Initializes the RelatedContentHelper by loading related content configurations from the data store.intload()Load the configuration.voidsetJsQueryPlaceHolder(String jsQueryPlaceHolder) Sets the placeholder string used in regex-based related content templates for JavaScript contexts.voidsetQueryPlaceHolder(String queryPlaceHolder) Sets the placeholder string used in regex-based related content templates.voidsetRegexPrefix(String regexPrefix) Sets the prefix used to identify regex patterns in related content terms.voidsetUrlQueryPlaceHolder(String urlQueryPlaceHolder) Sets the placeholder string used in regex-based related content templates for URL contexts.Methods inherited from class org.codelibs.fess.helper.AbstractConfigHelper
setReloadInterval, update, waitForNext
-
Field Details
-
regexPrefix
Prefix used to identify regex patterns in related content terms. When a term starts with this prefix, it is treated as a regular expression pattern rather than an exact match term. -
queryPlaceHolder
Placeholder string used in regex-based related content templates. This placeholder is replaced with the actual search query when a regex pattern matches the query. -
urlQueryPlaceHolder
Placeholder string used in regex-based related content templates for URL contexts. This placeholder is replaced with the URL-encoded search query when a regex pattern matches the query, making it safe to embed in URL parameters (e.g.href="...?q=..."). -
jsQueryPlaceHolder
Placeholder string used in regex-based related content templates for JavaScript contexts. This placeholder is replaced with the JavaScript-escaped search query when a regex pattern matches the query, making it safe to embed inside a JavaScript string literal (e.g. inside a<script>block).
-
Constructor Details
-
RelatedContentHelper
public RelatedContentHelper()Default constructor for RelatedContentHelper. The constructor does not perform any initialization logic as the actual initialization is handled by theinit()method annotated with@PostConstruct.
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the RelatedContentHelper by loading related content configurations from the data store. This method is called automatically after dependency injection is complete. PostConstruct annotation ensures this method is called after the bean has been constructed and all dependencies have been injected. -
getAvailableRelatedContentList
public List<org.codelibs.fess.opensearch.config.exentity.RelatedContent> getAvailableRelatedContentList()Retrieves all available related content configurations from the data store. The results are ordered by sort order ascending, then by term ascending. The number of results is limited by the configured maximum fetch size.- Returns:
- List of RelatedContent entities containing all available related content configurations
-
load
public int load()Description copied from class:AbstractConfigHelperLoad the configuration.- Specified by:
loadin classAbstractConfigHelper- Returns:
- The number of loaded configurations.
-
getHostKey
Extracts the virtual host key from a RelatedContent entity. If the virtual host is blank or null, returns an empty string. This key is used to organize related content by virtual host.- Parameters:
entity- the RelatedContent entity to extract the host key from- Returns:
- the virtual host key, or empty string if not specified
-
getRelatedContents
Retrieves related content for a given search query. First checks for exact term matches, then evaluates regex patterns. For regex matches, the query placeholders are replaced with the actual query, each encoded for its target context (HTML / URL / JavaScript) to prevent reflected XSS.- Parameters:
query- the search query to find related content for- Returns:
- array of related content strings, or empty array if no matches found
-
applyQueryPlaceHolders
Substitutes the query placeholders in the given template with the search query, encoding the query for each placeholder's target context (HTML / URL / JavaScript).The three placeholders are replaced in a single regex pass: the template is scanned once and each matched placeholder is replaced with its context-encoded query value. Only the query value is encoded; the admin template itself is never modified or re-encoded. Because substituted values are never re-scanned, an encoded value cannot be re-substituted through another placeholder (which would otherwise allow escaping to be bypassed, e.g. attribute injection).
- Parameters:
template- the admin-registered content template containing placeholdersquery- the untrusted search query to substitute- Returns:
- the template with placeholders replaced by context-encoded query values
-
setRegexPrefix
Sets the prefix used to identify regex patterns in related content terms. When a term starts with this prefix, it is treated as a regular expression pattern rather than an exact match term.- Parameters:
regexPrefix- the prefix string to identify regex patterns (default: "regex:")
-
setQueryPlaceHolder
Sets the placeholder string used in regex-based related content templates. This placeholder is replaced with the actual search query when a regex pattern matches the query.- Parameters:
queryPlaceHolder- the placeholder string to be replaced with the query (default: "__QUERY__")
-
setUrlQueryPlaceHolder
Sets the placeholder string used in regex-based related content templates for URL contexts. This placeholder is replaced with the URL-encoded search query when a regex pattern matches the query.- Parameters:
urlQueryPlaceHolder- the placeholder string to be replaced with the URL-encoded query (default: "__URL_QUERY__")
-
setJsQueryPlaceHolder
Sets the placeholder string used in regex-based related content templates for JavaScript contexts. This placeholder is replaced with the JavaScript-escaped search query when a regex pattern matches the query.- Parameters:
jsQueryPlaceHolder- the placeholder string to be replaced with the JavaScript-escaped query (default: "__JS_QUERY__")
-