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.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 javax.servlet.ServletRequest;
051import javax.servlet.http.HttpServletRequest;
052import javax.servlet.http.HttpServletResponse;
053import javax.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("&nbsp;(").append(entry.getScore()).append("&nbsp;%)\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(new StringBuffer(search.getPreviousUrl()).append('&').append(
224                                CmsLocaleManager.PARAMETER_LOCALE).append("=").append(
225                                    m_jsp.getRequestContext().getLocale()).toString())));
226                    result.append("\">");
227                    result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_BACK_0));
228                    result.append(" &lt;&lt;</a>&nbsp;&nbsp;\n");
229                }
230                Map<Integer, String> pageLinks = search.getPageLinks();
231                Iterator<Integer> i = pageLinks.keySet().iterator();
232                while (i.hasNext()) {
233                    int pageNumber = i.next().intValue();
234                    result.append(" ");
235                    if (pageNumber != search.getSearchPage()) {
236                        result.append("<a href=\"").append(
237                            getSearchPageLink(
238                                m_jsp.link(new StringBuffer(pageLinks.get(new Integer(pageNumber))).append('&').append(
239                                    CmsLocaleManager.PARAMETER_LOCALE).append("=").append(
240                                        m_jsp.getRequestContext().getLocale()).toString())));
241                        result.append("\" target=\"_self\">").append(pageNumber).append("</a>\n");
242                    } else {
243                        result.append(pageNumber);
244                    }
245                }
246                if (search.getNextUrl() != null) {
247                    result.append("&nbsp;&nbsp;&nbsp;<a href=\"");
248                    result.append(
249                        getSearchPageLink(
250                            new StringBuffer(m_jsp.link(search.getNextUrl())).append('&').append(
251                                CmsLocaleManager.PARAMETER_LOCALE).append("=").append(
252                                    m_jsp.getRequestContext().getLocale()).toString()));
253                    result.append("\">&gt;&gt;");
254                    result.append(messages.key(org.opencms.search.Messages.GUI_HELP_BUTTON_NEXT_0));
255                    result.append("</a>\n");
256                }
257                result.append("</p>\n");
258            }
259
260        }
261
262        // include the post forms for the page links:
263        Iterator<String> values = m_formCache.values().iterator();
264        while (values.hasNext()) {
265            result.append(values.next());
266        }
267        return result.toString();
268
269    }
270
271    /**
272     * Returns true if the links to search results shall point to exported content, false else. <p>
273     * @return true if the links to search results shall point to exported content, false else
274     */
275    public boolean isExportLinks() {
276
277        return m_exportLinks;
278    }
279
280    /**
281     * Set wether the links to search results point to exported content or not. <p>
282     *
283     * @param exportLinks The value that decides Set wether the links to search results point to exported content or not.
284     */
285    public void setExportLinks(boolean exportLinks) {
286
287        m_exportLinks = exportLinks;
288    }
289
290    /**
291     * Set a proprietary resource uri for the search page. <p>
292     *
293     * This is optional but allows to override the standard search result links
294     * (for next or previous pages) that point to
295     * <code>getJsp().getRequestContext().getUri()</code> whenever the search
296     * uri is element of some template and should not be linked directly.<p>
297     *
298     * @param uri the proprietary resource uri for the search page
299     */
300    public void setSearchRessourceUrl(String uri) {
301
302        m_searchRessourceUrl = uri;
303    }
304
305    /**
306     * Returns the resource uri to the search page with respect to the
307     * optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code>
308     * with the request parameters of the given argument.<p>
309     *
310     * This is a workaround for Tomcat bug 35775
311     * (http://issues.apache.org/bugzilla/show_bug.cgi?id=35775). After it has been
312     * fixed the version 1.1 should be restored (or at least this codepath should be switched back.<p>
313     *
314     *
315     * @param link the suggestion of the search result bean ( a previous, next or page number url)
316     * @return the resource uri to the search page with respect to the
317     *         optionally configured value <code>{@link #setSearchRessourceUrl(String)}</code>
318     *         with the request parameters of the given argument
319     */
320    private String getSearchPageLink(String link) {
321
322        if (m_searchRessourceUrl != null) {
323            // for the form to generate we need params.
324            String pageParams = "";
325            int paramIndex = link.indexOf('?');
326            if (paramIndex > 0) {
327                pageParams = link.substring(paramIndex);
328            }
329            String formurl = new StringBuffer(m_searchRessourceUrl).append(pageParams).toString();
330            toPostParameters(formurl);
331
332            link = new StringBuffer("javascript:document.forms['").append("form").append(m_formCache.size() - 1).append(
333                "'].submit()").toString();
334        }
335        return link;
336    }
337
338    /**
339     * Initialize the search for the help pages.<p>
340     *
341     * @param search the search configuration (will be initialized)
342     */
343    private void initSearch(CmsSearch search) {
344
345        //  Collect the objects required to access the OpenCms VFS from the request
346        CmsObject cmsObject = m_jsp.getCmsObject();
347
348        ServletRequest request = m_jsp.getRequest();
349        search.init(cmsObject);
350        search.setField(
351            new String[] {
352                CmsSearchField.FIELD_TITLE,
353                CmsSearchField.FIELD_KEYWORDS,
354                CmsSearchField.FIELD_DESCRIPTION,
355                CmsSearchField.FIELD_CONTENT});
356        search.setMatchesPerPage(5);
357        search.setDisplayPages(7);
358        search.setSearchRoot(
359            new StringBuffer("/system/workplace/locales/").append(m_jsp.getRequestContext().getLocale()).append(
360                "/help/").toString());
361
362        String query = request.getParameter("query");
363        search.setQuery(query);
364    }
365
366    /**
367     * Generates a html form (named form&lt;n&gt;) with parameters found in
368     * the given GET request string (appended params with "?value=param&value2=param2).
369     * &gt;n&lt; is the number of forms that already have been generated.
370     * This is a content-expensive bugfix for http://issues.apache.org/bugzilla/show_bug.cgi?id=35775
371     * and should be replaced with the 1.1 revision as soon that bug is fixed.<p>
372     *
373     * The forms action will point to the given uri's path info part: All links in the page
374     * that includes this generated html and that are related to the get request should
375     * have "src='#'" and "onclick=documents.forms['&lt;getRequestUri&gt;'].submit()". <p>
376     *
377     * The generated form lands in the internal <code>Map {@link #m_formCache}</code> as mapping
378     * from uris to Strings and has to be appended to the output at a valid position. <p>
379     *
380     * Warning: Does not work with multiple values mapped to one parameter ("key = value1,value2..").<p>
381     *
382     *
383     *
384     * @param getRequestUri a request uri with optional query, will be used as name of the form too.
385     */
386    private void toPostParameters(String getRequestUri) {
387
388        StringBuffer result;
389        if (!m_formCache.containsKey(getRequestUri)) {
390
391            result = new StringBuffer();
392            int index = getRequestUri.indexOf('?');
393            String query = "";
394            String path = "";
395            if (index > 0) {
396                query = getRequestUri.substring(index + 1);
397                path = getRequestUri.substring(0, index);
398                result.append("\n<form method=\"post\" name=\"form").append(m_formCache.size()).append("\" action=\"");
399                result.append(path).append("\">\n");
400
401                // "key=value" pairs as tokens:
402                StringTokenizer entryTokens = new StringTokenizer(query, "&", false);
403                while (entryTokens.hasMoreTokens()) {
404                    StringTokenizer keyValueToken = new StringTokenizer(entryTokens.nextToken(), "=", false);
405                    if (keyValueToken.countTokens() != 2) {
406                        continue;
407                    }
408                    // Undo the possible already performed url encoding for the given url
409                    String key = CmsEncoder.decode(keyValueToken.nextToken());
410                    String value = CmsEncoder.decode(keyValueToken.nextToken());
411                    result.append("  <input type=\"hidden\" name=\"");
412                    result.append(key).append("\" value=\"");
413                    result.append(value).append("\" />\n");
414                }
415                result.append("</form>\n");
416                m_formCache.put(getRequestUri, result.toString());
417            }
418        }
419    }
420}