source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryCommandline.java@ 28218

Last change on this file since 28218 was 27617, checked in by sjm84, 11 years ago

Various improvements and fixes mostly to do with adding depositor functionality

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
RevLine 
[3235]1/*
[26497]2 * LibraryCommandline.java
[3235]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]19package org.greenstone.gsdl3;
20
21import org.greenstone.gsdl3.core.*;
22import org.greenstone.gsdl3.util.*;
[26453]23import org.greenstone.util.GlobalProperties;
[3222]24
[4904]25import org.w3c.dom.Document;
26import org.w3c.dom.Element;
[16688]27import org.w3c.dom.Node;
[4904]28
[3222]29import java.io.BufferedReader;
30import java.io.InputStreamReader;
31import java.io.File;
32import java.io.IOException;
[4706]33import java.util.HashMap;
[4904]34import java.util.Set;
35import java.util.Iterator;
[24982]36
[3235]37/**
[4904]38 * A program to take cgi args from the command line and return html
[24982]39 *
[25727]40 * @author Katherine Don
[3235]41 * @version $Revision: 27617 $
42 */
[3363]43
[26497]44final public class LibraryCommandline
[24982]45{
[4904]46
[24982]47 protected XMLConverter converter = null;
48 protected Document doc = null;
[3222]49
[25635]50 protected HashMap<String, String> saved_args = null;
[24982]51 protected GSParams params = null;
52 protected DefaultReceptionist recept = null;
53
[26497]54 public LibraryCommandline()
[24982]55 {
56 this.converter = new XMLConverter();
57 this.doc = converter.newDOM();
[25635]58 this.saved_args = new HashMap<String, String>();
[24982]59 this.params = new GSParams();
60 this.recept = new DefaultReceptionist();
[3222]61 }
62
[24982]63 public void configure(String site_name, String interface_name)
64 {
[4904]65
[27617]66 HashMap<String, Object> config_params = new HashMap<String, Object>();
[24982]67 //config_params.put(GSConstants.GSDL3_HOME, gsdl_home);
68 config_params.put(GSConstants.SITE_NAME, site_name);
69 config_params.put(GSConstants.INTERFACE_NAME, interface_name);
[26453]70 config_params.put(GSConstants.ALLOW_CLIENT_SIDE_XSLT, false);
71
[24982]72 // new message router - create it and pass a handle to recept.
73 // the servlet wont use this directly
74 MessageRouter message_router = new MessageRouter();
[3292]75
[24982]76 message_router.setSiteName(site_name);
77 message_router.configure();
78 // new receptionist
79 recept.setConfigParams(config_params);
80 recept.setMessageRouter(message_router);
81 recept.setParams(params);
82 recept.configure();
[3363]83 }
84
[26453]85 /*
86 * On Linux, run as:
[26497]87 * GS3> java -classpath "web/WEB-INF/lib/*":"lib/jni/*" org.greenstone.gsdl3.LibraryCommandline localsite default
[26453]88 * Press enter to accept the default cgi-args to pass in.
89 *
[26454]90 * For how to include all jars in a folder into the classpath to run a java program, see
91 * http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath
[26453]92 * http://stackoverflow.com/questions/6780678/run-class-in-jar-file
93 */
[24982]94 public static void main(String args[])
95 {
[4904]96
[26494]97 if (System.getenv("GSDL3SRCHOME") == null) {
98 System.out.println("Before calling this script, run: source gs3-setup.sh");
99 System.exit(1);
100 }
101
[24982]102 if (args.length != 2)
103 {
[26497]104 System.out.println("Usage: LibraryCommandline <site name> <interface name>");
[24982]105 System.exit(1);
106 }
[4904]107
[26453]108 // force GlobalProperties to default GSDL3HOME to GSDL3SRCHOME/web if not already set
109 GlobalProperties.loadGlobalProperties("");
110
[26497]111 LibraryCommandline library = new LibraryCommandline();
[24982]112 library.configure(args[0], args[1]);
[4904]113
[24982]114 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
115 String query = null;
116 String result = null;
117 while (true)
118 {
119 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
120 try
121 {
122 query = br.readLine();
123 }
124 catch (Exception e)
125 {
[26497]126 System.err.println("LibraryCommandline exception:" + e.getMessage());
[24982]127 }
128 if (query.startsWith("exit"))
129 {
130 System.exit(1);
131 }
132
133 result = library.process(query);
134
135 System.out.println(result);
136
137 }
138
[4904]139 }
[24982]140
141 protected String process(String query)
142 {
143 Element xml_message = generateRequest(query);
144 System.out.println("*********************");
145 System.out.println(converter.getPrettyString(xml_message));
146 Node xml_result = recept.process(xml_message);
147 return this.converter.getPrettyString(xml_result);
[4904]148 }
149
[24982]150 protected Element generateRequest(String cgiargs)
151 {
[4904]152
[24982]153 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
[24993]154 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", new UserContext());
[24982]155 xml_message.appendChild(xml_request);
156 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
157 xml_request.appendChild(xml_param_list);
[4904]158
[24982]159 // the defaults
160 String action = "p";
161 String subaction = "home";
[25635]162 String lang = saved_args.get(GSParams.LANGUAGE);
[24982]163 if (lang == null)
164 {
165 lang = "en";
166 }
167 String output = "html";
168
169 String args[] = cgiargs.split("&");
170 for (int i = 0; i < args.length; i++)
171 {
172 String arg = args[i];
173 int pos = arg.indexOf('=');
174 if (pos == -1)
175 continue;
176 String name = arg.substring(0, pos);
177 String value = arg.substring(pos + 1);
178 if (name.equals(GSParams.ACTION))
179 {
180 action = value;
181 }
182 else if (name.equals(GSParams.SUBACTION))
183 {
184 subaction = (value);
185 }
186 else if (name.equals(GSParams.LANGUAGE))
187 {
188 lang = value;
189 saved_args.put(name, value);
190 }
191 else if (name.equals(GSParams.OUTPUT))
192 {
193 output = value;
194 }
195 else if (params.shouldSave(name))
196 {
197 saved_args.put(name, value);
198 }
199 else
200 {
201 // make a param now
202 Element param = doc.createElement(GSXML.PARAM_ELEM);
203 param.setAttribute(GSXML.NAME_ATT, name);
204 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
205 xml_param_list.appendChild(param);
206 }
207 }
208
209 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
210 xml_request.setAttribute(GSXML.ACTION_ATT, action);
211 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
212 xml_request.setAttribute(GSXML.LANG_ATT, lang);
213
214 // put in all the params from the session cache
[25635]215 Set<String> params = saved_args.keySet();
216 Iterator<String> i = params.iterator();
[24982]217 while (i.hasNext())
218 {
[25635]219 String name = i.next();
[24982]220 if (name.equals(GSParams.LANGUAGE))
221 continue;
222 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
223 param.setAttribute(GSXML.NAME_ATT, name);
[25635]224 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(saved_args.get(name)));
[24982]225 xml_param_list.appendChild(param);
226 }
227
228 return xml_message;
[4904]229 }
[3222]230}
Note: See TracBrowser for help on using the repository browser.