Package org.codelibs.fess.helper
Class ViewHelper
java.lang.Object
org.codelibs.fess.helper.ViewHelper
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHook class for customizing action processing.static classRepresents a text fragment for URL-based text highlighting. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ViewHelper.ActionHookAction hook for custom processingprotected static final StringCache key for cache messageprotected StringTemplate name for cache contentprotected static final StringHTTP header name for content dispositionprotected static final StringEllipsis string for text truncationprotected booleanWhether to encode URL linksprotected StringHTML-escaped highlight suffixprotected StringHTML-escaped highlight prefixprotected com.google.common.cache.Cache<String, FacetResponse> Cache for facet responsesprotected longDuration for facet cache in seconds (10 minutes)protected final List<FacetQueryView> List of facet query viewsprotected String[]Fields that should be highlighted in search resultsprotected StringConfigured highlight tag suffixprotected StringConfigured highlight tag prefixSet of terminal characters for highlightingprotected static final StringCache key for highlighted cache contentInitial facet parameter mappingsInitial geographic parameter mappingsSet of MIME types that should be displayed inlineprotected static final PatternPattern for matching local file pathsprotected StringOriginal highlight tag suffixprotected StringOriginal highlight tag prefixCache for page pathsprotected static final StringCache key for search queriesprotected static final StringRequest attribute key for screen widthprotected static final PatternPattern for matching shared folder pathsprotected static final intTablet width threshold for responsive designprotected intLength of text fragment prefixprotected intSize of text fragmentsprotected intLength of text fragment suffixprotected StringCharacter encoding for URL linksprotected booleanWhether to use HTTP sessions -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFacetQueryView(FacetQueryView facetQueryView) Adds a facet query view to the list.voidaddInitFacetParam(String key, String value) Adds an initial facet parameter mapping.voidaddInitGeoParam(String key, String value) Adds an initial geographic parameter mapping.voidaddInlineMimeType(String mimeType) Adds a MIME type to the inline display set.protected StringappendHTMLSearchWord(Map<String, Object> document, String url) Appends text fragment parameters to HTML URLs for highlighting.protected StringappendPDFSearchWord(Map<String, Object> document, String url) Appends search parameters to PDF URLs for highlighting.protected StringappendQueryParameter(Map<String, Object> document, String url) Appends query parameters to URLs based on document type.org.lastaflute.web.response.StreamResponseasContentResponse(Map<String, Object> doc) Creates a stream response for document content delivery.protected voidconfigureSessionCookie(jakarta.servlet.ServletContext servletContext, org.codelibs.fess.mylasta.direction.FessConfig fessConfig) Configures the session cookie (JSESSIONID) on the given servlet context.createCacheContent(Map<String, Object> doc, String[] queries) Creates cached content with highlighting for a document.Creates highlight information based on screen width.createHighlightText(org.opensearch.search.fetch.subphase.highlight.HighlightField highlightField) Creates highlighted text from highlight field fragments.createTextFragmentsByHighlight(org.opensearch.search.fetch.subphase.highlight.HighlightField[] fields) Creates text fragments from highlight fields for URL fragment navigation.Creates text fragments from search queries.protected StringescapeHighlight(String text) Escapes HTML and applies highlighting to text.Gets the action hook for custom processing.getCachedFacetResponse(String query) Gets a cached facet response for the given query.getClientIp(jakarta.servlet.http.HttpServletRequest request) Gets the client IP address from the request.getContentDescription(Map<String, Object> document) Gets the content description from highlighted fields.getContentTitle(Map<String, Object> document) Gets the display title for a document.Gets the list of facet query views.Gets the initial facet parameter mappings.Gets the initial geographic parameter mappings.getPagePath(String page) Gets the localized page path for a given page name.Gets the set of highlight queries from the current request.getSitePath(Map<String, Object> docMap) Gets the site path for display purposes.getUrlLink(Map<String, Object> document) Gets the URL link for a document with proper protocol handling.voidinit()Initializes the ViewHelper with configuration settings.booleanChecks if HTTP sessions are enabled.protected StringremoveHighlightTag(String str) Removes highlight tags from a string.protected StringreplaceHighlightQueries(String cache, String[] queries) Replaces search queries with highlighted versions in cached content.voidsetActionHook(ViewHelper.ActionHook actionHook) Sets the action hook for custom processing.voidsetCacheTemplateName(String cacheTemplateName) Sets the cache template name.voidsetEncodeUrlLink(boolean encodeUrlLink) Sets whether to encode URL links.voidsetFacetCacheDuration(long facetCacheDuration) Sets the facet cache duration in seconds.voidsetOriginalHighlightTagPost(String originalHighlightTagPost) Sets the original highlight tag suffix.voidsetOriginalHighlightTagPre(String originalHighlightTagPre) Sets the original highlight tag prefix.voidsetUrlLinkEncoding(String urlLinkEncoding) Sets the character encoding for URL links.voidsetUseSession(boolean useSession) Sets whether to use HTTP sessions.protected StringupdateFileProtocol(String url) Updates file protocol based on user agent type.protected voidupdateHighlightInfo(HighlightInfo highlightInfo, int width) Updates highlight information based on screen width.protected org.lastaflute.web.response.StreamResponsewriteContent(String configId, String url, org.codelibs.fess.crawler.client.CrawlerClient client) Writes content from a crawler client to a stream response.protected voidwriteContentType(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData) Writes content type header to the response.protected voidwriteFileName(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData) Writes content disposition header with filename.protected voidwriteNoCache(org.lastaflute.web.response.StreamResponse response, org.codelibs.fess.crawler.entity.ResponseData responseData) Writes no-cache headers to the response.
-
Field Details
-
SCREEN_WIDTH
Request attribute key for screen width- See Also:
-
TABLET_WIDTH
protected static final int TABLET_WIDTHTablet width threshold for responsive design- See Also:
-
CONTENT_DISPOSITION
HTTP header name for content disposition- See Also:
-
HL_CACHE
Cache key for highlighted cache content- See Also:
-
QUERIES
Cache key for search queries- See Also:
-
CACHE_MSG
Cache key for cache message- See Also:
-
LOCAL_PATH_PATTERN
Pattern for matching local file paths -
SHARED_FOLDER_PATTERN
Pattern for matching shared folder paths -
ELLIPSIS
Ellipsis string for text truncation- See Also:
-
encodeUrlLink
protected boolean encodeUrlLinkWhether to encode URL links -
urlLinkEncoding
Character encoding for URL links -
highlightedFields
Fields that should be highlighted in search results -
originalHighlightTagPre
Original highlight tag prefix -
originalHighlightTagPost
Original highlight tag suffix -
highlightTagPre
Configured highlight tag prefix -
highlightTagPost
Configured highlight tag suffix -
useSession
protected boolean useSessionWhether to use HTTP sessions -
pageCacheMap
Cache for page paths -
initFacetParamMap
Initial facet parameter mappings -
initGeoParamMap
Initial geographic parameter mappings -
facetQueryViewList
List of facet query views -
cacheTemplateName
Template name for cache content -
escapedHighlightPre
HTML-escaped highlight prefix -
escapedHighlightPost
HTML-escaped highlight suffix -
highlightTerminalCharSet
Set of terminal characters for highlighting -
actionHook
Action hook for custom processing -
inlineMimeTypeSet
Set of MIME types that should be displayed inline -
facetCache
Cache for facet responses -
facetCacheDuration
protected long facetCacheDurationDuration for facet cache in seconds (10 minutes) -
textFragmentPrefixLength
protected int textFragmentPrefixLengthLength of text fragment prefix -
textFragmentSuffixLength
protected int textFragmentSuffixLengthLength of text fragment suffix -
textFragmentSize
protected int textFragmentSizeSize 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, whensession.cookie.secureis enabled, adds theSecureattribute (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 whichSessionCookieConfigmay be mutated.- Parameters:
servletContext- the servlet context to configurefessConfig- the configuration holding the session cookie settings
-
getContentTitle
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
Gets the set of highlight queries from the current request.- Returns:
- OptionalThing containing the query set
-
getContentDescription
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
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
Removes highlight tags from a string.- Parameters:
str- the string containing highlight tags- Returns:
- the string with highlight tags removed
-
createHighlightInfo
Creates highlight information based on screen width. Adjusts fragment size for mobile devices.- Returns:
- the highlight information
-
updateHighlightInfo
Updates highlight information based on screen width. Reduces fragment size for smaller screens.- Parameters:
highlightInfo- the highlight info to updatewidth- the screen width
-
getUrlLink
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
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
Appends query parameters to URLs based on document type. Adds search highlighting for HTML and PDF documents.- Parameters:
document- the document data mapurl- the base URL- Returns:
- the URL with appended query parameters
-
appendHTMLSearchWord
Appends text fragment parameters to HTML URLs for highlighting.- Parameters:
document- the document data mapurl- the HTML URL- Returns:
- the URL with text fragment parameters
-
appendPDFSearchWord
Appends search parameters to PDF URLs for highlighting.- Parameters:
document- the document data mapurl- the PDF URL- Returns:
- the URL with search parameters
-
getPagePath
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
Creates cached content with highlighting for a document. Uses Handlebars templates to render the cached content.- Parameters:
doc- the document data mapqueries- the search queries for highlighting- Returns:
- the rendered cache content
-
replaceHighlightQueries
Replaces search queries with highlighted versions in cached content. Preserves HTML tags while highlighting text content.- Parameters:
cache- the cached contentqueries- the search queries to highlight- Returns:
- the content with highlighted queries
-
getSitePath
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
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 IDurl- the document URLclient- 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 responseresponseData- 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 responseresponseData- 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 responseresponseData- the response data
-
getClientIp
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
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
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
Adds an initial facet parameter mapping.- Parameters:
key- the parameter keyvalue- the parameter value
-
getInitFacetParamMap
Gets the initial facet parameter mappings.- Returns:
- the facet parameter map
-
addInitGeoParam
Adds an initial geographic parameter mapping.- Parameters:
key- the parameter keyvalue- the parameter value
-
getInitGeoParamMap
Gets the initial geographic parameter mappings.- Returns:
- the geographic parameter map
-
addFacetQueryView
Adds a facet query view to the list.- Parameters:
facetQueryView- the facet query view to add
-
getFacetQueryViewList
Gets the list of facet query views.- Returns:
- the list of facet query views
-
addInlineMimeType
Adds a MIME type to the inline display set.- Parameters:
mimeType- the MIME type to display inline
-
getActionHook
Gets the action hook for custom processing.- Returns:
- the action hook
-
setActionHook
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
Sets the character encoding for URL links.- Parameters:
urlLinkEncoding- the character encoding to use
-
setOriginalHighlightTagPre
Sets the original highlight tag prefix.- Parameters:
originalHighlightTagPre- the highlight tag prefix
-
setOriginalHighlightTagPost
Sets the original highlight tag suffix.- Parameters:
originalHighlightTagPost- the highlight tag suffix
-
setCacheTemplateName
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
-