001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://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: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://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.tools.searchindex; 029 030import org.opencms.file.CmsProject; 031import org.opencms.i18n.CmsEncoder; 032import org.opencms.i18n.CmsLocaleManager; 033import org.opencms.i18n.CmsMessages; 034import org.opencms.jsp.CmsJspActionElement; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037import org.opencms.search.CmsSearch; 038import org.opencms.search.CmsSearchResult; 039import org.opencms.search.fields.CmsSearchField; 040import org.opencms.util.CmsStringUtil; 041import org.opencms.workplace.CmsWidgetDialog; 042 043import java.util.Collections; 044import java.util.Iterator; 045import java.util.List; 046import java.util.Locale; 047import java.util.Map; 048import java.util.SortedMap; 049import java.util.StringTokenizer; 050import java.util.TreeMap; 051 052import javax.servlet.http.HttpServletRequest; 053import javax.servlet.http.HttpServletResponse; 054import javax.servlet.jsp.PageContext; 055 056/** 057 * Displays the result of a <code>{@link org.opencms.search.CmsSearch}</code>.<p> 058 * 059 * Requires the following request parameters (see constructor): 060 * <ul> 061 * <li> 062 * index:<br>the String identifying the required search index. 063 * <li> 064 * query:<br>the search query to run. 065 * </ul> 066 * <p> 067 * 068 * @since 6.0.0 069 */ 070public class CmsSearchResultView { 071 072 /** 073 * A simple wrapper around html for a form and it's name for value 074 * side of form cache, necessary as order in Map is arbitrary, 075 * SortedMap unusable as backlinks have higher order, lower formname... <p> 076 */ 077 private final class HTMLForm { 078 079 /** The html of the generated form. **/ 080 String m_formHtml; 081 082 /** The name of the generated form. **/ 083 String m_formName; 084 085 /** 086 * Creates an instance with the given formname and the given html code for the form. <p> 087 * 088 * @param formName the form name of the form 089 * @param formHtml the form html of the form 090 */ 091 HTMLForm(String formName, String formHtml) { 092 093 m_formName = formName; 094 m_formHtml = formHtml; 095 } 096 097 /** 098 * Returns the form html.<p> 099 * 100 * @return the form html 101 * 102 * @see java.lang.Object#toString() 103 */ 104 @Override 105 public String toString() { 106 107 return m_formHtml; 108 } 109 } 110 111 /** The project that forces evaluation of all dynamic content. */ 112 protected CmsProject m_offlineProject; 113 114 /** The project that allows static export. */ 115 protected CmsProject m_onlineProject; 116 117 /** The flag that decides whether links to search result point to their exported version or not. */ 118 private boolean m_exportLinks; 119 120 /** 121 * A small cache for the forms generated by <code>{@link #toPostParameters(String, CmsSearch)}</code> during a request. <p> 122 * 123 * Avoids duplicate forms.<p> 124 */ 125 private SortedMap<String, HTMLForm> m_formCache; 126 127 /** The CmsJspActionElement to use. **/ 128 private CmsJspActionElement m_jsp; 129 130 /** The url to a proprietary search url (different from m_jsp.getRequestContext().getUri). **/ 131 private String m_searchRessourceUrl; 132 133 /** 134 * Constructor with the action element to use. <p> 135 * 136 * @param action the action element to use 137 */ 138 public CmsSearchResultView(CmsJspActionElement action) { 139 140 m_jsp = action; 141 m_formCache = new TreeMap<String, HTMLForm>(); 142 try { 143 m_onlineProject = m_jsp.getCmsObject().readProject(CmsProject.ONLINE_PROJECT_ID); 144 m_offlineProject = m_jsp.getRequestContext().getCurrentProject(); 145 } catch (CmsException e) { 146 // failed to get online project, at least avoid NPE 147 m_onlineProject = m_offlineProject; 148 } 149 150 } 151 152 /** 153 * Constructor with arguments for construction of a <code>{@link CmsJspActionElement}</code>. <p> 154 * 155 * @param pageContext the page context to use 156 * @param request the current request 157 * @param response the current response 158 */ 159 public CmsSearchResultView(PageContext pageContext, HttpServletRequest request, HttpServletResponse response) { 160 161 this(new CmsJspActionElement(pageContext, request, response)); 162 163 } 164 165 /** 166 * Returns the formatted search results.<p> 167 * 168 * @param search the pre-configured search bean 169 * @return the formatted search results 170 */ 171 public String displaySearchResult(CmsSearch search) { 172 173 StringBuffer result = new StringBuffer(800); 174 Locale locale = m_jsp.getRequestContext().getLocale(); 175 CmsMessages messages = org.opencms.search.Messages.get().getBundle(locale); 176 177 result.append("<h3>\n"); 178 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_RESULT_TITLE_0)); 179 result.append("\n</h1>\n"); 180 List<CmsSearchResult> searchResult; 181 if (CmsStringUtil.isEmptyOrWhitespaceOnly(search.getQuery())) { 182 search.setQuery(""); 183 searchResult = Collections.emptyList(); 184 } else { 185 search.setMatchesPerPage(5); 186 searchResult = search.getSearchResult(); 187 } 188 189 HttpServletRequest request = m_jsp.getRequest(); 190 // get the action to perform from the request 191 String action = request.getParameter("action"); 192 193 if ((action != null) && (searchResult == null)) { 194 result.append("<p class=\"formerror\">\n"); 195 if (search.getLastException() != null) { 196 197 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_UNAVAILABLE_0)); 198 result.append("\n<!-- ").append(search.getLastException().toString()); 199 result.append(" //-->\n"); 200 } else { 201 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_NOMATCH_1, search.getQuery())); 202 result.append("\n"); 203 } 204 result.append("</p>\n"); 205 } else if ((action != null) && (searchResult.size() <= 0)) { 206 result.append("<p class=\"formerror\">\n"); 207 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_NOMATCH_1, search.getQuery())); 208 result.append("\n"); 209 result.append("</p>\n"); 210 } else if ((action != null) && (searchResult.size() > 0)) { 211 result.append("<p>\n"); 212 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_RESULT_START_0)); 213 result.append("\n"); 214 result.append("</p>\n<p>\n"); 215 216 Iterator<CmsSearchResult> iterator = searchResult.iterator(); 217 218 try { 219 if (m_exportLinks) { 220 m_jsp.getRequestContext().setCurrentProject(m_onlineProject); 221 } 222 223 String name; 224 String path; 225 while (iterator.hasNext()) { 226 CmsSearchResult entry = iterator.next(); 227 result.append("\n<div class=\"searchResult\">"); 228 result.append("<a class=\"navhelp\" href=\"#\" onclick=\"javascript:window.open('"); 229 // CmsJspActionElement.link() is not programmed for using from root site 230 // because it assumes the "default CmsSite" when no configured site matches the 231 // root site "/": 232 if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_jsp.getRequestContext().getSiteRoot())) { 233 // link will always cut off "/sites/default" if we are in the 234 // root site, this call is only used to get the scheme, host, context name and servlet name... 235 path = m_jsp.link(OpenCms.getSiteManager().getDefaultSite().getSiteRoot() + "/") 236 + entry.getPath().substring(1); 237 } else { 238 path = m_jsp.link(entry.getPath()); 239 } 240 result.append(path); 241 result.append( 242 "', '_blank', 'width='+screen.availWidth+', height='+ screen.availHeight+', scrollbars=yes, menubar=yes, toolbar=yes')\""); 243 result.append("\">\n"); 244 name = entry.getField(CmsSearchField.FIELD_TITLE); 245 if (name == null) { 246 name = entry.getPath(); 247 } 248 result.append(name); 249 result.append("</a>"); 250 result.append(" (").append(entry.getScore()).append(" %)\n"); 251 252 result.append("<span class=\"searchExcerpt\">\n"); 253 String excerptString = entry.getExcerpt(); 254 if (CmsStringUtil.isEmptyOrWhitespaceOnly(excerptString)) { 255 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_SEARCH_EXCERPT_UNAVAILABLE_0)); 256 } else { 257 result.append(excerptString).append('\n'); 258 } 259 result.append("</span>\n"); 260 result.append("</div>\n"); 261 } 262 } finally { 263 m_jsp.getRequestContext().setCurrentProject(m_offlineProject); 264 } 265 266 result.append("</p>\n"); 267 268 // search page links below results 269 if ((search.getPreviousUrl() != null) || (search.getNextUrl() != null)) { 270 result.append("<p>"); 271 if (search.getPreviousUrl() != null) { 272 273 result.append("<a class=\"searchlink\" href=\""); 274 result.append( 275 getSearchPageLink( 276 m_jsp.link(new StringBuffer(search.getPreviousUrl()).append('&').append( 277 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 278 m_jsp.getRequestContext().getLocale()).toString()), 279 search)); 280 result.append("\">"); 281 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_BACK_0)); 282 result.append(" <<</a> \n"); 283 } 284 Map<Integer, String> pageLinks = search.getPageLinks(); 285 Iterator<Integer> i = pageLinks.keySet().iterator(); 286 while (i.hasNext()) { 287 int pageNumber = (i.next()).intValue(); 288 289 result.append(" "); 290 if (pageNumber != search.getSearchPage()) { 291 result.append("<a class=\"searchlink\" href=\"").append( 292 getSearchPageLink( 293 m_jsp.link(new StringBuffer(pageLinks.get(Integer.valueOf(pageNumber))).append('&').append( 294 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 295 m_jsp.getRequestContext().getLocale()).toString()), 296 search)); 297 result.append("\" target=\"_self\">").append(pageNumber).append("</a>\n"); 298 } else { 299 result.append(pageNumber); 300 } 301 } 302 if (search.getNextUrl() != null) { 303 result.append(" <a class=\"searchlink\" href=\""); 304 result.append( 305 getSearchPageLink( 306 new StringBuffer(m_jsp.link(search.getNextUrl())).append('&').append( 307 CmsLocaleManager.PARAMETER_LOCALE).append("=").append( 308 m_jsp.getRequestContext().getLocale()).toString(), 309 search)); 310 result.append("\">>>"); 311 result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_NEXT_0)); 312 result.append("</a>\n"); 313 } 314 result.append("</p>\n"); 315 } 316 317 } 318 319 // include the post forms for the page links: 320 Iterator<HTMLForm> values = m_formCache.values().iterator(); 321 while (values.hasNext()) { 322 result.append(values.next()); 323 } 324 return result.toString(); 325 326 } 327 328 /** 329 * Returns true if the links to search results shall point to exported content, false else. <p> 330 * @return true if the links to search results shall point to exported content, false else 331 */ 332 public boolean isExportLinks() { 333 334 return m_exportLinks; 335 } 336 337 /** 338 * Set wether the links to search results point to exported content or not. <p> 339 * 340 * @param exportLinks The value that decides Set wether the links to search results point to exported content or not. 341 */ 342 public void setExportLinks(boolean exportLinks) { 343 344 m_exportLinks = exportLinks; 345 } 346 347 /** 348 * Set a proprietary resource uri for the search page. <p> 349 * 350 * This is optional but allows to override the standard search result links 351 * (for next or previous pages) that point to 352 * <code>getJsp().getRequestContext().getUri()</code> whenever the search 353 * uri is element of some template and should not be linked directly.<p> 354 * 355 * @param uri the proprietary resource uri for the search page 356 */ 357 public void setSearchRessourceUrl(String uri) { 358 359 m_searchRessourceUrl = uri; 360 } 361 362 /** 363 * Returns the resource uri to the search page with respect to the 364 * optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code> 365 * with the request parameters of the given argument.<p> 366 * 367 * This is a workaround for Tomcat bug 35775 368 * (http://issues.apache.org/bugzilla/show_bug.cgi?id=35775). After it has been 369 * fixed the version 1.1 should be restored (or at least this codepath should be switched back.<p> 370 * 371 * @param link the suggestion of the search result bean ( a previous, next or page number url) 372 * @param search the search bean 373 * 374 * @return the resource uri to the search page with respect to the 375 * optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code> 376 * with the request parameters of the given argument 377 */ 378 private String getSearchPageLink(String link, CmsSearch search) { 379 380 if (m_searchRessourceUrl != null) { 381 // for the form to generate we need params. 382 String pageParams = ""; 383 int paramIndex = link.indexOf('?'); 384 if (paramIndex > 0) { 385 pageParams = link.substring(paramIndex); 386 } 387 StringBuffer formurl = new StringBuffer(m_searchRessourceUrl); 388 if (m_searchRessourceUrl.indexOf('?') != -1) { 389 // the search page url already has a query string, don't start params of search-generated link 390 // with '?' 391 pageParams = new StringBuffer("&").append(pageParams.substring(1)).toString(); 392 } 393 formurl.append(pageParams).toString(); 394 String formname = toPostParameters(formurl.toString(), search); 395 396 link = new StringBuffer("javascript:document.forms['").append(formname).append("'].submit()").toString(); 397 } 398 return link; 399 } 400 401 /** 402 * Generates a html form (named form<n>) with parameters found in 403 * the given GET request string (appended params with "?value=param&value2=param2). 404 * >n< is the number of forms that already have been generated. 405 * This is a content-expensive bugfix for http://issues.apache.org/bugzilla/show_bug.cgi?id=35775 406 * and should be replaced with the 1.1 revision as soon that bug is fixed.<p> 407 * 408 * The forms action will point to the given uri's path info part: All links in the page 409 * that includes this generated html and that are related to the get request should 410 * have "src='#'" and "onclick=documents.forms['<getRequestUri>'].submit()". <p> 411 * 412 * The generated form lands in the internal <code>Map {@link #m_formCache}</code> as mapping 413 * from uris to Strings and has to be appended to the output at a valid position. <p> 414 * 415 * Warning: Does not work with multiple values mapped to one parameter ("key = value1,value2..").<p> 416 * 417 * 418 * 419 * @param getRequestUri a request uri with optional query, will be used as name of the form too 420 * @param search the search bean to get the parameters from 421 * 422 * @return the formname of the the generated form that contains the parameters of the given request uri or 423 * the empty String if there weren't any get parameters in the given request uri. 424 */ 425 private String toPostParameters(String getRequestUri, CmsSearch search) { 426 427 StringBuffer result; 428 String formname = ""; 429 430 if (!m_formCache.containsKey(getRequestUri)) { 431 432 result = new StringBuffer(); 433 int index = getRequestUri.indexOf('?'); 434 String query = ""; 435 String path = ""; 436 if (index > 0) { 437 query = getRequestUri.substring(index + 1); 438 path = getRequestUri.substring(0, index); 439 formname = new StringBuffer("searchform").append(m_formCache.size()).toString(); 440 441 result.append("\n<form method=\"post\" name=\"").append(formname).append("\" action=\""); 442 result.append(path).append("\">\n"); 443 // "key=value" pairs as tokens: 444 StringTokenizer entryTokens = new StringTokenizer(query, "&", false); 445 while (entryTokens.hasMoreTokens()) { 446 StringTokenizer keyValueToken = new StringTokenizer(entryTokens.nextToken(), "=", false); 447 if (keyValueToken.countTokens() != 2) { 448 continue; 449 } 450 // Undo the possible already performed url encoding for the given url 451 String key = CmsEncoder.decode(keyValueToken.nextToken()); 452 String value = CmsEncoder.decode(keyValueToken.nextToken()); 453 if ("action".equals(key)) { 454 // cannot use the "search"-action value in combination with CmsWidgetDialog: prepareCommit would be left out! 455 value = CmsWidgetDialog.DIALOG_SAVE; 456 } 457 result.append(" <input type=\"hidden\" name=\""); 458 result.append(key).append("\" value=\""); 459 result.append(value).append("\" />\n"); 460 } 461 462 // custom search index code for making category widget - compatible 463 // this is needed for transforming e.g. the CmsSearch-generated 464 // "&category=a,b,c" to widget fields categories.0..categories.n. 465 List<String> categories = search.getParameters().getCategories(); 466 Iterator<String> it = categories.iterator(); 467 int count = 0; 468 while (it.hasNext()) { 469 result.append(" <input type=\"hidden\" name=\""); 470 result.append("categories.").append(count).append("\" value=\""); 471 result.append(it.next()).append("\" />\n"); 472 count++; 473 } 474 List<String> roots = search.getParameters().getRoots(); 475 it = roots.iterator(); 476 count = 0; 477 while (it.hasNext()) { 478 result.append(" <input type=\"hidden\" name=\""); 479 result.append("roots.").append(count).append("\" value=\""); 480 result.append(it.next()).append("\" />\n"); 481 count++; 482 } 483 484 result.append(" <input type=\"hidden\" name=\""); 485 result.append("fields").append("\" value=\""); 486 result.append(CmsStringUtil.collectionAsString(search.getParameters().getFields(), ",")); 487 result.append("\" />\n"); 488 489 result.append(" <input type=\"hidden\" name=\""); 490 result.append("sortfields.").append(0).append("\" value=\""); 491 result.append(search.getParameters().getSortName()).append("\" />\n"); 492 493 result.append("</form>\n"); 494 HTMLForm form = new HTMLForm(formname, result.toString()); 495 496 m_formCache.put(getRequestUri, form); 497 return formname; 498 } 499 // empty String for no get parameters 500 return formname; 501 502 } else { 503 HTMLForm form = m_formCache.get(getRequestUri); 504 return form.m_formName; 505 } 506 } 507}