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