Class ViewHelper

java.lang.Object
org.codelibs.fess.helper.ViewHelper

public class ViewHelper extends Object
Helper class for handling view-related operations in the Fess search system. This class provides utilities for content rendering, URL processing, highlighting, caching, pagination, and user interface functionality.
  • Field Details

    • SCREEN_WIDTH

      protected static final String SCREEN_WIDTH
      Request attribute key for screen width
      See Also:
    • TABLET_WIDTH

      protected static final int TABLET_WIDTH
      Tablet width threshold for responsive design
      See Also:
    • CONTENT_DISPOSITION

      protected static final String CONTENT_DISPOSITION
      HTTP header name for content disposition
      See Also:
    • HL_CACHE

      protected static final String HL_CACHE
      Cache key for highlighted cache content
      See Also:
    • QUERIES

      protected static final String QUERIES
      Cache key for search queries
      See Also:
    • CACHE_MSG

      protected static final String CACHE_MSG
      Cache key for cache message
      See Also:
    • LOCAL_PATH_PATTERN

      protected static final Pattern LOCAL_PATH_PATTERN
      Pattern for matching local file paths
    • SHARED_FOLDER_PATTERN

      protected static final Pattern SHARED_FOLDER_PATTERN
      Pattern for matching shared folder paths
    • ELLIPSIS

      protected static final String ELLIPSIS
      Ellipsis string for text truncation
      See Also:
    • urlLinkEncoding

      protected String urlLinkEncoding
      Character encoding for URL links
    • highlightedFields

      protected String[] highlightedFields
      Fields that should be highlighted in search results
    • originalHighlightTagPre

      protected String originalHighlightTagPre
      Original highlight tag prefix
    • originalHighlightTagPost

      protected String originalHighlightTagPost
      Original highlight tag suffix
    • highlightTagPre

      protected String highlightTagPre
      Configured highlight tag prefix
    • highlightTagPost

      protected String highlightTagPost
      Configured highlight tag suffix
    • useSession

      protected boolean useSession
      Whether to use HTTP sessions
    • pageCacheMap

      protected final Map<String,String> pageCacheMap
      Cache for page paths
    • initFacetParamMap

      protected final Map<String,String> initFacetParamMap
      Initial facet parameter mappings
    • initGeoParamMap

      protected final Map<String,String> initGeoParamMap
      Initial geographic parameter mappings
    • facetQueryViewList

      protected final List<FacetQueryView> facetQueryViewList
      List of facet query views
    • cacheTemplateName

      protected String cacheTemplateName
      Template name for cache content
    • escapedHighlightPre

      protected String escapedHighlightPre
      HTML-escaped highlight prefix
    • escapedHighlightPost

      protected String escapedHighlightPost
      HTML-escaped highlight suffix
    • highlightTerminalCharSet

      protected Set<Integer> highlightTerminalCharSet
      Set of terminal characters for highlighting
    • actionHook

      protected ViewHelper.ActionHook actionHook
      Action hook for custom processing
    • inlineMimeTypeSet

      protected final Set<String> inlineMimeTypeSet
      Set of MIME types that should be displayed inline
    • facetCache

      protected com.google.common.cache.Cache<String,FacetResponse> facetCache
      Cache for facet responses
    • facetCacheDuration

      protected long facetCacheDuration
      Duration for facet cache in seconds (10 minutes)
    • textFragmentPrefixLength

      protected int textFragmentPrefixLength
      Length of text fragment prefix
    • textFragmentSuffixLength

      protected int textFragmentSuffixLength
      Length of text fragment suffix
    • textFragmentSize

      protected int textFragmentSize
      Size of text fragments
  • Constructor Details

    • ViewHelper

      public ViewHelper()
      Default constructor for ViewHelper.
  • Method Details

    • init

      @PostConstruct public void init()
      Initializes the ViewHelper with configuration settings. Sets up highlighting, caching, and other view-related configurations.
    • configureSessionCookie

      protected void configureSessionCookie(jakarta.servlet.ServletContext servletContext, org.codelibs.fess.mylasta.direction.FessConfig fessConfig)
      Configures the session cookie (JSESSIONID) on the given servlet context. Applies the configured session tracking modes and, when session.cookie.secure is enabled, adds the Secure attribute (recommended for HTTPS deployments). Must be called while the servlet context is still starting (i.e. before it becomes available), which is the only state in which SessionCookieConfig may be mutated.
      Parameters:
      servletContext - the servlet context to configure
      fessConfig - the configuration holding the session cookie settings
    • getContentTitle

      public String getContentTitle(Map<String,Object> document)
      Gets the display title for a document. Falls back to filename or URL if title is not available. Applies highlighting if enabled.
      Parameters:
      document - the document data map
      Returns:
      the content title with optional highlighting
    • getQuerySet

      protected org.dbflute.optional.OptionalThing<Set<String>> getQuerySet()
      Gets the set of highlight queries from the current request.
      Returns:
      OptionalThing containing the query set
    • getContentDescription

      public String getContentDescription(Map<String,Object> document)
      Gets the content description from highlighted fields. Returns the first non-blank highlighted field content.
      Parameters:
      document - the document data map
      Returns:
      the content description with highlighting
    • escapeHighlight

      protected String escapeHighlight(String text)
      Escapes HTML and applies highlighting to text. Handles boundary position detection if enabled.
      Parameters:
      text - the text to process
      Returns:
      the escaped and highlighted text
    • removeHighlightTag

      protected String removeHighlightTag(String str)
      Removes highlight tags from a string.
      Parameters:
      str - the string containing highlight tags
      Returns:
      the string with highlight tags removed
    • createHighlightInfo

      public HighlightInfo createHighlightInfo()
      Creates highlight information based on screen width. Adjusts fragment size for mobile devices.
      Returns:
      the highlight information
    • updateHighlightInfo

      protected void updateHighlightInfo(HighlightInfo highlightInfo, int width)
      Updates highlight information based on screen width. Reduces fragment size for smaller screens.
      Parameters:
      highlightInfo - the highlight info to update
      width - the screen width
    • getUrlLink

      public String getUrlLink(Map<String,Object> document)
      Gets the URL link for a document with proper protocol handling. Handles file, SMB, FTP, and HTTP protocols.
      Parameters:
      document - the document data map
      Returns:
      the processed URL link
    • updateFileProtocol

      protected String updateFileProtocol(String url)
      Updates file protocol based on user agent type. Handles different browser-specific file protocol formats.
      Parameters:
      url - the file URL to update
      Returns:
      the updated URL with appropriate file protocol
    • appendQueryParameter

      protected String appendQueryParameter(Map<String,Object> document, String url)
      Appends query parameters to URLs based on document type. Adds search highlighting for HTML and PDF documents.
      Parameters:
      document - the document data map
      url - the base URL
      Returns:
      the URL with appended query parameters
    • appendHTMLSearchWord

      protected String appendHTMLSearchWord(Map<String,Object> document, String url)
      Appends text fragment parameters to HTML URLs for highlighting.
      Parameters:
      document - the document data map
      url - the HTML URL
      Returns:
      the URL with text fragment parameters
    • appendPDFSearchWord

      protected String appendPDFSearchWord(Map<String,Object> document, String url)
      Appends search parameters to PDF URLs for highlighting.
      Parameters:
      document - the document data map
      url - the PDF URL
      Returns:
      the URL with search parameters
    • getPagePath

      public String getPagePath(String page)
      Gets the localized page path for a given page name. Checks for locale-specific versions before falling back to default.
      Parameters:
      page - the page name
      Returns:
      the localized page path
    • createCacheContent

      public String createCacheContent(Map<String,Object> doc, String[] queries)
      Creates cached content with highlighting for a document. Uses Handlebars templates to render the cached content.
      Parameters:
      doc - the document data map
      queries - the search queries for highlighting
      Returns:
      the rendered cache content
    • replaceHighlightQueries

      protected String replaceHighlightQueries(String cache, String[] queries)
      Replaces search queries with highlighted versions in cached content. Preserves HTML tags while highlighting text content.
      Parameters:
      cache - the cached content
      queries - the search queries to highlight
      Returns:
      the content with highlighted queries
    • getSitePath

      public Object getSitePath(Map<String,Object> docMap)
      Gets the site path for display purposes. Extracts and formats the site path from document URL.
      Parameters:
      docMap - the document data map
      Returns:
      the formatted site path
    • asContentResponse

      public org.lastaflute.web.response.StreamResponse asContentResponse(Map<String,Object> doc)
      Creates a stream response for document content delivery. Handles content retrieval and streaming to the client.
      Parameters:
      doc - the document data map
      Returns:
      the stream response containing document content
      Throws:
      FessSystemException - if content cannot be retrieved
    • writeContent

      protected org.lastaflute.web.response.StreamResponse writeContent(String configId, String url, org.codelibs.fess.crawler.client.CrawlerClient client)
      Writes content from a crawler client to a stream response.
      Parameters:
      configId - the configuration ID
      url - the document URL
      client - the crawler client
      Returns:
      the stream response with document content
    • writeNoCache

      protected void writeNoCache(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData)
      Writes no-cache headers to the response.
      Parameters:
      response - the stream response
      responseData - the response data
    • writeFileName

      protected void writeFileName(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData)
      Writes content disposition header with filename.
      Parameters:
      response - the stream response
      responseData - the response data
    • writeContentType

      protected void writeContentType(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData)
      Writes content type header to the response.
      Parameters:
      response - the stream response
      responseData - the response data
    • getClientIp

      public String getClientIp(jakarta.servlet.http.HttpServletRequest request)
      Gets the client IP address from the request. Checks X-Forwarded-For header before using remote address.
      Parameters:
      request - the HTTP servlet request
      Returns:
      the client IP address
    • getCachedFacetResponse

      public FacetResponse getCachedFacetResponse(String query)
      Gets a cached facet response for the given query. Creates and caches the response if not already cached.
      Parameters:
      query - the search query
      Returns:
      the facet response
      Throws:
      FessSystemException - if facet data cannot be loaded
    • createHighlightText

      public String createHighlightText(org.opensearch.search.fetch.subphase.highlight.HighlightField highlightField)
      Creates highlighted text from highlight field fragments.
      Parameters:
      highlightField - the highlight field containing fragments
      Returns:
      the combined highlighted text
    • createTextFragmentsByHighlight

      public ViewHelper.TextFragment[] createTextFragmentsByHighlight(org.opensearch.search.fetch.subphase.highlight.HighlightField[] fields)
      Creates text fragments from highlight fields for URL fragment navigation.
      Parameters:
      fields - the highlight fields
      Returns:
      array of text fragments
    • createTextFragmentsByQuery

      public ViewHelper.TextFragment[] createTextFragmentsByQuery()
      Creates text fragments from search queries.
      Returns:
      array of text fragments based on current queries
    • isUseSession

      public boolean isUseSession()
      Checks if HTTP sessions are enabled.
      Returns:
      true if sessions are used, false otherwise
    • setUseSession

      public void setUseSession(boolean useSession)
      Sets whether to use HTTP sessions.
      Parameters:
      useSession - true to enable sessions, false to disable
    • addInitFacetParam

      public void addInitFacetParam(String key, String value)
      Adds an initial facet parameter mapping.
      Parameters:
      key - the parameter key
      value - the parameter value
    • getInitFacetParamMap

      public Map<String,String> getInitFacetParamMap()
      Gets the initial facet parameter mappings.
      Returns:
      the facet parameter map
    • addInitGeoParam

      public void addInitGeoParam(String key, String value)
      Adds an initial geographic parameter mapping.
      Parameters:
      key - the parameter key
      value - the parameter value
    • getInitGeoParamMap

      public Map<String,String> getInitGeoParamMap()
      Gets the initial geographic parameter mappings.
      Returns:
      the geographic parameter map
    • addFacetQueryView

      public void addFacetQueryView(FacetQueryView facetQueryView)
      Adds a facet query view to the list.
      Parameters:
      facetQueryView - the facet query view to add
    • getFacetQueryViewList

      public List<FacetQueryView> getFacetQueryViewList()
      Gets the list of facet query views.
      Returns:
      the list of facet query views
    • addInlineMimeType

      public void addInlineMimeType(String mimeType)
      Adds a MIME type to the inline display set.
      Parameters:
      mimeType - the MIME type to display inline
    • getActionHook

      public ViewHelper.ActionHook getActionHook()
      Gets the action hook for custom processing.
      Returns:
      the action hook
    • setActionHook

      public void setActionHook(ViewHelper.ActionHook actionHook)
      Sets the action hook for custom processing.
      Parameters:
      actionHook - the action hook to set
    • setEncodeUrlLink

      public void setEncodeUrlLink(boolean encodeUrlLink)
      Sets whether to encode URL links.
      Parameters:
      encodeUrlLink - true to encode URL links, false otherwise
    • setUrlLinkEncoding

      public void setUrlLinkEncoding(String urlLinkEncoding)
      Sets the character encoding for URL links.
      Parameters:
      urlLinkEncoding - the character encoding to use
    • setOriginalHighlightTagPre

      public void setOriginalHighlightTagPre(String originalHighlightTagPre)
      Sets the original highlight tag prefix.
      Parameters:
      originalHighlightTagPre - the highlight tag prefix
    • setOriginalHighlightTagPost

      public void setOriginalHighlightTagPost(String originalHighlightTagPost)
      Sets the original highlight tag suffix.
      Parameters:
      originalHighlightTagPost - the highlight tag suffix
    • setCacheTemplateName

      public void setCacheTemplateName(String cacheTemplateName)
      Sets the cache template name.
      Parameters:
      cacheTemplateName - the template name for cache content
    • setFacetCacheDuration

      public void setFacetCacheDuration(long facetCacheDuration)
      Sets the facet cache duration in seconds.
      Parameters:
      facetCacheDuration - the cache duration in seconds