001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.servlets;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.KernelServiceException;
039: import org.libresource.kernel.interfaces.KernelService;
040:
041: import org.libresource.search.LibresourceSearchResult;
042:
043: import org.libresource.web.SearchResult;
044: import org.libresource.web.config.Config;
045:
046: import java.util.Vector;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050:
051: public class Search extends BaseServlet {
052: protected void process(HttpServletRequest request,
053: HttpServletResponse response) throws Exception {
054: KernelService kernelService = (KernelService) Libresource
055: .getService(KernelConstants.SERVICE);
056:
057: request.setAttribute("search", Boolean.TRUE);
058: request.setAttribute("types", Config.getInstance(
059: getServletContext()).listResourceTypeForSearchFilter());
060:
061: String type = request.getParameter("resourceType");
062: request.setAttribute("resourceType", type);
063:
064: if (request.getParameter("query") == null) {
065: request.setAttribute("uriPattern", "/*");
066: request.getRequestDispatcher("/pages/search.jsp").forward(
067: request, response);
068:
069: return;
070: }
071:
072: request.setAttribute("uriPattern", request
073: .getParameter("uriPattern"));
074:
075: // do not search empty string
076: if (request.getParameter("query").trim().length() == 0) {
077: request.setAttribute("error_search",
078: "Cannot search empty string");
079: request.getRequestDispatcher("/pages/search.jsp").forward(
080: request, response);
081:
082: return;
083: }
084:
085: String query = request.getParameter("query").replaceAll("<",
086: "<");
087: query = query.replaceAll(">", ">");
088: request.setAttribute("query", query);
089:
090: if (type.toLowerCase().equals("all")) {
091: type = null;
092: }
093:
094: LibresourceSearchResult[] results = null;
095:
096: try {
097: results = kernelService.search(request
098: .getParameter("uriPattern"), null, type, request
099: .getParameter("query"));
100: } catch (KernelServiceException e) {
101: request.setAttribute("error_search", "Invalid query");
102: }
103:
104: Vector searchResults = null;
105:
106: if (results != null) {
107: searchResults = new Vector();
108:
109: for (int i = 0; i < results.length; i++) {
110: SearchResult result = new SearchResult(results[i]
111: .getScore(), results[i].getUri(), results[i]
112: .getName(), results[i].getHighlightedFragment(
113: "<span class=\"highlighted\">", "</span>"));
114: searchResults.add(result);
115: }
116: }
117:
118: request.setAttribute("results", searchResults);
119:
120: request.getRequestDispatcher("/pages/search.jsp").forward(
121: request, response);
122: }
123: }
|