source: greenstone3/trunk/src/java/org/greenstone/gsdl3/LibraryServlet.java@ 14539

Last change on this file since 14539 was 14539, checked in by shaoqun, 17 years ago

fix the session bug

  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1package org.greenstone.gsdl3;
2
3import org.greenstone.gsdl3.comms.*;
4import org.greenstone.gsdl3.core.*;
5import org.greenstone.gsdl3.util.*;
6import org.greenstone.gsdl3.action.PageAction; // used to get the default action
7import org.w3c.dom.Document;
8import org.w3c.dom.Element;
9import org.w3c.dom.NodeList;
10import java.io.*;
11import javax.servlet.*;
12import javax.servlet.http.*;
13import java.util.Enumeration;
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.io.File;
17import java.util.Hashtable;
18import org.apache.log4j.*;
19
20
21/** a servlet to serve the greenstone library - we are using servlets instead
22 * of cgi
23 * the init method is called only once - the first time the servlet classes
24 * are loaded. Each time a request comes in to the servlet, the session()
25 * method is called in a new thread (calls doGet/doPut etc)
26 * takes the a=p&p=home type args and builds a simple request to send to
27 * its receptionist, which returns a result in html, cos output=html
28 * is set in the request
29 *
30 * 18/Jul/07 xiao
31 * modify to make the cached parameters collection-specific.
32 * Most of the work is done in doGet(), except adding an inner class UserSessionCache.
33 *
34 * @see Receptionist
35 */
36public class LibraryServlet extends HttpServlet {
37
38 /** the receptionist to send messages to */
39 protected Receptionist recept=null;
40 /** the default language - is specified by setting a servlet param,
41 * otherwise DEFAULT_LANG is used*/
42 protected String default_lang= null;
43 /** The default default - used if a default lang is not specified
44 * in the servlet params */
45 protected final String DEFAULT_LANG = "en";
46 /** container Document to create XML Nodes */
47 protected Document doc=null;
48 /** a converter class to parse XML and create Docs */
49 protected XMLConverter converter=null;
50 /** the cgi stuff - the Receptionist can add new args to this
51 *
52 * its used by the servlet to determine what args to save */
53 protected GSParams params = null;
54
55 /** user id - new one per session. This doesn't work if session state is saved between restarts - this requires this value to be saved too. */
56 protected int next_user_id = 0;
57
58 /** a hash that contains all the active session IDs mapped to the cached items
59 * It is updated whenever the whole site or a particular collection is reconfigured
60 * using the command a=s&sa=c or a=s&sa=c&c=xxx
61 * It is in the form: sid -> (UserSessionCache object)
62 */
63 protected Hashtable session_ids_table = new Hashtable();
64 //this name is combined with the collection name and used for caching
65 protected String valid_site_name = "";
66
67 /** the maximum interval that the cached info remains in session_ids_table (in seconds)
68 * This is set in web.xml
69 */
70 protected int session_expiration = 1800;
71
72 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.LibraryServlet.class.getName());
73
74 /** initialise the servlet
75 */
76 public void init(ServletConfig config) throws ServletException {
77 // always call super.init;
78 super.init(config);
79 // disable preferences - does this work anyway??
80 //System.setProperty("java.util.prefs.PreferencesFactory", "org.greenstone.gsdl3.util.DisabledPreferencesFactory");
81
82
83 String library_name = config.getInitParameter(GSConstants.LIBRARY_NAME);
84 String gsdl3_home = config.getInitParameter(GSConstants.GSDL3_HOME);
85 String interface_name = config.getInitParameter(GSConstants.INTERFACE_NAME);
86 this.default_lang = config.getInitParameter(GSConstants.DEFAULT_LANG);
87 String sess_expire = config.getInitParameter(GSXML.SESSION_EXPIRATION);
88 if (sess_expire != null && !sess_expire.equals("")) {
89 this.session_expiration = Integer.parseInt(sess_expire);
90 }
91
92 if (library_name == null || interface_name ==null) {
93 // must have this
94 System.err.println("initialisation parameters not all set!");
95 System.err.println(" you must have libraryname and interfacename");
96 System.exit(1);
97 }
98
99 String site_name = config.getInitParameter(GSConstants.SITE_NAME);
100 String remote_site_name = null;
101 String remote_site_type = null;
102 String remote_site_address = null;
103
104 if (site_name == null) {
105 // no site, try for communicator
106 remote_site_name = config.getInitParameter("remote_site_name");
107 remote_site_type = config.getInitParameter("remote_site_type");
108 remote_site_address = config.getInitParameter("remote_site_address");
109 if (remote_site_name == null || remote_site_type == null || remote_site_address == null) {
110 System.err.println("initialisation paramters not all set!");
111 System.err.println("if site_name is not set, then you must have remote_site_name, remote_site_type and remote_site_address set");
112 System.exit(1);
113 }
114 }
115 valid_site_name = (site_name != null)? site_name : remote_site_name;
116
117 if (this.default_lang == null) {
118 // choose english
119 this.default_lang = DEFAULT_LANG;
120 }
121
122 HashMap config_params = new HashMap();
123
124 config_params.put(GSConstants.LIBRARY_NAME, library_name);
125 config_params.put(GSConstants.INTERFACE_NAME, interface_name);
126 if (site_name != null) {
127 config_params.put(GSConstants.SITE_NAME, site_name);
128 }
129 this.converter = new XMLConverter();
130 this.doc = this.converter.newDOM();
131
132 // the receptionist -the servlet will talk to this
133 String recept_name = (String)config.getInitParameter("receptionist_class");
134 if (recept_name == null) {
135 this.recept = new DefaultReceptionist();
136 } else {
137 try {
138 this.recept = (Receptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
139 } catch (Exception e) { // cant use this new one, so use normal one
140 System.err.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage());
141 e.printStackTrace();
142 this.recept = new DefaultReceptionist();
143 }
144 }
145 this.recept.setConfigParams(config_params);
146
147 // 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.
148 if (site_name != null) {
149 String mr_name = (String)config.getInitParameter("messagerouter_class");
150 MessageRouter message_router = null;
151 if (mr_name == null) { // just use the normal MR
152 message_router = new MessageRouter();
153 } else { // try the specified one
154 try {
155 message_router = (MessageRouter)Class.forName("org.greenstone.gsdl3.core."+mr_name).newInstance();
156 } catch (Exception e) { // cant use this new one, so use normal one
157 System.err.println("LibraryServlet configure exception when trying to use a new MessageRouter "+mr_name+": "+e.getMessage());
158 e.printStackTrace();
159 message_router = new MessageRouter();
160 }
161 }
162
163 message_router.setSiteName(site_name);
164 message_router.setLibraryName(library_name);
165 message_router.configure();
166 this.recept.setMessageRouter(message_router);
167 } else {
168 // talking to a remote site, create a communicator
169 Communicator communicator = null;
170 // we need to create the XML to configure the communicator
171 Element site_elem = this.doc.createElement(GSXML.SITE_ELEM);
172 site_elem.setAttribute(GSXML.TYPE_ATT, remote_site_type);
173 site_elem.setAttribute(GSXML.NAME_ATT, remote_site_name);
174 site_elem.setAttribute(GSXML.ADDRESS_ATT, remote_site_address);
175
176 if (remote_site_type.equals(GSXML.COMM_TYPE_SOAP_JAVA)) {
177 communicator = new SOAPCommunicator();
178 } else {
179 System.err.println("LibraryServlet.init Error: invalid Communicator type: "+remote_site_type);
180 System.exit(1);
181 }
182
183 if (!communicator.configure(site_elem)) {
184 System.err.println("LibraryServlet.init Error: Couldn't configure communicator");
185 System.exit(1);
186 }
187 this.recept.setMessageRouter(communicator);
188 }
189
190 // the params arg thingy
191
192 String params_name = (String)config.getInitParameter("params_class");
193 if (params_name == null) {
194 this.params = new GSParams();
195 } else {
196 try {
197 this.params = (GSParams)Class.forName("org.greenstone.gsdl3.util."+params_name).newInstance();
198 } catch (Exception e) {
199 System.err.println("LibraryServlet configure exception when trying to use a new params thing "+params_name+": "+e.getMessage());
200 e.printStackTrace();
201 this.params = new GSParams();
202 }
203 }
204 // pass it to the receptionist
205 this.recept.setParams(this.params);
206 this.recept.configure();
207
208 }
209
210
211 private void logUsageInfo(HttpServletRequest request){
212 String usageInfo = "";
213
214 //session-info: get params stored in the session
215 HttpSession session = request.getSession(true);
216 Enumeration attributeNames = session.getAttributeNames();
217 while(attributeNames.hasMoreElements()) {
218 String name = (String)attributeNames.nextElement();
219 usageInfo +=name+"="+session.getAttribute(name)+" ";
220 }
221
222 //logged info = general-info + session-info
223 usageInfo =
224 request.getServletPath()+" "+ //serlvet
225 "["+request.getQueryString()+"]" +" "+ //the query string
226 "["+usageInfo.trim()+"]" +" "+ // params stored in a session
227 request.getRemoteAddr()+" "+ //remote address
228 request.getRequestedSessionId()+" "+ //session id
229 request.getHeader("user-agent")+" "; //the remote brower info
230
231 logger.info(usageInfo);
232
233 }
234
235 public class UserSessionCache implements HttpSessionBindingListener {
236
237 String session_id = "";
238
239 /** a hash that maps the session ID to a hashtable that maps the coll_name to its parameters
240 * coll_name -> Hashtable (param_name -> param_value)
241 */
242 protected Hashtable coll_name_params_table = null;
243
244 public UserSessionCache(String id, Hashtable table) {
245 session_id = id;
246 coll_name_params_table = (table == null)? new Hashtable() : table;
247 }
248
249 protected void cleanupCache(String coll_name) {
250 if (coll_name_params_table.containsKey(coll_name)) {
251 coll_name_params_table.remove(coll_name);
252 }
253 }
254 protected Hashtable getParamsTable() {
255 return coll_name_params_table;
256 }
257 public void valueBound(HttpSessionBindingEvent event) {
258 // Do nothing
259 }
260
261 public void valueUnbound(HttpSessionBindingEvent event) {
262 if(session_ids_table.containsKey(session_id)) {
263 session_ids_table.remove(session_id);
264 }
265 }
266 public int tableSize() {
267 return (coll_name_params_table == null)? 0 : coll_name_params_table.size();
268 }
269 }
270
271 public void doGet (HttpServletRequest request,
272 HttpServletResponse response)
273 throws ServletException, IOException {
274 logUsageInfo (request);
275
276 String query_string = request.getQueryString();
277 if (query_string!=null){
278 String[] query_arr = query_string.split("&");
279 boolean redirect = false;
280 String href = null;
281 String rl = null;
282 for (int i=0;i<query_arr.length;i++){
283 if (query_arr[i].startsWith("el")){
284 redirect = true;
285 }else if(query_arr[i].startsWith("href")){
286 href = query_arr[i].substring(query_arr[i].indexOf("=")+1,query_arr[i].length());
287 href = href.replaceAll("%2f", "/");
288 href = href.replaceAll("%7e", "~");
289 href = href.replaceAll("%3f", "?");
290 href = href.replaceAll("%3A", "\\:");
291 }else if(query_arr[i].startsWith("rl")){
292 rl = query_arr[i].substring(query_arr[i].indexOf("=")+1,query_arr[i].length());
293 }
294 }
295 //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
296 //"rl=0" this is an external link
297 //"rl=1" this is an internal link
298 if ((redirect) && (href != null) && (rl.equals("0"))){// This is an external link, the web page is re-directed to the external URL (&el=&rl=0&href="http://...")
299 response.setContentType("text/xml");
300 response.sendRedirect(href);
301 }
302 }
303 // Nested Diagnostic Configurator to identify the client for
304
305 HttpSession session = request.getSession (true);
306 session.setMaxInactiveInterval(session_expiration);
307 String uid = (String)session.getAttribute (GSXML.USER_ID_ATT);
308 if (uid ==null) {
309 uid = ""+getNextUserId ();
310 session.setAttribute (GSXML.USER_ID_ATT, uid);
311 }
312 request.setCharacterEncoding ("UTF-8");
313 response.setContentType ("text/html;charset=UTF-8");
314 PrintWriter out = response.getWriter ();
315
316 String lang = request.getParameter (GSParams.LANGUAGE);
317 if (lang==null || lang.equals ("")) {
318 // try the session cached lang
319 lang = (String)session.getAttribute (GSParams.LANGUAGE);
320 if (lang==null || lang.equals ("")) {
321 // still not set, use the default
322 lang = this.default_lang;
323 }
324 }
325
326 // set the lang in the session
327 session.setAttribute (GSParams.LANGUAGE, lang);
328
329 String output = request.getParameter (GSParams.OUTPUT);
330 if (output==null || output.equals ("")) {
331 output = "html"; // uses html by default
332 }
333
334 // the request to the receptionist
335 Element xml_message = this.doc.createElement (GSXML.MESSAGE_ELEM);
336 Element xml_request = GSXML.createBasicRequest (this.doc, GSXML.REQUEST_TYPE_PAGE, "", lang, uid);
337 xml_request.setAttribute (GSXML.OUTPUT_ATT, output);
338 xml_message.appendChild (xml_request);
339
340 String action = request.getParameter (GSParams.ACTION);
341 String subaction = request.getParameter (GSParams.SUBACTION);
342 String collection = request.getParameter(GSParams.COLLECTION);
343
344 //specifically we clean up the cache session_ids_table if the two reconfigure command
345 //are issued: a=s&sa=c and a=s&sa=c&c=coll_name, in which case there is no caching action to be taken
346 boolean should_cache = true;
347 if(action != null && action.equals(GSParams.SYSTEM)
348 && subaction != null && subaction.equals(GSParams.CONFIGURE)) {
349 if (collection == null || collection.equals("")) {
350 //user reconfiugred the whole site, clean up all cached info
351 //logger.info("clear cache for the whole site.");
352 session_ids_table = new Hashtable();
353 session.removeAttribute(GSXML.USER_SESSION_CACHE_ATT);
354 } else {
355 //clean up all cache info related to the collection
356 //logger.info("clear cache for collection: " + collection);
357 ArrayList cache_list = new ArrayList(session_ids_table.values());
358 for (int i=0; i<cache_list.size(); i++) {
359 UserSessionCache cache = (UserSessionCache)cache_list.get(i);
360 cache.cleanupCache(collection);
361 }
362 }
363 should_cache = false;
364 }
365
366 // logger.info("should_cache= " + should_cache);
367 //clear the collection-specific cache in the session, since we have no way to know whether this session is
368 //about the same collection as the last session or not.
369 Enumeration attributeNames = session.getAttributeNames();
370 while(attributeNames.hasMoreElements()) {
371 String name = (String)attributeNames.nextElement();
372 if (!name.equals (GSXML.USER_SESSION_CACHE_ATT)
373 && !name.equals (GSParams.LANGUAGE)
374 && !name.equals (GSXML.USER_ID_ATT)) {
375 session.removeAttribute(name);
376 }
377 }
378
379 UserSessionCache session_cache = null;
380 Hashtable param_table = null;
381 Hashtable table = null;
382 String sid = session.getId();
383 if (should_cache == true && collection != null && !collection.equals("")) {
384 String key_str = valid_site_name + collection;
385 if (session_ids_table.containsKey(sid)) {
386 session_cache = (UserSessionCache)session_ids_table.get(sid);
387 param_table = session_cache.getParamsTable();
388 logger.info("collections in table: " + tableToString(param_table));
389 if (param_table.containsKey(key_str)) {
390 //logger.info("existing table: " + collection);
391 table = (Hashtable)param_table.get(key_str);
392 } else {
393 table = new Hashtable();
394 param_table.put(key_str, table);
395 //logger.info("new table: " + collection);
396 }
397 } else {
398 param_table = new Hashtable();
399 table = new Hashtable();
400 param_table.put(key_str, table);
401 session_cache = new UserSessionCache(sid, param_table);
402 session_ids_table.put(sid, session_cache);
403 session.setAttribute(GSXML.USER_SESSION_CACHE_ATT, session_cache);
404 //logger.info("new session id");
405 }
406 }
407
408 if (action==null || action.equals ("")) {
409 // should we do all the following stuff if using default page?
410 // display the home page - the default page
411 action = "p";
412 subaction = PageAction.HOME_PAGE;
413
414 xml_request.setAttribute (GSXML.ACTION_ATT, action);
415 xml_request.setAttribute (GSXML.SUBACTION_ATT, subaction);
416
417 } else {
418
419 xml_request.setAttribute (GSXML.ACTION_ATT, action);
420 if (subaction != null) {
421 xml_request.setAttribute (GSXML.SUBACTION_ATT, subaction);
422 }
423
424 // create the param list for the greenstone request - includes
425 // the params from the current request and any others from the saved session
426 Element xml_param_list = this.doc.createElement (GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
427 xml_request.appendChild (xml_param_list);
428
429 Enumeration params = request.getParameterNames ();
430 while(params.hasMoreElements ()) {
431 String name = (String)params.nextElement ();
432 if (!name.equals (GSParams.ACTION)
433 && !name.equals (GSParams.SUBACTION)
434 && !name.equals (GSParams.LANGUAGE)
435 && !name.equals (GSParams.OUTPUT)) {// we have already dealt with these
436 String value="";
437 String [] values = request.getParameterValues (name);
438 value = values[0];
439 if (values.length > 1) {
440 for (int i=1; i< values.length; i++) {
441 value += ","+values[i];
442 }
443 }
444 // either add it to the param list straight away, or save it to the session and add it later
445 if (this.params.shouldSave (name)) {
446 if (table != null) {
447 table.put(name, value);
448 }
449 } else {
450 Element param = this.doc.createElement (GSXML.PARAM_ELEM);
451 param.setAttribute (GSXML.NAME_ATT, name);
452 param.setAttribute (GSXML.VALUE_ATT, GSXML.xmlSafe (value));
453 xml_param_list.appendChild (param);
454 }
455 }
456 }
457 //put everything in the table into the session
458 if (table != null) {
459 Enumeration keys = table.keys ();
460 while(keys.hasMoreElements ()) {
461 String name = (String)keys.nextElement();
462 session.setAttribute(name, (String)table.get(name));
463 }
464 }
465
466 // put in all the params from the session cache
467 params = session.getAttributeNames ();
468 while(params.hasMoreElements ()) {
469 String name = (String)params.nextElement ();
470
471 if ( !name.equals (GSXML.USER_SESSION_CACHE_ATT)
472 && !name.equals (GSParams.LANGUAGE)
473 && !name.equals (GSXML.USER_ID_ATT)) {
474 // lang and uid are stored but we dont want it in the param list cos its already in the request
475 Element param = this.doc.createElement (GSXML.PARAM_ELEM);
476 param.setAttribute (GSXML.NAME_ATT, name);
477 String value = GSXML.xmlSafe ((String)session.getAttribute (name));
478 // ugly hack to undo : escaping
479 value = value.replaceAll ("%3A", "\\:");
480 param.setAttribute (GSXML.VALUE_ATT,value);
481 xml_param_list.appendChild (param);
482 }
483 }
484 }
485
486 if (!output.equals ("html")) {
487 response.setContentType ("text/xml"); // for now use text
488 }
489
490 //GSXML.printXMLNode(xml_message);
491
492 Element xml_result = this.recept.process (xml_message);
493 encodeURLs (xml_result, response);
494 out.println (this.converter.getPrettyString (xml_result));
495
496 displaySize(session_ids_table);
497 }
498 //a debugging method
499 private void displaySize(Hashtable table) {
500 if(table == null) {
501 logger.info("cached table is null");
502 return;
503 }
504 if (table.size() == 0) {
505 logger.info("cached table size is zero");
506 return;
507 }
508 int num_cached_coll = 0;
509 ArrayList cache_list = new ArrayList(table.values());
510 for (int i=0; i<cache_list.size(); i++) {
511 num_cached_coll += ((UserSessionCache)cache_list.get(i)).tableSize();
512 }
513 logger.info("Number of sessions : total number of cached collection info = " + table.size() + " : " + num_cached_coll);
514 }
515 /** merely a debugging method! */
516 private String tableToString(Hashtable table) {
517 String str = "";
518 Enumeration keys = table.keys ();
519 while(keys.hasMoreElements ()) {
520 String name = (String)keys.nextElement();
521 str += name + ", ";
522 }
523 return str;
524 }
525
526 /** this goes through each URL and adds in a session id if needed--
527 * its needed if the browser doesn't accept cookies
528 * also escapes things if needed
529 */
530 protected void encodeURLs(Element data, HttpServletResponse response) {
531
532 if (data == null) {
533 return;
534 }
535 // get all the <a> elements
536 NodeList hrefs = data.getElementsByTagName("a");
537 for (int i=0; i<hrefs.getLength(); i++) {
538 Element a = (Element)hrefs.item(i);
539 // ugly hack to get rid of : in the args - interferes with session handling
540 String href = a.getAttribute("href");
541 if (!href.equals("")) {
542 if (href.indexOf("?")!=-1) {
543 String[] parts = href.split("\\?", -1);
544 parts[1]=parts[1].replaceAll(":", "%3A");
545 href = parts[0]+"?"+parts[1];
546 }
547 a.setAttribute("href", response.encodeURL(href));
548 }
549 }
550
551 // now find any submit bits - get all the <form> elements
552 NodeList forms = data.getElementsByTagName("form");
553 for (int i=0; i<forms.getLength(); i++) {
554 Element form = (Element)forms.item(i);
555 form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
556 }
557 // are these the only cases where URLs occur??
558 // we should only do this for greenstone urls?
559
560 }
561
562 synchronized protected int getNextUserId() {
563 next_user_id++;
564 return next_user_id;
565 }
566
567 public void doPost(HttpServletRequest request,
568 HttpServletResponse response)
569 throws ServletException, IOException {
570 doGet(request,response);
571
572 }
573}
Note: See TracBrowser for help on using the repository browser.