001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.help; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.i18n.CmsEncoder; 033import org.opencms.i18n.CmsLocaleManager; 034import org.opencms.i18n.CmsMessages; 035import org.opencms.jsp.CmsJspActionElement; 036import org.opencms.main.CmsException; 037import org.opencms.search.CmsSearch; 038import org.opencms.search.CmsSearchResult; 039import org.opencms.search.fields.CmsSearchField; 040import org.opencms.util.CmsStringUtil; 041 042import java.util.ArrayList; 043import java.util.Iterator; 044import java.util.List; 045import java.util.Map; 046import java.util.SortedMap; 047import java.util.StringTokenizer; 048import java.util.TreeMap; 049 050import jakarta.servlet.ServletRequest; 051import jakarta.servlet.http.HttpServletRequest; 052import jakarta.servlet.http.HttpServletResponse; 053import jakarta.servlet.jsp.PageContext; 054 055/** 056 * Displays the result of a <code>{@link org.opencms.search.CmsSearch}</code>.<p> 057 * 058 * Requires the following request parameters (see constructor): 059 * <ul> 060 * <li> 061 * index:<br>the String identifying the required search index. 062 * <li> 063 * query:<br>the search query to run. 064 * </ul> 065 * <p> 066 * 067 * @since 6.0.0 068 */ 069public class CmsHelpSearchResultView { 070 071 /** The project that forces evaluation of all dynamic content. */ 072 protected CmsProject m_offlineProject; 073 074 /** The project that allows static export. */ 075 protected CmsProject m_onlineProject; 076 077 /** The flag that decides wethter links to search result point to their exported version or not. */ 078 private boolean m_exportLinks; 079 080 /** 081 * A small cache for the forms generated by <code>{@link #toPostParameters(String)}</code> during a request. <p> 082 * 083 * Avoids duplicate forms.</p> 084 * 085 */ 086 private SortedMap<String, String> m_formCache; 087 088 /** The CmsJspActionElement to use. **/ 089 private CmsJspActionElement m_jsp; 090 091 /** The url to a proprietary search url (different from m_jsp.getRequestContext().getUri). **/ 092 private String m_searchRessourceUrl; 093 094 /** 095 * Constructor with the action element to use. <p> 096 * 097 * @param action the action element to use 098 */ 099 public CmsHelpSearchResultView(CmsJspActionElement action) { 100 101 m_jsp = action; 102 m_formCache = new TreeMap<String, String>(); 103 try { 104 m_onlineProject = m_jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID); 105 m_offlineProject = m_jsp.getRequestContext().getCurrentProject(); 106 } catch (CmsException e) { 107 // failed to get online project, at least avoid NPE 108 m_onlineProject = m_offlineProject; 109 } 110 111 } 112 113 /** 114 * Constructor with arguments for construction of a <code>{@link CmsJspActionElement}</code>. <p> 115 * 116 * @param pageContext the page context to use 117 * @param request the current request 118 * @param response the current response 119 */ 120 public CmsHelpSearchResultView(PageContext pageContext, HttpServletRequest request, HttpServletResponse response) { 121 122 this(new CmsJspActionElement(pageContext, request, response)); 123 124 } 125 126 /** 127 * Returns the formatted search results.<p> 128 * 129 * @param search the preconfigured search bean 130 * @return the formatted search results 131 */ 132 public String displaySearchResult(CmsSearch search) { 133 134 initSearch(search); 135 StringBuffer result = new StringBuffer(800); 136 CmsMessages messages = org.opencms.search.Messages.get().getBundle(m_jsp.getRequestContext().getLocale()); 137 138 result.append("<h1>\n"); 139 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_RESULT_TITLE_0)); 140 result.append("\n</h1>\n"); 141 List<CmsSearchResult> searchResult; 142 if (CmsStringUtil.isEmptyOrWhitespaceOnly(search.getQuery())) { 143 search.setQuery(""); 144 searchResult = new ArrayList<CmsSearchResult>(); 145 } else { 146 searchResult = search.getSearchResult(); 147 } 148 149 HttpServletRequest request = m_jsp.getRequest(); 150 // get the action to perform from the request 151 String action = request.getParameter("action"); 152 153 if ((action != null) && (searchResult == null)) { 154 result.append("<p class=\"formerror\">\n"); 155 if (search.getLastException() != null) { 156 157 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_UNAVAILABLE_0)); 158 result.append("\n<!-- ").append(search.getLastException().toString()); 159 result.append(" //-->\n"); 160 } else { 161 result.append( 162 messages.key( 163 org.opencms.search.Messages.GUI_HELP_SEARCH_NOMATCH_1, 164 CmsEncoder.escapeXml(search.getQuery()))); 165 result.append("\n"); 166 } 167 result.append("</p>\n"); 168 } else if ((action != null) && (searchResult.size() <= 0)) { 169 result.append("<p class=\"formerror\">\n"); 170 result.append( 171 messages.key( 172 org.opencms.search.Messages.GUI_HELP_SEARCH_NOMATCH_1, 173 CmsEncoder.escapeXml(search.getQuery()))); 174 result.append("\n"); 175 result.append("</p>\n"); 176 } else if ((action != null) && (searchResult.size() > 0)) { 177 result.append("<p>\n"); 178 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_RESULT_START_0)); 179 result.append("\n"); 180 result.append("</p>\n<p>\n"); 181 182 Iterator<CmsSearchResult> iterator = searchResult.iterator(); 183 184 try { 185 if (m_exportLinks) { 186 m_jsp.getRequestContext().setCurrentProject(m_onlineProject); 187 } 188 189 while (iterator.hasNext()) { 190 CmsSearchResult entry = iterator.next(); 191 result.append("\n<div class=\"searchResult\"><a class=\"navhelp\" href=\""); 192 193 result.append( 194 m_jsp.link( 195 new StringBuffer( 196 "/system/modules/org.opencms.workplace.help/jsptemplates/help_body.jsp?helpresource=").append( 197 m_jsp.getRequestContext().removeSiteRoot(entry.getPath())).append("&").append( 198 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 199 m_jsp.getRequestContext().getLocale()).toString())); 200 result.append("\">\n"); 201 result.append(entry.getField(CmsSearchField.FIELD_TITLE)); 202 result.append("</a>"); 203 result.append(" (").append(entry.getScore()).append(" %)\n"); 204 result.append("<span class=\"searchExcerpt\">\n"); 205 result.append(entry.getExcerpt()).append('\n'); 206 result.append("</span>\n"); 207 result.append("</div>\n"); 208 } 209 } finally { 210 m_jsp.getRequestContext().setCurrentProject(m_offlineProject); 211 } 212 213 result.append("</p>\n"); 214 215 // search page links below results 216 if ((search.getPreviousUrl() != null) || (search.getNextUrl() != null)) { 217 result.append("<p>"); 218 if (search.getPreviousUrl() != null) { 219 220 result.append("<a href=\""); 221 result.append( 222 getSearchPageLink( 223 m_jsp.link( 224 new StringBuffer(search.getPreviousUrl()).append('&').append( 225 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 226 m_jsp.getRequestContext().getLocale()).toString()))); 227 result.append("\">"); 228 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_BACK_0)); 229 result.append(" <<</a> \n"); 230 } 231 Map<Integer, String> pageLinks = search.getPageLinks(); 232 Iterator<Integer> i = pageLinks.keySet().iterator(); 233 while (i.hasNext()) { 234 int pageNumber = i.next().intValue(); 235 result.append(" "); 236 if (pageNumber != search.getSearchPage()) { 237 result.append("<a href=\"").append( 238 getSearchPageLink( 239 m_jsp.link( 240 new StringBuffer(pageLinks.get(Integer.valueOf(pageNumber))).append('&').append( 241 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 242 m_jsp.getRequestContext().getLocale()).toString()))); 243 result.append("\" target=\"_self\">").append(pageNumber).append("</a>\n"); 244 } else { 245 result.append(pageNumber); 246 } 247 } 248 if (search.getNextUrl() != null) { 249 result.append(" <a href=\""); 250 result.append( 251 getSearchPageLink( 252 new StringBuffer(m_jsp.link(search.getNextUrl())).append('&').append( 253 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 254 m_jsp.getRequestContext().getLocale()).toString())); 255 result.append("\">>>"); 256 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_NEXT_0)); 257 result.append("</a>\n"); 258 } 259 result.append("</p>\n"); 260 } 261 262 } 263 264 // include the post forms for the page links: 265 Iterator<String> values = m_formCache.values().iterator(); 266 while (values.hasNext()) { 267 result.append(values.next()); 268 } 269 return result.toString(); 270 271 } 272 273 /** 274 * Returns true if the links to search results shall point to exported content, false else. <p> 275 * @return true if the links to search results shall point to exported content, false else 276 */ 277 public boolean isExportLinks() { 278 279 return m_exportLinks; 280 } 281 282 /** 283 * Set wether the links to search results point to exported content or not. <p> 284 * 285 * @param exportLinks The value that decides Set wether the links to search results point to exported content or not. 286 */ 287 public void setExportLinks(boolean exportLinks) { 288 289 m_exportLinks = exportLinks; 290 } 291 292 /** 293 * Set a proprietary resource uri for the search page. <p> 294 * 295 * This is optional but allows to override the standard search result links 296 * (for next or previous pages) that point to 297 * <code>getJsp().getRequestContext().getUri()</code> whenever the search 298 * uri is element of some template and should not be linked directly.<p> 299 * 300 * @param uri the proprietary resource uri for the search page 301 */ 302 public void setSearchRessourceUrl(String uri) { 303 304 m_searchRessourceUrl = uri; 305 } 306 307 /** 308 * Returns the resource uri to the search page with respect to the 309 * optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code> 310 * with the request parameters of the given argument.<p> 311 * 312 * This is a workaround for Tomcat bug 35775 313 * (http://issues.apache.org/bugzilla/show_bug.cgi?id=35775). After it has been 314 * fixed the version 1.1 should be restored (or at least this codepath should be switched back.<p> 315 * 316 * 317 * @param link the suggestion of the search result bean ( a previous, next or page number url) 318 * @return the resource uri to the search page with respect to the 319 * optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code> 320 * with the request parameters of the given argument 321 */ 322 private String getSearchPageLink(String link) { 323 324 if (m_searchRessourceUrl != null) { 325 // for the form to generate we need params. 326 String pageParams = ""; 327 int paramIndex = link.indexOf('?'); 328 if (paramIndex > 0) { 329 pageParams = link.substring(paramIndex); 330 } 331 String formurl = new StringBuffer(m_searchRessourceUrl).append(pageParams).toString(); 332 toPostParameters(formurl); 333 334 link = new StringBuffer("javascript:document.forms['").append("form").append(m_formCache.size() - 1).append( 335 "'].submit()").toString(); 336 } 337 return link; 338 } 339 340 /** 341 * Initialize the search for the help pages.<p> 342 * 343 * @param search the search configuration (will be initialized) 344 */ 345 private void initSearch(CmsSearch search) { 346 347 // Collect the objects required to access the OpenCms VFS from the request 348 CmsObject cmsObject = m_jsp.getCmsObject(); 349 350 ServletRequest request = m_jsp.getRequest(); 351 search.init(cmsObject); 352 search.setField( 353 new String[] { 354 CmsSearchField.FIELD_TITLE, 355 CmsSearchField.FIELD_KEYWORDS, 356 CmsSearchField.FIELD_DESCRIPTION, 357 CmsSearchField.FIELD_CONTENT}); 358 search.setMatchesPerPage(5); 359 search.setDisplayPages(7); 360 search.setSearchRoot( 361 new StringBuffer("/system/workplace/locales/").append(m_jsp.getRequestContext().getLocale()).append( 362 "/help/").toString()); 363 364 String query = request.getParameter("query"); 365 search.setQuery(query); 366 } 367 368 /** 369 * Generates a html form (named form<n>) with parameters found in 370 * the given GET request string (appended params with "?value=param&value2=param2). 371 * >n< is the number of forms that already have been generated. 372 * This is a content-expensive bugfix for http://issues.apache.org/bugzilla/show_bug.cgi?id=35775 373 * and should be replaced with the 1.1 revision as soon that bug is fixed.<p> 374 * 375 * The forms action will point to the given uri's path info part: All links in the page 376 * that includes this generated html and that are related to the get request should 377 * have "src='#'" and "onclick=documents.forms['<getRequestUri>'].submit()". <p> 378 * 379 * The generated form lands in the internal <code>Map {@link #m_formCache}</code> as mapping 380 * from uris to Strings and has to be appended to the output at a valid position. <p> 381 * 382 * Warning: Does not work with multiple values mapped to one parameter ("key = value1,value2..").<p> 383 * 384 * 385 * 386 * @param getRequestUri a request uri with optional query, will be used as name of the form too. 387 */ 388 private void toPostParameters(String getRequestUri) { 389 390 StringBuffer result; 391 if (!m_formCache.containsKey(getRequestUri)) { 392 393 result = new StringBuffer(); 394 int index = getRequestUri.indexOf('?'); 395 String query = ""; 396 String path = ""; 397 if (index > 0) { 398 query = getRequestUri.substring(index + 1); 399 path = getRequestUri.substring(0, index); 400 result.append("\n<form method=\"post\" name=\"form").append(m_formCache.size()).append("\" action=\""); 401 result.append(path).append("\">\n"); 402 403 // "key=value" pairs as tokens: 404 StringTokenizer entryTokens = new StringTokenizer(query, "&", false); 405 while (entryTokens.hasMoreTokens()) { 406 StringTokenizer keyValueToken = new StringTokenizer(entryTokens.nextToken(), "=", false); 407 if (keyValueToken.countTokens() != 2) { 408 continue; 409 } 410 // Undo the possible already performed url encoding for the given url 411 String key = CmsEncoder.decode(keyValueToken.nextToken()); 412 String value = CmsEncoder.decode(keyValueToken.nextToken()); 413 result.append(" <input type=\"hidden\" name=\""); 414 result.append(key).append("\" value=\""); 415 result.append(value).append("\" />\n"); 416 } 417 result.append("</form>\n"); 418 m_formCache.put(getRequestUri, result.toString()); 419 } 420 } 421 } 422}