Changeset 4259


Ignore:
Timestamp:
2003-05-08T16:17:29+12:00 (21 years ago)
Author:
kjdon
Message:

LibraryServlet now caches cgi args, and adds them into the request

File:
1 edited

Legend:

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

    r4152 r4259  
    66import org.w3c.dom.Document;
    77import org.w3c.dom.Element;
     8import org.w3c.dom.NodeList;
    89import java.io.*;
    910import javax.servlet.*;
     
    2526   
    2627    /** the receptionist to send messages to */
    27     protected Receptionist recept_=null;
     28    protected WebReceptionist recept=null;
    2829    /** the default language - is specified by setting a servlet param,
    29      * otherwise DEFUALT_LANG is used*/
    30     protected String default_lang_= null;
     30     * otherwise DEFAULT_LANG is used*/
     31    protected String default_lang= null;
    3132    /** The default default - used if a default lang is not specified
    3233     * in the servlet params */
    3334    protected final String DEFAULT_LANG = "en";
    3435    /** container Document to create XML Nodes */
    35     protected Document doc_=null;   
     36    protected Document doc=null;   
    3637    /** a converter class to parse XML and create Docs */
    37     protected XMLConverter converter_=null;
    38 
     38    protected XMLConverter converter=null;
     39    /** the cgi stuff - the Receptionist can add new args to this
     40     * its used by the servlet to determine what args to save */
     41    protected GSCGI cgi = null;
    3942    /** initialise the servlet
    4043     */
     
    4851    String site_name = config.getInitParameter("sitename");
    4952    String interface_name = config.getInitParameter("interfacename");
    50     default_lang_ = config.getInitParameter("defaultlang");
     53    this.default_lang = config.getInitParameter("defaultlang");
    5154    if (library_name == null || gsdl3_home == null || site_name ==null ||  interface_name ==null) {
    5255        // must have this
     
    5659    }
    5760
    58     if (default_lang_ == null) {
     61    if (this.default_lang == null) {
    5962        // choose english
    60         default_lang_ = DEFAULT_LANG;
     63        this.default_lang = DEFAULT_LANG;
    6164    }
    6265
     
    6568    config_vars.site_name_ = site_name;
    6669    config_vars.interface_name_ =interface_name;
    67     config_vars.default_language_ = default_lang_;
     70    config_vars.default_language_ = this.default_lang;
    6871    config_vars.createXML();
    6972   
    70     converter_ = new XMLConverter();
    71     doc_ = converter_.newDOM();
    72 
     73    this.converter = new XMLConverter();
     74    this.doc = this.converter.newDOM();
     75   
    7376    // now try and create MR and recpt
    7477    // new message router - create it and pass reference to recept.
     
    8588        e.printStackTrace();
    8689        message_router = new MessageRouter();
    87         }
    88        
    89     }
     90        }   
     91    }
     92   
    9093    message_router.setSiteHome(GSFile.siteHome(config_vars.gsdl3_home_,
    9194                           config_vars.site_name_));
     
    9598    String recept_name = (String)config.getInitParameter("receptionist");
    9699    if (recept_name == null) {
    97         recept_ = new Receptionist();
     100        this.recept = new DefaultReceptionist();
    98101    } else {
    99102        try {
    100         recept_ = (Receptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
     103        this.recept = (WebReceptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
    101104        } catch (Exception e) { // cant use this new one, so use normal one
    102105        System.out.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage());
    103106        e.printStackTrace();
    104         recept_ = new Receptionist();
    105         }
    106     }
    107     recept_.setConfigVars(config_vars);
    108     recept_.setMessageRouter(message_router);
    109     recept_.configure();
    110    
     107        this.recept = new DefaultReceptionist();
     108        }
     109    }
     110    this.recept.setConfigVars(config_vars);
     111    this.recept.setMessageRouter(message_router);
     112    this.recept.configure();
     113    // the cgi arg thingy
     114   
     115    String cgi_name = (String)config.getInitParameter("cgiargs");
     116    if (cgi_name == null) {
     117        this.cgi = new GSCGI();
     118    } else {
     119        try {
     120        this.cgi = (GSCGI)Class.forName("org.greenstone.gsdl3.util."+cgi_name).newInstance();
     121        } catch (Exception e) {
     122        System.out.println("LibraryServlet configure exception when trying to use a new cgi thing "+cgi_name+": "+e.getMessage());
     123        e.printStackTrace();
     124        this.cgi = new GSCGI();
     125        }
     126    }
     127    // pass it to the receptionist
     128    this.recept.setCGI(this.cgi);
    111129    }
    112130   
     
    115133    throws ServletException, IOException {
    116134   
     135    HttpSession session = request.getSession(true);
     136
     137    // print them out to test it
     138//      Enumeration stored_names = session.getAttributeNames();
     139//      System.out.println("session values are:");
     140//      while (stored_names.hasMoreElements()) {
     141//          String name = (String)stored_names.nextElement();
     142//          System.out.println("stored "+name+":" +session.getAttribute(name));
     143//      }
     144
    117145    request.setCharacterEncoding("UTF-8");
    118146    response.setContentType("text/html;charset=UTF-8");
     
    121149    String lang = request.getParameter(GSCGI.LANGUAGE_ARG);
    122150    if (lang==null || lang.equals("")) {
    123         lang = default_lang_; // the default
    124     }
     151        // try the session cached lang
     152        lang = (String)session.getAttribute(GSCGI.LANGUAGE_ARG);
     153        if (lang==null || lang.equals("")) {
     154        // still not set, use the default
     155        lang = this.default_lang;
     156        }
     157    }
     158   
     159    // set the lang in the session
     160    session.setAttribute(GSCGI.LANGUAGE_ARG, lang);
     161
    125162    String output = request.getParameter(GSCGI.OUTPUT_ARG);
    126163    if (output==null || output.equals("")) {
     
    129166
    130167    // the request to the receptionist 
    131     Element xml_message = doc_.createElement(GSXML.MESSAGE_ELEM);
    132     Element xml_request = doc_.createElement(GSXML.REQUEST_ELEM);
     168    Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     169    Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_CGI, "", lang);
     170    xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
    133171    xml_message.appendChild(xml_request);
    134     xml_request.setAttribute(GSXML.LANG_ATT, lang);
    135     xml_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CGI);
    136     xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
     172   
    137173   
    138174    String action = request.getParameter(GSCGI.ACTION_ARG);
     
    146182        xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
    147183       
    148         Element xml_result = recept_.process(xml_message);
    149         out.println(converter_.getString(xml_result));
    150         return;
    151     }
    152    
    153     xml_request.setAttribute(GSXML.ACTION_ATT, action);
    154     if (subaction != null) {
    155         xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
    156     }
    157     Element xml_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    158     xml_request.appendChild(xml_param_list);
    159     Enumeration params = request.getParameterNames();
    160     while(params.hasMoreElements()) {
    161         String name = (String)params.nextElement();
    162         if (!name.equals(GSCGI.ACTION_ARG) && !name.equals(GSCGI.SUBACTION_ARG) && !name.equals(GSCGI.LANGUAGE_ARG) && !name.equals(GSCGI.OUTPUT_ARG)) { // these ones are in the header so leave out
    163         String value="";
    164         String []values = request.getParameterValues(name);
    165         value = values[0];
    166         if (values.length > 1) {
    167             for (int i=1; i< values.length; i++) {
    168             value += ","+values[i];
     184    } else {
     185   
     186        xml_request.setAttribute(GSXML.ACTION_ATT, action);
     187        if (subaction != null) {
     188        xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
     189        }
     190
     191        //  create the param list for the greenstone request - includes
     192        // the params from the current request and any others from the saved session
     193        Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     194        xml_request.appendChild(xml_param_list);
     195       
     196        Enumeration params = request.getParameterNames();
     197        while(params.hasMoreElements()) {
     198        String name = (String)params.nextElement();
     199        if (!name.equals(GSCGI.ACTION_ARG) && !name.equals(GSCGI.SUBACTION_ARG) && !name.equals(GSCGI.LANGUAGE_ARG) && !name.equals(GSCGI.OUTPUT_ARG)) {// we have already dealt with these
     200            String value="";
     201            String [] values = request.getParameterValues(name);
     202            value = values[0];
     203            if (values.length > 1) {
     204            for (int i=1; i< values.length; i++) {
     205                value += ","+values[i];
     206            }
     207            }
     208            // either add it to the param list straight away, or save it to the session and add it later
     209            if (this.cgi.shouldSave(name)) {           
     210            session.setAttribute(name, value);
     211            } else {
     212            Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     213            param.setAttribute(GSXML.NAME_ATT, name);
     214            param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
     215            xml_param_list.appendChild(param);
    169216            }
    170217        }
    171         Element param = doc_.createElement(GSXML.PARAM_ELEM);
    172         param.setAttribute(GSXML.NAME_ATT, name);
    173         param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
    174         xml_param_list.appendChild(param);
     218        }
     219       
     220        // put in all the params from the session cache
     221        params = session.getAttributeNames();
     222        while(params.hasMoreElements()) {
     223        String name = (String)params.nextElement();
    175224       
    176         }
    177     }
    178 
     225        if ( !name.equals(GSCGI.LANGUAGE_ARG) ) { // lang is stored but we dont want it in the param list cos its already in the request
     226           
     227            Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     228            param.setAttribute(GSXML.NAME_ATT, name);
     229            param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)session.getAttribute(name)));
     230            xml_param_list.appendChild(param);
     231        }
     232        }
     233
     234   
     235    }
     236   
    179237    if (!output.equals("html")) {
    180238        response.setContentType("text/xml"); // for now use text
    181239    }
    182    
    183     Element xml_result = recept_.process(xml_message);
    184     out.println(converter_.getPrettyString(xml_result));
     240   
     241    Element xml_result = this.recept.process(xml_message);
     242    encodeURLs(xml_result, response);
     243    out.println(this.converter.getPrettyString(xml_result));
     244    }
     245
     246    /** this goes through each URL and adds in a session id if needed--
     247     * its needed if the browser doesn't accept cookies
     248     */
     249    protected void encodeURLs(Element data, HttpServletResponse response) {
     250
     251    // get all the <a> elements
     252    NodeList hrefs = data.getElementsByTagName("a");
     253    for (int i=0; i<hrefs.getLength(); i++) {
     254        Element a = (Element)hrefs.item(i);
     255        a.setAttribute("href", response.encodeURL(a.getAttribute("href")));
     256    }
     257   
     258    // now find any submit bits - get all the <form> elements
     259    NodeList forms = data.getElementsByTagName("form");
     260    for (int i=0; i<forms.getLength(); i++) {
     261        Element form = (Element)forms.item(i);
     262        form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
     263    }
     264    // are these the only cases where URLs occur??
     265    // we should only do this for greenstone urls?
     266   
    185267    }
    186268}
Note: See TracChangeset for help on using the changeset viewer.