Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2005 University of New Mexico and the 
4
 *             Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *   '$Author: costa $'
7
 *     '$Date: 2005-11-16 09:57:33 -0800 (Wed, 16 Nov 2005) $'
8
 * '$Revision: 2741 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.advancedsearch;
26

    
27
import java.io.IOException;
28
import javax.servlet.RequestDispatcher;
29
import javax.servlet.ServletContext;
30
import javax.servlet.ServletException;
31
import javax.servlet.http.HttpServlet;
32
import javax.servlet.http.HttpServletRequest;
33
import javax.servlet.http.HttpServletResponse;
34
import javax.servlet.http.HttpSession;
35

    
36
import edu.ucsb.nceas.metacat.client.*;
37

    
38
/** 
39
 * @author dcosta
40
 * 
41
 * The SearchServlet class executes a search action. This corresponds to when a
42
 * user fills in a simple search text box and clicks "Search".
43
 */
44
public class SearchServlet extends HttpServlet {
45

    
46
  // Instance Variables -- (Not used because they are not thread-safe.)
47

    
48
  // Methods
49
  
50
  /**
51
   * Executes a simple search.
52
   */
53
  public void doPost(HttpServletRequest request, HttpServletResponse response)
54
      throws IOException, ServletException {
55
    AdvancedSearch advancedSearch;
56
    RequestDispatcher dispatcher;
57
    HttpSession httpSession = request.getSession();
58
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
59
    String metacatURL;
60
    String result = "";
61
    final String resultsJSP = "style/skins/default/advancedsearchresults.jsp";
62
    String searchValue;
63
    ServletContext servletContext = httpSession.getServletContext();
64
    String stylesheet = (String) httpSession.getAttribute("stylesheet");
65
    String xslPath = servletContext.getRealPath("style/common/resultset.xsl");
66

    
67
    // First check whether the metacat URL was set as a context-param.
68
    metacatURL = servletContext.getInitParameter("metacatURL");
69

    
70
    // If no metacat URL was configured, then derive the metacat URL from the
71
    // server name and server port.
72
    if (metacatURL == null || metacatURL.equals("")) {
73
      MetacatHelper metacatHelper = new MetacatHelper();
74
      String serverName = request.getServerName();
75
      int serverPort = request.getServerPort();
76
      metacatURL = metacatHelper.constructMetacatURL(serverName, serverPort);
77
    }
78

    
79
    // Tell the web server that the response is HTML
80
    response.setContentType("text/html");
81

    
82
    // Fetch the searchValue parameter from the request and execute a search
83
    advancedSearch = new AdvancedSearch(null);
84
    searchValue = request.getParameter("searchValue");
85
    System.err.println("Search Servlet: " + searchValue);
86
    result = advancedSearch.executeSearch(metacatURL, metacat, xslPath, searchValue);
87

    
88
    // Store the result HTML string in the request for retrieval by
89
    // the metacatpathqueryresults.jsp form.
90
    request.setAttribute("result", result);
91

    
92
    dispatcher = request.getRequestDispatcher(resultsJSP);
93
    dispatcher.forward(request, response);
94
  }
95
  
96
}
(13-13/14)