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