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