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

Last change on this file since 28972 was 28972, checked in by kjdon, 10 years ago

removing this.converter and this.doc - just create a new document when we need one, and all XMLConverter methods are now static so don't need this.converter

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