01: /**
02: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
03: *
04: * This program is free software; you can redistribute it and/or modify
05: * it under the terms of the latest version of the GNU Lesser General
06: * Public License as published by the Free Software Foundation;
07: *
08: * This program is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: * GNU Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public License
14: * along with this program (LICENSE.txt); if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16: */package org.jamwiki.servlets;
17:
18: import java.util.Collection;
19: import javax.servlet.http.HttpServletRequest;
20: import javax.servlet.http.HttpServletResponse;
21: import org.apache.commons.lang.StringUtils;
22: import org.jamwiki.WikiBase;
23: import org.jamwiki.WikiMessage;
24: import org.jamwiki.utils.LinkUtil;
25: import org.jamwiki.utils.WikiLogger;
26: import org.jamwiki.utils.WikiUtil;
27: import org.springframework.web.servlet.ModelAndView;
28:
29: /**
30: * Used to display search results.
31: *
32: * @see org.jamwiki.search.SearchEngine
33: */
34: public class SearchServlet extends JAMWikiServlet {
35:
36: private static final WikiLogger logger = WikiLogger
37: .getLogger(SearchServlet.class.getName());
38: protected static final String JSP_SEARCH = "search.jsp";
39: protected static final String JSP_SEARCH_RESULTS = "search-results.jsp";
40:
41: /**
42: *
43: */
44: protected ModelAndView handleJAMWikiRequest(
45: HttpServletRequest request, HttpServletResponse response,
46: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
47: if (request.getParameter("jumpto") == null) {
48: search(request, next, pageInfo);
49: } else {
50: jumpTo(request, next, pageInfo);
51: }
52: return next;
53: }
54:
55: /**
56: *
57: */
58: private void jumpTo(HttpServletRequest request, ModelAndView next,
59: WikiPageInfo pageInfo) throws Exception {
60: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
61: String topic = request.getParameter("text");
62: if (LinkUtil.isExistingArticle(virtualWiki, topic)) {
63: ServletUtil.redirect(next, virtualWiki, topic);
64: } else {
65: next.addObject("notopic", topic);
66: this .search(request, next, pageInfo);
67: }
68: }
69:
70: /**
71: *
72: */
73: private void search(HttpServletRequest request, ModelAndView next,
74: WikiPageInfo pageInfo) throws Exception {
75: String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
76: String searchField = request.getParameter("text");
77: if (request.getParameter("text") == null) {
78: pageInfo.setPageTitle(new WikiMessage("search.title"));
79: } else {
80: pageInfo.setPageTitle(new WikiMessage("searchresult.title",
81: searchField));
82: }
83: // forward back to the search page if the request is blank or null
84: if (StringUtils.isBlank(searchField)) {
85: pageInfo.setContentJsp(JSP_SEARCH);
86: pageInfo.setSpecial(true);
87: return;
88: }
89: // grab search engine instance and find
90: Collection results = WikiBase.getSearchEngine().findResults(
91: virtualWiki, searchField);
92: next.addObject("searchField", searchField);
93: next.addObject("results", results);
94: pageInfo.setContentJsp(JSP_SEARCH_RESULTS);
95: pageInfo.setSpecial(true);
96: }
97: }
|