Ignore:
Timestamp:
2003-07-11T16:30:27+12:00 (21 years ago)
Author:
kjdon
Message:

trying to make this work with cgi args - not quite there yet I think

File:
1 edited

Legend:

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

    r4706 r4904  
    2222import org.greenstone.gsdl3.util.*;
    2323
     24import org.w3c.dom.Document;
     25import org.w3c.dom.Element;
     26
    2427import java.io.BufferedReader;
    2528import java.io.InputStreamReader;
     
    2730import java.io.IOException;
    2831import java.util.HashMap;
     32import java.util.Set;
     33import java.util.Iterator;
    2934/**
    30  * A program to take XML cgi-input from the command line and return html
     35 * A program to take cgi args from the command line and return html
    3136 *
    3237 * @author <a href="mailto:[email protected]">Katherine Don</a>
     
    3641final public class Library2 {
    3742 
     43    protected XMLConverter converter = null;
     44    protected Document doc = null;
     45   
     46    protected HashMap saved_args = null;
     47    protected GSCGI cgi = null;
     48    protected WebReceptionist recept = null;
     49   
     50    public Library2 (){
     51    this.converter = new XMLConverter();
     52    this.doc = converter.newDOM();
     53    this.saved_args = new HashMap();
     54    this.cgi = new GSCGI();
     55    this.recept = new WebReceptionist();
     56    }
     57   
     58    public void configure(String gsdl_home, String site_name, String interface_name) {
     59
     60    HashMap config_params = new HashMap();
     61    config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
     62    config_params.put(GSConstants.SITE_NAME, site_name);
     63    config_params.put(GSConstants.INTERFACE_NAME, interface_name);
     64    // new message router - create it and pass a handle to recept.
     65    // the servlet wont use this directly
     66    MessageRouter message_router = new MessageRouter();
     67   
     68    message_router.setSiteHome(GSFile.siteHome(gsdl_home, site_name));
     69    message_router.configure();
     70    // new receptionist
     71    recept.setConfigParams(config_params);
     72    recept.setMessageRouter(message_router);
     73    recept.configure();
     74    recept.setCGI(cgi);
     75    }
    3876    public static void main(String args[]) {
    3977
     
    4280        System.exit(1);
    4381    }
    44 
    45     HashMap config_params = new HashMap();
    46     config_params.put(GSConstants.GSDL3_HOME, args[0]);
    47     config_params.put(GSConstants.SITE_NAME, args[1]);
    48     config_params.put(GSConstants.INTERFACE_NAME, args[2]);
    49     // new message router - create it and pass a handle to recept.
    50     // the servlet wont use this directly
    51     MessageRouter message_router_ = new MessageRouter();
    5282   
    53     message_router_.setSiteHome(GSFile.siteHome(args[0], args[1]));
    54     message_router_.configure();
    55     // new receptionist
    56     Receptionist recept_ = new Receptionist();
    57     recept_.setConfigParams(config_params);
    58     recept_.setMessageRouter(message_router_);
    59     recept_.configure();
     83    Library2 library = new Library2();
     84    library.configure(args[0], args[1], args[2]);
     85   
    6086
    6187    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
    6389    String result=null;
    6490    while (true) {
    65         System.out.println("Please enter an XML representation of cgi params (all on one line), or 'exit' to quit");
     91        System.out.println("Please enter the  cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
    6692        try {
    6793        query = br.readLine();
     
    7298        System.exit(1);
    7399        }
     100
     101        result = library.process(query);
    74102       
    75         result = recept_.process(query);
    76 
    77103        System.out.println(result);
    78104
     
    80106    }
    81107
    82     }   
     108    }
     109
     110    protected String process(String query) {
     111    Element xml_message  = generateRequest(query);
     112    System.out.println("*********************");
     113    System.out.println(converter.getPrettyString(xml_message));
     114    Element xml_result = recept.process(xml_message);
     115    return this.converter.getPrettyString(xml_result);
     116    }
     117    protected Element generateRequest(String cgiargs) {
     118
     119    Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
     120    Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "");
     121    xml_message.appendChild(xml_request);
     122    Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     123    xml_request.appendChild(xml_param_list);
     124
     125    // the defaults
     126    String action = "p";
     127    String subaction = "home";
     128    String lang = (String)saved_args.get(GSCGI.LANGUAGE_ARG);
     129    if (lang == null) {
     130        lang = "en";
     131    }
     132    String output = "html";
     133   
     134    String args [] = cgiargs.split("&");
     135    for (int i=0; i<args.length; i++) {
     136        String arg = args[i];
     137        int pos = arg.indexOf('=');
     138        if (pos == -1) continue;
     139        String name = arg.substring(0, pos);
     140        String value = arg.substring(pos+1);
     141        if (name.equals(GSCGI.ACTION_ARG)) {
     142        action = value;
     143        } else if (name.equals(GSCGI.SUBACTION_ARG)) {
     144        subaction = (value);
     145        } else if (name.equals(GSCGI.LANGUAGE_ARG)) {
     146        lang = value;
     147        saved_args.put(name, value);
     148        } else if (name.equals(GSCGI.OUTPUT_ARG)) {
     149        output = value;
     150        } else if ( cgi.shouldSave(name)) {
     151        saved_args.put(name, value);
     152        } else {
     153        // make a param now
     154        Element param = doc.createElement(GSXML.PARAM_ELEM);
     155        param.setAttribute(GSXML.NAME_ATT, name);
     156        param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
     157        }
     158    }
     159
     160    xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
     161    xml_request.setAttribute(GSXML.ACTION_ATT, action);
     162    xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
     163    xml_request.setAttribute(GSXML.LANG_ATT, lang);
     164
     165
     166    // put in all the params from the session cache
     167    Set params = saved_args.keySet();
     168    Iterator i = params.iterator();
     169    while (i.hasNext()) {
     170        String name = (String)i.next();
     171        if (name.equals(GSCGI.LANGUAGE_ARG)) continue;
     172        Element param = this.doc.createElement(GSXML.PARAM_ELEM);
     173        param.setAttribute(GSXML.NAME_ATT, name);
     174        param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name)));
     175        xml_param_list.appendChild(param);
     176    }
     177
     178    return xml_message;
     179    }
    83180}
    84181
Note: See TracChangeset for help on using the changeset viewer.