1 | package org.greenstone.gsdl3;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.comms.*;
|
---|
4 | import org.greenstone.gsdl3.core.*;
|
---|
5 | import org.greenstone.gsdl3.util.*;
|
---|
6 | import org.greenstone.gsdl3.action.PageAction; // used to get the default action
|
---|
7 | import org.greenstone.util.GlobalProperties;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 | import org.w3c.dom.Node;
|
---|
11 | import org.w3c.dom.NodeList;
|
---|
12 | import java.io.*;
|
---|
13 | import javax.servlet.*;
|
---|
14 | import javax.servlet.http.*;
|
---|
15 | import java.util.Enumeration;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.Iterator;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 | import java.util.Set;
|
---|
22 | import java.io.File;
|
---|
23 | import java.lang.reflect.Type;
|
---|
24 | import java.util.Hashtable;
|
---|
25 | import org.apache.log4j.*;
|
---|
26 |
|
---|
27 | import com.google.gson.Gson;
|
---|
28 | import com.google.gson.reflect.TypeToken;
|
---|
29 |
|
---|
30 | // Apache Commons
|
---|
31 | import org.apache.commons.fileupload.FileItem;
|
---|
32 | import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
---|
33 | import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
---|
34 | import org.apache.commons.lang3.*;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * a servlet to serve the greenstone library - we are using servlets instead of
|
---|
38 | * cgi the init method is called only once - the first time the servlet classes
|
---|
39 | * are loaded. Each time a request comes in to the servlet, the session() method
|
---|
40 | * is called in a new thread (calls doGet/doPut etc) takes the a=p&p=home type
|
---|
41 | * args and builds a simple request to send to its receptionist, which returns a
|
---|
42 | * result in html, cos output=html is set in the request
|
---|
43 | *
|
---|
44 | * 18/Jul/07 xiao modify to make the cached parameters collection-specific. Most
|
---|
45 | * of the work is done in doGet(), except adding an inner class
|
---|
46 | * UserSessionCache.
|
---|
47 | *
|
---|
48 | * @see Receptionist
|
---|
49 | */
|
---|
50 | public class LibraryServlet extends HttpServlet
|
---|
51 | {
|
---|
52 |
|
---|
53 | /** the receptionist to send messages to */
|
---|
54 | protected Receptionist recept = null;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * the default language - is specified by setting a servlet param, otherwise
|
---|
58 | * DEFAULT_LANG is used
|
---|
59 | */
|
---|
60 | protected String default_lang = null;
|
---|
61 |
|
---|
62 | /** Whether or not client-side XSLT support should be exposed */
|
---|
63 | protected boolean supports_client_xslt = false;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * The default default - used if a default lang is not specified in the
|
---|
67 | * servlet params
|
---|
68 | */
|
---|
69 | protected final String DEFAULT_LANG = "en";
|
---|
70 |
|
---|
71 | /** container Document to create XML Nodes */
|
---|
72 | protected Document doc = null;
|
---|
73 |
|
---|
74 | /** a converter class to parse XML and create Docs */
|
---|
75 | protected XMLConverter converter = null;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * the cgi stuff - the Receptionist can add new args to this
|
---|
79 | *
|
---|
80 | * its used by the servlet to determine what args to save
|
---|
81 | */
|
---|
82 | protected GSParams params = null;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * user id - new one per session. This doesn't work if session state is
|
---|
86 | * saved between restarts - this requires this value to be saved too.
|
---|
87 | */
|
---|
88 | protected int next_user_id = 0;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * a hash that contains all the active session IDs mapped to the cached
|
---|
92 | * items It is updated whenever the whole site or a particular collection is
|
---|
93 | * reconfigured using the command a=s&sa=c or a=s&sa=c&c=xxx It is in the
|
---|
94 | * form: sid -> (UserSessionCache object)
|
---|
95 | */
|
---|
96 | protected Hashtable session_ids_table = new Hashtable();
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * the maximum interval that the cached info remains in session_ids_table
|
---|
100 | * (in seconds) This is set in web.xml
|
---|
101 | */
|
---|
102 | protected int session_expiration = 1800;
|
---|
103 |
|
---|
104 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.LibraryServlet.class.getName());
|
---|
105 |
|
---|
106 | /** initialise the servlet */
|
---|
107 | public void init(ServletConfig config) throws ServletException
|
---|
108 | {
|
---|
109 | // always call super.init;
|
---|
110 | super.init(config);
|
---|
111 | // disable preferences - does this work anyway??
|
---|
112 | //System.setProperty("java.util.prefs.PreferencesFactory", "org.greenstone.gsdl3.util.DisabledPreferencesFactory");
|
---|
113 |
|
---|
114 | String library_name = config.getInitParameter(GSConstants.LIBRARY_NAME);
|
---|
115 | String gsdl3_home = config.getInitParameter(GSConstants.GSDL3_HOME);
|
---|
116 | String interface_name = config.getInitParameter(GSConstants.INTERFACE_NAME);
|
---|
117 |
|
---|
118 | String allowXslt = (String) config.getInitParameter(GSConstants.ALLOW_CLIENT_SIDE_XSLT);
|
---|
119 | supports_client_xslt = allowXslt != null && allowXslt.equals("true");
|
---|
120 |
|
---|
121 | this.default_lang = config.getInitParameter(GSConstants.DEFAULT_LANG);
|
---|
122 | String sess_expire = config.getInitParameter(GSXML.SESSION_EXPIRATION);
|
---|
123 |
|
---|
124 | if (sess_expire != null && !sess_expire.equals(""))
|
---|
125 | {
|
---|
126 | this.session_expiration = Integer.parseInt(sess_expire);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (library_name == null || interface_name == null)
|
---|
130 | {
|
---|
131 | // must have this
|
---|
132 | System.err.println("initialisation parameters not all set!");
|
---|
133 | System.err.println(" you must have libraryname and interfacename");
|
---|
134 | System.exit(1);
|
---|
135 | }
|
---|
136 |
|
---|
137 | String site_name = config.getInitParameter(GSConstants.SITE_NAME);
|
---|
138 | String remote_site_name = null;
|
---|
139 | String remote_site_type = null;
|
---|
140 | String remote_site_address = null;
|
---|
141 |
|
---|
142 | if (site_name == null)
|
---|
143 | {
|
---|
144 | // no site, try for communicator
|
---|
145 | remote_site_name = config.getInitParameter("remote_site_name");
|
---|
146 | remote_site_type = config.getInitParameter("remote_site_type");
|
---|
147 | remote_site_address = config.getInitParameter("remote_site_address");
|
---|
148 | if (remote_site_name == null || remote_site_type == null || remote_site_address == null)
|
---|
149 | {
|
---|
150 | System.err.println("initialisation paramters not all set!");
|
---|
151 | System.err.println("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
|
---|
152 | System.exit(1);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (this.default_lang == null)
|
---|
157 | {
|
---|
158 | // choose english
|
---|
159 | this.default_lang = DEFAULT_LANG;
|
---|
160 | }
|
---|
161 |
|
---|
162 | HashMap config_params = new HashMap();
|
---|
163 |
|
---|
164 | config_params.put(GSConstants.LIBRARY_NAME, library_name);
|
---|
165 | config_params.put(GSConstants.INTERFACE_NAME, interface_name);
|
---|
166 | config_params.put(GSConstants.ALLOW_CLIENT_SIDE_XSLT, supports_client_xslt);
|
---|
167 |
|
---|
168 | if (site_name != null)
|
---|
169 | {
|
---|
170 | config_params.put(GSConstants.SITE_NAME, site_name);
|
---|
171 | }
|
---|
172 | this.converter = new XMLConverter();
|
---|
173 | this.doc = this.converter.newDOM();
|
---|
174 |
|
---|
175 | // the receptionist -the servlet will talk to this
|
---|
176 | String recept_name = (String) config.getInitParameter("receptionist_class");
|
---|
177 | if (recept_name == null)
|
---|
178 | {
|
---|
179 | this.recept = new DefaultReceptionist();
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | try
|
---|
184 | {
|
---|
185 | this.recept = (Receptionist) Class.forName("org.greenstone.gsdl3.core." + recept_name).newInstance();
|
---|
186 | }
|
---|
187 | catch (Exception e)
|
---|
188 | { // cant use this new one, so use normal one
|
---|
189 | System.err.println("LibraryServlet configure exception when trying to use a new Receptionist " + recept_name + ": " + e.getMessage());
|
---|
190 | e.printStackTrace();
|
---|
191 | this.recept = new DefaultReceptionist();
|
---|
192 | }
|
---|
193 | }
|
---|
194 | this.recept.setConfigParams(config_params);
|
---|
195 |
|
---|
196 | // the receptionist uses a MessageRouter or Communicator to send its requests to. We either create a MessageRouter here for the designated site (if site_name set), or we create a Communicator for a remote site. The is given to teh Receptionist, and the servlet never talks to it again.directly.
|
---|
197 | if (site_name != null)
|
---|
198 | {
|
---|
199 | String mr_name = (String) config.getInitParameter("messagerouter_class");
|
---|
200 | MessageRouter message_router = null;
|
---|
201 | if (mr_name == null)
|
---|
202 | { // just use the normal MR
|
---|
203 | message_router = new MessageRouter();
|
---|
204 | }
|
---|
205 | else
|
---|
206 | { // try the specified one
|
---|
207 | try
|
---|
208 | {
|
---|
209 | message_router = (MessageRouter) Class.forName("org.greenstone.gsdl3.core." + mr_name).newInstance();
|
---|
210 | }
|
---|
211 | catch (Exception e)
|
---|
212 | { // cant use this new one, so use normal one
|
---|
213 | System.err.println("LibraryServlet configure exception when trying to use a new MessageRouter " + mr_name + ": " + e.getMessage());
|
---|
214 | e.printStackTrace();
|
---|
215 | message_router = new MessageRouter();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | message_router.setSiteName(site_name);
|
---|
220 | message_router.setLibraryName(library_name);
|
---|
221 | message_router.configure();
|
---|
222 | this.recept.setMessageRouter(message_router);
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | // talking to a remote site, create a communicator
|
---|
227 | Communicator communicator = null;
|
---|
228 | // we need to create the XML to configure the communicator
|
---|
229 | Element site_elem = this.doc.createElement(GSXML.SITE_ELEM);
|
---|
230 | site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
|
---|
231 | site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
|
---|
232 | site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
|
---|
233 |
|
---|
234 | if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA))
|
---|
235 | {
|
---|
236 | communicator = new SOAPCommunicator();
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | System.err.println("LibraryServlet.init Error: invalid Communicator type: " + remote_site_type);
|
---|
241 | System.exit(1);
|
---|
242 | }
|
---|
243 |
|
---|
244 | if (!communicator.configure(site_elem))
|
---|
245 | {
|
---|
246 | System.err.println("LibraryServlet.init Error: Couldn't configure communicator");
|
---|
247 | System.exit(1);
|
---|
248 | }
|
---|
249 | this.recept.setMessageRouter(communicator);
|
---|
250 | }
|
---|
251 |
|
---|
252 | // the params arg thingy
|
---|
253 |
|
---|
254 | String params_name = (String) config.getInitParameter("params_class");
|
---|
255 | if (params_name == null)
|
---|
256 | {
|
---|
257 | this.params = new GSParams();
|
---|
258 | }
|
---|
259 | else
|
---|
260 | {
|
---|
261 | try
|
---|
262 | {
|
---|
263 | this.params = (GSParams) Class.forName("org.greenstone.gsdl3.util." + params_name).newInstance();
|
---|
264 | }
|
---|
265 | catch (Exception e)
|
---|
266 | {
|
---|
267 | System.err.println("LibraryServlet configure exception when trying to use a new params thing " + params_name + ": " + e.getMessage());
|
---|
268 | e.printStackTrace();
|
---|
269 | this.params = new GSParams();
|
---|
270 | }
|
---|
271 | }
|
---|
272 | // pass it to the receptionist
|
---|
273 | this.recept.setParams(this.params);
|
---|
274 | this.recept.configure();
|
---|
275 |
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void logUsageInfo(HttpServletRequest request)
|
---|
279 | {
|
---|
280 | String usageInfo = "";
|
---|
281 |
|
---|
282 | //session-info: get params stored in the session
|
---|
283 | HttpSession session = request.getSession(true);
|
---|
284 | Enumeration attributeNames = session.getAttributeNames();
|
---|
285 | while (attributeNames.hasMoreElements())
|
---|
286 | {
|
---|
287 | String name = (String) attributeNames.nextElement();
|
---|
288 | usageInfo += name + "=" + session.getAttribute(name) + " ";
|
---|
289 | }
|
---|
290 |
|
---|
291 | //logged info = general-info + session-info
|
---|
292 | usageInfo = request.getServletPath() + " " + //serlvet
|
---|
293 | "[" + request.getQueryString() + "]" + " " + //the query string
|
---|
294 | "[" + usageInfo.trim() + "]" + " " + // params stored in a session
|
---|
295 | request.getRemoteAddr() + " " + //remote address
|
---|
296 | request.getRequestedSessionId() + " " + //session id
|
---|
297 | request.getHeader("user-agent") + " "; //the remote brower info
|
---|
298 |
|
---|
299 | logger.info(usageInfo);
|
---|
300 |
|
---|
301 | }
|
---|
302 |
|
---|
303 | public class UserSessionCache implements HttpSessionBindingListener
|
---|
304 | {
|
---|
305 |
|
---|
306 | String session_id = "";
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * a hash that maps the session ID to a hashtable that maps the
|
---|
310 | * coll_name to its parameters coll_name -> Hashtable (param_name ->
|
---|
311 | * param_value)
|
---|
312 | */
|
---|
313 | protected Hashtable coll_name_params_table = null;
|
---|
314 |
|
---|
315 | public UserSessionCache(String id, Hashtable table)
|
---|
316 | {
|
---|
317 | session_id = id;
|
---|
318 | coll_name_params_table = (table == null) ? new Hashtable() : table;
|
---|
319 | }
|
---|
320 |
|
---|
321 | protected void cleanupCache(String coll_name)
|
---|
322 | {
|
---|
323 | if (coll_name_params_table.containsKey(coll_name))
|
---|
324 | {
|
---|
325 | coll_name_params_table.remove(coll_name);
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | protected Hashtable getParamsTable()
|
---|
330 | {
|
---|
331 | return coll_name_params_table;
|
---|
332 | }
|
---|
333 |
|
---|
334 | public void valueBound(HttpSessionBindingEvent event)
|
---|
335 | {
|
---|
336 | // Do nothing
|
---|
337 | }
|
---|
338 |
|
---|
339 | public void valueUnbound(HttpSessionBindingEvent event)
|
---|
340 | {
|
---|
341 | if (session_ids_table.containsKey(session_id))
|
---|
342 | {
|
---|
343 | session_ids_table.remove(session_id);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | public int tableSize()
|
---|
348 | {
|
---|
349 | return (coll_name_params_table == null) ? 0 : coll_name_params_table.size();
|
---|
350 | }
|
---|
351 |
|
---|
352 | }
|
---|
353 |
|
---|
354 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
355 | {
|
---|
356 | logUsageInfo(request);
|
---|
357 |
|
---|
358 | String query_string;
|
---|
359 | if (request.getMethod().equals("GET"))
|
---|
360 | {
|
---|
361 | query_string = request.getQueryString();
|
---|
362 | }
|
---|
363 | else if (request.getMethod().equals("POST"))
|
---|
364 | {
|
---|
365 | query_string = "";
|
---|
366 | Map paramMap = request.getParameterMap();
|
---|
367 | Iterator keyIter = paramMap.keySet().iterator();
|
---|
368 |
|
---|
369 | while (keyIter.hasNext())
|
---|
370 | {
|
---|
371 | String current = (String) keyIter.next();
|
---|
372 | query_string += current + "=" + ((String[]) paramMap.get(current))[0];
|
---|
373 | if (keyIter.hasNext())
|
---|
374 | {
|
---|
375 | query_string += "&";
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
|
---|
380 |
|
---|
381 | int sizeLimit = System.getProperties().containsKey("servlet.upload.filesize.limit") ? Integer.parseInt(System.getProperty("servlet.upload.filesize.limit")) : 20 * 1024 * 1024;
|
---|
382 |
|
---|
383 | fileItemFactory.setSizeThreshold(sizeLimit);
|
---|
384 | fileItemFactory.setRepository(new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp"));
|
---|
385 |
|
---|
386 | ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
|
---|
387 |
|
---|
388 | String storageLocation = "";
|
---|
389 | File uploadedFile = null;
|
---|
390 | try
|
---|
391 | {
|
---|
392 | List items = uploadHandler.parseRequest(request);
|
---|
393 | Iterator iter = items.iterator();
|
---|
394 | while (iter.hasNext())
|
---|
395 | {
|
---|
396 | FileItem current = (FileItem) iter.next();
|
---|
397 | if (current.isFormField())
|
---|
398 | {
|
---|
399 | query_string += current.getFieldName() + "=" + current.getString();
|
---|
400 | if (iter.hasNext())
|
---|
401 | {
|
---|
402 | query_string += "&";
|
---|
403 | }
|
---|
404 |
|
---|
405 | if (current.getFieldName().equals(GSParams.FILE_LOCATION))
|
---|
406 | {
|
---|
407 | storageLocation = current.getString();
|
---|
408 | }
|
---|
409 | }
|
---|
410 | else
|
---|
411 | {
|
---|
412 | File file = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + current.getName());
|
---|
413 | File tmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp");
|
---|
414 | if (!tmpFolder.exists())
|
---|
415 | {
|
---|
416 | tmpFolder.mkdirs();
|
---|
417 | }
|
---|
418 | current.write(file);
|
---|
419 |
|
---|
420 | uploadedFile = file;
|
---|
421 | }
|
---|
422 | }
|
---|
423 |
|
---|
424 | if (!storageLocation.equals("") && uploadedFile != null)
|
---|
425 | {
|
---|
426 | String[] locations = storageLocation.split(":");
|
---|
427 |
|
---|
428 | for (String location : locations)
|
---|
429 | {
|
---|
430 | File toFile = new File(GlobalProperties.getGSDL3Home() + location);
|
---|
431 | if (toFile.exists())
|
---|
432 | {
|
---|
433 | File backupFile = new File(toFile.getAbsolutePath() + System.currentTimeMillis());
|
---|
434 |
|
---|
435 | logger.info("Backing up file (" + toFile.getAbsolutePath() + ") to " + backupFile.getAbsolutePath());
|
---|
436 | toFile.renameTo(backupFile);
|
---|
437 | }
|
---|
438 |
|
---|
439 | logger.info("Moving uploaded file (" + uploadedFile.getAbsolutePath() + ") to " + toFile.getAbsolutePath());
|
---|
440 | uploadedFile.renameTo(toFile);
|
---|
441 | }
|
---|
442 | }
|
---|
443 | }
|
---|
444 | catch (Exception e)
|
---|
445 | {
|
---|
446 | logger.error("Exception in LibraryServlet -> " + e.getMessage());
|
---|
447 | }
|
---|
448 |
|
---|
449 | if (query_string.equals(""))
|
---|
450 | {
|
---|
451 | query_string = null;
|
---|
452 | }
|
---|
453 | }
|
---|
454 | else
|
---|
455 | {
|
---|
456 | query_string = null;
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (query_string != null)
|
---|
460 | {
|
---|
461 | String[] query_arr = StringUtils.split(query_string, "&");
|
---|
462 | boolean redirect = false;
|
---|
463 | String href = null;
|
---|
464 | String rl = null;
|
---|
465 | String[] nameval = new String[2]; // Reuse it for memory efficiency purposes
|
---|
466 |
|
---|
467 | for (int i = 0; i < query_arr.length; i++)
|
---|
468 | {
|
---|
469 | if (query_arr[i].startsWith("el="))
|
---|
470 | {
|
---|
471 | if (query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length()).equals("direct"))
|
---|
472 | {
|
---|
473 | redirect = true;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | else if (query_arr[i].startsWith("href="))
|
---|
477 | {
|
---|
478 | href = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
|
---|
479 | href = StringUtils.replace(href, "%2f", "/");
|
---|
480 | href = StringUtils.replace(href, "%7e", "~");
|
---|
481 | href = StringUtils.replace(href, "%3f", "?");
|
---|
482 | href = StringUtils.replace(href, "%3A", "\\:");
|
---|
483 | }
|
---|
484 | else if (query_arr[i].startsWith("rl="))
|
---|
485 | {
|
---|
486 | rl = query_arr[i].substring(query_arr[i].indexOf("=") + 1, query_arr[i].length());
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | //if query_string contains "el=", the web page will be redirected to the external URl, otherwise a greenstone page with an external URL will be displayed
|
---|
491 | //"rl=0" this is an external link
|
---|
492 | //"rl=1" this is an internal link
|
---|
493 | if ((redirect) && (href != null) && (rl.equals("0")))
|
---|
494 | {// This is an external link, the web page is re-directed to the external URL (&el=&rl=0&href="http://...")
|
---|
495 | response.setContentType("text/xml");
|
---|
496 | response.sendRedirect(href);
|
---|
497 | }
|
---|
498 | }
|
---|
499 |
|
---|
500 | // Nested Diagnostic Configurator to identify the client for
|
---|
501 | HttpSession session = request.getSession(true);
|
---|
502 | session.setMaxInactiveInterval(session_expiration);
|
---|
503 | String uid = (String) session.getAttribute(GSXML.USER_ID_ATT);
|
---|
504 | if (uid == null)
|
---|
505 | {
|
---|
506 | uid = "" + getNextUserId();
|
---|
507 | session.setAttribute(GSXML.USER_ID_ATT, uid);
|
---|
508 | }
|
---|
509 |
|
---|
510 | request.setCharacterEncoding("UTF-8");
|
---|
511 | response.setContentType("text/html;charset=UTF-8");
|
---|
512 | PrintWriter out = response.getWriter();
|
---|
513 |
|
---|
514 | String lang = request.getParameter(GSParams.LANGUAGE);
|
---|
515 | if (lang == null || lang.equals(""))
|
---|
516 | {
|
---|
517 | // try the session cached lang
|
---|
518 | lang = (String) session.getAttribute(GSParams.LANGUAGE);
|
---|
519 | if (lang == null || lang.equals(""))
|
---|
520 | {
|
---|
521 | // still not set, use the default
|
---|
522 | lang = this.default_lang;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | // set the lang in the session
|
---|
527 | session.setAttribute(GSParams.LANGUAGE, lang);
|
---|
528 |
|
---|
529 | String output = request.getParameter(GSParams.OUTPUT);
|
---|
530 | if (output == null || output.equals(""))
|
---|
531 | {
|
---|
532 | output = "html"; // uses html by default
|
---|
533 | }
|
---|
534 |
|
---|
535 | // If server output, force a switch to traditional interface
|
---|
536 | //output = (output.equals("server")) ? "html" : output;
|
---|
537 |
|
---|
538 | // Force change the output mode if client-side XSLT is supported - server vs. client
|
---|
539 | // BUT only if the library allows client-side transforms
|
---|
540 | if (supports_client_xslt)
|
---|
541 | {
|
---|
542 | // MUST be done before the xml_message is built
|
---|
543 | Cookie[] cookies = request.getCookies();
|
---|
544 | Cookie xsltCookie = null;
|
---|
545 |
|
---|
546 | // The client has cookies enabled and a value set - use it!
|
---|
547 | if (cookies != null)
|
---|
548 | {
|
---|
549 | for (Cookie c : cookies)
|
---|
550 | {
|
---|
551 | if (c.getName().equals("supportsXSLT"))
|
---|
552 | {
|
---|
553 | xsltCookie = c;
|
---|
554 | break;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | output = (xsltCookie != null && xsltCookie.getValue().equals("true") && output.equals("html")) ? "xsltclient" : output;
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | // the request to the receptionist
|
---|
562 | Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
563 | Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", lang, uid);
|
---|
564 | xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
|
---|
565 |
|
---|
566 | xml_message.appendChild(xml_request);
|
---|
567 |
|
---|
568 | String action = request.getParameter(GSParams.ACTION);
|
---|
569 | String subaction = request.getParameter(GSParams.SUBACTION);
|
---|
570 | String collection = request.getParameter(GSParams.COLLECTION);
|
---|
571 | String service = request.getParameter(GSParams.SERVICE);
|
---|
572 |
|
---|
573 | // We clean up the cache session_ids_table if system
|
---|
574 | // commands are issued (and also don't need to do caching for this request)
|
---|
575 | boolean should_cache = true;
|
---|
576 | if (action != null && action.equals(GSParams.SYSTEM))
|
---|
577 | {
|
---|
578 | should_cache = false;
|
---|
579 |
|
---|
580 | // we may want to remove all collection cache info, or just a specific collection
|
---|
581 | boolean clean_all = true;
|
---|
582 | String clean_collection = null;
|
---|
583 | // system commands are to activate/deactivate stuff
|
---|
584 | // collection param is in the sc parameter.
|
---|
585 | // don't like the fact that it is hard coded here
|
---|
586 | String coll = request.getParameter(GSParams.SYSTEM_CLUSTER);
|
---|
587 | if (coll != null && !coll.equals(""))
|
---|
588 | {
|
---|
589 | clean_all = false;
|
---|
590 | clean_collection = coll;
|
---|
591 | }
|
---|
592 | else
|
---|
593 | {
|
---|
594 | // check other system types
|
---|
595 | if (subaction.equals("a") || subaction.equals("d"))
|
---|
596 | {
|
---|
597 | String module_name = request.getParameter("sn");
|
---|
598 | if (module_name != null && !module_name.equals(""))
|
---|
599 | {
|
---|
600 | clean_all = false;
|
---|
601 | clean_collection = module_name;
|
---|
602 | }
|
---|
603 | }
|
---|
604 | }
|
---|
605 | if (clean_all)
|
---|
606 | {
|
---|
607 | session_ids_table = new Hashtable();
|
---|
608 | session.removeAttribute(GSXML.USER_SESSION_CACHE_ATT);
|
---|
609 | }
|
---|
610 | else
|
---|
611 | {
|
---|
612 | // just clean up info for clean_collection
|
---|
613 | ArrayList cache_list = new ArrayList(session_ids_table.values());
|
---|
614 | for (int i = 0; i < cache_list.size(); i++)
|
---|
615 | {
|
---|
616 | UserSessionCache cache = (UserSessionCache) cache_list.get(i);
|
---|
617 | cache.cleanupCache(clean_collection);
|
---|
618 | }
|
---|
619 |
|
---|
620 | }
|
---|
621 |
|
---|
622 | }
|
---|
623 |
|
---|
624 | // cache_key is the collection name, or service name
|
---|
625 | String cache_key = collection;
|
---|
626 | if (cache_key == null || cache_key.equals(""))
|
---|
627 | {
|
---|
628 | cache_key = service;
|
---|
629 | }
|
---|
630 |
|
---|
631 | // logger.info("should_cache= " + should_cache);
|
---|
632 |
|
---|
633 | //clear the collection-specific cache in the session, since we have no way to know whether this session is
|
---|
634 | //about the same collection as the last session or not.
|
---|
635 | Enumeration attributeNames = session.getAttributeNames();
|
---|
636 | while (attributeNames.hasMoreElements())
|
---|
637 | {
|
---|
638 | String name = (String) attributeNames.nextElement();
|
---|
639 | if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
|
---|
640 | {
|
---|
641 |
|
---|
642 | session.removeAttribute(name);
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | UserSessionCache session_cache = null;
|
---|
647 | Hashtable param_table = null;
|
---|
648 | Hashtable table = null;
|
---|
649 | String sid = session.getId();
|
---|
650 | if (should_cache == true && cache_key != null && !cache_key.equals(""))
|
---|
651 | {
|
---|
652 | if (session_ids_table.containsKey(sid))
|
---|
653 | {
|
---|
654 | session_cache = (UserSessionCache) session_ids_table.get(sid);
|
---|
655 | param_table = session_cache.getParamsTable();
|
---|
656 | logger.info("collections in table: " + tableToString(param_table));
|
---|
657 | if (param_table.containsKey(cache_key))
|
---|
658 | {
|
---|
659 | //logger.info("existing table: " + collection);
|
---|
660 | table = (Hashtable) param_table.get(cache_key);
|
---|
661 | }
|
---|
662 | else
|
---|
663 | {
|
---|
664 | table = new Hashtable();
|
---|
665 | param_table.put(cache_key, table);
|
---|
666 | //logger.info("new table: " + collection);
|
---|
667 | }
|
---|
668 | }
|
---|
669 | else
|
---|
670 | {
|
---|
671 | param_table = new Hashtable();
|
---|
672 | table = new Hashtable();
|
---|
673 | param_table.put(cache_key, table);
|
---|
674 | session_cache = new UserSessionCache(sid, param_table);
|
---|
675 | session_ids_table.put(sid, session_cache);
|
---|
676 | session.setAttribute(GSXML.USER_SESSION_CACHE_ATT, session_cache);
|
---|
677 | //logger.info("new session id");
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | if (action == null || action.equals(""))
|
---|
682 | {
|
---|
683 | // should we do all the following stuff if using default page?
|
---|
684 | // display the home page - the default page
|
---|
685 | xml_request.setAttribute(GSXML.ACTION_ATT, "p");
|
---|
686 | xml_request.setAttribute(GSXML.SUBACTION_ATT, PageAction.HOME_PAGE);
|
---|
687 | }
|
---|
688 | else
|
---|
689 | {
|
---|
690 | xml_request.setAttribute(GSXML.ACTION_ATT, action);
|
---|
691 | if (subaction != null)
|
---|
692 | {
|
---|
693 | xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
|
---|
694 | }
|
---|
695 |
|
---|
696 | // create the param list for the greenstone request - includes
|
---|
697 | // the params from the current request and any others from the saved session
|
---|
698 | Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
699 | xml_request.appendChild(xml_param_list);
|
---|
700 |
|
---|
701 | Enumeration params = request.getParameterNames();
|
---|
702 | while (params.hasMoreElements())
|
---|
703 | {
|
---|
704 | String name = (String) params.nextElement();
|
---|
705 | if (!name.equals(GSParams.ACTION) && !name.equals(GSParams.SUBACTION) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSParams.OUTPUT))
|
---|
706 | {// we have already dealt with these
|
---|
707 |
|
---|
708 | String value = "";
|
---|
709 | String[] values = request.getParameterValues(name);
|
---|
710 | value = values[0];
|
---|
711 | if (values.length > 1)
|
---|
712 | {
|
---|
713 | for (int i = 1; i < values.length; i++)
|
---|
714 | {
|
---|
715 | value += "," + values[i];
|
---|
716 | }
|
---|
717 | }
|
---|
718 | // either add it to the param list straight away, or save it to the session and add it later
|
---|
719 | if (this.params.shouldSave(name) && table != null)
|
---|
720 | {
|
---|
721 | table.put(name, value);
|
---|
722 | }
|
---|
723 | else
|
---|
724 | {
|
---|
725 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
726 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
727 | param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
|
---|
728 | xml_param_list.appendChild(param);
|
---|
729 | }
|
---|
730 | }
|
---|
731 | }
|
---|
732 | //put everything in the table into the session
|
---|
733 | // do we need to do this? why not just put from table into param list
|
---|
734 | if (table != null)
|
---|
735 | {
|
---|
736 | Enumeration keys = table.keys();
|
---|
737 | while (keys.hasMoreElements())
|
---|
738 | {
|
---|
739 | String name = (String) keys.nextElement();
|
---|
740 | session.setAttribute(name, (String) table.get(name));
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | // put in all the params from the session cache
|
---|
745 | params = session.getAttributeNames();
|
---|
746 | while (params.hasMoreElements())
|
---|
747 | {
|
---|
748 | String name = (String) params.nextElement();
|
---|
749 |
|
---|
750 | if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
|
---|
751 | {
|
---|
752 |
|
---|
753 | // lang and uid are stored but we dont want it in the param list cos its already in the request
|
---|
754 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
755 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
756 | String value = GSXML.xmlSafe((String) session.getAttribute(name));
|
---|
757 |
|
---|
758 | // ugly hack to undo : escaping
|
---|
759 | value = StringUtils.replace(value, "%3A", "\\:");
|
---|
760 | param.setAttribute(GSXML.VALUE_ATT, value);
|
---|
761 | xml_param_list.appendChild(param);
|
---|
762 | }
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | if (!output.equals("html") && !output.equals("server") && !output.equals("xsltclient"))
|
---|
767 | {
|
---|
768 | response.setContentType("text/xml"); // for now use text
|
---|
769 | }
|
---|
770 |
|
---|
771 | //Add custom HTTP headers if requested
|
---|
772 | String httpHeadersParam = request.getParameter(GSParams.HTTPHEADERFIELDS);
|
---|
773 | if (httpHeadersParam != null && httpHeadersParam.length() > 0)
|
---|
774 | {
|
---|
775 | Gson gson = new Gson();
|
---|
776 | Type type = new TypeToken<List<Map<String, String>>>()
|
---|
777 | {
|
---|
778 | }.getType();
|
---|
779 | List<Map<String, String>> httpHeaders = gson.fromJson(httpHeadersParam, type);
|
---|
780 | if (httpHeaders != null && httpHeaders.size() > 0)
|
---|
781 | {
|
---|
782 |
|
---|
783 | for (int j = 0; j < httpHeaders.size(); j++)
|
---|
784 | {
|
---|
785 | Map nameValueMap = (Map) httpHeaders.get(j);
|
---|
786 | String name = (String) nameValueMap.get("name");
|
---|
787 | String value = (String) nameValueMap.get("value");
|
---|
788 |
|
---|
789 | if (name != null && value != null)
|
---|
790 | {
|
---|
791 | response.setHeader(name, value);
|
---|
792 | }
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | Node xml_result = this.recept.process(xml_message);
|
---|
798 | encodeURLs(xml_result, response);
|
---|
799 | out.println(this.converter.getPrettyString(xml_result));
|
---|
800 |
|
---|
801 | displaySize(session_ids_table);
|
---|
802 |
|
---|
803 | } //end of doGet(HttpServletRequest, HttpServletResponse)
|
---|
804 |
|
---|
805 | //a debugging method
|
---|
806 | private void displaySize(Hashtable table)
|
---|
807 | {
|
---|
808 | if (table == null)
|
---|
809 | {
|
---|
810 | logger.info("cached table is null");
|
---|
811 | return;
|
---|
812 | }
|
---|
813 | if (table.size() == 0)
|
---|
814 | {
|
---|
815 | logger.info("cached table size is zero");
|
---|
816 | return;
|
---|
817 | }
|
---|
818 | int num_cached_coll = 0;
|
---|
819 | ArrayList cache_list = new ArrayList(table.values());
|
---|
820 | for (int i = 0; i < cache_list.size(); i++)
|
---|
821 | {
|
---|
822 | num_cached_coll += ((UserSessionCache) cache_list.get(i)).tableSize();
|
---|
823 | }
|
---|
824 | logger.info("Number of sessions : total number of cached collection info = " + table.size() + " : " + num_cached_coll);
|
---|
825 | }
|
---|
826 |
|
---|
827 | /** merely a debugging method! */
|
---|
828 | private String tableToString(Hashtable table)
|
---|
829 | {
|
---|
830 | String str = "";
|
---|
831 | Enumeration keys = table.keys();
|
---|
832 | while (keys.hasMoreElements())
|
---|
833 | {
|
---|
834 | String name = (String) keys.nextElement();
|
---|
835 | str += name + ", ";
|
---|
836 | }
|
---|
837 | return str;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * this goes through each URL and adds in a session id if needed-- its
|
---|
842 | * needed if the browser doesn't accept cookies also escapes things if
|
---|
843 | * needed
|
---|
844 | */
|
---|
845 | protected void encodeURLs(Node dataNode, HttpServletResponse response)
|
---|
846 | {
|
---|
847 |
|
---|
848 | if (dataNode == null)
|
---|
849 | {
|
---|
850 | return;
|
---|
851 | }
|
---|
852 |
|
---|
853 | Element data = null;
|
---|
854 |
|
---|
855 | short nodeType = dataNode.getNodeType();
|
---|
856 | if (nodeType == Node.DOCUMENT_NODE)
|
---|
857 | {
|
---|
858 | Document docNode = (Document) dataNode;
|
---|
859 | data = docNode.getDocumentElement();
|
---|
860 | }
|
---|
861 | else
|
---|
862 | {
|
---|
863 | data = (Element) dataNode;
|
---|
864 | }
|
---|
865 |
|
---|
866 | if (data != null)
|
---|
867 | {
|
---|
868 |
|
---|
869 | // get all the <a> elements
|
---|
870 | NodeList hrefs = data.getElementsByTagName("a");
|
---|
871 | // Instead of calculating each iteration...
|
---|
872 | int hrefscount = hrefs.getLength();
|
---|
873 |
|
---|
874 | for (int i = 0; hrefs != null && i < hrefscount; i++)
|
---|
875 | {
|
---|
876 | Element a = (Element) hrefs.item(i);
|
---|
877 | // ugly hack to get rid of : in the args - interferes with session handling
|
---|
878 | String href = a.getAttribute("href");
|
---|
879 | if (!href.equals(""))
|
---|
880 | {
|
---|
881 | if (href.indexOf("?") != -1)
|
---|
882 | {
|
---|
883 | String[] parts = StringUtils.split(href, "\\?", -1);
|
---|
884 | if (parts.length == 1)
|
---|
885 | {
|
---|
886 | parts[0] = StringUtils.replace(parts[0], ":", "%3A");
|
---|
887 | href = "?" + parts[0];
|
---|
888 | }
|
---|
889 | else
|
---|
890 | {
|
---|
891 | parts[1] = StringUtils.replace(parts[1], ":", "%3A");
|
---|
892 | href = parts[0] + "?" + parts[1];
|
---|
893 | }
|
---|
894 |
|
---|
895 | }
|
---|
896 | a.setAttribute("href", response.encodeURL(href));
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | // now find any submit bits - get all the <form> elements
|
---|
901 | NodeList forms = data.getElementsByTagName("form");
|
---|
902 | int formscount = forms.getLength();
|
---|
903 | for (int i = 0; forms != null && i < formscount; i++)
|
---|
904 | {
|
---|
905 | Element form = (Element) forms.item(i);
|
---|
906 | form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
|
---|
907 | }
|
---|
908 | // are these the only cases where URLs occur??
|
---|
909 | // we should only do this for greenstone urls?
|
---|
910 | }
|
---|
911 |
|
---|
912 | }
|
---|
913 |
|
---|
914 | synchronized protected int getNextUserId()
|
---|
915 | {
|
---|
916 | next_user_id++;
|
---|
917 | return next_user_id;
|
---|
918 | }
|
---|
919 |
|
---|
920 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
921 | {
|
---|
922 | doGet(request, response);
|
---|
923 | }
|
---|
924 | }
|
---|