1 | /** |
---|
2 | * This software is copyright (c) 2011 by |
---|
3 | * - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
4 | * This is free software. You can redistribute it |
---|
5 | * and/or modify it under the terms described in |
---|
6 | * the GNU General Public License v3 of which you |
---|
7 | * should have received a copy. Otherwise you can download |
---|
8 | * it from |
---|
9 | * |
---|
10 | * http://www.gnu.org/licenses/gpl-3.0.txt |
---|
11 | * |
---|
12 | * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
13 | * |
---|
14 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
15 | * GNU General Public License v3 |
---|
16 | */ |
---|
17 | package eu.clarin.sru.server.utils; |
---|
18 | |
---|
19 | import java.io.IOException; |
---|
20 | import java.lang.reflect.Constructor; |
---|
21 | import java.lang.reflect.InvocationTargetException; |
---|
22 | import java.net.MalformedURLException; |
---|
23 | import java.net.URL; |
---|
24 | import java.util.Enumeration; |
---|
25 | import java.util.HashMap; |
---|
26 | import java.util.Map; |
---|
27 | |
---|
28 | import javax.servlet.ServletConfig; |
---|
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 | |
---|
35 | import eu.clarin.sru.server.SRUConfigException; |
---|
36 | import eu.clarin.sru.server.SRUException; |
---|
37 | import eu.clarin.sru.server.SRUServer; |
---|
38 | import eu.clarin.sru.server.SRUServerConfig; |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * A Servlet implementation, which provides an environment for running a |
---|
43 | * {@link SRUServer} in a Servlet container. Your search engine <b>must</b> use |
---|
44 | * {@link SRUSearchEngineBase} as base class. |
---|
45 | * |
---|
46 | * <p> |
---|
47 | * Add the following to the web.xml of your web applications web.xml to define a |
---|
48 | * SRU server. Of course, the value of the Servlet parameter |
---|
49 | * "sruServerSerachEngineClass" must be adapted to match the name of your search |
---|
50 | * engine implementation. Furthermore, you can choose different url-pattern, to |
---|
51 | * match your needs. |
---|
52 | * </p> |
---|
53 | * |
---|
54 | * <pre> |
---|
55 | * <servlet> |
---|
56 | * <servlet-name>SRUServerServlet</servlet-name> |
---|
57 | * <servlet-class>eu.clarin.sru.server.utils.SRUServerServlet</servlet-class> |
---|
58 | * <init-param> |
---|
59 | * <param-name>sruServerSerachEngineClass</param-name> |
---|
60 | * <param-value>com.acme.MySearchEngine</param-value> |
---|
61 | * </init-param> |
---|
62 | * </servlet> |
---|
63 | * <servlet-mapping> |
---|
64 | * <servlet-name>SRUServerServlet</servlet-name> |
---|
65 | * <url-pattern>/sru</url-pattern> |
---|
66 | * </servlet-mapping> |
---|
67 | * </pre> |
---|
68 | */ |
---|
69 | public final class SRUServerServlet extends HttpServlet { |
---|
70 | /** |
---|
71 | * Servlet initialization parameter name for the location of the SRU server |
---|
72 | * configuration. |
---|
73 | */ |
---|
74 | public static final String SRU_SERVER_CONFIG_LOCATION_PARAM = |
---|
75 | "sruServerConfigLocation"; |
---|
76 | /** |
---|
77 | * Servlet initialization parameter name for the class that implements the |
---|
78 | * SRU search engine. |
---|
79 | */ |
---|
80 | public static final String SRU_SERVER_SERACH_ENGINE_CLASS_PARAM = |
---|
81 | "sruServerSerachEngineClass"; |
---|
82 | /** |
---|
83 | * Default value for the location of the SRU server configuration. |
---|
84 | */ |
---|
85 | public static final String SRU_SERVER_CONFIG_LOCATION_DEFAULT = |
---|
86 | "/WEB-INF/sru-server-config.xml"; |
---|
87 | private static final long serialVersionUID = 1L; |
---|
88 | private SRUServer sruServer; |
---|
89 | private SRUSearchEngineBase searchEngine; |
---|
90 | |
---|
91 | |
---|
92 | /** |
---|
93 | * Initialize the SRU server Servlet. |
---|
94 | * |
---|
95 | * @see javax.servlet.GenericServlet#init() |
---|
96 | */ |
---|
97 | @Override |
---|
98 | public void init() throws ServletException { |
---|
99 | final ServletConfig cfg = getServletConfig(); |
---|
100 | final ServletContext ctx = getServletContext(); |
---|
101 | |
---|
102 | String sruServerConfigLocation = |
---|
103 | cfg.getInitParameter(SRU_SERVER_CONFIG_LOCATION_PARAM); |
---|
104 | if (sruServerConfigLocation == null) { |
---|
105 | sruServerConfigLocation = SRU_SERVER_CONFIG_LOCATION_DEFAULT; |
---|
106 | } |
---|
107 | |
---|
108 | URL sruServerConfigFile; |
---|
109 | try { |
---|
110 | sruServerConfigFile = ctx.getResource(sruServerConfigLocation); |
---|
111 | } catch (MalformedURLException e) { |
---|
112 | throw new ServletException("init parameter '" + |
---|
113 | SRU_SERVER_CONFIG_LOCATION_PARAM + "' is not a valid URL", |
---|
114 | e); |
---|
115 | } |
---|
116 | if (sruServerConfigFile == null) { |
---|
117 | throw new ServletException("init parameter '" + |
---|
118 | SRU_SERVER_CONFIG_LOCATION_PARAM + |
---|
119 | "' points to non-existing resource (" + |
---|
120 | sruServerConfigLocation + ")"); |
---|
121 | } |
---|
122 | |
---|
123 | String sruServerSearchEngineClass = |
---|
124 | cfg.getInitParameter(SRU_SERVER_SERACH_ENGINE_CLASS_PARAM); |
---|
125 | if (sruServerSearchEngineClass == null) { |
---|
126 | throw new ServletException("init parameter '" + |
---|
127 | SRU_SERVER_SERACH_ENGINE_CLASS_PARAM + |
---|
128 | "' not defined in servlet configuration"); |
---|
129 | } |
---|
130 | |
---|
131 | /* |
---|
132 | * get init-parameters from ServletConfig ... |
---|
133 | */ |
---|
134 | final HashMap<String, String> params = new HashMap<String, String>(); |
---|
135 | for (Enumeration<?> i = cfg.getInitParameterNames(); |
---|
136 | i.hasMoreElements();) { |
---|
137 | String key = (String) i.nextElement(); |
---|
138 | String value = cfg.getInitParameter(key); |
---|
139 | if ((value != null) && !value.isEmpty()) { |
---|
140 | params.put(key, value); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /* |
---|
145 | * ... and get more init-parameters from ServletContext and potentially |
---|
146 | * overriding parameters from servlet configuration. |
---|
147 | */ |
---|
148 | for (Enumeration<?> i = ctx.getInitParameterNames(); |
---|
149 | i.hasMoreElements();) { |
---|
150 | String key = (String) i.nextElement(); |
---|
151 | String value = ctx.getInitParameter(key); |
---|
152 | if ((value != null) && !value.isEmpty()) { |
---|
153 | params.put(key, value); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * Set some defaults (aka "plug and play" for development deployment) |
---|
159 | * Override those for a production deployment through your Servlet |
---|
160 | * container's context configuration! |
---|
161 | */ |
---|
162 | if (!params.containsKey(SRUServerConfig.SRU_TRANSPORT)) { |
---|
163 | setDefaultConfigParam(params, SRUServerConfig.SRU_TRANSPORT, |
---|
164 | "http"); |
---|
165 | } |
---|
166 | if (!params.containsKey(SRUServerConfig.SRU_HOST)) { |
---|
167 | setDefaultConfigParam(params, SRUServerConfig.SRU_HOST, |
---|
168 | "127.0.0.1"); |
---|
169 | } |
---|
170 | if (!params.containsKey(SRUServerConfig.SRU_PORT)) { |
---|
171 | setDefaultConfigParam(params, SRUServerConfig.SRU_PORT, |
---|
172 | "8080"); |
---|
173 | } |
---|
174 | if (!params.containsKey(SRUServerConfig.SRU_DATABASE)) { |
---|
175 | String contextPath; |
---|
176 | try { |
---|
177 | /* |
---|
178 | * this only works with Servlet 2.5 API ... |
---|
179 | */ |
---|
180 | contextPath = ctx.getContextPath(); |
---|
181 | } catch (NoSuchMethodError e) { |
---|
182 | /* |
---|
183 | * if we fail, put at least something here ... |
---|
184 | */ |
---|
185 | contextPath = "/"; |
---|
186 | log("NOTE: auto-configuration for parameter '" + |
---|
187 | SRUServerConfig.SRU_DATABASE + |
---|
188 | "' failed and will contain only a dummy value!"); |
---|
189 | } |
---|
190 | setDefaultConfigParam(params, SRUServerConfig.SRU_DATABASE, |
---|
191 | contextPath); |
---|
192 | } |
---|
193 | |
---|
194 | // parse configuration |
---|
195 | SRUServerConfig sruServerConfig; |
---|
196 | try { |
---|
197 | sruServerConfig = |
---|
198 | SRUServerConfig.parse(params, sruServerConfigFile); |
---|
199 | } catch (SRUConfigException e) { |
---|
200 | throw new ServletException("sru server configuration is invalid", e); |
---|
201 | } |
---|
202 | |
---|
203 | /* |
---|
204 | * create an instance of the search engine ... |
---|
205 | */ |
---|
206 | try { |
---|
207 | @SuppressWarnings("unchecked") |
---|
208 | Class<SRUSearchEngineBase> clazz = (Class<SRUSearchEngineBase>) |
---|
209 | Class.forName(sruServerSearchEngineClass); |
---|
210 | Constructor<SRUSearchEngineBase> constructor = |
---|
211 | clazz.getConstructor(); |
---|
212 | searchEngine = constructor.newInstance(); |
---|
213 | } catch (ClassNotFoundException e) { |
---|
214 | throw new ServletException("error inisializing sru server", e); |
---|
215 | } catch (ClassCastException e) { |
---|
216 | throw new ServletException("error inisializing sru server", e); |
---|
217 | } catch (NoSuchMethodException e) { |
---|
218 | throw new ServletException("error inisializing sru server", e); |
---|
219 | } catch (SecurityException e) { |
---|
220 | throw new ServletException("error inisializing sru server", e); |
---|
221 | } catch (InstantiationException e) { |
---|
222 | throw new ServletException("error inisializing sru server", e); |
---|
223 | } catch (IllegalAccessException e) { |
---|
224 | throw new ServletException("error inisializing sru server", e); |
---|
225 | } catch (IllegalArgumentException e) { |
---|
226 | throw new ServletException("error inisializing sru server", e); |
---|
227 | } catch (InvocationTargetException e) { |
---|
228 | throw new ServletException("error inisializing sru server", e); |
---|
229 | } |
---|
230 | |
---|
231 | /* |
---|
232 | * finally initialize the SRU server ... |
---|
233 | */ |
---|
234 | try { |
---|
235 | searchEngine.init(sruServerConfig, params); |
---|
236 | sruServer = new SRUServer(sruServerConfig, searchEngine); |
---|
237 | } catch (SRUConfigException e) { |
---|
238 | throw new ServletException("error inisializing sru server", e); |
---|
239 | } catch (SRUException e) { |
---|
240 | throw new ServletException("error inisializing sru server", e); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | /** |
---|
246 | * Destroy the SRU server Servlet. |
---|
247 | * |
---|
248 | * @see javax.servlet.GenericServlet#destroy() |
---|
249 | */ |
---|
250 | @Override |
---|
251 | public void destroy() { |
---|
252 | if (searchEngine != null) { |
---|
253 | searchEngine.destroy(); |
---|
254 | } |
---|
255 | super.destroy(); |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | /** |
---|
260 | * Handle a HTTP get request. |
---|
261 | * |
---|
262 | * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, |
---|
263 | * javax.servlet.http.HttpServletResponse) |
---|
264 | */ |
---|
265 | @Override |
---|
266 | protected void doGet(HttpServletRequest request, |
---|
267 | HttpServletResponse response) throws ServletException, IOException { |
---|
268 | if (sruServer == null) { |
---|
269 | throw new ServletException("servlet is not properly initalized"); |
---|
270 | } |
---|
271 | sruServer.handleRequest(request, response); |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | /** |
---|
276 | * Handle a HTTP post request. |
---|
277 | * |
---|
278 | * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, |
---|
279 | * javax.servlet.http.HttpServletResponse) |
---|
280 | */ |
---|
281 | @Override |
---|
282 | protected void doPost(HttpServletRequest request, |
---|
283 | HttpServletResponse response) throws ServletException, IOException { |
---|
284 | if (sruServer == null) { |
---|
285 | throw new ServletException("servlet is not properly initalized"); |
---|
286 | } |
---|
287 | sruServer.handleRequest(request, response); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | private void setDefaultConfigParam(Map<String, String> params, String key, |
---|
292 | String value) { |
---|
293 | if (!params.containsKey(key)) { |
---|
294 | params.put(key, value); |
---|
295 | log("NOTE: using default '" + value + "' for parameter '" + key + |
---|
296 | "', because it was not defined in context configuration."); |
---|
297 | log("THIS IS NOT RECOMMENDED FOR PRODUCTION DEPLOYMENT!"); |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | } // class SRUServerServlet |
---|