[3235] | 1 | /*
|
---|
| 2 | * Library2.java
|
---|
| 3 | * Copyright (C) 2002 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 | */
|
---|
[3222] | 19 | package org.greenstone.gsdl3;
|
---|
| 20 |
|
---|
| 21 | import org.greenstone.gsdl3.core.*;
|
---|
| 22 | import org.greenstone.gsdl3.util.*;
|
---|
| 23 |
|
---|
[4904] | 24 | import org.w3c.dom.Document;
|
---|
| 25 | import org.w3c.dom.Element;
|
---|
[16688] | 26 | import org.w3c.dom.Node;
|
---|
[4904] | 27 |
|
---|
[3222] | 28 | import java.io.BufferedReader;
|
---|
| 29 | import java.io.InputStreamReader;
|
---|
| 30 | import java.io.File;
|
---|
| 31 | import java.io.IOException;
|
---|
[4706] | 32 | import java.util.HashMap;
|
---|
[4904] | 33 | import java.util.Set;
|
---|
| 34 | import java.util.Iterator;
|
---|
[3235] | 35 | /**
|
---|
[4904] | 36 | * A program to take cgi args from the command line and return html
|
---|
[3235] | 37 | *
|
---|
| 38 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 39 | * @version $Revision: 16688 $
|
---|
| 40 | */
|
---|
[3363] | 41 |
|
---|
[3222] | 42 | final public class Library2 {
|
---|
[3363] | 43 |
|
---|
[4904] | 44 | protected XMLConverter converter = null;
|
---|
| 45 | protected Document doc = null;
|
---|
| 46 |
|
---|
| 47 | protected HashMap saved_args = null;
|
---|
[4994] | 48 | protected GSParams params = null;
|
---|
| 49 | protected DefaultReceptionist recept = null;
|
---|
[4904] | 50 |
|
---|
| 51 | public Library2 (){
|
---|
| 52 | this.converter = new XMLConverter();
|
---|
| 53 | this.doc = converter.newDOM();
|
---|
| 54 | this.saved_args = new HashMap();
|
---|
[4994] | 55 | this.params = new GSParams();
|
---|
| 56 | this.recept = new DefaultReceptionist();
|
---|
[4904] | 57 | }
|
---|
| 58 |
|
---|
[10296] | 59 | public void configure(String site_name, String interface_name) {
|
---|
[4904] | 60 |
|
---|
| 61 | HashMap config_params = new HashMap();
|
---|
[10296] | 62 | //config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
|
---|
[4904] | 63 | config_params.put(GSConstants.SITE_NAME, site_name);
|
---|
| 64 | config_params.put(GSConstants.INTERFACE_NAME, interface_name);
|
---|
| 65 | // new message router - create it and pass a handle to recept.
|
---|
| 66 | // the servlet wont use this directly
|
---|
| 67 | MessageRouter message_router = new MessageRouter();
|
---|
| 68 |
|
---|
[10296] | 69 | message_router.setSiteName(site_name);
|
---|
[4904] | 70 | message_router.configure();
|
---|
| 71 | // new receptionist
|
---|
| 72 | recept.setConfigParams(config_params);
|
---|
| 73 | recept.setMessageRouter(message_router);
|
---|
[4994] | 74 | recept.setParams(params);
|
---|
[4904] | 75 | recept.configure();
|
---|
| 76 | }
|
---|
[3222] | 77 | public static void main(String args[]) {
|
---|
| 78 |
|
---|
[10296] | 79 | if (args.length != 2) {
|
---|
| 80 | System.out.println("Usage: Library2 <site name> <interface name>");
|
---|
[3222] | 81 | System.exit(1);
|
---|
| 82 | }
|
---|
[3476] | 83 |
|
---|
[4904] | 84 | Library2 library = new Library2();
|
---|
[10296] | 85 | library.configure(args[0], args[1]);
|
---|
[4904] | 86 |
|
---|
[3222] | 87 |
|
---|
| 88 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
---|
| 89 | String query=null;
|
---|
| 90 | String result=null;
|
---|
| 91 | while (true) {
|
---|
[4904] | 92 | System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
|
---|
[3222] | 93 | try {
|
---|
| 94 | query = br.readLine();
|
---|
[3363] | 95 | } catch (Exception e) {
|
---|
[4706] | 96 | System.err.println("Library2 exception:"+e.getMessage());
|
---|
[3363] | 97 | }
|
---|
| 98 | if (query.startsWith("exit")) {
|
---|
| 99 | System.exit(1);
|
---|
| 100 | }
|
---|
[4904] | 101 |
|
---|
| 102 | result = library.process(query);
|
---|
[3363] | 103 |
|
---|
| 104 | System.out.println(result);
|
---|
[3292] | 105 |
|
---|
[3363] | 106 |
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[4904] | 109 | }
|
---|
| 110 |
|
---|
| 111 | protected String process(String query) {
|
---|
| 112 | Element xml_message = generateRequest(query);
|
---|
| 113 | System.out.println("*********************");
|
---|
| 114 | System.out.println(converter.getPrettyString(xml_message));
|
---|
[16688] | 115 | Node xml_result = recept.process(xml_message);
|
---|
[4904] | 116 | return this.converter.getPrettyString(xml_result);
|
---|
| 117 | }
|
---|
| 118 | protected Element generateRequest(String cgiargs) {
|
---|
| 119 |
|
---|
| 120 | Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[6300] | 121 | Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", "", "");
|
---|
[4904] | 122 | xml_message.appendChild(xml_request);
|
---|
| 123 | Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 124 | xml_request.appendChild(xml_param_list);
|
---|
| 125 |
|
---|
| 126 | // the defaults
|
---|
| 127 | String action = "p";
|
---|
| 128 | String subaction = "home";
|
---|
[4994] | 129 | String lang = (String)saved_args.get(GSParams.LANGUAGE);
|
---|
[4904] | 130 | if (lang == null) {
|
---|
| 131 | lang = "en";
|
---|
| 132 | }
|
---|
| 133 | String output = "html";
|
---|
| 134 |
|
---|
| 135 | String args [] = cgiargs.split("&");
|
---|
| 136 | for (int i=0; i<args.length; i++) {
|
---|
| 137 | String arg = args[i];
|
---|
| 138 | int pos = arg.indexOf('=');
|
---|
| 139 | if (pos == -1) continue;
|
---|
| 140 | String name = arg.substring(0, pos);
|
---|
| 141 | String value = arg.substring(pos+1);
|
---|
[4994] | 142 | if (name.equals(GSParams.ACTION)) {
|
---|
[4904] | 143 | action = value;
|
---|
[4994] | 144 | } else if (name.equals(GSParams.SUBACTION)) {
|
---|
[4904] | 145 | subaction = (value);
|
---|
[4994] | 146 | } else if (name.equals(GSParams.LANGUAGE)) {
|
---|
[4904] | 147 | lang = value;
|
---|
| 148 | saved_args.put(name, value);
|
---|
[4994] | 149 | } else if (name.equals(GSParams.OUTPUT)) {
|
---|
[4904] | 150 | output = value;
|
---|
[4994] | 151 | } else if (params.shouldSave(name)) {
|
---|
[4904] | 152 | saved_args.put(name, value);
|
---|
| 153 | } else {
|
---|
| 154 | // make a param now
|
---|
| 155 | Element param = doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 156 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
| 157 | param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
|
---|
[4994] | 158 | xml_param_list.appendChild(param);
|
---|
[4904] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
|
---|
| 163 | xml_request.setAttribute(GSXML.ACTION_ATT, action);
|
---|
| 164 | xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
|
---|
| 165 | xml_request.setAttribute(GSXML.LANG_ATT, lang);
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | // put in all the params from the session cache
|
---|
| 169 | Set params = saved_args.keySet();
|
---|
| 170 | Iterator i = params.iterator();
|
---|
| 171 | while (i.hasNext()) {
|
---|
| 172 | String name = (String)i.next();
|
---|
[4994] | 173 | if (name.equals(GSParams.LANGUAGE)) continue;
|
---|
[4904] | 174 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 175 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
| 176 | param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)saved_args.get(name)));
|
---|
| 177 | xml_param_list.appendChild(param);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return xml_message;
|
---|
| 181 | }
|
---|
[3222] | 182 | }
|
---|
| 183 |
|
---|
| 184 |
|
---|