Class RelatedContentHelper

java.lang.Object
org.codelibs.fess.helper.AbstractConfigHelper
org.codelibs.fess.helper.RelatedContentHelper

public class RelatedContentHelper extends AbstractConfigHelper
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 Details

    • relatedContentMap

      protected Map<String,org.codelibs.core.misc.Pair<Map<String,String>,List<org.codelibs.core.misc.Pair<Pattern,String>>>> relatedContentMap
      Cache map storing related content configurations organized by virtual host key. The outer map key is the virtual host key, and the value is a pair containing: - First: Map of exact term matches (term -> content) - Second: List of regex pattern matches (Pattern -> content template)
    • regexPrefix

      protected String 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

      protected String 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

      protected String 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

      protected String 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 the init() 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: AbstractConfigHelper
      Load the configuration.
      Specified by:
      load in class AbstractConfigHelper
      Returns:
      The number of loaded configurations.
    • getHostKey

      protected String getHostKey(org.codelibs.fess.opensearch.config.exentity.RelatedContent entity)
      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

      public String[] getRelatedContents(String query)
      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

      protected String applyQueryPlaceHolders(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).

      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 placeholders
      query - the untrusted search query to substitute
      Returns:
      the template with placeholders replaced by context-encoded query values
    • setRegexPrefix

      public void setRegexPrefix(String regexPrefix)
      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

      public void setQueryPlaceHolder(String queryPlaceHolder)
      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

      public void setUrlQueryPlaceHolder(String urlQueryPlaceHolder)
      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

      public void setJsQueryPlaceHolder(String jsQueryPlaceHolder)
      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__")