Changeset 33612

Show
Ignore:
Timestamp:
31.10.2019 11:00:37 (2 weeks ago)
Author:
kjdon
Message:

work to do with params. add in default values to params if they are not already defined in the session/table. global params (as determined by gs_params.isGlobal()) are kept in the session, not stored in the table. home page now gets stuff from session too. - all this done to make the favouritebasket param work properly, from setting default value in config file, to changing the value on home/collection prefs page

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java

    r33387 r33612  
     1/* 
     2 *    LibraryServlet.java 
     3 *    Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org 
     4 * 
     5 *    This program is free software; you can redistribute it and/or modify 
     6 *    it under the terms of the GNU General Public License as published by 
     7 *    the Free Software Foundation; either version 2 of the License, or 
     8 *    (at your option) any later version. 
     9 * 
     10 *    This program is distributed in the hope that it will be useful, 
     11 *    but WITHOUT ANY WARRANTY; without even the implied warranty of 
     12 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     13 *    GNU General Public License for more details. 
     14 * 
     15 *    You should have received a copy of the GNU General Public License 
     16 *    along with this program; if not, write to the Free Software 
     17 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
     18 */ 
     19 
    120package org.greenstone.gsdl3; 
    221 
     
    726import java.lang.reflect.Type; 
    827import java.util.ArrayList; 
     28import java.util.Arrays; 
    929import java.util.Enumeration; 
    1030import java.util.HashMap; 
     
    5272/** 
    5373 * a servlet to serve the greenstone library - we are using servlets instead of 
    54  * cgi the init method is called only once - the first time the servlet classes 
     74 * cgi  
     75 * the init method is called only once - the first time the servlet classes 
    5576 * are loaded. Each time a request comes in to the servlet, the session() method 
    5677 * is called in a new thread (calls doGet/doPut etc) takes the a=p&p=home type 
     
    6990    protected Receptionist recept = null; 
    7091 
     92  /** We record the library name for later */ 
     93  protected String library_name = null; 
     94    /** Whether or not client-side XSLT support should be exposed */ 
     95    protected boolean supports_client_xslt = false; 
     96 
    7197    /** 
    7298     * the default language - is specified by setting a servlet param, otherwise 
     
    74100     */ 
    75101    protected String default_lang = null; 
    76  
    77   /** We record the library name for later */ 
    78   protected String library_name = null; 
    79     /** Whether or not client-side XSLT support should be exposed */ 
    80     protected boolean supports_client_xslt = false; 
    81  
    82102    /** 
    83103     * The default default - used if a default lang is not specified in the 
     
    304324        //Allow the message router and the document to be accessed from anywhere in this servlet context 
    305325        this.getServletContext().setAttribute(library_name+"Router", this.recept.getMessageRouter()); 
    306         //this.getServletContext().setAttribute("LibraryName", library_name); 
    307326    } 
    308327 
    309     private void logUsageInfo(HttpServletRequest request) 
     328  private void logUsageInfo(HttpServletRequest request, Map<String, String[]> queryMap) 
    310329    { 
    311330        String usageInfo = ""; 
     
    320339        } 
    321340 
     341        String queryParamStr = ""; 
     342        if (queryMap != null) 
     343        { 
     344            Iterator<String> queryIter = queryMap.keySet().iterator(); 
     345            while (queryIter.hasNext()) { 
     346              String q = queryIter.next(); 
     347              String[] vals = queryMap.get(q); 
     348              queryParamStr += q +"="+String.join(",",vals)+" "; 
     349            } 
     350        } 
    322351        //logged info = general-info + session-info 
    323352        usageInfo = request.getServletPath() + " " + //serlvet 
    324         "[" + request.getQueryString() + "]" + " " + //the query string 
    325         "[" + usageInfo.trim() + "]" + " " + // params stored in a session 
    326         request.getRemoteAddr() + " " + //remote address 
    327         request.getRequestedSessionId() + " " + //session id 
    328         request.getHeader("user-agent") + " "; //the remote brower info 
     353          "[" + queryParamStr.trim()+"]"+" " + // query map params  
     354          "session:[" + usageInfo.trim() + "]" + " " + // params stored in a session 
     355          request.getRemoteAddr() + " " + //remote address 
     356          request.getRequestedSessionId() + " " + //session id 
     357          request.getHeader("user-agent") + " "; //the remote brower info 
    329358 
    330359        logger.info(usageInfo); 
     
    367396        } 
    368397 
     398      // if session cache id removed from session, this triggers this valueUnbound method on the value=this object 
    369399        public void valueUnbound(HttpSessionBindingEvent event) 
    370400        { 
     
    388418    public void doGetOrPost(HttpServletRequest request, HttpServletResponse response, Map<String, String[]> queryMap) throws ServletException, IOException 
    389419    { 
    390         logUsageInfo(request); 
     420      logUsageInfo(request, queryMap); 
    391421 
    392422        if (queryMap != null) 
     
    541571 
    542572        // We clean up the cache session_ids_table if system 
    543         // commands are issued (and also don't need to do caching for this request) 
     573        // commands are issued, and also don't need to do caching for these request requests 
    544574        boolean should_cache = true; 
    545575        if (action != null && action.equals(GSParams.SYSTEM_ACTION)  
     
    578608                // TODO 
    579609                session_ids_table = new Hashtable<String, UserSessionCache>(); 
    580                 session.removeAttribute(GSXML.USER_SESSION_CACHE_ATT); // triggers valueUnbound(), which removes the session id from the session_ids_table 
     610                session.removeAttribute(GSParams.USER_SESSION_CACHE); // triggers valueUnbound(), which removes the session id from the session_ids_table 
    581611            } 
    582612            else 
     
    604634        } 
    605635         
    606         //clear the collection-specific cache in the session, since we have no way to know whether this session is  
    607         //about the same collection as the last session or not. 
     636        //clear the collection-specific cache in the session, since we have no way to know whether this page is  
     637        //about the same collection as the last page or not. 
    608638        Enumeration attributeNames = session.getAttributeNames(); 
    609639        while (attributeNames.hasMoreElements()) 
    610640        { 
    611641            String name = (String) attributeNames.nextElement(); 
    612             if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT)) 
     642            if (!name.equals(GSParams.USER_SESSION_CACHE) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT) && !this.gs_params.isGlobal(name)) 
    613643            { 
    614644                session.removeAttribute(name); 
    615             } 
     645            }  
    616646        } 
    617647 
     
    621651        boolean new_table = false; 
    622652        String sid = session.getId(); 
     653        boolean new_session = false; 
     654        if (!session_ids_table.containsKey(sid)) { 
     655          new_session = true; 
     656        } 
    623657        if (should_cache == true && cache_key != null && !cache_key.equals("")) 
    624658        { 
    625             if (session_ids_table.containsKey(sid)) 
     659            if (!new_session) 
    626660            { 
    627661                session_cache = session_ids_table.get(sid); 
     
    640674            else 
    641675            { 
     676               
    642677                param_table = new Hashtable<String, Hashtable<String, String>>(); 
    643678                table = new Hashtable<String, String>(); 
     
    645680                session_cache = new UserSessionCache(sid, param_table); 
    646681                session_ids_table.put(sid, session_cache); 
    647                 session.setAttribute(GSXML.USER_SESSION_CACHE_ATT, session_cache); 
     682                session.setAttribute(GSParams.USER_SESSION_CACHE, session_cache); 
    648683                new_table = true; 
    649684            } 
    650685 
    651             // here we add in default params if we have a new table 
    652             ArrayList<String> defp = this.gs_params.getParamsWithDefaults(); 
    653             Iterator i = this.gs_params.getParamsWithDefaults().iterator(); 
    654             while (i.hasNext()) { 
    655               String p = (String)i.next(); 
    656               String v = this.gs_params.getParamDefault(p); 
    657               if (!p.equals(GSParams.ACTION) && !p.equals(GSParams.SUBACTION) && !p.equals(GSParams.LANGUAGE) && !p.equals(GSParams.OUTPUT)) { 
    658                 table.put(p,v); 
    659               } 
    660             } 
    661              
     686        } 
    662687         
    663         } 
     688        // here we add in default params values if need be - if we have a new session, or if we have a new table 
     689        // in an existing session 
     690        // don't override any existing values 
     691        if (new_session || new_table) { 
     692 
     693          Iterator i = this.gs_params.getParamsWithDefaults().iterator(); 
     694          while (i.hasNext()) { 
     695            String p = (String)i.next(); 
     696            String v = this.gs_params.getParamDefault(p); 
     697              if (this.gs_params.isGlobal(p)) { 
     698            //need to add in to session unless its already there 
     699            if (session.getAttribute(p) == null) { 
     700              session.setAttribute(p, v); 
     701            } 
     702              } else { 
     703            // add to table unless its already there 
     704            if (new_table && !table.containsKey(p)) { 
     705              table.put(p,v); 
     706            } 
     707              } 
     708             
     709          } 
     710            } 
    664711 
    665712        if (action == null || action.equals("")) 
    666713        { 
    667             // should we do all the following stuff if using default page? 
    668             // display the home page  - the default page 
     714                // display the home page  - the default page 
    669715            xml_request.setAttribute(GSXML.ACTION_ATT, "p"); 
    670716            xml_request.setAttribute(GSXML.SUBACTION_ATT, PageAction.HOME_PAGE); 
     
    677723                xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 
    678724            } 
    679  
     725        } 
     726         
    680727            //  create the param list for the greenstone request - includes 
    681728            // the params from the current request and any others from the saved session 
     
    702749                        } 
    703750                    } 
    704                     // either add it to the param list straight away, or save it to the session and add it later 
    705                     if (this.gs_params.shouldSave(name) && table != null) 
    706                     { 
    707                         table.put(name, value); 
     751                    // if we need to save the value, then add it to the session/cache table. 
     752                    // otherwise add directly to the paramList 
     753                    if (this.gs_params.shouldSave(name)) { 
     754                      if (this.gs_params.isGlobal(name) || table == null) { 
     755                        session.setAttribute(name, value); 
     756                      } else { 
     757                        table.put(name, value); 
     758                      }  
    708759                    } 
    709760                    else 
     
    721772            } 
    722773            //put everything in the table into the session 
    723             // do we need to do this? why not just put from table into param list 
    724774            if (table != null) 
    725775            { 
     
    737787            { 
    738788                String name = (String) params.nextElement(); 
    739                 if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT)) 
     789                if (!name.equals(GSParams.USER_SESSION_CACHE) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT)) 
    740790                { 
    741791 
     
    751801                } 
    752802            } 
    753         } 
     803     
    754804 
    755805        if (output.equals("json"))