Project

General

Profile

1 2741 costa
/**
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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 2822 costa
import java.io.File;
28 2741 costa
import java.io.IOException;
29
import javax.servlet.RequestDispatcher;
30 2822 costa
import javax.servlet.ServletConfig;
31 2741 costa
import javax.servlet.ServletContext;
32
import javax.servlet.ServletException;
33
import javax.servlet.http.HttpServlet;
34
import javax.servlet.http.HttpServletRequest;
35
import javax.servlet.http.HttpServletResponse;
36
import javax.servlet.http.HttpSession;
37
38
import edu.ucsb.nceas.metacat.client.*;
39 2822 costa
import edu.ucsb.nceas.utilities.Options;
40 2741 costa
41
/**
42
 * @author dcosta
43
 *
44
 * The SearchServlet class executes a search action. This corresponds to when a
45
 * user fills in a simple search text box and clicks "Search".
46
 */
47
public class SearchServlet extends HttpServlet {
48
49 2822 costa
  /*
50
   * Class fields
51
   */
52
  private static final String CONFIG_DIR = "WEB-INF";
53
  private static final String CONFIG_NAME = "metacat.properties";
54 3011 costa
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
55 2822 costa
56
  // Instance Variables
57
  private String contextString = "";
58 2741 costa
59
  // Methods
60
61
  /**
62
   * Executes a simple search.
63
   */
64
  public void doPost(HttpServletRequest request, HttpServletResponse response)
65
      throws IOException, ServletException {
66
    AdvancedSearch advancedSearch;
67
    RequestDispatcher dispatcher;
68
    HttpSession httpSession = request.getSession();
69
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
70 3011 costa
    MetacatHelper metacatHelper = new MetacatHelper();
71 2741 costa
    String metacatURL;
72 3011 costa
    String qformat = (String) httpSession.getAttribute("qformat");
73 2741 costa
    String result = "";
74 3011 costa
    final String resultsJSP = metacatHelper.getResultsJSP();
75 2741 costa
    String searchValue;
76
    ServletContext servletContext = httpSession.getServletContext();
77 3011 costa
    String xslPath = metacatHelper.getResultsetXSL(request);
78 2741 costa
79
    // First check whether the metacat URL was set as a context-param.
80
    metacatURL = servletContext.getInitParameter("metacatURL");
81
82
    // If no metacat URL was configured, then derive the metacat URL from the
83
    // server name and server port.
84
    if (metacatURL == null || metacatURL.equals("")) {
85
      String serverName = request.getServerName();
86
      int serverPort = request.getServerPort();
87 2822 costa
      metacatURL =
88 3011 costa
       metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
89 2741 costa
    }
90
91
    // Tell the web server that the response is HTML
92
    response.setContentType("text/html");
93
94
    // Fetch the searchValue parameter from the request and execute a search
95
    advancedSearch = new AdvancedSearch(null);
96
    searchValue = request.getParameter("searchValue");
97
    System.err.println("Search Servlet: " + searchValue);
98 3011 costa
    result = advancedSearch.executeSearch(metacatURL, metacat, qformat,
99
                                          xslPath, searchValue);
100 2741 costa
101
    // Store the result HTML string in the request for retrieval by
102
    // the metacatpathqueryresults.jsp form.
103
    request.setAttribute("result", result);
104
105
    dispatcher = request.getRequestDispatcher(resultsJSP);
106
    dispatcher.forward(request, response);
107
  }
108
109 2822 costa
110
  /**
111
   * Initializes the servlet. Reads properties and initializes object fields.
112
   *
113
   * @throws ServletException
114
   */
115
  public void init(ServletConfig config) throws ServletException {
116
	ServletContext context = null;
117
    String dirPath;
118
    Options options = null;
119
120
    super.init(config);
121
    context = config.getServletContext();
122
    dirPath = context.getRealPath(CONFIG_DIR);
123
    File propertyFile = new File(dirPath, CONFIG_NAME);
124
125
    try {
126
      options = Options.initialize(propertyFile);
127
    }
128
    catch (IOException e) {
129
      System.err.println("Error in loading options: " + e.getMessage());
130
    }
131
132
    this.contextString = options.getOption("context");
133
  }
134
135 2741 costa
}