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: daigle $'
7
 *     '$Date: 2008-12-26 13:17:22 -0800 (Fri, 26 Dec 2008) $'
8
 * '$Revision: 4708 $'
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.File;
28
import java.io.IOException;
29
import javax.servlet.RequestDispatcher;
30
import javax.servlet.ServletConfig;
31
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
import edu.ucsb.nceas.metacat.service.PropertyService;
40
import edu.ucsb.nceas.metacat.service.ServiceException;
41
import edu.ucsb.nceas.metacat.util.SystemUtil;
42
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
43

    
44
/** 
45
 * @author dcosta
46
 * 
47
 * The SearchServlet class executes a search action. This corresponds to when a
48
 * user fills in a simple search text box and clicks "Search".
49
 */
50
public class SearchServlet extends HttpServlet {
51

    
52
  /*
53
   * Class fields
54
   */
55
  private static final String CONFIG_DIR = "WEB-INF";
56
  private static final String CONFIG_NAME = "metacat.properties";
57
  static final long serialVersionUID = 0;  // Needed for Eclipse warning.
58
			   
59
  // Instance Variables
60
  private String contextString = "";
61

    
62
  // Methods
63
  
64
  /**
65
   * Executes a simple search.
66
   */
67
  public void doPost(HttpServletRequest request, HttpServletResponse response)
68
      throws IOException, ServletException {
69
    AdvancedSearch advancedSearch;
70
    RequestDispatcher dispatcher;
71
    HttpSession httpSession = request.getSession();
72
    Metacat metacat = (Metacat) httpSession.getAttribute("metacat");
73
    MetacatHelper metacatHelper = new MetacatHelper();
74
    String metacatURL;
75
    String qformat = (String) httpSession.getAttribute("qformat");
76
    String result = "";
77
    final String resultsJSP = metacatHelper.getResultsJSP();
78
    String searchValue;
79
    ServletContext servletContext = httpSession.getServletContext();
80
    String xslPath = metacatHelper.getResultsetXSL(request);
81

    
82
    // First check whether the metacat URL was set as a context-param.
83
    metacatURL = servletContext.getInitParameter("metacatURL");
84

    
85
    // If no metacat URL was configured, then derive the metacat URL from the
86
    // server name and server port.
87
    if (metacatURL == null || metacatURL.equals("")) {
88
      String serverName = request.getServerName();
89
      int serverPort = request.getServerPort();
90
      metacatURL = 
91
       metacatHelper.constructMetacatURL(serverName, serverPort, contextString);
92
    }
93

    
94
    // Tell the web server that the response is HTML
95
    response.setContentType("text/html");
96

    
97
    // Fetch the searchValue parameter from the request and execute a search
98
    advancedSearch = new AdvancedSearch(null);
99
    searchValue = request.getParameter("searchValue");
100
    System.err.println("Search Servlet: " + searchValue);
101
    result = advancedSearch.executeSearch(metacatURL, metacat, qformat, 
102
                                          xslPath, searchValue);
103

    
104
    // Store the result HTML string in the request for retrieval by
105
    // the metacatpathqueryresults.jsp form.
106
    request.setAttribute("result", result);
107

    
108
    dispatcher = request.getRequestDispatcher(resultsJSP);
109
    dispatcher.forward(request, response);
110
  }
111
  
112

    
113
  /**
114
	 * Initializes the servlet. Reads properties and initializes object fields.
115
	 * 
116
	 * @throws ServletException
117
	 */
118
	public void init(ServletConfig config) throws ServletException {
119
		ServletContext context = null;
120
		String dirPath;
121

    
122
		super.init(config);
123
		context = config.getServletContext();
124
		dirPath = context.getRealPath(CONFIG_DIR);
125

    
126
		try {
127
			PropertyService.getInstance();
128
			this.contextString = PropertyService.getProperty("application.context");
129
		} catch (ServiceException se) {
130
			System.err.println("Error in loading properties: " + se.getMessage());
131
		} catch (PropertyNotFoundException pnfe) {
132
			System.err.println("couldn't read property during initialization: "
133
					+ pnfe.getMessage());
134
		}
135
	}
136
}
(13-13/14)