source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/Library2.java@ 26458

Last change on this file since 26458 was 26454, checked in by ak19, 11 years ago

Adding the missing link to the previous commit. Previous commit message: Committing the changes necessary to get Library2.java working again: it takes library cgi args and produces a page.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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 */
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 * @author Katherine Don
41 * @version $Revision: 26454 $
42 */
43
44final public class Library2
45{
46
47 protected XMLConverter converter = null;
48 protected Document doc = null;
49
50 protected HashMap<String, String> saved_args = null;
51 protected GSParams params = null;
52 protected DefaultReceptionist recept = null;
53
54 public Library2()
55 {
56 this.converter = new XMLConverter();
57 this.doc = converter.newDOM();
58 this.saved_args = new HashMap<String, String>();
59 this.params = new GSParams();
60 this.recept = new DefaultReceptionist();
61 }
62
63 public void configure(String site_name, String interface_name)
64 {
65
66 HashMap<String, Comparable> config_params = new HashMap<String, Comparable>();
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);
70 config_params.put(GSConstants.ALLOW_CLIENT_SIDE_XSLT, false);
71
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();
75
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();
83 }
84
85 /*
86 * On Linux, run as:
87 * GS3> java -classpath "web/WEB-INF/lib/*":"lib/jni/*" org.greenstone.gsdl3.Library2 localsite default
88 * Press enter to accept the default cgi-args to pass in.
89 *
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
92 * http://stackoverflow.com/questions/6780678/run-class-in-jar-file
93 */
94 public static void main(String args[])
95 {
96
97 if (args.length != 2)
98 {
99 System.out.println("Usage: Library2 <site name> <interface name>");
100 System.exit(1);
101 }
102
103 // force GlobalProperties to default GSDL3HOME to GSDL3SRCHOME/web if not already set
104 GlobalProperties.loadGlobalProperties("");
105
106 Library2 library = new Library2();
107 library.configure(args[0], args[1]);
108
109 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
110 String query = null;
111 String result = null;
112 while (true)
113 {
114 System.out.println("Please enter the cgi args (all on one line), or 'exit' to quit (default a=p&sa=home)");
115 try
116 {
117 query = br.readLine();
118 }
119 catch (Exception e)
120 {
121 System.err.println("Library2 exception:" + e.getMessage());
122 }
123 if (query.startsWith("exit"))
124 {
125 System.exit(1);
126 }
127
128 result = library.process(query);
129
130 System.out.println(result);
131
132 }
133
134 }
135
136 protected String process(String query)
137 {
138 Element xml_message = generateRequest(query);
139 System.out.println("*********************");
140 System.out.println(converter.getPrettyString(xml_message));
141 Node xml_result = recept.process(xml_message);
142 return this.converter.getPrettyString(xml_result);
143 }
144
145 protected Element generateRequest(String cgiargs)
146 {
147
148 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
149 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", new UserContext());
150 xml_message.appendChild(xml_request);
151 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
152 xml_request.appendChild(xml_param_list);
153
154 // the defaults
155 String action = "p";
156 String subaction = "home";
157 String lang = saved_args.get(GSParams.LANGUAGE);
158 if (lang == null)
159 {
160 lang = "en";
161 }
162 String output = "html";
163
164 String args[] = cgiargs.split("&");
165 for (int i = 0; i < args.length; i++)
166 {
167 String arg = args[i];
168 int pos = arg.indexOf('=');
169 if (pos == -1)
170 continue;
171 String name = arg.substring(0, pos);
172 String value = arg.substring(pos + 1);
173 if (name.equals(GSParams.ACTION))
174 {
175 action = value;
176 }
177 else if (name.equals(GSParams.SUBACTION))
178 {
179 subaction = (value);
180 }
181 else if (name.equals(GSParams.LANGUAGE))
182 {
183 lang = value;
184 saved_args.put(name, value);
185 }
186 else if (name.equals(GSParams.OUTPUT))
187 {
188 output = value;
189 }
190 else if (params.shouldSave(name))
191 {
192 saved_args.put(name, value);
193 }
194 else
195 {
196 // make a param now
197 Element param = doc.createElement(GSXML.PARAM_ELEM);
198 param.setAttribute(GSXML.NAME_ATT, name);
199 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
200 xml_param_list.appendChild(param);
201 }
202 }
203
204 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
205 xml_request.setAttribute(GSXML.ACTION_ATT, action);
206 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
207 xml_request.setAttribute(GSXML.LANG_ATT, lang);
208
209 // put in all the params from the session cache
210 Set<String> params = saved_args.keySet();
211 Iterator<String> i = params.iterator();
212 while (i.hasNext())
213 {
214 String name = i.next();
215 if (name.equals(GSParams.LANGUAGE))
216 continue;
217 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
218 param.setAttribute(GSXML.NAME_ATT, name);
219 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(saved_args.get(name)));
220 xml_param_list.appendChild(param);
221 }
222
223 return xml_message;
224 }
225}
Note: See TracBrowser for help on using the repository browser.